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, "duH:i|t|F:o|v(verbose)[!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).
43 static char *strpad(int fd, unsigned len, unsigned align)
47 align = (align + len) & 3;
48 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;
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)) break;
99 // If you want to extract absolute paths, "cd /" and run cpio.
100 while (*name == '/') name++;
102 // Align to 4 bytes. Note header is 110 bytes which is 2 bytes over.
104 size = x8u(toybuf+54);
105 mode = x8u(toybuf+14);
107 if (toys.optflags & (FLAG_t|FLAG_v)) puts(name);
109 if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) {
110 perror_msg("mkpath '%s'", name);
114 // Consume entire record even if it couldn't create file, so we're
115 // properly aligned with next file.
118 if (!test) err = mkdir(name, mode);
119 } else if (S_ISLNK(mode)) {
120 data = strpad(afd, size, 0);
121 if (!test) err = symlink(data, name);
122 } else if (S_ISREG(mode)) {
125 // If write fails, we still need to read/discard data to continue with
126 // archive. Since doing so overwrites errno, report error now
127 fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
129 perror_msg("create %s", name);
135 if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
136 else xreadall(afd, toybuf, sizeof(toybuf));
137 if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
138 if (data != toybuf) {
142 size -= sizeof(toybuf);
146 err = mknod(name, mode, makedev(x8u(toybuf+62), x8u(toybuf+70)));
148 if (err<0) perror_msg("create '%s'", name);
151 // Output cpio archive
159 unsigned nlen = strlen(name)+1, error = 0, zero = 0;
163 len = getline(&name, &size, stdin);
165 if (name[len-1] == '\n') name[--len] = 0;
167 || (S_ISREG(st.st_mode) && (fd = open(name, O_RDONLY))<0))
169 perror_msg("%s", name);
173 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
174 if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
176 llen = sprintf(toybuf,
177 "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
178 (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
179 (int)st.st_mtime, (int)st.st_size, major(st.st_dev),
180 minor(st.st_dev), major(st.st_rdev), minor(st.st_rdev), nlen, 0);
181 xwrite(afd, toybuf, llen);
182 xwrite(afd, name, nlen);
184 // NUL Pad header up to 4 multiple bytes.
185 llen = (llen + nlen) & 3;
186 if (llen) xwrite(afd, &zero, 4-llen);
188 // Write out body for symlink or regular file
190 if (S_ISLNK(st.st_mode)) {
191 if (readlink(name, toybuf, sizeof(toybuf)-1) == llen)
192 xwrite(afd, toybuf, llen);
193 else perror_msg("readlink '%s'", name);
194 } else while (llen) {
195 nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
197 // If read fails, write anyway (already wrote size in header)
198 if (nlen != readall(fd, toybuf, nlen))
199 if (!error++) perror_msg("bad read from file '%s'", name);
200 xwrite(afd, toybuf, nlen);
202 llen = st.st_size & 3;
203 if (llen) write(afd, &zero, 4-llen);
210 sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!%c%c%c",
211 1, 0x0b, 0, 0, 0, 0));