1 /* vi: set sw=4 ts=4: */
3 * Mini cpio implementation for busybox
5 * Copyright (C) 2001 by Glenn McGrath
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 * Only supports new ASCII and CRC formats
15 #include "unarchive.h"
18 CPIO_OPT_EXTRACT = (1 << 0),
19 CPIO_OPT_TEST = (1 << 1),
20 CPIO_OPT_UNCONDITIONAL = (1 << 2),
21 CPIO_OPT_VERBOSE = (1 << 3),
22 CPIO_OPT_FILE = (1 << 4),
23 CPIO_OPT_CREATE_LEADING_DIR = (1 << 5),
24 CPIO_OPT_PRESERVE_MTIME = (1 << 6),
25 CPIO_OPT_CREATE = (1 << 7),
26 CPIO_OPT_FORMAT = (1 << 8),
29 #if ENABLE_FEATURE_CPIO_O
30 static off_t cpio_pad4(off_t size)
41 /* Return value will become exit code.
42 * It's ok to exit instead of return. */
43 static int cpio_o(void)
50 struct inodes_s *next;
55 struct inodes_s *links = NULL;
56 off_t bytes = 0; /* output bytes count */
63 line = xmalloc_fgetline(stdin);
66 /* Strip leading "./[./]..." from the filename */
68 while (name[0] == '.' && name[1] == '/') {
69 while (*++name == '/')
72 if (!*name) { /* line is empty */
76 if (lstat(name, &st)) {
78 bb_simple_perror_msg_and_die(name);
81 if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode)))
82 st.st_size = 0; /* paranoia */
84 /* Store hardlinks for later processing, dont output them */
85 if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) {
89 /* Do we have this hardlink remembered? */
93 /* Not found: add new item to "links" list */
94 l = xzalloc(sizeof(*l));
100 if (l->st.st_ino == st.st_ino) {
106 /* Add new name to "l->names" list */
107 n = xmalloc(sizeof(*n) + strlen(name));
108 strcpy(n->name, name);
116 } else { /* line == NULL: EOF */
119 /* Output hardlink's data */
121 name = links->names->name;
122 links->names = links->names->next;
123 /* GNU cpio is reported to emit file data
124 * only for the last instance. Mimic that. */
125 if (links->names == NULL)
129 /* NB: we leak links->names and/or links,
130 * this is intended (we exit soon anyway) */
132 /* If no (more) hardlinks to output,
133 * output "trailer" entry */
135 /* st.st_size == 0 is a must, but for uniformity
136 * in the output, we zero out everything */
137 memset(&st, 0, sizeof(st));
138 /* st.st_nlink = 1; - GNU cpio does this */
142 bytes += printf("070701"
143 "%08X%08X%08X%08X%08X%08X%08X"
144 "%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
145 /* strlen+1: */ "%08X"
146 /* chksum: */ "00000000" /* (only for "070702" files) */
147 /* name,NUL: */ "%s%c",
148 (unsigned)(uint32_t) st.st_ino,
149 (unsigned)(uint32_t) st.st_mode,
150 (unsigned)(uint32_t) st.st_uid,
151 (unsigned)(uint32_t) st.st_gid,
152 (unsigned)(uint32_t) st.st_nlink,
153 (unsigned)(uint32_t) st.st_mtime,
154 (unsigned)(uint32_t) st.st_size,
155 (unsigned)(uint32_t) major(st.st_dev),
156 (unsigned)(uint32_t) minor(st.st_dev),
157 (unsigned)(uint32_t) major(st.st_rdev),
158 (unsigned)(uint32_t) minor(st.st_rdev),
159 (unsigned)(strlen(name) + 1),
161 bytes = cpio_pad4(bytes);
164 if (S_ISLNK(st.st_mode)) {
165 char *lpath = xmalloc_readlink_or_warn(name);
168 bytes += printf("%s", lpath);
170 } else { /* S_ISREG */
171 int fd = xopen(name, O_RDONLY);
173 /* We must abort if file got shorter too! */
174 if (bb_copyfd_size(fd, STDOUT_FILENO, st.st_size) != st.st_size) {
175 bb_error_msg_and_die("I/O error, of file '%s' was truncated", name);
180 bytes = cpio_pad4(bytes);
186 /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */
191 } /* end of "while (1)" */
195 int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
196 int cpio_main(int argc ATTRIBUTE_UNUSED, char **argv)
198 archive_handle_t *archive_handle;
200 #if ENABLE_FEATURE_CPIO_O
201 const char *cpio_fmt = "";
206 archive_handle = init_handle();
207 archive_handle->src_fd = STDIN_FILENO;
208 archive_handle->seek = seek_by_read;
209 archive_handle->flags = ARCHIVE_EXTRACT_NEWER | ARCHIVE_PRESERVE_DATE;
211 #if ENABLE_FEATURE_CPIO_O
212 opt = getopt32(argv, "ituvF:dmoH:", &cpio_filename, &cpio_fmt);
214 if (opt & CPIO_OPT_CREATE) {
215 if (*cpio_fmt != 'n')
217 if (opt & CPIO_OPT_FILE) {
219 stdout = fopen(cpio_filename, "w");
220 /* Paranoia: I don't trust libc that much */
221 xdup2(fileno(stdout), STDOUT_FILENO);
226 opt = getopt32(argv, "ituvF:dm", &cpio_filename);
230 /* One of either extract or test options must be given */
231 if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) {
235 if (opt & CPIO_OPT_TEST) {
236 /* if both extract and test options are given, ignore extract option */
237 if (opt & CPIO_OPT_EXTRACT) {
238 opt &= ~CPIO_OPT_EXTRACT;
240 archive_handle->action_header = header_list;
242 if (opt & CPIO_OPT_EXTRACT) {
243 archive_handle->action_data = data_extract_all;
245 if (opt & CPIO_OPT_UNCONDITIONAL) {
246 archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
247 archive_handle->flags &= ~ARCHIVE_EXTRACT_NEWER;
249 if (opt & CPIO_OPT_VERBOSE) {
250 if (archive_handle->action_header == header_list) {
251 archive_handle->action_header = header_verbose_list;
253 archive_handle->action_header = header_list;
256 if (opt & CPIO_OPT_FILE) { /* -F */
257 archive_handle->src_fd = xopen(cpio_filename, O_RDONLY);
258 archive_handle->seek = seek_by_jump;
260 if (opt & CPIO_OPT_CREATE_LEADING_DIR) {
261 archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
265 archive_handle->filter = filter_accept_list;
266 llist_add_to(&(archive_handle->accept), *argv);
270 while (get_header_cpio(archive_handle) == EXIT_SUCCESS)