forked from michaelrsweet/epm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epminstall.c
403 lines (339 loc) · 8.99 KB
/
epminstall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Install program replacement for the ESP Package Manager (EPM).
*
* Copyright 1999-2019 by Michael R Sweet
* Copyright 1999-2010 by Easy Software Products.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* Include necessary headers...
*/
#include "epm.h"
/*
* Global variable used by dist functions...
*/
int Verbosity = 0;
/*
* Local functions...
*/
static file_t *find_file(dist_t *dist, const char *dst);
static void info(void);
static void usage(void)
#ifdef __GNUC__
__attribute__((__noreturn__))
#endif /* __GNUC__ */
;
/*
* 'main()' - Add or replace files, directories, and symlinks.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line arguments */
char *argv[]) /* I - Command-line arguments */
{
int i; /* Looping var */
int mode, /* Permissions */
directories; /* Installing directories? */
char *user, /* Owner */
*group, /* Group */
*listname, /* List filename */
*src, /* Source filename */
dst[1024], /* Destination filename */
linkname[1024]; /* Symlink name */
ssize_t linklen; /* Length of symlink */
int num_files; /* Number of files to install */
char *files[1000]; /* Files to install */
struct stat fileinfo; /* File information */
dist_t *dist; /* Distribution */
file_t *file; /* File in distribution */
struct utsname platform; /* Platform information */
/*
* Parse the command-line arguments...
*/
num_files = 0;
mode = 0;
user = "root";
group = "sys";
directories = 0;
if ((listname = getenv("EPMLIST")) == NULL)
listname = "epm.list";
for (i = 1; i < argc; i ++)
if (strcmp(argv[i], "-b") == 0)
continue;
else if (strcmp(argv[i], "-c") == 0)
continue;
else if (strcmp(argv[i], "-d") == 0)
directories = 1;
else if (strcmp(argv[i], "-g") == 0)
{
i ++;
if (i < argc)
group = argv[i];
else
usage();
}
else if (strcmp(argv[i], "-m") == 0)
{
i ++;
if (i < argc)
mode = (int)strtol(argv[i], NULL, 8);
else
usage();
}
else if (strcmp(argv[i], "-o") == 0)
{
i ++;
if (i < argc)
user = argv[i];
else
usage();
}
else if (strcmp(argv[i], "-s") == 0)
continue;
else if (strcmp(argv[i], "--list-file") == 0)
{
i ++;
if (i < argc)
listname = argv[i];
else
usage();
}
else if (argv[i][0] == '-')
usage();
else if (num_files < (sizeof(files) / sizeof(files[0])))
{
files[num_files] = argv[i];
num_files ++;
}
else
{
fputs("epminstall: Too many filenames on command-line!\n", stderr);
usage();
}
if (num_files == 0 || (num_files < 2 && !directories))
usage();
get_platform(&platform);
if ((dist = read_dist(listname, &platform, "")) == NULL)
{
fprintf(stderr, "epminstall: Unable to read list file \"%s\": %s\n",
listname, strerror(errno));
return (1);
}
/*
* Check to see if we are installing files or directories...
*/
if (directories)
{
if (!mode)
mode = 0755;
for (i = 0; i < num_files; i ++)
{
/*
* Copy the directory name into a new file entry, which is cleared
* by add_file()...
*/
if ((file = find_file(dist, files[i])) == NULL)
file = add_file(dist, NULL);
file->type = 'd';
file->mode = mode & 07777;
strlcpy(file->user, user, sizeof(file->user));
strlcpy(file->group, group, sizeof(file->group));
strlcpy(file->dst, files[i], sizeof(file->dst));
strlcpy(file->src, "-", sizeof(file->src));
}
}
else
{
/*
* Check to see if we have 1 or more files and a directory or
* a source and destination file...
*/
if (num_files == 2)
{
file = find_file(dist, files[1]);
if (file == NULL || file->type != 'd')
{
if (!file)
file = add_file(dist, NULL);
if (stat(files[0], &fileinfo))
{
fprintf(stderr, "epminstall: Unable to stat \"%s\": %s\n",
files[0], strerror(errno));
fileinfo.st_mode = mode;
}
if (S_ISLNK(fileinfo.st_mode))
{
file->type = 'l';
if ((linklen = readlink(files[0], linkname, sizeof(linkname) - 1)) < 0)
{
fprintf(stderr, "epminstall: Unable to read symlink \"%s\": %s\n",
files[0], strerror(errno));
files[0] = "BROKEN-LINK";
}
else
{
linkname[linklen] = '\0';
files[0] = linkname;
}
}
else
file->type = 'f';
if (mode)
file->mode = mode & 07777;
else if (fileinfo.st_mode & 0111)
file->mode = 0755;
else
file->mode = 0644;
strlcpy(file->user, user, sizeof(file->user));
strlcpy(file->group, group, sizeof(file->group));
strlcpy(file->dst, files[1], sizeof(file->dst));
strlcpy(file->src, files[0], sizeof(file->src));
}
else
num_files --;
}
if (num_files != 2)
{
if (num_files > 1)
num_files --;
if ((file = find_file(dist, files[num_files])) == NULL)
{
/*
* Add the installation directory to the file list...
*/
file = add_file(dist, NULL);
file->type = 'd';
file->mode = 0755;
strlcpy(file->user, user, sizeof(file->user));
strlcpy(file->group, group, sizeof(file->group));
strlcpy(file->dst, files[num_files], sizeof(file->dst));
strlcpy(file->src, "-", sizeof(file->src));
}
else if (file->type != 'd')
{
fprintf(stderr, "epminstall: Destination path \"%s\" is not a directory!\n",
files[num_files]);
return (1);
}
for (i = 0; i < num_files; i ++)
{
/*
* Add/update the file in the distribution...
*/
if ((src = strrchr(files[i], '/')) != NULL)
src ++;
else
src = files[i];
snprintf(dst, sizeof(dst), "%s/%s", files[num_files], src);
if ((file = find_file(dist, dst)) == NULL)
file = add_file(dist, NULL);
if (stat(files[i], &fileinfo))
{
fprintf(stderr, "epminstall: Unable to stat \"%s\": %s\n",
files[i], strerror(errno));
fileinfo.st_mode = mode;
}
if (S_ISLNK(fileinfo.st_mode))
{
file->type = 'l';
if ((linklen = readlink(files[i], linkname, sizeof(linkname) - 1)) < 0)
{
fprintf(stderr, "epminstall: Unable to read symlink \"%s\": %s\n",
files[i], strerror(errno));
files[i] = "BROKEN-LINK";
}
else
{
linkname[linklen] = '\0';
files[i] = linkname;
}
}
else
file->type = 'f';
if (mode)
file->mode = mode & 07777;
else if (fileinfo.st_mode & 0111)
file->mode = 0755;
else
file->mode = 0644;
strlcpy(file->user, user, sizeof(file->user));
strlcpy(file->group, group, sizeof(file->group));
strlcpy(file->dst, dst, sizeof(file->dst));
strlcpy(file->src, files[i], sizeof(file->src));
}
}
}
/*
* Sort the files to make the final list file easier to check...
*/
sort_dist_files(dist);
/*
* Write the distribution file...
*/
if (write_dist(listname, dist))
{
fprintf(stderr, "epminstall: Unable to read list file \"%s\": %s\n",
listname, strerror(errno));
return (1);
}
/*
* Return with no errors...
*/
return (0);
}
/*
* 'find_file()' - Find a file in the distribution...
*/
static file_t * /* O - File entry or NULL */
find_file(dist_t *dist, /* I - Distribution to search */
const char *dst) /* I - Destination filename */
{
int i; /* Looping var */
file_t *file; /* Current file */
for (i = dist->num_files, file = dist->files; i > 0; i --, file ++)
if (strcmp(file->dst, dst) == 0)
return (file);
return (NULL);
}
/*
* 'info()' - Show the EPM copyright and license.
*/
static void
info(void)
{
puts(EPM_VERSION);
puts("Copyright 1999-2019 by Michael R Sweet.");
puts("");
puts("EPM is free software and comes with ABSOLUTELY NO WARRANTY; for details");
puts("see the GNU General Public License in the file COPYING or at");
puts("\"http://www.fsf.org/gpl.html\". Report all problems to");
puts("\"https://github.com/michaelrsweet/epm/issues\".");
puts("");
}
/*
* 'usage()' - Show command-line usage instructions.
*/
static void
usage(void)
{
info();
puts("Usage: epminstall [options] file1 file2 ... fileN directory");
puts(" epminstall [options] file1 file2");
puts(" epminstall [options] -d directory1 directory2 ... directoryN");
puts("Options:");
puts("-g group");
puts(" Set group of installed file(s).");
puts("-m mode");
puts(" Set permissions of installed file(s).");
puts("-u owner");
puts(" Set owner of installed file(s).");
exit(1);
}