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
|
/* $Source: /u/mark/src/pax/RCS/create.c,v $
*
* $Revision: 1.3 $
*
* create.c - Create a tape archive.
*
* DESCRIPTION
*
* These functions are used to create/write and archive from an set of
* named files.
*
* AUTHOR
*
* Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
*
* Sponsored by The USENIX Association for public distribution.
*
* Copyright (c) 1989 Mark H. Colburn.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice is duplicated in all such
* forms and that any documentation, advertising materials, and other
* materials related to such distribution and use acknowledge that the
* software was developed * by Mark H. Colburn and sponsored by The
* USENIX Association.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Log: create.c,v $
* Revision 1.3 89/02/12 10:29:37 mark
* Fixed misspelling of Replstr
*
* Revision 1.2 89/02/12 10:04:17 mark
* 1.2 release fixes
*
* Revision 1.1 88/12/23 18:02:06 mark
* Initial revision
*
*/
#ifndef lint
static char *ident = "$Id: create.c,v 1.3 89/02/12 10:29:37 mark Exp Locker: mark $";
static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
#endif /* ! lint */
/* Headers */
#include "pax.h"
/* Function Prototypes */
#ifdef __STDC__
static void writetar(char *, Stat *);
static void writecpio(char *, Stat *);
static char tartype(int);
#else /* !__STDC__ */
static void writetar();
static void writecpio();
static char tartype();
#endif /* __STDC__ */
/* create_archive - create a tar archive.
*
* DESCRIPTION
*
* Create_archive is used as an entry point to both create and append
* archives. Create archive goes through the files specified by the
* user and writes each one to the archive if it can. Create_archive
* knows how to write both cpio and tar headers and the padding which
* is needed for each type of archive.
*
* RETURNS
*
* Always returns 0
*/
#ifdef __STDC__
int create_archive(void)
#else
int create_archive()
#endif
{
char name[PATH_MAX + 1];
Stat sb;
int fd;
while (name_next(name, &sb) != -1) {
if ((fd = openin(name, &sb)) < 0) {
/* FIXME: pax wants to exit here??? */
continue;
}
if (rplhead != (Replstr *)NULL) {
rpl_name(name);
if (strlen(name) == 0) {
continue;
}
}
if (get_disposition("add", name) || get_newname(name, sizeof(name))) {
/* skip file... */
if (fd) {
close(fd);
}
continue;
}
if (!f_link && sb.sb_nlink > 1) {
if (islink(name, &sb)) {
sb.sb_size = 0;
}
linkto(name, &sb);
}
if (ar_format == TAR) {
writetar(name, &sb);
} else {
writecpio(name, &sb);
}
if (fd) {
outdata(fd, name, sb.sb_size);
}
if (f_verbose) {
print_entry(name, &sb);
}
}
write_eot();
close_archive();
return (0);
}
/* writetar - write a header block for a tar file
*
* DESCRIPTION
*
* Make a header block for the file name whose stat info is in st.
* Return header pointer for success, NULL if the name is too long.
*
* The tar header block is structured as follows:
*
* FIELD NAME OFFSET SIZE
* -------------|---------------|------
* name 0 100
* mode 100 8
* uid 108 8
* gid 116 8
* size 124 12
* mtime 136 12
* chksum 148 8
* typeflag 156 1
* linkname 157 100
* magic 257 6
* version 263 2
* uname 265 32
* gname 297 32
* devmajor 329 8
* devminor 337 8
* prefix 345 155
*
* PARAMETERS
*
* char *name - name of file to create a header block for
* Stat *asb - pointer to the stat structure for the named file
*
*/
#ifdef __STDC__
static void writetar(char *name, Stat *asb)
#else
static void writetar(name, asb)
char *name;
Stat *asb;
#endif
{
char *p;
char *prefix = (char *)NULL;
int i;
int sum;
char hdr[BLOCKSIZE];
Link *from;
memset(hdr, 0, BLOCKSIZE);
if (strlen(name) > 255) {
warn(name, "name too long");
return;
}
/*
* If the pathname is longer than TNAMLEN, but less than 255, then
* we can split it up into the prefix and the filename.
*/
if (strlen(name) > 100) {
prefix = name;
name += 155;
while (name > prefix && *name != '/') {
name--;
}
/* no slash found....hmmm.... */
if (name == prefix) {
warn(prefix, "Name too long");
return;
}
*name++ = '\0';
}
#ifdef S_IFLNK
if ((asb->sb_mode & S_IFMT) == S_IFLNK) {
strcpy(&hdr[157], asb->sb_link);
asb->sb_size = 0;
}
#endif
strcpy(hdr, name);
sprintf(&hdr[100], "%06o \0", asb->sb_mode & ~S_IFMT);
sprintf(&hdr[108], "%06o \0", asb->sb_uid);
sprintf(&hdr[116], "%06o \0", asb->sb_gid);
sprintf(&hdr[124], "%011lo ", (long) asb->sb_size);
sprintf(&hdr[136], "%011lo ", (long) asb->sb_mtime);
strncpy(&hdr[148], " ", 8);
hdr[156] = tartype(asb->sb_mode);
if (asb->sb_nlink > 1 && (from = linkfrom(name, asb)) != (Link *)NULL) {
strcpy(&hdr[157], from->l_name);
hdr[156] = LNKTYPE;
}
strcpy(&hdr[257], TMAGIC);
strncpy(&hdr[263], TVERSION, 2);
strcpy(&hdr[265], finduname((int) asb->sb_uid));
strcpy(&hdr[297], findgname((int) asb->sb_gid));
sprintf(&hdr[329], "%06o \0", major(asb->sb_rdev));
sprintf(&hdr[337], "%06o \0", minor(asb->sb_rdev));
if (prefix != (char *)NULL) {
strncpy(&hdr[345], prefix, 155);
}
/* Calculate the checksum */
sum = 0;
p = hdr;
for (i = 0; i < 500; i++) {
sum += 0xFF & *p++;
}
/* Fill in the checksum field. */
sprintf(&hdr[148], "%06o \0", sum);
outwrite(hdr, BLOCKSIZE);
}
/* tartype - return tar file type from file mode
*
* DESCRIPTION
*
* tartype returns the character which represents the type of file
* indicated by "mode".
*
* PARAMETERS
*
* int mode - file mode from a stat block
*
* RETURNS
*
* The character which represents the particular file type in the
* ustar standard headers.
*/
#ifdef __STDC__
static char tartype(int mode)
#else
static char tartype(mode)
int mode;
#endif
{
switch (mode & S_IFMT) {
#ifdef S_IFCTG
case S_IFCTG:
return(CONTTYPE);
#endif
case S_IFDIR:
return (DIRTYPE);
#ifdef S_IFLNK
case S_IFLNK:
return (SYMTYPE);
#endif
#ifdef S_IFFIFO
case S_IFIFO:
return (FIFOTYPE);
#endif
#ifdef S_IFCHR
case S_IFCHR:
return (CHRTYPE);
#endif
#ifdef S_IFBLK
case S_IFBLK:
return (BLKTYPE);
#endif
default:
return (REGTYPE);
}
}
/* writecpio - write a cpio archive header
*
* DESCRIPTION
*
* Writes a new CPIO style archive header for the file specified.
*
* PARAMETERS
*
* char *name - name of file to create a header block for
* Stat *asb - pointer to the stat structure for the named file
*/
#ifdef __STDC__
static void writecpio(char *name, Stat *asb)
#else
static void writecpio(name, asb)
char *name;
Stat *asb;
#endif
{
uint namelen;
char header[M_STRLEN + H_STRLEN + 1];
namelen = (uint) strlen(name) + 1;
strcpy(header, M_ASCII);
sprintf(header + M_STRLEN, "%06o%06o%06o%06o%06o",
USH(asb->sb_dev), USH(asb->sb_ino), USH(asb->sb_mode),
USH(asb->sb_uid), USH(asb->sb_gid));
sprintf(header + M_STRLEN + 30, "%06o%06o%011lo%06o%011lo",
#ifdef _POSIX_SOURCE
USH(asb->sb_nlink), USH(0),
#else
USH(asb->sb_nlink), USH(asb->sb_rdev),
#endif
f_mtime ? asb->sb_mtime : time((time_t *) 0),
namelen, asb->sb_size);
outwrite(header, M_STRLEN + H_STRLEN);
outwrite(name, namelen);
#ifdef S_IFLNK
if ((asb->sb_mode & S_IFMT) == S_IFLNK) {
outwrite(asb->sb_link, (uint) asb->sb_size);
}
#endif /* S_IFLNK */
}
|