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 * Portions Copyright 2015 by Frontier Silicon Ltd.
8 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
9 * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
11 * Yes, that's SUSv2, the newer standards removed it around the time RPM
12 * and initramfs started heavily using this archive format.
14 * Modern cpio expanded header to 110 bytes (first field 6 bytes, rest are 8).
15 * In order: magic ino mode uid gid nlink mtime filesize devmajor devminor
16 * rdevmajor rdevminor namesize check
17 * This is the equiavlent of mode -H newc when using GNU CPIO.
19 USE_CPIO(NEWTOY(cpio, "(no-preserve-owner)mduH:p:|i|t|F:v(verbose)o|[!pio][!pot][!pF]", TOYFLAG_BIN))
25 usage: cpio -{o|t|i|p DEST} [-v] [--verbose] [-F FILE] [--no-preserve-owner]
26 [ignored: -mdu -H newc]
28 copy files into and out of a "newc" format cpio archive
30 -F FILE use archive FILE instead of stdin/stdout
31 -p DEST copy-pass mode, copy stdin file list to directory DEST
32 -i extract from archive into file system (stdin=archive)
33 -o create archive (stdin=list of files, stdout=archive)
34 -t test files (list only, stdin=archive, stdout=list of files)
35 -v verbose (list files during create/extract)
36 --no-preserve-owner (don't set ownership during extract)
48 // Read strings, tail padded to 4 byte alignment. Argument "align" is amount
49 // by which start of string isn't aligned (usually 0, but header is 110 bytes
50 // which is 2 bytes off because the first field wasn't expanded from 6 to 8).
51 static char *strpad(int fd, unsigned len, unsigned align)
55 align = (align + len) & 3;
56 if (align) len += (4-align);
57 xreadall(fd, str = xmalloc(len+1), len);
58 str[len]=0; // redundant, in case archive is bad
63 //convert hex to uint; mostly to allow using bits of non-terminated strings
64 unsigned x8u(char *hex)
66 unsigned val, inpos = 8, outpos;
71 if (!--inpos) return 0;
73 // Because scanf gratuitously treats %*X differently than printf does.
74 sprintf(pattern, "%%%dX%%n", inpos);
75 sscanf(hex, pattern, &val, &outpos);
76 if (inpos != outpos) error_exit("bad header");
83 // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout.
84 int pipe, afd = toys.optflags & FLAG_o;
87 // In passthrough mode, parent stays in original dir and generates archive
88 // to pipe, child does chdir to new dir and reads archive from stdin (pipe).
91 // xpopen() doesn't return from child due to vfork(), instead restarts
92 // with !toys.stacktop
93 pid = xpopen(0, &pipe, 0);
97 toys.optflags |= FLAG_i;
103 int perm = (toys.optflags & FLAG_o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
105 afd = xcreate(TT.archive, perm, 0644);
110 if (toys.optflags & (FLAG_i|FLAG_t)) for (;;) {
111 char *name, *tofree, *data;
112 unsigned size, mode, uid, gid, timestamp;
113 int test = toys.optflags & FLAG_t, err = 0;
115 // Read header and name.
116 xreadall(afd, toybuf, 110);
117 tofree = name = strpad(afd, x8u(toybuf+94), 110);
118 if (!strcmp("TRAILER!!!", name)) {
119 if (CFG_TOYBOX_FREE) free(tofree);
123 // If you want to extract absolute paths, "cd /" and run cpio.
124 while (*name == '/') name++;
125 // TODO: remove .. entries
127 size = x8u(toybuf+54);
128 mode = x8u(toybuf+14);
129 uid = x8u(toybuf+22);
130 gid = x8u(toybuf+30);
131 timestamp = x8u(toybuf+46); // unsigned 32 bit, so year 2100 problem
133 if (toys.optflags & (FLAG_t|FLAG_v)) puts(name);
135 if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) {
136 perror_msg("mkpath '%s'", name);
140 // Consume entire record even if it couldn't create file, so we're
141 // properly aligned with next file.
144 if (!test) err = mkdir(name, mode);
145 } else if (S_ISLNK(mode)) {
146 data = strpad(afd, size, 0);
147 if (!test) err = symlink(data, name);
149 // Can't get a filehandle to a symlink, so do special chown
150 if (!err && !geteuid() && !(toys.optflags & FLAG_no_preserve_owner))
151 err = lchown(name, uid, gid);
152 } else if (S_ISREG(mode)) {
153 int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
155 // If write fails, we still need to read/discard data to continue with
156 // archive. Since doing so overwrites errno, report error now
158 perror_msg("create %s", name);
164 if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
165 else xreadall(afd, toybuf, sizeof(toybuf));
166 if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
167 if (data != toybuf) {
171 size -= sizeof(toybuf);
175 // set owner, restore dropped suid bit
176 if (!geteuid() && !(toys.optflags & FLAG_no_preserve_owner)) {
177 err = fchown(fd, uid, gid);
178 if (!err) err = fchmod(fd, mode);
183 err = mknod(name, mode, makedev(x8u(toybuf+78), x8u(toybuf+86)));
185 // Set ownership and timestamp.
187 // Creading dir/dev doesn't give us a filehandle, we have to refer to it
188 // by name to chown/utime, but how do we know it's the same item?
189 // Check that we at least have the right type of entity open, and do
190 // NOT restore dropped suid bit in this case.
191 if (!S_ISREG(mode) && !S_ISLNK(mode) && !geteuid()
192 && !(toys.optflags & FLAG_no_preserve_owner))
194 int fd = open(name, O_RDONLY|O_NOFOLLOW);
197 if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == (mode&S_IFMT))
198 err = fchown(fd, uid, gid);
206 struct timespec times[2];
208 memset(times, 0, sizeof(struct timespec)*2);
209 times[0].tv_sec = times[1].tv_sec = timestamp;
210 err = utimensat(AT_FDCWD, name, times, AT_SYMLINK_NOFOLLOW);
214 if (err) perror_msg("'%s'", name);
217 // Output cpio archive
225 unsigned nlen, error = 0, zero = 0;
229 len = getline(&name, &size, stdin);
231 if (name[len-1] == '\n') name[--len] = 0;
233 if (lstat(name, &st) || (S_ISREG(st.st_mode)
234 && st.st_size && (fd = open(name, O_RDONLY))<0))
236 perror_msg("%s", name);
240 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
241 if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
243 llen = sprintf(toybuf,
244 "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
245 (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
246 (int)st.st_mtime, (int)st.st_size, major(st.st_dev),
247 minor(st.st_dev), major(st.st_rdev), minor(st.st_rdev), nlen, 0);
248 xwrite(afd, toybuf, llen);
249 xwrite(afd, name, nlen);
251 // NUL Pad header up to 4 multiple bytes.
252 llen = (llen + nlen) & 3;
253 if (llen) xwrite(afd, &zero, 4-llen);
255 // Write out body for symlink or regular file
257 if (S_ISLNK(st.st_mode)) {
258 if (readlink(name, toybuf, sizeof(toybuf)-1) == llen)
259 xwrite(afd, toybuf, llen);
260 else perror_msg("readlink '%s'", name);
261 } else while (llen) {
262 nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
264 // If read fails, write anyway (already wrote size in header)
265 if (nlen != readall(fd, toybuf, nlen))
266 if (!error++) perror_msg("bad read from file '%s'", name);
267 xwrite(afd, toybuf, nlen);
269 llen = st.st_size & 3;
270 if (llen) xwrite(afd, &zero, 4-llen);
276 memset(toybuf, 0, sizeof(toybuf));
278 sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0)+4);
280 if (TT.archive) xclose(afd);
282 if (TT.pass) toys.exitval |= xpclose(pid, pipe);