OSDN Git Service

900e5128ad4275e387687be03cdd59c7100a6af6
[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, "duH:i|t|F:o|v(verbose)[!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).
43 static char *strpad(int fd, unsigned len, unsigned align)
44 {
45   char *str;
46
47   align = (align + len) & 3;
48   if (align) len += (4-align);
49
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;
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)) break;
98
99     // If you want to extract absolute paths, "cd /" and run cpio.
100     while (*name == '/') name++;
101
102     // Align to 4 bytes. Note header is 110 bytes which is 2 bytes over.
103
104     size = x8u(toybuf+54);
105     mode = x8u(toybuf+14);
106
107     if (toys.optflags & (FLAG_t|FLAG_v)) puts(name);
108
109     if (!test && strrchr(name, '/') && mkpathat(AT_FDCWD, name, 0, 2)) {
110       perror_msg("mkpath '%s'", name);
111       test++;
112     }
113
114     // Consume entire record even if it couldn't create file, so we're
115     // properly aligned with next file.
116
117     if (S_ISDIR(mode)) {
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)) {
123       int fd;
124
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);
128       if (fd < 0) {
129         perror_msg("create %s", name);
130         test++;
131       }
132
133       data = toybuf;
134       while (size) {
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) {
139           free(data);
140           break;
141         }
142         size -= sizeof(toybuf);
143       }
144       close(fd);
145     } else if (!test)
146       err = mknod(name, mode, makedev(x8u(toybuf+62), x8u(toybuf+70)));
147
148     if (err<0) perror_msg("create '%s'", name);
149     free(tofree);
150
151   // Output cpio archive
152
153   } else {
154     char *name = 0;
155     size_t size = 0;
156
157     for (;;) {
158       struct stat st;
159       unsigned nlen = strlen(name)+1, error = 0, zero = 0;
160       int len, fd = -1;
161       ssize_t llen;
162
163       len = getline(&name, &size, stdin);
164       if (len<1) break;
165       if (name[len-1] == '\n') name[--len] = 0;
166       if (lstat(name, &st)
167           || (S_ISREG(st.st_mode) && (fd = open(name, O_RDONLY))<0))
168       {
169         perror_msg("%s", name);
170         continue;
171       }
172
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);
175       else {
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);
183
184         // NUL Pad header up to 4 multiple bytes.
185         llen = (llen + nlen) & 3;
186         if (llen) xwrite(afd, &zero, 4-llen); 
187
188         // Write out body for symlink or regular file
189         llen = st.st_size;
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;
196           llen -= nlen;
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);
201         }
202         llen = st.st_size & 3;
203         if (llen) write(afd, &zero, 4-llen);
204       }
205       close(fd);
206     }
207     free(name);
208
209     xwrite(afd, toybuf,
210       sprintf(toybuf, "070701%040X%056X%08XTRAILER!!!%c%c%c",
211               1, 0x0b, 0, 0, 0, 0));
212   }
213 }