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