1 /* $Source: /u/mark/src/pax/RCS/create.c,v $
5 * create.c - Create a tape archive.
9 * These functions are used to create/write and archive from an set of
14 * Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
16 * Sponsored by The USENIX Association for public distribution.
18 * Copyright (c) 1989 Mark H. Colburn.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms are permitted
22 * provided that the above copyright notice is duplicated in all such
23 * forms and that any documentation, advertising materials, and other
24 * materials related to such distribution and use acknowledge that the
25 * software was developed * by Mark H. Colburn and sponsored by The
28 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
33 * Revision 1.3 89/02/12 10:29:37 mark
34 * Fixed misspelling of Replstr
36 * Revision 1.2 89/02/12 10:04:17 mark
39 * Revision 1.1 88/12/23 18:02:06 mark
45 static char *ident = "$Id: create.c,v 1.3 89/02/12 10:29:37 mark Exp Locker: mark $";
46 static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
55 /* Function Prototypes */
59 static void writetar(char *, Stat *);
60 static void writecpio(char *, Stat *);
61 static char tartype(int);
65 static void writetar();
66 static void writecpio();
67 static char tartype();
72 /* create_archive - create a tar archive.
76 * Create_archive is used as an entry point to both create and append
77 * archives. Create archive goes through the files specified by the
78 * user and writes each one to the archive if it can. Create_archive
79 * knows how to write both cpio and tar headers and the padding which
80 * is needed for each type of archive.
89 int create_archive(void)
97 char name[PATH_MAX + 1];
101 while (name_next(name, &sb) != -1) {
102 if ((fd = openin(name, &sb)) < 0) {
103 /* FIXME: pax wants to exit here??? */
107 if (rplhead != (Replstr *)NULL) {
109 if (strlen(name) == 0) {
113 if (get_disposition("add", name) || get_newname(name, sizeof(name))) {
121 if (!f_link && sb.sb_nlink > 1) {
122 if (islink(name, &sb)) {
127 if (ar_format == TAR) {
130 writecpio(name, &sb);
133 outdata(fd, name, sb.sb_size);
136 print_entry(name, &sb);
146 /* writetar - write a header block for a tar file
150 * Make a header block for the file name whose stat info is in st.
151 * Return header pointer for success, NULL if the name is too long.
153 * The tar header block is structured as follows:
155 * FIELD NAME OFFSET SIZE
156 * -------------|---------------|------
176 * char *name - name of file to create a header block for
177 * Stat *asb - pointer to the stat structure for the named file
183 static void writetar(char *name, Stat *asb)
187 static void writetar(name, asb)
194 char *prefix = (char *)NULL;
200 memset(hdr, 0, BLOCKSIZE);
201 if (strlen(name) > 255) {
202 warn(name, "name too long");
207 * If the pathname is longer than TNAMLEN, but less than 255, then
208 * we can split it up into the prefix and the filename.
210 if (strlen(name) > 100) {
213 while (name > prefix && *name != '/') {
217 /* no slash found....hmmm.... */
218 if (name == prefix) {
219 warn(prefix, "Name too long");
226 if ((asb->sb_mode & S_IFMT) == S_IFLNK) {
227 strcpy(&hdr[157], asb->sb_link);
232 sprintf(&hdr[100], "%06o \0", asb->sb_mode & ~S_IFMT);
233 sprintf(&hdr[108], "%06o \0", asb->sb_uid);
234 sprintf(&hdr[116], "%06o \0", asb->sb_gid);
235 sprintf(&hdr[124], "%011lo ", (long) asb->sb_size);
236 sprintf(&hdr[136], "%011lo ", (long) asb->sb_mtime);
237 strncpy(&hdr[148], " ", 8);
238 hdr[156] = tartype(asb->sb_mode);
239 if (asb->sb_nlink > 1 && (from = linkfrom(name, asb)) != (Link *)NULL) {
240 strcpy(&hdr[157], from->l_name);
243 strcpy(&hdr[257], TMAGIC);
244 strncpy(&hdr[263], TVERSION, 2);
245 strcpy(&hdr[265], finduname((int) asb->sb_uid));
246 strcpy(&hdr[297], findgname((int) asb->sb_gid));
247 sprintf(&hdr[329], "%06o \0", major(asb->sb_rdev));
248 sprintf(&hdr[337], "%06o \0", minor(asb->sb_rdev));
249 if (prefix != (char *)NULL) {
250 strncpy(&hdr[345], prefix, 155);
253 /* Calculate the checksum */
257 for (i = 0; i < 500; i++) {
261 /* Fill in the checksum field. */
263 sprintf(&hdr[148], "%06o \0", sum);
265 outwrite(hdr, BLOCKSIZE);
269 /* tartype - return tar file type from file mode
273 * tartype returns the character which represents the type of file
274 * indicated by "mode".
278 * int mode - file mode from a stat block
282 * The character which represents the particular file type in the
283 * ustar standard headers.
288 static char tartype(int mode)
292 static char tartype(mode)
297 switch (mode & S_IFMT) {
333 /* writecpio - write a cpio archive header
337 * Writes a new CPIO style archive header for the file specified.
341 * char *name - name of file to create a header block for
342 * Stat *asb - pointer to the stat structure for the named file
347 static void writecpio(char *name, Stat *asb)
351 static void writecpio(name, asb)
358 char header[M_STRLEN + H_STRLEN + 1];
360 namelen = (uint) strlen(name) + 1;
361 strcpy(header, M_ASCII);
362 sprintf(header + M_STRLEN, "%06o%06o%06o%06o%06o",
363 USH(asb->sb_dev), USH(asb->sb_ino), USH(asb->sb_mode),
364 USH(asb->sb_uid), USH(asb->sb_gid));
365 sprintf(header + M_STRLEN + 30, "%06o%06o%011lo%06o%011lo",
367 USH(asb->sb_nlink), USH(0),
369 USH(asb->sb_nlink), USH(asb->sb_rdev),
371 f_mtime ? asb->sb_mtime : time((time_t *) 0),
372 namelen, asb->sb_size);
373 outwrite(header, M_STRLEN + H_STRLEN);
374 outwrite(name, namelen);
376 if ((asb->sb_mode & S_IFMT) == S_IFLNK) {
377 outwrite(asb->sb_link, (uint) asb->sb_size);