Mercurial > hg > toybox
annotate toys/pending/cpio.c @ 1098:46d83f3c7546 draft
Here's a revised cpio.
I've reduced the use of malloc(), dropped an extra function call, and
-at least in theory- allowed proper handling of non-regular files.
(If we have a file we can't read, we still should record it when it's
of a type where file content is ignored).
author | Isaac Dunham <ibid.ag@gmail.com> |
---|---|
date | Sun, 27 Oct 2013 19:11:07 -0500 |
parents | b73a61542297 |
children | 977f0a4dc562 |
rev | line source |
---|---|
1087
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
1 /* cpio.c - a basic cpio |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
2 * |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
3 * Written 2013 AD by Isaac Dunham; this code is placed under the |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
4 * same license as toybox or as CC0, at your option. |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
5 USE_CPIO(NEWTOY(cpio, "H:iot", TOYFLAG_BIN)) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
6 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
7 config CPIO |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
8 bool "cpio" |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
9 default n |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
10 help |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
11 usage: cpio { -i | -o | -t } [-H fmt] |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
12 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
13 copy files into and out of an archive |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
14 -i extract from archive into file system (stdin is an archive) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
15 -o create archive (stdin is a list of files, stdout is an archive) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
16 -t list files (stdin is an archive, stdout is a list of files) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
17 -H fmt Write archive in specified format: |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
18 newc SVR4 new character format (default) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
19 */ |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
20 #define FOR_cpio |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
21 #include "toys.h" |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
22 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
23 GLOBALS( |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
24 char * fmt; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
25 ) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
26 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
27 /* Iterate through a list of files, read from stdin. |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
28 * No users need rw. |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
29 */ |
1098 | 30 void loopfiles_stdin(void (*function)(int fd, char *name, struct stat st)) |
1087
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
31 { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
32 int fd; |
1098 | 33 struct stat st; |
1087
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
34 char *name = toybuf; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
35 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
36 while (name != NULL){ |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
37 memset(toybuf, 0, sizeof(toybuf)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
38 name = fgets(toybuf, sizeof(toybuf) - 1, stdin); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
39 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
40 if (name != NULL) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
41 if (toybuf[strlen(name) - 1] == '\n' ) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
42 toybuf[strlen(name) - 1 ] = '\0'; |
1098 | 43 if (lstat(name, &st) == -1) continue; |
1087
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
44 fd = open(name, O_RDONLY); |
1098 | 45 if (fd > 0 || !S_ISREG(st.st_mode)) { |
46 function(fd, name, st); | |
1087
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
47 close(fd); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
48 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
49 errno = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
50 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
51 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
52 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
53 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
54 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
55 struct newc_header { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
56 char c_magic[6]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
57 char c_ino[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
58 char c_mode[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
59 char c_uid[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
60 char c_gid[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
61 char c_nlink[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
62 char c_mtime[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
63 char c_filesize[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
64 char c_devmajor[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
65 char c_devminor[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
66 char c_rdevmajor[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
67 char c_rdevminor[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
68 char c_namesize[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
69 char c_check[8]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
70 }; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
71 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
72 void write_cpio_member(int fd, char *name, struct stat buf) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
73 { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
74 char ahdr[sizeof(struct newc_header) + 1]; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
75 struct newc_header *hdr = (struct newc_header *)ahdr; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
76 size_t out = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
77 unsigned int n = 0x00000000, nlen = strlen(name) + 1; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
78 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
79 memset(hdr, '0', sizeof(struct newc_header)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
80 if (S_ISDIR(buf.st_mode) || S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
81 || S_ISFIFO(buf.st_mode) || S_ISSOCK(buf.st_mode)) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
82 buf.st_size = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
83 snprintf((char *)(hdr), sizeof(struct newc_header)+1, |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
84 "070701%08X%08X" "%08X%08X" |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
85 "%08X%08X%08X" |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
86 "%08X%08X" "%08X%08X%08X00000000", |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
87 (unsigned int)(buf.st_ino), buf.st_mode, buf.st_uid, buf.st_gid, |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
88 buf.st_nlink, (uint32_t)(buf.st_mtime), (uint32_t)(buf.st_size), |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
89 major(buf.st_dev), minor(buf.st_dev), |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
90 major(buf.st_rdev), minor(buf.st_rdev), nlen); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
91 write(1, hdr, sizeof(struct newc_header)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
92 write(1, name, nlen); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
93 if ((nlen + 2) % 4) write(1, &n, 4 - ((nlen + 2) % 4)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
94 if (S_ISLNK(buf.st_mode)) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
95 ssize_t llen = readlink(name, toybuf, sizeof(toybuf) - 1); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
96 if (llen > 0) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
97 toybuf[llen] = '\0'; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
98 write(1, toybuf, buf.st_size); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
99 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
100 } else if (buf.st_size) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
101 for (; (lseek(fd, 0, SEEK_CUR) < (uint32_t)(buf.st_size));) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
102 out = read(fd, toybuf, sizeof(toybuf)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
103 if (out > 0) write(1, toybuf, out); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
104 if (errno || out < sizeof(toybuf)) break; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
105 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
106 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
107 if (buf.st_size % 4) write(1, &n, 4 - (buf.st_size % 4)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
108 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
109 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
110 //convert hex to uint; mostly to allow using bits of non-terminated strings |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
111 unsigned int htou(char * hex) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
112 { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
113 unsigned int ret = 0, i = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
114 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
115 for (;(i < 8 && hex[i]);) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
116 ret *= 16; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
117 switch(hex[i]) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
118 case '0': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
119 break; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
120 case '1': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
121 case '2': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
122 case '3': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
123 case '4': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
124 case '5': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
125 case '6': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
126 case '7': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
127 case '8': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
128 case '9': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
129 ret += hex[i] - '1' + 1; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
130 break; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
131 case 'A': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
132 case 'B': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
133 case 'C': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
134 case 'D': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
135 case 'E': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
136 case 'F': |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
137 ret += hex[i] - 'A' + 10; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
138 break; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
139 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
140 i++; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
141 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
142 return ret; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
143 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
144 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
145 /* Read one cpio record. |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
146 * Returns 0 for last record, |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
147 * 1 for "continue". |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
148 */ |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
149 int read_cpio_member(int fd, int how) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
150 { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
151 uint32_t nsize, fsize; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
152 mode_t mode = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
153 int pad, ofd = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
154 struct newc_header hdr; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
155 char *name; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
156 dev_t dev = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
157 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
158 xreadall(fd, &hdr, sizeof(struct newc_header)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
159 nsize = htou(hdr.c_namesize); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
160 name = xmalloc(nsize); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
161 xreadall(fd, name, nsize); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
162 if (!strcmp("TRAILER!!!", name)) return 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
163 fsize = htou(hdr.c_filesize); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
164 mode += htou(hdr.c_mode); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
165 pad = 4 - ((nsize + 2) % 4); // 2 == sizeof(struct newc_header) % 4 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
166 if (pad < 4) xreadall(fd, toybuf, pad); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
167 pad = 4 - (fsize % 4); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
168 if (how & 1) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
169 if (S_ISDIR(mode)) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
170 ofd = mkdir(name, mode); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
171 } else if (S_ISLNK(mode)) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
172 memset(toybuf, 0, sizeof(toybuf)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
173 if (fsize < sizeof(toybuf)) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
174 pad = readall(fd, toybuf, fsize); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
175 if (pad < fsize) error_exit("short archive"); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
176 pad = 4 - (fsize % 4); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
177 fsize = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
178 if (symlink(toybuf, name)) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
179 perror_msg("could not write link %s", name); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
180 toys.exitval |= 1; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
181 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
182 } else { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
183 perror_msg("link too long: %s", name); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
184 toys.exitval |= 1; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
185 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
186 } else if (S_ISBLK(mode)||S_ISCHR(mode)||S_ISFIFO(mode)||S_ISSOCK(mode)) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
187 dev = makedev(htou(hdr.c_rdevmajor),htou(hdr.c_rdevminor)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
188 ofd = mknod(name, mode, dev); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
189 } else { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
190 ofd = creat(name, mode); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
191 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
192 if (ofd == -1) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
193 error_msg("could not create %s", name); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
194 toys.exitval |= 1; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
195 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
196 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
197 errno = 0; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
198 if (how & 2) puts(name); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
199 while (fsize) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
200 int i; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
201 memset(toybuf, 0, sizeof(toybuf)); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
202 i = readall(fd, toybuf, (fsize>sizeof(toybuf)) ? sizeof(toybuf) : fsize); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
203 if (i < 1) error_exit("archive too short"); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
204 if (ofd > 0) writeall(ofd, toybuf, i); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
205 fsize -= i; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
206 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
207 if (pad < 4) xreadall(fd, toybuf, pad); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
208 return 1; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
209 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
210 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
211 void read_cpio_archive(int fd, int how) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
212 { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
213 for(;;) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
214 if (!read_cpio_member(fd, how)) return; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
215 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
216 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
217 |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
218 void cpio_main(void) |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
219 { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
220 switch (toys.optflags & (FLAG_i | FLAG_o | FLAG_t)) { |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
221 case FLAG_o: |
1098 | 222 loopfiles_stdin(write_cpio_member); |
1087
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
223 write(1, "07070100000000000000000000000000000000000000010000000000000000" |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
224 "000000000000000000000000000000000000000B00000000TRAILER!!!\0\0\0", 124); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
225 break; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
226 case FLAG_i: |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
227 read_cpio_archive(0, 1); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
228 case FLAG_t: |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
229 case (FLAG_t | FLAG_i): |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
230 read_cpio_archive(0, 2); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
231 break; |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
232 default: |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
233 error_exit("must use one of -iot"); |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
234 } |
b73a61542297
I've finally gotten 'cpio' into a shape where it could be useable.
Isaac Dunham <ibid.ag@gmail.com>
parents:
diff
changeset
|
235 } |