This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
tar.c
574 lines (451 loc) · 13 KB
/
tar.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
572
573
574
/*
* TAR file functions 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"
/*
* Local globals...
*/
static char last_pathname[1024] = "";
/*
* 'tar_close()' - Close a tar file, padding as needed.
*/
int /* O - -1 on error, 0 on success */
tar_close(tarf_t *fp) /* I - File to write to */
{
size_t blocks; /* Number of blocks to write */
int status; /* Return status */
char padding[TAR_BLOCKS * TAR_BLOCK];
/* Padding for tar blocks */
if (fp->blocks > 0)
{
/*
* Zero the padding record...
*/
memset(padding, 0, sizeof(padding));
/*
* Write a single 0 block to signal the end of the archive...
*/
if (fwrite(padding, TAR_BLOCK, 1, fp->file) < 1)
return (-1);
fp->blocks ++;
/*
* Pad the tar files to TAR_BLOCKS blocks... This is bullshit of course,
* but old versions of tar need it...
*/
status = 0;
if ((blocks = fp->blocks % TAR_BLOCKS) > 0)
{
blocks = TAR_BLOCKS - blocks;
if (fwrite(padding, TAR_BLOCK, blocks, fp->file) < blocks)
status = -1;
}
else
{
/*
* Sun tar needs at least 2 0 blocks...
*/
if (fwrite(padding, TAR_BLOCK, blocks, fp->file) < blocks)
status = -1;
}
}
else
status = 0;
/*
* Close the file and free memory...
*/
if (fp->compressed)
{
if (pclose(fp->file))
status = -1;
}
else if (fclose(fp->file))
status = -1;
free(fp);
return (status);
}
/*
* 'tar_directory()' - Archive a directory.
*/
int /* O - 0 on success, -1 on error */
tar_directory(tarf_t *tar, /* I - Tar file to write to */
const char *srcpath, /* I - Source directory */
const char *dstpath) /* I - Destination directory */
{
DIR *dir; /* Directory */
DIRENT *dent; /* Directory entry */
char src[1024], /* Source file */
dst[1024], /* Destination file */
srclink[1024]; /* Symlink value */
struct stat srcinfo; /* Information on the source file */
/*
* Range check input...
*/
if (tar == NULL || srcpath == NULL || dstpath == NULL)
return (-1);
/*
* Try opening the source directory...
*/
if ((dir = opendir(srcpath)) == NULL)
{
fprintf(stderr, "epm: Unable to open directory \"%s\": %s\n", srcpath,
strerror(errno));
return (-1);
}
/*
* Read from the directory...
*/
while ((dent = readdir(dir)) != NULL)
{
/*
* Skip "." and ".."...
*/
if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
continue;
/*
* Get source file info...
*/
snprintf(src, sizeof(src), "%s/%s", srcpath, dent->d_name);
if (dstpath[0])
snprintf(dst, sizeof(dst), "%s/%s", dstpath, dent->d_name);
else
strlcpy(dst, dent->d_name, sizeof(dst));
if (stat(src, &srcinfo))
{
fprintf(stderr, "epm: Unable to stat \"%s\": %s\n", src,
strerror(errno));
continue;
}
/*
* Process accordingly...
*/
if (Verbosity)
puts(dst);
if (S_ISDIR(srcinfo.st_mode))
{
/*
* Directory...
*/
if (tar_header(tar, TAR_DIR, srcinfo.st_mode, 0,
srcinfo.st_mtime, "root", "sys", dst, NULL))
goto fail;
if (tar_directory(tar, src, dst))
goto fail;
}
else if (S_ISLNK(srcinfo.st_mode))
{
/*
* Symlink...
*/
if (readlink(src, srclink, sizeof(srclink)) < 0)
goto fail;
if (tar_header(tar, TAR_SYMLINK, srcinfo.st_mode, 0,
srcinfo.st_mtime, "root", "sys", dst, srclink))
goto fail;
}
else
{
/*
* Regular file...
*/
if (tar_header(tar, TAR_NORMAL, srcinfo.st_mode, srcinfo.st_size,
srcinfo.st_mtime, "root", "sys", dst, NULL))
goto fail;
if (tar_file(tar, src))
goto fail;
}
}
closedir(dir);
return (0);
fail:
closedir(dir);
return (-1);
}
/*
* 'tar_file()' - Write the contents of a file...
*/
int /* O - 0 on success, -1 on error */
tar_file(tarf_t *fp, /* I - Tar file to write to */
const char *filename) /* I - File to write */
{
FILE *file; /* File to write */
size_t nbytes, /* Number of bytes read */
tbytes, /* Total bytes read/written */
fill; /* Number of fill bytes needed */
char buffer[8192]; /* Copy buffer */
/*
* Try opening the file...
*/
if ((file = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "epm: Unable to open \"%s\": %s\n", filename, strerror(errno));
return (-1);
}
/*
* Copy the file to the tar file...
*/
tbytes = 0;
while ((nbytes = fread(buffer, 1, sizeof(buffer), file)) > 0)
{
/*
* Zero fill the file to a 512 byte record as needed.
*/
if (nbytes < sizeof(buffer))
{
fill = TAR_BLOCK - (nbytes & (TAR_BLOCK - 1));
if (fill < TAR_BLOCK)
{
memset(buffer + nbytes, 0, fill);
nbytes += fill;
}
}
/*
* Write the buffer to the file...
*/
if (fwrite(buffer, 1, nbytes, fp->file) < nbytes)
{
fprintf(stderr, "epm: Unable to write file data for \"%s\": %s\n", last_pathname, strerror(errno));
fclose(file);
return (-1);
}
tbytes += nbytes;
fp->blocks += nbytes / TAR_BLOCK;
}
/*
* Close the file and return...
*/
fclose(file);
return (0);
}
/*
* 'tar_header()' - Write a TAR header for the specified file...
*/
int /* O - 0 on success, -1 on error */
tar_header(tarf_t *fp, /* I - Tar file to write to */
int type, /* I - File type */
mode_t mode, /* I - File permissions */
off_t size, /* I - File size */
time_t mtime, /* I - File modification time */
const char *user, /* I - File owner */
const char *group, /* I - File group */
const char *pathname, /* I - File name */
const char *linkname) /* I - File link name (for links only) */
{
tar_t record; /* TAR header record */
size_t pathlen; /* Length of pathname */
const char *pathsep; /* Path separator */
int i, /* Looping var... */
sum; /* Checksum */
unsigned char *sumptr; /* Pointer into header record */
struct passwd *pwd; /* Pointer to user record */
struct group *grp; /* Pointer to group record */
/*
* Find the username and groupname IDs...
*/
pwd = getpwnam(user);
grp = getgrnam(group);
endpwent();
endgrent();
/*
* Format the header...
*/
memset(&record, 0, sizeof(record));
pathlen = strlen(pathname);
if ((pathlen < (sizeof(record.header.pathname) - 1) && type != TAR_DIR) ||
(pathlen < (sizeof(record.header.pathname) - 2) && type == TAR_DIR))
{
/*
* Pathname is short enough to fit in the initial pathname field...
*/
strlcpy(record.header.pathname, pathname, sizeof(record.header.pathname));
if (type == TAR_DIR && pathname[pathlen - 1] != '/')
record.header.pathname[pathlen] = '/';
}
else
{
/*
* Copy directory information to prefix buffer and filename information
* to pathname buffer.
*/
if ((pathsep = strrchr(pathname, '/')) == NULL)
{
/*
* No directory info...
*/
fprintf(stderr, "epm: Pathname \"%s\" is too long for a tar file!\n",
pathname);
return (-1);
}
if ((pathsep - pathname) > (sizeof(record.header.prefix) - 1))
{
/*
* Directory name too long...
*/
fprintf(stderr, "epm: Pathname \"%s\" is too long for a tar file!\n",
pathname);
return (-1);
}
if ((pathlen - (pathsep - pathname)) > (sizeof(record.header.pathname) - 1))
{
/*
* Filename too long...
*/
fprintf(stderr, "epm: Pathname \"%s\" is too long for a tar file!\n",
pathname);
return (-1);
}
strlcpy(record.header.pathname, pathsep + 1, sizeof(record.header.pathname));
if (type == TAR_DIR && pathname[pathlen - 1] != '/')
record.header.pathname[pathlen - (pathsep - pathname + 1)] = '/';
strlcpy(record.header.prefix, pathname, (size_t)(pathsep - pathname + 1));
}
snprintf(record.header.mode, sizeof(record.header.mode), "%-6o ", (unsigned)mode);
snprintf(record.header.uid, sizeof(record.header.uid), "%o ", pwd == NULL ? 0 : (unsigned)pwd->pw_uid);
snprintf(record.header.gid, sizeof(record.header.gid), "%o ", grp == NULL ? 0 : (unsigned)grp->gr_gid);
snprintf(record.header.size, sizeof(record.header.size), "%011o", (unsigned)size);
snprintf(record.header.mtime, sizeof(record.header.mtime), "%011o", (unsigned)mtime);
memset(&(record.header.chksum), ' ', sizeof(record.header.chksum));
record.header.linkflag = type;
if (type == TAR_SYMLINK)
strlcpy(record.header.linkname, linkname, sizeof(record.header.linkname));
strlcpy(record.header.magic, TAR_MAGIC, sizeof(record.header.magic));
memcpy(record.header.version, TAR_VERSION, 2);
strlcpy(record.header.uname, user, sizeof(record.header.uname));
strlcpy(record.header.gname, group, sizeof(record.header.uname));
/*
* Compute the checksum of the header...
*/
for (i = sizeof(record), sumptr = record.all, sum = 0; i > 0; i --)
sum += *sumptr++;
/*
* Put the correct checksum in place and write the header...
*/
snprintf(record.header.chksum, sizeof(record.header.chksum), "%6o", sum);
if (fwrite(&record, 1, sizeof(record), fp->file) < sizeof(record))
{
static const char * const types[] =
{
"file",
"link",
"symbolic link",
"character file",
"block file",
"directory",
"named pipe",
"contiguous file"
};
fprintf(stderr, "epm: Error writing %s header for \"%s\": %s\n", types[type - '0'], pathname, strerror(errno));
return (-1);
}
strlcpy(last_pathname, pathname, sizeof(last_pathname));
fp->blocks ++;
return (0);
}
/*
* 'tar_open()' - Open a TAR file for writing.
*/
tarf_t * /* O - New tar file */
tar_open(const char *filename, /* I - File to create */
int compress) /* I - Compress with gzip? */
{
tarf_t *fp; /* New tar file */
char command[1024]; /* Compression command */
/*
* Allocate memory for the tar file state...
*/
if ((fp = calloc(sizeof(tarf_t), 1)) == NULL)
return (NULL);
/*
* Open the output file or pipe into gzip for compressed output...
*/
if (compress)
{
snprintf(command, sizeof(command), EPM_GZIP " > %s", filename);
fp->file = popen(command, "w");
}
else
fp->file = fopen(filename, "wb");
/*
* If we couldn't open the output file, abort...
*/
if (fp->file == NULL)
{
free(fp);
return (NULL);
}
/*
* Save the compression state and return...
*/
fp->compressed = compress;
return (fp);
}
/*
* 'tar_package()' - Archive a package file.
*/
int /* O - 0 on success, -1 on failure */
tar_package(tarf_t *tar, /* I - Tar file */
const char *ext, /* I - Package filename extension */
const char *prodname, /* I - Product short name */
const char *directory, /* I - Directory for distribution files */
const char *platname, /* I - Platform name */
dist_t *dist, /* I - Distribution information */
const char *subpackage) /* I - Subpackage */
{
char prodfull[255], /* Full name of product */
name[1024], /* Full product name */
filename[1024]; /* File to archive */
struct stat filestat; /* File information */
/*
* Figure out the full name of the distribution...
*/
if (subpackage)
snprintf(prodfull, sizeof(prodfull), "%s-%s", prodname, subpackage);
else
strlcpy(prodfull, prodname, sizeof(prodfull));
/*
* Then the subdirectory name...
*/
if (dist->release[0])
snprintf(name, sizeof(name), "%s-%s-%s", prodfull, dist->version,
dist->release);
else
snprintf(name, sizeof(name), "%s-%s", prodfull, dist->version);
if (platname[0])
{
strlcat(name, "-", sizeof(name));
strlcat(name, platname, sizeof(name));
}
strlcat(name, ".", sizeof(name));
strlcat(name, ext, sizeof(name));
/*
* Find out more about the package file...
*/
snprintf(filename, sizeof(filename), "%s/%s", directory, name);
if (stat(filename, &filestat))
{
fprintf(stderr, "epm: Error reading package file \"%s\": %s\n", filename, strerror(errno));
return (-1);
}
/*
* Write the header and the file...
*/
if (tar_header(tar, TAR_NORMAL, (mode_t)0644, filestat.st_size,
filestat.st_mtime, "root", "root", name, NULL))
return (-1);
return (tar_file(tar, filename));
}