"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]; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 24 | char dummy[16]; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 25 | int namesize; |
| 26 | int major, minor, nlink, mode, inode; |
| 27 | unsigned size, uid, gid, mtime; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 29 | #define saved_hardlinks (*(hardlinks_t **)(&archive_handle->ah_priv[0])) |
| 30 | #define saved_hardlinks_created (*(hardlinks_t **)(&archive_handle->ah_priv[1])) |
| 31 | // if (!archive_handle->ah_priv_inited) { |
| 32 | // archive_handle->ah_priv_inited = 1; |
| 33 | // saved_hardlinks = NULL; |
| 34 | // saved_hardlinks_created = NULL; |
| 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 | ee1b3b1 | 2008-07-11 22:20:59 +0000 | [diff] [blame] | 40 | //TODO: this function is used only here, make it static? |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 41 | if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 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 | } |
| 44 | archive_handle->offset += 110; |
| 45 | |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 46 | if (strncmp(&cpio_header[0], "07070", 5) != 0 |
| 47 | || (cpio_header[5] != '1' && cpio_header[5] != '2') |
| 48 | ) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 49 | 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] | 50 | } |
| 51 | |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 52 | sscanf(cpio_header + 6, |
| 53 | "%8x" "%8x" "%8x" "%8x" |
| 54 | "%8x" "%8x" "%8x" /*maj,min:*/ "%16c" |
| 55 | /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum:*/ "%8c", |
| 56 | &inode, &mode, &uid, &gid, |
| 57 | &nlink, &mtime, &size, dummy, |
| 58 | &major, &minor, &namesize, dummy); |
| 59 | file_header->mode = mode; |
| 60 | file_header->uid = uid; |
| 61 | file_header->gid = gid; |
| 62 | file_header->mtime = mtime; |
| 63 | file_header->size = size; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | ee1b3b1 | 2008-07-11 22:20:59 +0000 | [diff] [blame] | 65 | namesize &= 0x1fff; /* paranoia: names can't be that long */ |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 66 | file_header->name = xzalloc(namesize + 1); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 67 | /* Read in filename */ |
| 68 | xread(archive_handle->src_fd, file_header->name, namesize); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 69 | archive_handle->offset += namesize; |
| 70 | |
| 71 | /* Update offset amount and skip padding before file contents */ |
| 72 | data_align(archive_handle, 4); |
| 73 | |
| 74 | if (strcmp(file_header->name, "TRAILER!!!") == 0) { |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 75 | /* Always round up. ">> 9" divides by 512 */ |
| 76 | printf("%"OFF_FMT"u blocks\n", (archive_handle->offset + 511) >> 9); |
| 77 | goto create_hardlinks; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (S_ISLNK(file_header->mode)) { |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 81 | file_header->link_target = xzalloc(file_header->size + 1); |
| 82 | 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] | 83 | archive_handle->offset += file_header->size; |
| 84 | file_header->size = 0; /* Stop possible seeks in future */ |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 85 | } else { |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 86 | file_header->link_target = NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 87 | } |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 88 | |
| 89 | // TODO: data_extract_all can't deal with hardlinks to non-files... |
| 90 | // (should be !S_ISDIR instead of S_ISREG here) |
| 91 | |
| 92 | if (nlink > 1 && S_ISREG(file_header->mode)) { |
| 93 | hardlinks_t *new = xmalloc(sizeof(*new) + namesize); |
| 94 | new->inode = inode; |
| 95 | new->mode = mode ; |
| 96 | new->mtime = mtime; |
| 97 | new->uid = uid ; |
| 98 | new->gid = gid ; |
| 99 | strcpy(new->name, file_header->name); |
| 100 | /* Put file on a linked list for later */ |
| 101 | if (size == 0) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 102 | new->next = saved_hardlinks; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 103 | saved_hardlinks = new; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 104 | return EXIT_SUCCESS; /* Skip this one */ |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 105 | /* TODO: this breaks cpio -t (it does not show hardlinks) */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 106 | } |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 107 | new->next = saved_hardlinks_created; |
| 108 | saved_hardlinks_created = new; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 109 | } |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 110 | file_header->device = makedev(major, minor); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 111 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 112 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 113 | archive_handle->action_data(archive_handle); |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 114 | archive_handle->action_header(file_header); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 115 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 116 | data_skip(archive_handle); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 117 | } |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 118 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 119 | archive_handle->offset += file_header->size; |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 121 | free(file_header->link_target); |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 122 | free(file_header->name); |
| 123 | file_header->link_target = NULL; |
| 124 | file_header->name = NULL; |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 125 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 126 | return EXIT_SUCCESS; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 127 | |
| 128 | create_hardlinks: |
| 129 | free(file_header->link_target); |
| 130 | free(file_header->name); |
| 131 | |
| 132 | while (saved_hardlinks) { |
| 133 | hardlinks_t *cur; |
| 134 | hardlinks_t *make_me = saved_hardlinks; |
| 135 | saved_hardlinks = make_me->next; |
| 136 | |
| 137 | memset(file_header, 0, sizeof(*file_header)); |
| 138 | file_header->name = make_me->name; |
| 139 | file_header->mode = make_me->mode; |
| 140 | /*file_header->size = 0;*/ |
| 141 | |
| 142 | /* Try to find a file we are hardlinked to */ |
| 143 | cur = saved_hardlinks_created; |
| 144 | while (cur) { |
| 145 | /* TODO: must match maj/min too! */ |
| 146 | if (cur->inode == make_me->inode) { |
| 147 | file_header->link_target = cur->name; |
| 148 | /* link_target != NULL, size = 0: "I am a hardlink" */ |
| 149 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) |
| 150 | archive_handle->action_data(archive_handle); |
| 151 | free(make_me); |
| 152 | goto next_link; |
| 153 | } |
Denis Vlasenko | 2b407b1 | 2008-07-11 21:42:12 +0000 | [diff] [blame] | 154 | cur = cur->next; |
Denis Vlasenko | 1af00ed | 2008-04-05 02:44:30 +0000 | [diff] [blame] | 155 | } |
| 156 | /* Oops... no file with such inode was created... do it now |
| 157 | * (happens when hardlinked files are empty (zero length)) */ |
| 158 | file_header->mtime = make_me->mtime; |
| 159 | file_header->uid = make_me->uid ; |
| 160 | file_header->gid = make_me->gid ; |
| 161 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) |
| 162 | archive_handle->action_data(archive_handle); |
| 163 | /* Move to the list of created hardlinked files */ |
| 164 | make_me->next = saved_hardlinks_created; |
| 165 | saved_hardlinks_created = make_me; |
| 166 | next_link: ; |
| 167 | } |
| 168 | |
| 169 | while (saved_hardlinks_created) { |
| 170 | hardlinks_t *p = saved_hardlinks_created; |
| 171 | saved_hardlinks_created = p->next; |
| 172 | free(p); |
| 173 | } |
| 174 | |
| 175 | return EXIT_FAILURE; /* "No more files to process" */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 176 | } |