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