forked from michaelrsweet/epm
-
Notifications
You must be signed in to change notification settings - Fork 8
/
mkepmlist.c
571 lines (454 loc) · 14.2 KB
/
mkepmlist.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* List file generation utility for the ESP Package Manager (EPM).
*
* Copyright © 2020 by Jim Jagielski
* Copyright 2003-2019 by Michael R Sweet
* Copyright 2003-2005 by Easy Software Products
* Copyright 2003 Andreas Voegele
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "epm.h"
/*
* Lookup hash table structure...
*/
#define HASH_M 101
struct node {
unsigned id; /* User or group ID */
char *name; /* User or group name */
};
/*
* Globals...
*/
char *DefaultUser = NULL, /* Default user for entries */
*DefaultGroup = NULL; /* Default group for entries */
struct node Users[HASH_M]; /* Hash table for users */
struct node Groups[HASH_M]; /* Hash table for groups */
/*
* Functions...
*/
char *get_group(gid_t gid);
char *get_user(uid_t uid);
void hash_deinit(struct node *a);
unsigned hash_id(unsigned id);
void hash_init(struct node *a);
char *hash_insert(struct node *a, unsigned id, const char *name);
char *hash_search(struct node *a, unsigned id);
void info(void);
int process_dir(const char *srcpath, const char *dstpath);
int process_file(const char *src, const char *dstpath);
char *quote_string(char *q, const char *s, size_t qsize);
void usage(void);
/*
* 'main()' - Scan directories to produce a distribution list.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line arguments */
char *argv[]) /* I - Command-line arguments */
{
int i; /* Looping var */
const char *prefix, /* Installation prefix */
*dstpath; /* Destination path */
char dst[1024], /* Destination */
*ptr; /* Pointer into filename */
struct stat info; /* File information */
/*
* Initialize the hash tables...
*/
hash_init(Users);
hash_init(Groups);
/*
* Loop through the command-line arguments, processing directories as
* needed...
*/
prefix = NULL;
for (i = 1; i < argc; i++)
if (strcmp(argv[i], "-u") == 0) {
/*
* -u username
*/
i++;
if (i >= argc)
usage();
DefaultUser = argv[i];
} else if (strcmp(argv[i], "-g") == 0) {
/*
* -g groupname
*/
i++;
if (i >= argc)
usage();
DefaultGroup = argv[i];
} else if (strcmp(argv[i], "--prefix") == 0) {
i++;
if (i >= argc)
usage();
prefix = argv[i];
} else if (argv[i][0] == '-') {
/*
* Unknown option...
*/
fprintf(stderr, "Unknown option \"%s\"!\n", argv[i]);
usage();
} else {
/*
* Directory name...
*/
if (prefix)
dstpath = prefix;
else {
if (argv[i][0] == '/')
dstpath = argv[i];
else {
if (argv[i][0] == '.') {
for (dstpath = argv[i]; *dstpath == '.';) {
if (strncmp(dstpath, "./", 2) == 0)
dstpath += 2;
else if (strncmp(dstpath, "../", 3) == 0)
dstpath += 3;
else
break;
}
if (strcmp(dstpath, ".") == 0)
dstpath = "";
} else
dstpath = argv[i];
if (dstpath[0]) {
snprintf(dst, sizeof(dst), "/%s", dstpath);
dstpath = dst;
}
}
}
if (!lstat(argv[i], &info)) {
if (!S_ISDIR(info.st_mode)) {
if (!prefix) {
/*
* Remove trailing filename component from destination...
*/
if (dstpath != dst) {
strlcpy(dst, dstpath, sizeof(dst));
dstpath = dst;
}
if ((ptr = strrchr(dstpath, '/')) != NULL)
*ptr = '\0';
}
process_file(argv[i], dstpath);
} else
process_dir(argv[i], dstpath);
}
}
/*
* Free any memory we have left allocated...
*/
hash_deinit(Users);
hash_deinit(Groups);
return (0);
}
/*
* 'get_group()' - Get a group name for the given group ID.
*/
char * /* O - Name of group or NULL */
get_group(gid_t gid) /* I - Group ID */
{
char *name; /* Name of group */
struct group *gr; /* Group entry for group */
char buf[16]; /* Temporary string buffer for group numbers */
/*
* Always return the default group if set...
*/
if (DefaultGroup)
return (DefaultGroup);
/*
* See if the group ID is already in the hash table...
*/
if ((name = hash_search(Groups, gid)) != NULL)
return (name);
/*
* Lookup the group ID in the password file...
*/
setgrent();
if ((gr = getgrgid(gid)) != NULL)
name = gr->gr_name;
else {
/*
* Unable to find group ID; just return the number...
*/
snprintf(buf, sizeof(buf), "%u", (unsigned)gid);
name = buf;
}
/*
* Add the name to the hash table and return it...
*/
return (hash_insert(Groups, gid, name));
}
/*
* 'get_user()' - Get a user name for the given user ID.
*/
char * /* O - Name of user or NULL */
get_user(uid_t uid) /* I - User ID */
{
char *name; /* Name of user */
struct passwd *pw; /* Password entry for user */
char buf[16]; /* Temporary string buffer for user numbers */
/*
* Always return the default user if set...
*/
if (DefaultUser)
return (DefaultUser);
/*
* See if the user ID is already in the hash table...
*/
if ((name = hash_search(Users, uid)) != NULL)
return (name);
/*
* Lookup the user ID in the password file...
*/
setpwent();
if ((pw = getpwuid(uid)) != NULL)
name = pw->pw_name;
else {
/*
* Unable to find user ID; just return the number...
*/
snprintf(buf, sizeof(buf), "%u", (unsigned)uid);
name = buf;
}
/*
* Add the name to the hash table and return it...
*/
return (hash_insert(Users, uid, name));
}
/*
* 'hash_deinit()' - Deinitialize a hash table.
*/
void hash_deinit(struct node *a) /* I - Hash table */
{
unsigned h; /* Looping var */
for (h = 0; h < HASH_M; h++)
if (a[h].name) {
free(a[h].name);
memset(a, 0, sizeof(struct node));
}
}
/*
* 'hash_id()' - Generate a hash for a user or group ID.
*/
unsigned /* O - Hash number */
hash_id(unsigned id) /* I - User or group ID */
{
return (id % HASH_M);
}
/*
* 'hash_init()' - Initialize a hash table.
*/
void hash_init(struct node *a) /* I - Hash table to initialize */
{
/*
* Zero out the hash table...
*/
memset(a, 0, HASH_M * sizeof(struct node));
}
/*
* 'hash_insert()' - Insert a new entry in a hash table.
*/
char * /* O - New user or group name or NULL */
hash_insert(struct node *a, /* I - Hash table */
unsigned id, /* I - User or group ID */
const char *name) /* I - User or group name */
{
unsigned h, /* Current hash value */
start; /* Starting hash value */
for (h = start = hash_id(id); a[h].name;) {
h = (h + 1) % HASH_M;
if (h == start)
return (NULL);
}
a[h].id = id;
a[h].name = strdup(name);
return (a[h].name);
}
/*
* 'hash_search()' - Find a user or group ID in the hash table...
*/
char * /* O - User or group name, or NULL */
hash_search(struct node *a, /* I - Hash table */
unsigned id) /* I - User or group ID */
{
unsigned h, /* Current hash value */
start; /* Starting hash value */
for (h = start = hash_id(id); a[h].name;) {
h = (h + 1) % HASH_M;
if (a[h].id == id)
return (a[h].name);
if (h == start)
break;
}
return (NULL);
}
/*
* 'info()' - Show the EPM copyright and license.
*/
void info(void) {
puts(EPM_VERSION);
puts("Copyright 1999-2019 by Michael R Sweet.");
puts("Copyright 2020 by Jim Jagielski.");
puts("");
puts("EPM is free software and comes with ABSOLUTELY NO WARRANTY; for details");
puts("see the Apache License version 2.0 in the file LICENSE or at");
puts("\"https://www.apache.org/licenses/LICENSE-2.0\". Report all problems to");
puts("\"https://github.com/jimjag/epm/issues\".");
puts("");
}
/*
* 'process_dir()' - Process a directory...
*/
int /* O - 0 on success, -1 on error */
process_dir(const char *srcpath, /* I - Source path */
const char *dstpath) /* I - Destination path */
{
DIR *dir; /* Directory to read from */
struct dirent *dent; /* Current directory entry */
size_t srclen; /* Length of source path */
char src[1024]; /* Temporary source path */
/*
* Try opening the source directory...
*/
if ((dir = opendir(srcpath)) == NULL) {
fprintf(stderr, "mkepmlist: Unable to open directory \"%s\": %s.\n", srcpath,
strerror(errno));
return (-1);
}
/*
* Read from the directory...
*/
srclen = strlen(srcpath);
while ((dent = readdir(dir)) != NULL) {
/*
* Skip "." and ".."...
*/
if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0)
continue;
/*
* Process file...
*/
if (srclen > 0 && srcpath[srclen - 1] == '/')
snprintf(src, sizeof(src), "%s%s", srcpath, dent->d_name);
else
snprintf(src, sizeof(src), "%s/%s", srcpath, dent->d_name);
if (process_file(src, dstpath)) {
closedir(dir);
return (-1);
}
}
closedir(dir);
return (0);
}
/*
* 'process_file()' - Process a file...
*/
int /* O - 0 on success, -1 on error */
process_file(const char *src, /* I - Source path */
const char *dstpath) /* I - Destination path */
{
const char *srcptr; /* Pointer into source path */
struct stat srcinfo; /* Information on the source file */
ssize_t linklen; /* Length of link path */
size_t dstlen; /* Length of destination path */
char link[1024], /* Link for source */
dst[1024], /* Temporary destination path */
qdst[1024], /* Quoted destination */
qsrc[1024]; /* Quoted source/link */
/*
* Get source file info...
*/
dstlen = strlen(dstpath);
if ((srcptr = strrchr(src, '/')) != NULL)
srcptr++;
else
srcptr = src;
if (dstlen > 0 && dstpath[dstlen - 1] == '/')
snprintf(dst, sizeof(dst), "%s%s", dstpath, srcptr);
else
snprintf(dst, sizeof(dst), "%s/%s", dstpath, srcptr);
if (lstat(src, &srcinfo)) {
fprintf(stderr, "mkepmlist: Unable to stat \"%s\": %s.\n", src, strerror(errno));
return (-1);
}
/*
* Process accordingly...
*/
if (S_ISDIR(srcinfo.st_mode)) {
/*
* Directory...
*/
printf("d %o %s %s %s -\n", (unsigned)(srcinfo.st_mode & 07777),
get_user(srcinfo.st_uid), get_group(srcinfo.st_gid),
quote_string(qdst, dst, sizeof(qdst)));
if (process_dir(src, dst))
return (-1);
} else if (S_ISLNK(srcinfo.st_mode)) {
/*
* Symlink...
*/
if ((linklen = readlink(src, link, sizeof(link) - 1)) < 0) {
fprintf(stderr, "mkepmlist: Unable to read symlink \"%s\": %s.\n", src,
strerror(errno));
return (-1);
}
link[linklen] = '\0';
printf("l %o %s %s %s %s\n", (unsigned)(srcinfo.st_mode & 07777),
get_user(srcinfo.st_uid), get_group(srcinfo.st_gid),
quote_string(qdst, dst, sizeof(qdst)),
quote_string(qsrc, link, sizeof(qsrc)));
} else if (S_ISREG(srcinfo.st_mode)) {
/*
* Regular file...
*/
printf("f %o %s %s %s %s\n", (unsigned)(srcinfo.st_mode & 07777),
get_user(srcinfo.st_uid), get_group(srcinfo.st_gid),
quote_string(qdst, dst, sizeof(qdst)),
quote_string(qsrc, src, sizeof(qsrc)));
}
return (0);
}
/*
* 'quote_string()' - Quote space, backslash, and $ in a string...
*/
char * /* O - Quoted string */
quote_string(char *q, /* I - Quoted string buffer */
const char *s, /* I - Original string */
size_t qsize) /* I - Size of buffer */
{
char *qptr, *qend;
for (qptr = q, qend = q + qsize - 2; *s && qptr < qend;) {
if (*s == '\\' || *s == ' ' || *s == '\t')
*qptr++ = '\\';
else if (*s == '$')
*qptr++ = '$';
*qptr++ = *s++;
}
*qptr = '\0';
return (q);
}
/*
* 'usage()' - Show command-line usage instructions.
*/
void usage(void) {
info();
puts("Usage: mkepmlist [options] directory [... directory] >filename.list");
puts("Options:");
puts("-g group Set group name for files.");
puts("-u user Set user name for files.");
puts("--prefix directory Set directory prefix for files.");
exit(1);
}