OSDN Git Service

Ignore -m option to cpio, it's what we do anyway so it's not an error.
[android-x86/external-toybox.git] / toys / posix / cpio.c
1 /* 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.
5  *
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
8  *
9  * 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
15
16 USE_CPIO(NEWTOY(cpio, "mduH:i|t|F:v(verbose)o|[!io][!ot]", TOYFLAG_BIN))
17
18 config CPIO
19   bool "cpio"
20   default y
21   help
22     usage: cpio -{o|t|i} [-v] [--verbose] [-F FILE] [ignored: -du -H newc]
23
24     copy files into and out of a "newc" format cpio archive
25
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)
31 */
32
33 #define FOR_cpio
34 #include "toys.h"
35
36 GLOBALS(
37   char *archive;
38   char *fmt;
39 )
40
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)
45 {
46   char *str;
47
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
52
53   return str;
54 }
55
56 //convert hex to uint; mostly to allow using bits of non-terminated strings
57 unsigned x8u(char *hex)
58 {
59   unsigned val, inpos = 8, outpos;
60   char pattern[6];
61
62   while (*hex == '0') {
63     hex++;
64     if (!--inpos) return 0;
65   }
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");
70
71   return val;
72 }
73
74 void cpio_main(void)
75 {
76   int afd;
77
78   // Subtle bit: FLAG_o is 1 so we can just use it to select stdin/stdout.
79
80   afd = toys.optflags & FLAG_o;
81   if (TT.archive) {
82     int perm = (toys.optflags & FLAG_o) ? O_CREAT|O_WRONLY|O_TRUNC : O_RDONLY;
83
84     afd = xcreate(TT.archive, perm, 0644);
85   }
86
87   // read cpio archive
88
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;
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)) {
98       if (CFG_TOYBOX_FREE) free(tofree);
99       break;
100     }
101
102     // If you want to extract absolute paths, "cd /" and run cpio.
103     while (*name == '/') name++;
104     // TODO: remove .. entries
105
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
111
112     if (toys.optflags & (FLAG_t|FLAG_v)) puts(name);
113
114     if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) {
115       perror_msg("mkpath '%s'", name);
116       test++;
117     }
118
119     // Consume entire record even if it couldn't create file, so we're
120     // properly aligned with next file.
121
122     if (S_ISDIR(mode)) {
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);
127       free(data);
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);
132
133       // If write fails, we still need to read/discard data to continue with
134       // archive. Since doing so overwrites errno, report error now
135       if (fd < 0) {
136         perror_msg("create %s", name);
137         test++;
138       }
139
140       data = toybuf;
141       while (size) {
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) {
146           free(data);
147           break;
148         }
149         size -= sizeof(toybuf);
150       }
151
152       if (!test) {
153         // set owner, restore dropped suid bit
154         if (!getpid()) {
155           err = fchown(fd, uid, gid);
156           if (!err) err = fchmod(fd, mode);
157         }
158         close(fd);
159       }
160     } else if (!test)
161       err = mknod(name, mode, makedev(x8u(toybuf+62), x8u(toybuf+70)));
162
163     // Set ownership and timestamp.
164     if (!test && !err) {
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);
171         struct stat st;
172
173         if (fd != -1 && !fstat(fd, &st) && (st.st_mode&S_IFMT) == mode)
174           err = fchown(fd, uid, gid);
175         else err = 1;
176
177         close(fd);
178       }
179
180       // set timestamp
181       if (!err) {
182         struct timespec times[2];
183
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);
187       }
188     }
189
190     if (err) perror_msg("'%s'", name);
191     free(tofree);
192
193   // Output cpio archive
194
195   } else {
196     char *name = 0;
197     size_t size = 0;
198
199     for (;;) {
200       struct stat st;
201       unsigned nlen, error = 0, zero = 0;
202       int len, fd = -1;
203       ssize_t llen;
204
205       len = getline(&name, &size, stdin);
206       if (len<1) break;
207       if (name[len-1] == '\n') name[--len] = 0;
208       nlen = len+1;
209       if (lstat(name, &st) || (S_ISREG(st.st_mode) 
210           && st.st_size && (fd = open(name, O_RDONLY))<0))
211       {
212         perror_msg("%s", name);
213         continue;
214       }
215
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);
218       else {
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);
226
227         // NUL Pad header up to 4 multiple bytes.
228         llen = (llen + nlen) & 3;
229         if (llen) xwrite(afd, &zero, 4-llen); 
230
231         // Write out body for symlink or regular file
232         llen = st.st_size;
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;
239           llen -= nlen;
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);
244         }
245         llen = st.st_size & 3;
246         if (llen) write(afd, &zero, 4-llen);
247       }
248       close(fd);
249     }
250     free(name);
251
252     memset(toybuf, 0, sizeof(toybuf));
253     xwrite(afd, toybuf,
254       sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0)+4);
255   }
256   if (TT.archive) xclose(afd);
257 }