annotate toys/posix/cpio.c @ 1468:6fd5e556f89d draft

Ignore -m option to cpio, it's what we do anyway so it's not an error.
author Rob Landley <rob@landley.net>
date Tue, 09 Sep 2014 23:44:48 -0500
parents 2148d1502264
children 84727402ee86
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
1 /* cpio.c - a basic cpio
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
2 *
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
3 * Written 2013 AD by Isaac Dunham; this code is placed under the
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
4 * same license as toybox or as CC0, at your option.
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
5 *
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
7 * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
8 *
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
9 * Yes, that's SUSv2, the newer standards removed it around the time RPM
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
10 * and initramfs started heavily using this archive format.
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
11 *
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
12 * Modern cpio expanded header to 110 bytes (first field 6 bytes, rest are 8).
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
13 * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
14 * rdevmajor rdevminor namesize check
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
15
1468
6fd5e556f89d Ignore -m option to cpio, it's what we do anyway so it's not an error.
Rob Landley <rob@landley.net>
parents: 1465
diff changeset
16 USE_CPIO(NEWTOY(cpio, "mduH:i|t|F:v(verbose)o|[!io][!ot]", TOYFLAG_BIN))
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
17
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
18 config CPIO
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
19 bool "cpio"
1229
34d4ee60e78f Promote cpio out of pending.
Rob Landley <rob@landley.net>
parents: 1228
diff changeset
20 default y
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
21 help
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
22 usage: cpio -{o|t|i} [-v] [--verbose] [-F FILE] [ignored: -du -H newc]
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
23
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
24 copy files into and out of a "newc" format cpio archive
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
25
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
26 -F FILE use archive FILE instead of stdin/stdout
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
27 -i extract from archive into file system (stdin=archive)
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
28 -o create archive (stdin=list of files, stdout=archive)
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
29 -t test files (list only, stdin=archive, stdout=list of files)
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
30 -v verbose (list files during create/extract)
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
31 */
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
32
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
33 #define FOR_cpio
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
34 #include "toys.h"
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
35
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
36 GLOBALS(
1135
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
37 char *archive;
581c35250cff Minor drive-by cleanups to cpio. Whitespace, curly brackets, replace %4 with &3, turn a switch/case into if/else.
Rob Landley <rob@landley.net>
parents: 1121
diff changeset
38 char *fmt;
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
39 )
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
40
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
41 // Read strings, tail padded to 4 byte alignment. Argument "align" is amount
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
42 // by which start of string isn't aligned (usually 0, but header is 110 bytes
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
43 // which is 2 bytes off because the first field wasn't expanded from 6 to 8).
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
44 static char *strpad(int fd, unsigned len, unsigned align)
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
45 {
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
46 char *str;
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
47
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
48 align = (align + len) & 3;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
49 if (align) len += (4-align);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
50 xreadall(fd, str = xmalloc(len+1), len);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
51 str[len]=0; // redundant, in case archive is bad
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
52
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
53 return str;
1221
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
54 }
8451065c5a19 Cleanup cpio: fiddle with help text, make option parsing require/exclude -iot combinations, move loopfiles_stdin() down after write_cpio_member() so we can hardwire it instead of using a function pointer that only ever has one value.
Rob Landley <rob@landley.net>
parents: 1220
diff changeset
55
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
56 //convert hex to uint; mostly to allow using bits of non-terminated strings
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
57 unsigned x8u(char *hex)
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
58 {
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
59 unsigned val, inpos = 8, outpos;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
60 char pattern[6];
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
61
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
62 while (*hex == '0') {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
63 hex++;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
64 if (!--inpos) return 0;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
65 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
66 // Because scanf gratuitously treats %*X differently than printf does.
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
67 sprintf(pattern, "%%%dX%%n", inpos);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
68 sscanf(hex, pattern, &val, &outpos);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
69 if (inpos != outpos) error_exit("bad header");
1220
10d4dd6621cb Patch from Isaac Dunham to add cpio -d, with a few tweaks by me.
Rob Landley <rob@landley.net>
parents: 1135
diff changeset
70
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
71 return val;
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
72 }
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
73
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
74 void cpio_main(void)
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
75 {
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
76 int afd;
1223
a7d5b93111a5 Next round of cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1222
diff changeset
77
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
78 // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout.
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
79
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
80 afd = toys.optflags & FLAG_o;
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
81 if (TT.archive) {
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
82 int perm = (toys.optflags & FLAG_o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
83
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
84 afd = xcreate(TT.archive, perm, 0644);
1121
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
85 }
977f0a4dc562 Support -F, and ignore -u since that's what we do anyway.
Isaac Dunham <ibid.ag@gmail.com>
parents: 1098
diff changeset
86
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
87 // read cpio archive
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
88
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
89 if (toys.optflags & (FLAG_i|FLAG_t)) for (;;) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
90 char *name, *tofree, *data;
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
91 unsigned size, mode, uid, gid, timestamp;
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
92 int test = toys.optflags & FLAG_t, err = 0;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
93
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
94 // Read header and name.
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
95 xreadall(afd, toybuf, 110);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
96 tofree = name = strpad(afd, x8u(toybuf+94), 110);
1465
2148d1502264 Fix more memory leaks reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1331
diff changeset
97 if (!strcmp("TRAILER!!!", name)) {
2148d1502264 Fix more memory leaks reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1331
diff changeset
98 if (CFG_TOYBOX_FREE) free(tofree);
2148d1502264 Fix more memory leaks reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1331
diff changeset
99 break;
2148d1502264 Fix more memory leaks reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1331
diff changeset
100 }
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
101
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
102 // If you want to extract absolute paths, "cd /" and run cpio.
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
103 while (*name == '/') name++;
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
104 // TODO: remove .. entries
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
105
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
106 size = x8u(toybuf+54);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
107 mode = x8u(toybuf+14);
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
108 uid = x8u(toybuf+30);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
109 gid = x8u(toybuf+38);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
110 timestamp = x8u(toybuf+46); // unsigned 32 bit, so year 2100 problem
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
111
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
112 if (toys.optflags & (FLAG_t|FLAG_v)) puts(name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
113
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
114 if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
115 perror_msg("mkpath '%s'", name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
116 test++;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
117 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
118
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
119 // Consume entire record even if it couldn't create file, so we're
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
120 // properly aligned with next file.
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
121
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
122 if (S_ISDIR(mode)) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
123 if (!test) err = mkdir(name, mode);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
124 } else if (S_ISLNK(mode)) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
125 data = strpad(afd, size, 0);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
126 if (!test) err = symlink(data, name);
1465
2148d1502264 Fix more memory leaks reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1331
diff changeset
127 free(data);
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
128 // Can't get a filehandle to a symlink, so do special chown
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
129 if (!err && !getpid()) err = lchown(name, uid, gid);
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
130 } else if (S_ISREG(mode)) {
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
131 int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
132
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
133 // If write fails, we still need to read/discard data to continue with
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
134 // archive. Since doing so overwrites errno, report error now
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
135 if (fd < 0) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
136 perror_msg("create %s", name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
137 test++;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
138 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
139
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
140 data = toybuf;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
141 while (size) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
142 if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
143 else xreadall(afd, toybuf, sizeof(toybuf));
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
144 if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
145 if (data != toybuf) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
146 free(data);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
147 break;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
148 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
149 size -= sizeof(toybuf);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
150 }
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
151
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
152 if (!test) {
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
153 // set owner, restore dropped suid bit
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
154 if (!getpid()) {
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
155 err = fchown(fd, uid, gid);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
156 if (!err) err = fchmod(fd, mode);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
157 }
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
158 close(fd);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
159 }
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
160 } else if (!test)
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
161 err = mknod(name, mode, makedev(x8u(toybuf+62), x8u(toybuf+70)));
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
162
1271
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
163 // Set ownership and timestamp.
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
164 if (!test && !err) {
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
165 // Creading dir/dev doesn't give us a filehandle, we have to refer to it
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
166 // by name to chown/utime, but how do we know it's the same item?
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
167 // Check that we at least have the right type of entity open, and do
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
168 // NOT restore dropped suid bit in this case.
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
169 if (!S_ISREG(mode) && !S_ISLNK(mode) && !getpid()) {
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
170 int fd = open(name, O_WRONLY|O_NOFOLLOW);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
171 struct stat st;
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
172
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
173 if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == mode)
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
174 err = fchown(fd, uid, gid);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
175 else err = 1;
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
176
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
177 close(fd);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
178 }
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
179
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
180 // set timestamp
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
181 if (!err) {
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
182 struct timespec times[2];
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
183
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
184 memset(times, 0, sizeof(struct timespec)*2);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
185 times[0].tv_sec = times[1].tv_sec = timestamp;
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
186 err = utimensat(AT_FDCWD, name, times, AT_SYMLINK_NOFOLLOW);
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
187 }
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
188 }
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
189
c214de62b18b Teach cpio to set uid/gid and timestamp. (Timestamp has year 2100 problem.)
Rob Landley <rob@landley.net>
parents: 1231
diff changeset
190 if (err) perror_msg("'%s'", name);
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
191 free(tofree);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
192
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
193 // Output cpio archive
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
194
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
195 } else {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
196 char *name = 0;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
197 size_t size = 0;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
198
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
199 for (;;) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
200 struct stat st;
1230
c78ffc6c330a More cpio bugfixes from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1229
diff changeset
201 unsigned nlen, error = 0, zero = 0;
1228
2366f9d73745 Several cpio bugfixes spotted by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1227
diff changeset
202 int len, fd = -1;
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
203 ssize_t llen;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
204
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
205 len = getline(&name, &size, stdin);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
206 if (len<1) break;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
207 if (name[len-1] == '\n') name[--len] = 0;
1230
c78ffc6c330a More cpio bugfixes from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1229
diff changeset
208 nlen = len+1;
1331
11fe363078b8 cpio: archive more files
Isaac Dunham <ibid.ag@gmail.com>
parents: 1271
diff changeset
209 if (lstat(name, &st) || (S_ISREG(st.st_mode)
11fe363078b8 cpio: archive more files
Isaac Dunham <ibid.ag@gmail.com>
parents: 1271
diff changeset
210 && st.st_size && (fd = open(name, O_RDONLY))<0))
1228
2366f9d73745 Several cpio bugfixes spotted by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1227
diff changeset
211 {
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
212 perror_msg("%s", name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
213 continue;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
214 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
215
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
216 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
217 if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
218 else {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
219 llen = sprintf(toybuf,
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
220 "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
221 (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
222 (int)st.st_mtime, (int)st.st_size, major(st.st_dev),
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
223 minor(st.st_dev), major(st.st_rdev), minor(st.st_rdev), nlen, 0);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
224 xwrite(afd, toybuf, llen);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
225 xwrite(afd, name, nlen);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
226
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
227 // NUL Pad header up to 4 multiple bytes.
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
228 llen = (llen + nlen) & 3;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
229 if (llen) xwrite(afd, &zero, 4-llen);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
230
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
231 // Write out body for symlink or regular file
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
232 llen = st.st_size;
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
233 if (S_ISLNK(st.st_mode)) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
234 if (readlink(name, toybuf, sizeof(toybuf)-1) == llen)
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
235 xwrite(afd, toybuf, llen);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
236 else perror_msg("readlink '%s'", name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
237 } else while (llen) {
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
238 nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
1228
2366f9d73745 Several cpio bugfixes spotted by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1227
diff changeset
239 llen -= nlen;
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
240 // If read fails, write anyway (already wrote size in header)
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
241 if (nlen != readall(fd, toybuf, nlen))
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
242 if (!error++) perror_msg("bad read from file '%s'", name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
243 xwrite(afd, toybuf, nlen);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
244 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
245 llen = st.st_size & 3;
1228
2366f9d73745 Several cpio bugfixes spotted by Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1227
diff changeset
246 if (llen) write(afd, &zero, 4-llen);
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
247 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
248 close(fd);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
249 }
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
250 free(name);
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
251
1230
c78ffc6c330a More cpio bugfixes from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1229
diff changeset
252 memset(toybuf, 0, sizeof(toybuf));
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
253 xwrite(afd, toybuf,
1230
c78ffc6c330a More cpio bugfixes from Isaac Dunham.
Rob Landley <rob@landley.net>
parents: 1229
diff changeset
254 sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0)+4);
1227
903acf85bd1d Most of the remaining cpio cleanup.
Rob Landley <rob@landley.net>
parents: 1223
diff changeset
255 }
1465
2148d1502264 Fix more memory leaks reported by Ashwini Sharma.
Rob Landley <rob@landley.net>
parents: 1331
diff changeset
256 if (TT.archive) xclose(afd);
1087
b73a61542297 I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff changeset
257 }