1 /* cpio.c - a basic cpio
3 * Written 2013 AD by Isaac Dunham; this code is placed under the
4 * same license as toybox or as CC0, at your option.
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
7 * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
9 * Yes, that's SUSv2, the newer standards removed it around the time RPM
10 * and initramfs started heavily using this archive format.
12 * Modern cpio expanded header to 110 bytes (first field 6 bytes, rest are 8).
13 * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor
14 * rdevmajor rdevminor namesize check
16 USE_CPIO(NEWTOY(cpio, "mduH:i|t|F:v(verbose)o|[!io][!ot]", TOYFLAG_BIN))
22 usage: cpio -{o|t|i} [-v] [--verbose] [-F FILE] [ignored: -du -H newc]
24 copy files into and out of a "newc" format cpio archive
26 -F FILE use archive FILE instead of stdin/stdout
27 -i extract from archive into file system (stdin=archive)
28 -o create archive (stdin=list of files, stdout=archive)
29 -t test files (list only, stdin=archive, stdout=list of files)
30 -v verbose (list files during create/extract)
41 // Read strings, tail padded to 4 byte alignment. Argument "align" is amount
42 // by which start of string isn't aligned (usually 0, but header is 110 bytes
43 // which is 2 bytes off because the first field wasn't expanded from 6 to 8).
44 static char *strpad(int fd, unsigned len, unsigned align)
48 align = (align + len) & 3;
49 if (align) len += (4-align);
50 xreadall(fd, str = xmalloc(len+1), len);
51 str[len]=0; // redundant, in case archive is bad
56 //convert hex to uint; mostly to allow using bits of non-terminated strings
57 unsigned x8u(char *hex)
59 unsigned val, inpos = 8, outpos;
64 if (!--inpos) return 0;
66 // Because scanf gratuitously treats %*X differently than printf does.
67 sprintf(pattern, "%%%dX%%n", inpos);
68 sscanf(hex, pattern, &val, &outpos);
69 if (inpos != outpos) error_exit("bad header");
78 // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout.
80 afd = toys.optflags & FLAG_o;
82 int perm = (toys.optflags & FLAG_o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
84 afd = xcreate(TT.archive, perm, 0644);
89 if (toys.optflags & (FLAG_i|FLAG_t)) for (;;) {
90 char *name, *tofree, *data;
91 unsigned size, mode, uid, gid, timestamp;
92 int test = toys.optflags & FLAG_t, err = 0;
94 // Read header and name.
95 xreadall(afd, toybuf, 110);
96 tofree = name = strpad(afd, x8u(toybuf+94), 110);
97 if (!strcmp("TRAILER!!!", name)) {
98 if (CFG_TOYBOX_FREE) free(tofree);
102 // If you want to extract absolute paths, "cd /" and run cpio.
103 while (*name == '/') name++;
104 // TODO: remove .. entries
106 size = x8u(toybuf+54);
107 mode = x8u(toybuf+14);
108 uid = x8u(toybuf+30);
109 gid = x8u(toybuf+38);
110 timestamp = x8u(toybuf+46); // unsigned 32 bit, so year 2100 problem
112 if (toys.optflags & (FLAG_t|FLAG_v)) puts(name);
114 if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) {
115 perror_msg("mkpath '%s'", name);
119 // Consume entire record even if it couldn't create file, so we're
120 // properly aligned with next file.
123 if (!test) err = mkdir(name, mode);
124 } else if (S_ISLNK(mode)) {
125 data = strpad(afd, size, 0);
126 if (!test) err = symlink(data, name);
128 // Can't get a filehandle to a symlink, so do special chown
129 if (!err && !getpid()) err = lchown(name, uid, gid);
130 } else if (S_ISREG(mode)) {
131 int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
133 // If write fails, we still need to read/discard data to continue with
134 // archive. Since doing so overwrites errno, report error now
136 perror_msg("create %s", name);
142 if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
143 else xreadall(afd, toybuf, sizeof(toybuf));
144 if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
145 if (data != toybuf) {
149 size -= sizeof(toybuf);
153 // set owner, restore dropped suid bit
155 err = fchown(fd, uid, gid);
156 if (!err) err = fchmod(fd, mode);
161 err = mknod(name, mode, makedev(x8u(toybuf+62), x8u(toybuf+70)));
163 // Set ownership and timestamp.
165 // Creading dir/dev doesn't give us a filehandle, we have to refer to it
166 // by name to chown/utime, but how do we know it's the same item?
167 // Check that we at least have the right type of entity open, and do
168 // NOT restore dropped suid bit in this case.
169 if (!S_ISREG(mode) && !S_ISLNK(mode) && !getpid()) {
170 int fd = open(name, O_WRONLY|O_NOFOLLOW);
173 if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == mode)
174 err = fchown(fd, uid, gid);
182 struct timespec times[2];
184 memset(times, 0, sizeof(struct timespec)*2);
185 times[0].tv_sec = times[1].tv_sec = timestamp;
186 err = utimensat(AT_FDCWD, name, times, AT_SYMLINK_NOFOLLOW);
190 if (err) perror_msg("'%s'", name);
193 // Output cpio archive
201 unsigned nlen, error = 0, zero = 0;
205 len = getline(&name, &size, stdin);
207 if (name[len-1] == '\n') name[--len] = 0;
209 if (lstat(name, &st) || (S_ISREG(st.st_mode)
210 && st.st_size && (fd = open(name, O_RDONLY))<0))
212 perror_msg("%s", name);
216 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
217 if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
219 llen = sprintf(toybuf,
220 "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
221 (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
222 (int)st.st_mtime, (int)st.st_size, major(st.st_dev),
223 minor(st.st_dev), major(st.st_rdev), minor(st.st_rdev), nlen, 0);
224 xwrite(afd, toybuf, llen);
225 xwrite(afd, name, nlen);
227 // NUL Pad header up to 4 multiple bytes.
228 llen = (llen + nlen) & 3;
229 if (llen) xwrite(afd, &zero, 4-llen);
231 // Write out body for symlink or regular file
233 if (S_ISLNK(st.st_mode)) {
234 if (readlink(name, toybuf, sizeof(toybuf)-1) == llen)
235 xwrite(afd, toybuf, llen);
236 else perror_msg("readlink '%s'", name);
237 } else while (llen) {
238 nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
240 // If read fails, write anyway (already wrote size in header)
241 if (nlen != readall(fd, toybuf, nlen))
242 if (!error++) perror_msg("bad read from file '%s'", name);
243 xwrite(afd, toybuf, nlen);
245 llen = st.st_size & 3;
246 if (llen) write(afd, &zero, 4-llen);
252 memset(toybuf, 0, sizeof(toybuf));
254 sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0)+4);
256 if (TT.archive) xclose(afd);