Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini cpio implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 by Glenn McGrath |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 8 | * |
| 9 | * Limitations: |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 10 | * Doesn't check CRC's |
| 11 | * Only supports new ASCII and CRC formats |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 12 | * |
| 13 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 15 | #include "unarchive.h" |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 17 | enum { |
| 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), |
| 27 | }; |
| 28 | |
| 29 | #if ENABLE_FEATURE_CPIO_O |
| 30 | static off_t cpio_pad4(off_t size) |
| 31 | { |
| 32 | int i; |
| 33 | |
| 34 | i = (- size) & 3; |
| 35 | size += i; |
| 36 | while (--i >= 0) |
| 37 | bb_putchar('\0'); |
| 38 | return size; |
| 39 | } |
| 40 | |
| 41 | /* Return value will become exit code. |
| 42 | * It's ok to exit instead of return. */ |
| 43 | static int cpio_o(void) |
| 44 | { |
| 45 | struct name_s { |
| 46 | struct name_s *next; |
| 47 | char name[1]; |
| 48 | }; |
| 49 | struct inodes_s { |
| 50 | struct inodes_s *next; |
| 51 | struct name_s *names; |
| 52 | struct stat st; |
| 53 | }; |
| 54 | |
| 55 | struct inodes_s *links = NULL; |
| 56 | off_t bytes = 0; /* output bytes count */ |
| 57 | |
| 58 | while (1) { |
| 59 | const char *name; |
| 60 | char *line; |
| 61 | struct stat st; |
| 62 | |
| 63 | line = xmalloc_fgetline(stdin); |
| 64 | |
| 65 | if (line) { |
| 66 | /* Strip leading "./[./]..." from the filename */ |
| 67 | name = line; |
| 68 | while (name[0] == '.' && name[1] == '/') { |
| 69 | while (*++name == '/') |
| 70 | continue; |
| 71 | } |
| 72 | if (!*name) { /* line is empty */ |
| 73 | free(line); |
| 74 | continue; |
| 75 | } |
| 76 | if (lstat(name, &st)) { |
| 77 | abort_cpio_o: |
| 78 | bb_simple_perror_msg_and_die(name); |
| 79 | } |
| 80 | |
| 81 | if (!(S_ISLNK(st.st_mode) || S_ISREG(st.st_mode))) |
| 82 | st.st_size = 0; /* paranoia */ |
| 83 | |
| 84 | /* Store hardlinks for later processing, dont output them */ |
| 85 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) { |
| 86 | struct name_s *n; |
| 87 | struct inodes_s *l; |
| 88 | |
| 89 | /* Do we have this hardlink remembered? */ |
| 90 | l = links; |
| 91 | while (1) { |
| 92 | if (l == NULL) { |
| 93 | /* Not found: add new item to "links" list */ |
| 94 | l = xzalloc(sizeof(*l)); |
| 95 | l->st = st; |
| 96 | l->next = links; |
| 97 | links = l; |
| 98 | break; |
| 99 | } |
| 100 | if (l->st.st_ino == st.st_ino) { |
| 101 | /* found */ |
| 102 | break; |
| 103 | } |
| 104 | l = l->next; |
| 105 | } |
| 106 | /* Add new name to "l->names" list */ |
| 107 | n = xmalloc(sizeof(*n) + strlen(name)); |
| 108 | strcpy(n->name, name); |
| 109 | n->next = l->names; |
| 110 | l->names = n; |
| 111 | |
| 112 | free(line); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | } else { /* line == NULL: EOF */ |
| 117 | next_link: |
| 118 | if (links) { |
| 119 | /* Output hardlink's data */ |
| 120 | st = links->st; |
| 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) |
| 126 | links = links->next; |
| 127 | else |
| 128 | st.st_size = 0; |
| 129 | /* NB: we leak links->names and/or links, |
| 130 | * this is intended (we exit soon anyway) */ |
| 131 | } else { |
| 132 | /* If no (more) hardlinks to output, |
| 133 | * output "trailer" entry */ |
| 134 | name = "TRAILER!!!"; |
| 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 */ |
| 139 | } |
| 140 | } |
| 141 | |
| 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), |
| 160 | name, '\0'); |
| 161 | bytes = cpio_pad4(bytes); |
| 162 | |
| 163 | if (st.st_size) { |
| 164 | if (S_ISLNK(st.st_mode)) { |
| 165 | char *lpath = xmalloc_readlink_or_warn(name); |
| 166 | if (!lpath) |
| 167 | goto abort_cpio_o; |
| 168 | bytes += printf("%s", lpath); |
| 169 | free(lpath); |
| 170 | } else { /* S_ISREG */ |
| 171 | int fd = xopen(name, O_RDONLY); |
| 172 | fflush(stdout); |
| 173 | /* We must abort if file got shorter too! */ |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 174 | bb_copyfd_exact_size(fd, STDOUT_FILENO, st.st_size); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 175 | bytes += st.st_size; |
| 176 | close(fd); |
| 177 | } |
| 178 | bytes = cpio_pad4(bytes); |
| 179 | } |
| 180 | |
| 181 | if (!line) { |
| 182 | if (links) |
| 183 | goto next_link; |
| 184 | /* TODO: GNU cpio pads trailer to 512 bytes, do we want that? */ |
| 185 | return EXIT_SUCCESS; |
| 186 | } |
| 187 | |
| 188 | free(line); |
| 189 | } /* end of "while (1)" */ |
| 190 | } |
| 191 | #endif |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 193 | int cpio_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 194 | int cpio_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 195 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 196 | archive_handle_t *archive_handle; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 197 | char *cpio_filename; |
| 198 | #if ENABLE_FEATURE_CPIO_O |
| 199 | const char *cpio_fmt = ""; |
| 200 | #endif |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 201 | unsigned opt; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 203 | /* Initialize */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 204 | archive_handle = init_handle(); |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 205 | archive_handle->src_fd = STDIN_FILENO; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 206 | archive_handle->seek = seek_by_read; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame^] | 207 | archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 209 | #if ENABLE_FEATURE_CPIO_O |
| 210 | opt = getopt32(argv, "ituvF:dmoH:", &cpio_filename, &cpio_fmt); |
| 211 | |
| 212 | if (opt & CPIO_OPT_CREATE) { |
| 213 | if (*cpio_fmt != 'n') |
| 214 | bb_show_usage(); |
| 215 | if (opt & CPIO_OPT_FILE) { |
| 216 | fclose(stdout); |
| 217 | stdout = fopen(cpio_filename, "w"); |
| 218 | /* Paranoia: I don't trust libc that much */ |
| 219 | xdup2(fileno(stdout), STDOUT_FILENO); |
| 220 | } |
| 221 | return cpio_o(); |
| 222 | } |
| 223 | #else |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 224 | opt = getopt32(argv, "ituvF:dm", &cpio_filename); |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 225 | #endif |
| 226 | argv += optind; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 227 | |
| 228 | /* One of either extract or test options must be given */ |
| 229 | if ((opt & (CPIO_OPT_TEST | CPIO_OPT_EXTRACT)) == 0) { |
| 230 | bb_show_usage(); |
| 231 | } |
| 232 | |
| 233 | if (opt & CPIO_OPT_TEST) { |
Rob Landley | b7128c6 | 2005-09-11 01:05:30 +0000 | [diff] [blame] | 234 | /* if both extract and test options are given, ignore extract option */ |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 235 | if (opt & CPIO_OPT_EXTRACT) { |
| 236 | opt &= ~CPIO_OPT_EXTRACT; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 237 | } |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 238 | archive_handle->action_header = header_list; |
| 239 | } |
| 240 | if (opt & CPIO_OPT_EXTRACT) { |
| 241 | archive_handle->action_data = data_extract_all; |
| 242 | } |
| 243 | if (opt & CPIO_OPT_UNCONDITIONAL) { |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame^] | 244 | archive_handle->ah_flags |= ARCHIVE_EXTRACT_UNCONDITIONAL; |
| 245 | archive_handle->ah_flags &= ~ARCHIVE_EXTRACT_NEWER; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 246 | } |
| 247 | if (opt & CPIO_OPT_VERBOSE) { |
| 248 | if (archive_handle->action_header == header_list) { |
| 249 | archive_handle->action_header = header_verbose_list; |
| 250 | } else { |
| 251 | archive_handle->action_header = header_list; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 254 | if (opt & CPIO_OPT_FILE) { /* -F */ |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 255 | archive_handle->src_fd = xopen(cpio_filename, O_RDONLY); |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 256 | archive_handle->seek = seek_by_jump; |
| 257 | } |
| 258 | if (opt & CPIO_OPT_CREATE_LEADING_DIR) { |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame^] | 259 | archive_handle->ah_flags |= ARCHIVE_CREATE_LEADING_DIRS; |
Glenn L McGrath | 10b7813 | 2004-02-25 09:30:06 +0000 | [diff] [blame] | 260 | } |
Denis Vlasenko | bbd55c9 | 2008-06-27 15:52:07 +0000 | [diff] [blame] | 261 | if (opt & CPIO_OPT_PRESERVE_MTIME) { |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame^] | 262 | archive_handle->ah_flags |= ARCHIVE_PRESERVE_DATE; |
Denis Vlasenko | bbd55c9 | 2008-06-27 15:52:07 +0000 | [diff] [blame] | 263 | } |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 265 | while (*argv) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 266 | archive_handle->filter = filter_accept_list; |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 267 | llist_add_to(&(archive_handle->accept), *argv); |
| 268 | argv++; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Denis Vlasenko | 261f237 | 2008-04-05 00:07:46 +0000 | [diff] [blame] | 271 | while (get_header_cpio(archive_handle) == EXIT_SUCCESS) |
| 272 | continue; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 274 | return EXIT_SUCCESS; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 275 | } |