"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 2 | /* Copyright 2002 Laurence Anderson |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 3 | * |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 4 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 7 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 8 | #include "unarchive.h" |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 9 | |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 10 | typedef struct hardlinks_t { |
| 11 | struct hardlinks_t *next; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 12 | int inode; /* TODO: must match maj/min too! */ |
| 13 | int mode ; |
| 14 | int mtime; /* These three are useful only in corner case */ |
| 15 | int uid ; /* of hardlinks with zero size body */ |
| 16 | int gid ; |
| 17 | char name[1]; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 18 | } hardlinks_t; |
| 19 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 20 | char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 21 | { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 22 | file_header_t *file_header = archive_handle->file_header; |
| 23 | char cpio_header[110]; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 24 | int namesize; |
| 25 | int major, minor, nlink, mode, inode; |
| 26 | unsigned size, uid, gid, mtime; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 28 | #define hardlinks_to_create (*(hardlinks_t **)(&archive_handle->ah_priv[0])) |
| 29 | #define created_hardlinks (*(hardlinks_t **)(&archive_handle->ah_priv[1])) |
Denis Vlasenko | d83676e | 2008-10-17 14:03:56 +0000 | [diff] [blame] | 30 | #define block_count (archive_handle->ah_priv[2]) |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 31 | // if (!archive_handle->ah_priv_inited) { |
| 32 | // archive_handle->ah_priv_inited = 1; |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 33 | // hardlinks_to_create = NULL; |
| 34 | // created_hardlinks = NULL; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 35 | // } |
| 36 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 37 | /* There can be padding before archive header */ |
| 38 | data_align(archive_handle, 4); |
| 39 | |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 40 | size = full_read(archive_handle->src_fd, cpio_header, 110); |
| 41 | if (size == 0) { |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 42 | goto create_hardlinks; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 43 | } |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 44 | if (size != 110) { |
| 45 | bb_error_msg_and_die("short read"); |
| 46 | } |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 47 | archive_handle->offset += 110; |
| 48 | |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 49 | if (strncmp(&cpio_header[0], "07070", 5) != 0 |
| 50 | || (cpio_header[5] != '1' && cpio_header[5] != '2') |
| 51 | ) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 52 | bb_error_msg_and_die("unsupported cpio format, use newc or crc"); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 55 | if (sscanf(cpio_header + 6, |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 56 | "%8x" "%8x" "%8x" "%8x" |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 57 | "%8x" "%8x" "%8x" /*maj,min:*/ "%*16c" |
| 58 | /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum: "%*8c"*/, |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 59 | &inode, &mode, &uid, &gid, |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 60 | &nlink, &mtime, &size, |
| 61 | &major, &minor, &namesize) != 10) |
| 62 | bb_error_msg_and_die("damaged cpio file"); |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 63 | file_header->mode = mode; |
| 64 | file_header->uid = uid; |
| 65 | file_header->gid = gid; |
| 66 | file_header->mtime = mtime; |
| 67 | file_header->size = size; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 69 | namesize &= 0x1fff; /* paranoia: limit names to 8k chars */ |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 70 | file_header->name = xzalloc(namesize + 1); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 71 | /* Read in filename */ |
| 72 | xread(archive_handle->src_fd, file_header->name, namesize); |
Denys Vlasenko | af1c8e8 | 2010-01-05 04:43:21 +0100 | [diff] [blame^] | 73 | if (file_header->name[0] == '/') { |
| 74 | /* Testcase: echo /etc/hosts | cpio -pvd /tmp |
| 75 | * Without this code, it tries to unpack /etc/hosts |
| 76 | * into "/etc/hosts", not "etc/hosts". |
| 77 | */ |
| 78 | char *p = file_header->name; |
| 79 | do p++; while (*p == '/'); |
| 80 | overlapping_strcpy(file_header->name, p); |
| 81 | } |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 82 | archive_handle->offset += namesize; |
| 83 | |
| 84 | /* Update offset amount and skip padding before file contents */ |
| 85 | data_align(archive_handle, 4); |
| 86 | |
| 87 | if (strcmp(file_header->name, "TRAILER!!!") == 0) { |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 88 | /* Always round up. ">> 9" divides by 512 */ |
Denis Vlasenko | d83676e | 2008-10-17 14:03:56 +0000 | [diff] [blame] | 89 | block_count = (void*)(ptrdiff_t) ((archive_handle->offset + 511) >> 9); |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 90 | goto create_hardlinks; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 93 | file_header->link_target = NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 94 | if (S_ISLNK(file_header->mode)) { |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 95 | file_header->size &= 0x1fff; /* paranoia: limit names to 8k chars */ |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 96 | file_header->link_target = xzalloc(file_header->size + 1); |
| 97 | xread(archive_handle->src_fd, file_header->link_target, file_header->size); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 98 | archive_handle->offset += file_header->size; |
| 99 | file_header->size = 0; /* Stop possible seeks in future */ |
| 100 | } |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 101 | |
| 102 | // TODO: data_extract_all can't deal with hardlinks to non-files... |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 103 | // when fixed, change S_ISREG to !S_ISDIR here |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 104 | |
| 105 | if (nlink > 1 && S_ISREG(file_header->mode)) { |
| 106 | hardlinks_t *new = xmalloc(sizeof(*new) + namesize); |
| 107 | new->inode = inode; |
| 108 | new->mode = mode ; |
| 109 | new->mtime = mtime; |
| 110 | new->uid = uid ; |
| 111 | new->gid = gid ; |
| 112 | strcpy(new->name, file_header->name); |
| 113 | /* Put file on a linked list for later */ |
| 114 | if (size == 0) { |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 115 | new->next = hardlinks_to_create; |
| 116 | hardlinks_to_create = new; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 117 | return EXIT_SUCCESS; /* Skip this one */ |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 118 | /* TODO: this breaks cpio -t (it does not show hardlinks) */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 119 | } |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 120 | new->next = created_hardlinks; |
| 121 | created_hardlinks = new; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 122 | } |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 123 | file_header->device = makedev(major, minor); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 124 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 125 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 126 | archive_handle->action_data(archive_handle); |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 127 | archive_handle->action_header(file_header); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 128 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 129 | data_skip(archive_handle); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 130 | } |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 131 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 132 | archive_handle->offset += file_header->size; |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 134 | free(file_header->link_target); |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 135 | free(file_header->name); |
| 136 | file_header->link_target = NULL; |
| 137 | file_header->name = NULL; |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 139 | return EXIT_SUCCESS; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 140 | |
| 141 | create_hardlinks: |
| 142 | free(file_header->link_target); |
| 143 | free(file_header->name); |
| 144 | |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 145 | while (hardlinks_to_create) { |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 146 | hardlinks_t *cur; |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 147 | hardlinks_t *make_me = hardlinks_to_create; |
| 148 | |
| 149 | hardlinks_to_create = make_me->next; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 150 | |
| 151 | memset(file_header, 0, sizeof(*file_header)); |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 152 | file_header->mtime = make_me->mtime; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 153 | file_header->name = make_me->name; |
| 154 | file_header->mode = make_me->mode; |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 155 | file_header->uid = make_me->uid; |
| 156 | file_header->gid = make_me->gid; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 157 | /*file_header->size = 0;*/ |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 158 | /*file_header->link_target = NULL;*/ |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 159 | |
| 160 | /* Try to find a file we are hardlinked to */ |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 161 | cur = created_hardlinks; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 162 | while (cur) { |
| 163 | /* TODO: must match maj/min too! */ |
| 164 | if (cur->inode == make_me->inode) { |
| 165 | file_header->link_target = cur->name; |
| 166 | /* link_target != NULL, size = 0: "I am a hardlink" */ |
| 167 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) |
| 168 | archive_handle->action_data(archive_handle); |
| 169 | free(make_me); |
| 170 | goto next_link; |
| 171 | } |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 172 | cur = cur->next; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 173 | } |
| 174 | /* Oops... no file with such inode was created... do it now |
| 175 | * (happens when hardlinked files are empty (zero length)) */ |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 176 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) |
| 177 | archive_handle->action_data(archive_handle); |
| 178 | /* Move to the list of created hardlinked files */ |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 179 | make_me->next = created_hardlinks; |
| 180 | created_hardlinks = make_me; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 181 | next_link: ; |
| 182 | } |
| 183 | |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 184 | while (created_hardlinks) { |
| 185 | hardlinks_t *p = created_hardlinks; |
| 186 | created_hardlinks = p->next; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 187 | free(p); |
| 188 | } |
| 189 | |
| 190 | return EXIT_FAILURE; /* "No more files to process" */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 191 | } |