wok-current view busybox/stuff/busybox-1.10.0-cpio.u @ rev 2458
pcmanfm: update build_depends
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Mar 14 10:07:15 2009 +0000 (2009-03-14) |
parents | 552d9bd2b795 |
children |
line source
1 diff -purN busybox-1.10.0/archival/Config.in busybox/archival/Config.in
2 --- busybox-1.10.0/archival/Config.in 2008-03-29 21:47:37.000000000 +0100
3 +++ busybox-1.10.0/archival/Config.in 2008-03-29 21:47:37.000000000 +0100
4 @@ -78,6 +78,14 @@ config CPIO
5 Unless you have a specific application which requires cpio, you should
6 probably say N here.
8 +config FEATURE_CPIO_O
9 + bool "Support for archive creation"
10 + default n
11 + depends on CPIO
12 + help
13 + This implementation of cpio can create cpio archives in the "newc"
14 + format only.
15 +
16 config DPKG
17 bool "dpkg"
18 default n
19 diff -purN busybox-1.10.0/archival/cpio.c busybox/archival/cpio.c
20 --- busybox-1.10.0/archival/cpio.c 2008-03-29 21:47:55.000000000 +0100
21 +++ busybox-1.10.0/archival/cpio.c 2008-03-29 21:47:55.000000000 +0100
22 @@ -21,12 +21,146 @@
23 #define CPIO_OPT_FILE 0x10
24 #define CPIO_OPT_CREATE_LEADING_DIR 0x20
25 #define CPIO_OPT_PRESERVE_MTIME 0x40
26 +#define CPIO_OPT_CREATE 0x80
27 +#define CPIO_OPT_FORMAT 0x100
28 +
29 +#if ENABLE_FEATURE_CPIO_O
30 +static void cpio_pad(off_t *size, int n)
31 +{
32 + int i;
33 + for (*size += i = (-*size) & n; --i >= 0; bb_putchar(0));
34 +}
35 +
36 +static void cpio_o(void)
37 +{
38 + struct name_s {
39 + struct name_s *next;
40 + char name[0];
41 + };
42 + struct inodes_s {
43 + struct name_s *names;
44 + struct inodes_s *next;
45 + struct stat st;
46 + } *links = NULL;
47 + off_t bytes = 0; // output bytes count
48 +#if CONFIG_FEATURE_COPYBUF_KB < 1
49 + char buf[1024];
50 +#else
51 + char buf[CONFIG_FEATURE_COPYBUF_KB * 1024];
52 +#endif
53 +
54 + while (1) {
55 + const char *name = "TRAILER!!!";
56 + char *line = xmalloc_getline(stdin);
57 + // allocate inode struct each loop to avoid struct stat copy
58 + struct inodes_s *inode = xzalloc(sizeof(*inode)); // die if fail
59 + off_t size;
60 +
61 + inode->st.st_nlink++; // =1
62 + if (line) {
63 + /* Strip leading `./' from the filename. */
64 + for (name = line; name[0] == '.' && name[1] == '/';) {
65 + while (*++name == '/');
66 + }
67 + if (!*name) goto free_and_continue; // line empty
68 + if (lstat(name, &inode->st)) {
69 + abort_cpio_o:
70 + bb_perror_msg_and_die(name);
71 + }
72 + if (!S_ISLNK(inode->st.st_mode) && !S_ISREG(inode->st.st_mode))
73 + inode->st.st_size = 0;
74 + }
75 + // hard links will are stored and will be processed later
76 + if (!S_ISDIR(inode->st.st_mode) && inode->st.st_nlink > 1) {
77 + struct name_s *n;
78 + struct inodes_s *l;
79 +
80 + for (l = links; l && l->st.st_ino != inode->st.st_ino; l = l->next);
81 + if (l == NULL) { // not found: new hard links set
82 + l = inode; // l->names = NULL; l->st = inode->st
83 + l->next = links;
84 + links = l;
85 + }
86 + n = xmalloc(sizeof(*n) + strlen(name) + 1); // die if fail
87 + strcpy(n->name, name);
88 + n->next = l->names;
89 + l->names = n; // will not free inode if l == inode
90 + goto free_and_continue;
91 + }
92 + size = inode->st.st_size;
93 + // no more files ? process hard links
94 + if (!line && links) {
95 + struct name_s *n;
96 +
97 + free(inode); // trailer pseudo inode
98 + inode = links;
99 + n = links->names;
100 + name = line = xstrdup(n->name); // line will free *name memory
101 + links->names = n->next;
102 + if (links->names == NULL) { // inode will free *links memory
103 + size = links->st.st_size;
104 + links = links->next;
105 + }
106 + free(n);
107 + }
108 + bytes += printf("070701%08lx%08lx%08lx%08lx%08lx%08lx%08lx"
109 + "%08lx%08lx%08lx%08lx%08lx%08lx%s%c",
110 + (unsigned long) inode->st.st_ino,
111 + (unsigned long) inode->st.st_mode,
112 + (unsigned long) inode->st.st_uid,
113 + (unsigned long) inode->st.st_gid,
114 + (unsigned long) inode->st.st_nlink,
115 + (unsigned long) inode->st.st_mtime,
116 + (unsigned long) size,
117 + (unsigned long) major(inode->st.st_dev),
118 + (unsigned long) minor(inode->st.st_dev),
119 + (unsigned long) major(inode->st.st_rdev),
120 + (unsigned long) minor(inode->st.st_rdev),
121 + strlen(name) + 1UL, 0UL, name, 0);
122 + cpio_pad(&bytes, (line) ? 4-1 : 512-1);
123 + if (size) {
124 + if (S_ISLNK(inode->st.st_mode)) {
125 + char *lpath = xmalloc_readlink_or_warn(name);
126 +
127 + if (!lpath) goto abort_cpio_o;
128 + bytes += printf("%s", lpath);
129 + free(lpath);
130 + }
131 + if (S_ISREG(inode->st.st_mode)) {
132 + int fd = open_or_warn(name, O_RDONLY);
133 +
134 + while (1) {
135 + int len = full_read(fd, buf, sizeof(buf));
136 + if (len < 0) goto abort_cpio_o;
137 + if (len == 0) break;
138 + bytes += len;
139 + fwrite(buf, 1, len, stdout);
140 + }
141 + close(fd);
142 + }
143 + cpio_pad(&bytes, 4-1);
144 + }
145 +
146 + if (!line) {
147 + fputc('\n', stderr); // GNU cpio do that...
148 + return; // was trailer
149 + }
150 +
151 + free_and_continue:
152 + if (!inode->names) free(inode);
153 + free(line);
154 + }
155 +}
156 +#endif
158 int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
159 int cpio_main(int argc, char **argv)
160 {
161 archive_handle_t *archive_handle;
162 char *cpio_filename = NULL;
163 +#if ENABLE_FEATURE_CPIO_O
164 + const char *cpio_fmt = "";
165 +#endif
166 unsigned opt;
168 /* Initialise */
169 @@ -35,10 +169,26 @@ int cpio_main(int argc, char **argv)
170 archive_handle->seek = seek_by_read;
171 archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
173 +#if ENABLE_FEATURE_CPIO_O
174 + opt = getopt32(argv, "ituvF:dmoH:", &cpio_filename,&cpio_fmt);
175 +
176 + if (opt & CPIO_OPT_CREATE) {
177 + if (*cpio_fmt != 'n')
178 + goto cpio_show_usage;
179 + if (cpio_filename) {
180 + fclose(stdout);
181 + stdout = fopen(cpio_filename,"w");
182 + }
183 + cpio_o();
184 + return EXIT_SUCCESS;
185 + }
186 +#else
187 opt = getopt32(argv, "ituvF:dm", &cpio_filename);
188 +#endif
190 /* One of either extract or test options must be given */
191 if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
192 + cpio_show_usage:
193 bb_show_usage();
194 }
196 diff -purN busybox-1.10.0/include/usage.h busybox/include/usage.h
197 --- busybox-1.10.0/include/usage.h 2008-03-29 21:48:22.000000000 +0100
198 +++ busybox-1.10.0/include/usage.h 2008-03-29 21:48:22.000000000 +0100
199 @@ -496,13 +496,19 @@
200 "\n -l,-s Create (sym)links" \
202 #define cpio_trivial_usage \
203 - "-[dimtuv][F cpiofile]"
204 + "-[dim" USE_FEATURE_CPIO_O("o") "tuv][F cpiofile]" \
205 + USE_FEATURE_CPIO_O( "[H newc]" )
206 #define cpio_full_usage \
207 - "Extract or list files from a cpio archive\n" \
208 + "Extract or list files from a cpio archive" \
209 + USE_FEATURE_CPIO_O( ", or create a cpio archive" ) "\n" \
210 "Main operation mode:" \
211 "\n d Make leading directories" \
212 "\n i Extract" \
213 "\n m Preserve mtime" \
214 + USE_FEATURE_CPIO_O( \
215 + "\n o Create" \
216 + "\n H newc Define format" \
217 + ) \
218 "\n t List" \
219 "\n v Verbose" \
220 "\n u Unconditional overwrite" \