blob: 958931e6b7eaf54060abc574b3e5e55f597560a5 [file] [log] [blame]
Isaac Dunhamc50057e2013-10-14 11:15:22 -05001/* cpio.c - a basic cpio
2 *
3 * Written 2013 AD by Isaac Dunham; this code is placed under the
4 * same license as toybox or as CC0, at your option.
Isaac Dunham59272f22013-11-16 10:37:49 -06005 *
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cpio.html
Rob Landleydba5a372014-03-24 08:19:21 -05007 * and http://pubs.opengroup.org/onlinepubs/7908799/xcu/cpio.html
Isaac Dunham59272f22013-11-16 10:37:49 -06008 *
Rob Landleydba5a372014-03-24 08:19:21 -05009 * Yes, that's SUSv2, the newer standards removed it around the time RPM
10 * and initramfs started heavily using this archive format.
11 *
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
Isaac Dunham59272f22013-11-16 10:37:49 -060015
Rob Landley392e1272014-03-26 06:07:06 -050016USE_CPIO(NEWTOY(cpio, "duH:i|t|F:v(verbose)o|[!io][!ot]", TOYFLAG_BIN))
Isaac Dunhamc50057e2013-10-14 11:15:22 -050017
18config CPIO
19 bool "cpio"
Rob Landleya2d55812014-03-25 07:35:56 -050020 default y
Isaac Dunhamc50057e2013-10-14 11:15:22 -050021 help
Rob Landleydba5a372014-03-24 08:19:21 -050022 usage: cpio -{o|t|i} [-v] [--verbose] [-F FILE] [ignored: -du -H newc]
Isaac Dunhamc50057e2013-10-14 11:15:22 -050023
Rob Landleybab79fe2014-03-13 19:42:42 -050024 copy files into and out of a "newc" format cpio archive
25
Rob Landleydba5a372014-03-24 08:19:21 -050026 -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)
Isaac Dunhamc50057e2013-10-14 11:15:22 -050031*/
Rob Landley83d3a0b2013-12-01 14:34:05 -060032
Isaac Dunhamc50057e2013-10-14 11:15:22 -050033#define FOR_cpio
34#include "toys.h"
35
36GLOBALS(
Rob Landley83d3a0b2013-12-01 14:34:05 -060037 char *archive;
38 char *fmt;
Isaac Dunhamc50057e2013-10-14 11:15:22 -050039)
40
Rob Landleydba5a372014-03-24 08:19:21 -050041// Read strings, tail padded to 4 byte alignment. Argument "align" is amount
Rob Landleyc54fdc92014-04-29 06:03:17 -050042// 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).
Rob Landleydba5a372014-03-24 08:19:21 -050044static char *strpad(int fd, unsigned len, unsigned align)
Isaac Dunhamc50057e2013-10-14 11:15:22 -050045{
Rob Landleydba5a372014-03-24 08:19:21 -050046 char *str;
Isaac Dunhamc50057e2013-10-14 11:15:22 -050047
Rob Landleydba5a372014-03-24 08:19:21 -050048 align = (align + len) & 3;
49 if (align) len += (4-align);
Rob Landleydba5a372014-03-24 08:19:21 -050050 xreadall(fd, str = xmalloc(len+1), len);
51 str[len]=0; // redundant, in case archive is bad
Rob Landley9ea99112014-03-15 15:41:09 -050052
Rob Landleydba5a372014-03-24 08:19:21 -050053 return str;
Rob Landleybab79fe2014-03-13 19:42:42 -050054}
55
Isaac Dunhamc50057e2013-10-14 11:15:22 -050056//convert hex to uint; mostly to allow using bits of non-terminated strings
Rob Landleydba5a372014-03-24 08:19:21 -050057unsigned x8u(char *hex)
Isaac Dunhamc50057e2013-10-14 11:15:22 -050058{
Rob Landleydba5a372014-03-24 08:19:21 -050059 unsigned val, inpos = 8, outpos;
60 char pattern[6];
Isaac Dunhamc50057e2013-10-14 11:15:22 -050061
Rob Landleydba5a372014-03-24 08:19:21 -050062 while (*hex == '0') {
63 hex++;
64 if (!--inpos) return 0;
Isaac Dunhamc50057e2013-10-14 11:15:22 -050065 }
Rob Landleydba5a372014-03-24 08:19:21 -050066 // 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");
Isaac Dunhamc50057e2013-10-14 11:15:22 -050070
Rob Landleydba5a372014-03-24 08:19:21 -050071 return val;
Isaac Dunhamc50057e2013-10-14 11:15:22 -050072}
73
74void cpio_main(void)
75{
Rob Landleydba5a372014-03-24 08:19:21 -050076 int afd;
Rob Landley9ea99112014-03-15 15:41:09 -050077
Rob Landleydba5a372014-03-24 08:19:21 -050078 // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout.
79
80 afd = toys.optflags & FLAG_o;
Isaac Dunham59272f22013-11-16 10:37:49 -060081 if (TT.archive) {
Rob Landleydba5a372014-03-24 08:19:21 -050082 int perm = (toys.optflags & FLAG_o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
83
84 afd = xcreate(TT.archive, perm, 0644);
Isaac Dunham59272f22013-11-16 10:37:49 -060085 }
86
Rob Landleydba5a372014-03-24 08:19:21 -050087 // read cpio archive
88
89 if (toys.optflags & (FLAG_i|FLAG_t)) for (;;) {
90 char *name, *tofree, *data;
Rob Landleyc54fdc92014-04-29 06:03:17 -050091 unsigned size, mode, uid, gid, timestamp;
Rob Landleydba5a372014-03-24 08:19:21 -050092 int test = toys.optflags & FLAG_t, err = 0;
93
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;
98
99 // If you want to extract absolute paths, "cd /" and run cpio.
100 while (*name == '/') name++;
Rob Landleyc54fdc92014-04-29 06:03:17 -0500101 // TODO: remove .. entries
Rob Landleydba5a372014-03-24 08:19:21 -0500102
103 size = x8u(toybuf+54);
104 mode = x8u(toybuf+14);
Rob Landleyc54fdc92014-04-29 06:03:17 -0500105 uid = x8u(toybuf+30);
106 gid = x8u(toybuf+38);
107 timestamp = x8u(toybuf+46); // unsigned 32 bit, so year 2100 problem
Rob Landleydba5a372014-03-24 08:19:21 -0500108
109 if (toys.optflags & (FLAG_t|FLAG_v)) puts(name);
110
111 if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) {
112 perror_msg("mkpath '%s'", name);
113 test++;
114 }
115
116 // Consume entire record even if it couldn't create file, so we're
117 // properly aligned with next file.
118
119 if (S_ISDIR(mode)) {
120 if (!test) err = mkdir(name, mode);
121 } else if (S_ISLNK(mode)) {
122 data = strpad(afd, size, 0);
123 if (!test) err = symlink(data, name);
Rob Landleyc54fdc92014-04-29 06:03:17 -0500124 // Can't get a filehandle to a symlink, so do special chown
125 if (!err && !getpid()) err = lchown(name, uid, gid);
Rob Landleydba5a372014-03-24 08:19:21 -0500126 } else if (S_ISREG(mode)) {
Rob Landleyc54fdc92014-04-29 06:03:17 -0500127 int fd = test ? 0 : open(name, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode);
Rob Landleydba5a372014-03-24 08:19:21 -0500128
129 // If write fails, we still need to read/discard data to continue with
130 // archive. Since doing so overwrites errno, report error now
Rob Landleydba5a372014-03-24 08:19:21 -0500131 if (fd < 0) {
132 perror_msg("create %s", name);
133 test++;
134 }
135
136 data = toybuf;
137 while (size) {
138 if (size < sizeof(toybuf)) data = strpad(afd, size, 0);
139 else xreadall(afd, toybuf, sizeof(toybuf));
140 if (!test) xwrite(fd, data, data == toybuf ? sizeof(toybuf) : size);
141 if (data != toybuf) {
142 free(data);
143 break;
144 }
145 size -= sizeof(toybuf);
146 }
Rob Landleyc54fdc92014-04-29 06:03:17 -0500147
148 if (!test) {
149 // set owner, restore dropped suid bit
150 if (!getpid()) {
151 err = fchown(fd, uid, gid);
152 if (!err) err = fchmod(fd, mode);
153 }
154 close(fd);
155 }
Rob Landleydba5a372014-03-24 08:19:21 -0500156 } else if (!test)
157 err = mknod(name, mode, makedev(x8u(toybuf+62), x8u(toybuf+70)));
158
Rob Landleyc54fdc92014-04-29 06:03:17 -0500159 // Set ownership and timestamp.
160 if (!test && !err) {
161 // Creading dir/dev doesn't give us a filehandle, we have to refer to it
162 // by name to chown/utime, but how do we know it's the same item?
163 // Check that we at least have the right type of entity open, and do
164 // NOT restore dropped suid bit in this case.
165 if (!S_ISREG(mode) && !S_ISLNK(mode) && !getpid()) {
166 int fd = open(name, O_WRONLY|O_NOFOLLOW);
167 struct stat st;
168
169 if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == mode)
170 err = fchown(fd, uid, gid);
171 else err = 1;
172
173 close(fd);
174 }
175
176 // set timestamp
177 if (!err) {
178 struct timespec times[2];
179
180 memset(times, 0, sizeof(struct timespec)*2);
181 times[0].tv_sec = times[1].tv_sec = timestamp;
182 err = utimensat(AT_FDCWD, name, times, AT_SYMLINK_NOFOLLOW);
183 }
184 }
185
186 if (err) perror_msg("'%s'", name);
Rob Landleydba5a372014-03-24 08:19:21 -0500187 free(tofree);
188
189 // Output cpio archive
190
191 } else {
192 char *name = 0;
193 size_t size = 0;
194
195 for (;;) {
196 struct stat st;
Rob Landley392e1272014-03-26 06:07:06 -0500197 unsigned nlen, error = 0, zero = 0;
Rob Landley6d796b62014-03-25 07:24:50 -0500198 int len, fd = -1;
Rob Landleydba5a372014-03-24 08:19:21 -0500199 ssize_t llen;
200
201 len = getline(&name, &size, stdin);
202 if (len<1) break;
203 if (name[len-1] == '\n') name[--len] = 0;
Rob Landley392e1272014-03-26 06:07:06 -0500204 nlen = len+1;
Rob Landley6d796b62014-03-25 07:24:50 -0500205 if (lstat(name, &st)
206 || (S_ISREG(st.st_mode) && (fd = open(name, O_RDONLY))<0))
207 {
Rob Landleydba5a372014-03-24 08:19:21 -0500208 perror_msg("%s", name);
209 continue;
210 }
211
212 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0;
213 if (st.st_size >> 32) perror_msg("skipping >2G file '%s'", name);
214 else {
215 llen = sprintf(toybuf,
216 "070701%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
217 (int)st.st_ino, st.st_mode, st.st_uid, st.st_gid, (int)st.st_nlink,
218 (int)st.st_mtime, (int)st.st_size, major(st.st_dev),
219 minor(st.st_dev), major(st.st_rdev), minor(st.st_rdev), nlen, 0);
220 xwrite(afd, toybuf, llen);
221 xwrite(afd, name, nlen);
222
223 // NUL Pad header up to 4 multiple bytes.
224 llen = (llen + nlen) & 3;
225 if (llen) xwrite(afd, &zero, 4-llen);
226
227 // Write out body for symlink or regular file
228 llen = st.st_size;
229 if (S_ISLNK(st.st_mode)) {
230 if (readlink(name, toybuf, sizeof(toybuf)-1) == llen)
231 xwrite(afd, toybuf, llen);
232 else perror_msg("readlink '%s'", name);
233 } else while (llen) {
234 nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
Rob Landley6d796b62014-03-25 07:24:50 -0500235 llen -= nlen;
Rob Landleydba5a372014-03-24 08:19:21 -0500236 // If read fails, write anyway (already wrote size in header)
237 if (nlen != readall(fd, toybuf, nlen))
238 if (!error++) perror_msg("bad read from file '%s'", name);
239 xwrite(afd, toybuf, nlen);
240 }
241 llen = st.st_size & 3;
Rob Landley6d796b62014-03-25 07:24:50 -0500242 if (llen) write(afd, &zero, 4-llen);
Rob Landleydba5a372014-03-24 08:19:21 -0500243 }
244 close(fd);
245 }
246 free(name);
247
Rob Landley392e1272014-03-26 06:07:06 -0500248 memset(toybuf, 0, sizeof(toybuf));
Rob Landleydba5a372014-03-24 08:19:21 -0500249 xwrite(afd, toybuf,
Rob Landley392e1272014-03-26 06:07:06 -0500250 sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0)+4);
Rob Landleydba5a372014-03-24 08:19:21 -0500251 }
Isaac Dunhamc50057e2013-10-14 11:15:22 -0500252}