"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 { |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 11 | char *name; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 12 | int inode; |
| 13 | struct hardlinks_s *next; |
| 14 | } hardlinks_t; |
| 15 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 16 | char get_header_cpio(archive_handle_t *archive_handle) |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 17 | { |
| 18 | static hardlinks_t *saved_hardlinks = NULL; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame^] | 19 | static unsigned pending_hardlinks = 0; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 20 | static int inode; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +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]; |
| 24 | int namesize; |
| 25 | char dummy[16]; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 26 | int major, minor, nlink; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 27 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 28 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 29 | hardlinks_t *tmp, *oldtmp; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 30 | |
| 31 | tmp = saved_hardlinks; |
| 32 | oldtmp = NULL; |
| 33 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 34 | file_header->link_target = file_header->name; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 35 | file_header->size = 0; |
| 36 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 37 | while (tmp) { |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 38 | if (tmp->inode != inode) { |
| 39 | tmp = tmp->next; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 40 | continue; |
| 41 | } |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 42 | |
| 43 | file_header->name = tmp->name; |
| 44 | |
| 45 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
| 46 | archive_handle->action_data(archive_handle); |
| 47 | archive_handle->action_header(archive_handle->file_header); |
| 48 | } |
| 49 | |
| 50 | pending_hardlinks--; |
| 51 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 52 | oldtmp = tmp; |
| 53 | tmp = tmp->next; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 54 | free(oldtmp->name); |
| 55 | free(oldtmp); |
| 56 | if (oldtmp == saved_hardlinks) |
| 57 | saved_hardlinks = tmp; |
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 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 60 | file_header->name = file_header->link_target; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 61 | |
| 62 | if (pending_hardlinks > 1) { |
| 63 | bb_error_msg("error resolving hardlink: archive made by GNU cpio 2.0-2.2?"); |
| 64 | } |
| 65 | |
| 66 | /* No more pending hardlinks, read next file entry */ |
| 67 | pending_hardlinks = 0; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* There can be padding before archive header */ |
| 71 | data_align(archive_handle, 4); |
| 72 | |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 73 | if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) { |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 74 | return EXIT_FAILURE; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 75 | } |
| 76 | archive_handle->offset += 110; |
| 77 | |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 78 | if (strncmp(&cpio_header[0], "07070", 5) != 0 |
| 79 | || (cpio_header[5] != '1' && cpio_header[5] != '2') |
| 80 | ) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 81 | 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] | 82 | } |
| 83 | |
Eric Andersen | f4b273c | 2002-12-11 21:45:08 +0000 | [diff] [blame] | 84 | { |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 85 | unsigned long tmpsize; |
| 86 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | dummy, &inode, (unsigned int*)&file_header->mode, |
Eric Andersen | f4b273c | 2002-12-11 21:45:08 +0000 | [diff] [blame] | 88 | (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid, |
| 89 | &nlink, &file_header->mtime, &tmpsize, |
| 90 | dummy, &major, &minor, &namesize, dummy); |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 91 | file_header->size = tmpsize; |
Eric Andersen | f4b273c | 2002-12-11 21:45:08 +0000 | [diff] [blame] | 92 | } |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 94 | free(file_header->name); |
| 95 | file_header->name = xzalloc(namesize + 1); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 96 | /* Read in filename */ |
| 97 | xread(archive_handle->src_fd, file_header->name, namesize); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 98 | archive_handle->offset += namesize; |
| 99 | |
| 100 | /* Update offset amount and skip padding before file contents */ |
| 101 | data_align(archive_handle, 4); |
| 102 | |
| 103 | if (strcmp(file_header->name, "TRAILER!!!") == 0) { |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 104 | /* Always round up */ |
| 105 | printf("%d blocks\n", (int) (archive_handle->offset % 512 ? |
| 106 | archive_handle->offset / 512 + 1 : |
Denis Vlasenko | 150f402 | 2007-01-13 21:06:21 +0000 | [diff] [blame] | 107 | archive_handle->offset / 512 |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 108 | )); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 109 | if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */ |
| 110 | hardlinks_t *tmp = saved_hardlinks; |
| 111 | hardlinks_t *oldtmp = NULL; |
| 112 | while (tmp) { |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 113 | bb_error_msg("%s not created: cannot resolve hardlink", tmp->name); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 114 | oldtmp = tmp; |
| 115 | tmp = tmp->next; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 116 | free(oldtmp->name); |
| 117 | free(oldtmp); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 118 | } |
| 119 | saved_hardlinks = NULL; |
| 120 | pending_hardlinks = 0; |
| 121 | } |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 122 | return EXIT_FAILURE; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (S_ISLNK(file_header->mode)) { |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 126 | file_header->link_target = xzalloc(file_header->size + 1); |
| 127 | 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] | 128 | archive_handle->offset += file_header->size; |
| 129 | file_header->size = 0; /* Stop possible seeks in future */ |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 130 | } else { |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 131 | file_header->link_target = NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 132 | } |
| 133 | if (nlink > 1 && !S_ISDIR(file_header->mode)) { |
| 134 | if (file_header->size == 0) { /* Put file on a linked list for later */ |
| 135 | hardlinks_t *new = xmalloc(sizeof(hardlinks_t)); |
| 136 | new->next = saved_hardlinks; |
| 137 | new->inode = inode; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 138 | /* name current allocated, freed later */ |
| 139 | new->name = file_header->name; |
| 140 | file_header->name = NULL; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 141 | saved_hardlinks = new; |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 142 | return EXIT_SUCCESS; /* Skip this one */ |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 143 | } |
Denis Vlasenko | 3eb91c2 | 2006-11-21 00:55:46 +0000 | [diff] [blame] | 144 | /* Found the file with data in */ |
| 145 | pending_hardlinks = nlink; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 146 | } |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 147 | file_header->device = makedev(major, minor); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 148 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 149 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 150 | archive_handle->action_data(archive_handle); |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 151 | archive_handle->action_header(archive_handle->file_header); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 152 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 153 | data_skip(archive_handle); |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 154 | } |
Glenn L McGrath | 4cee66d | 2003-08-28 19:12:23 +0000 | [diff] [blame] | 155 | |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 156 | archive_handle->offset += file_header->size; |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 158 | free(file_header->link_target); |
Glenn L McGrath | faa3546 | 2004-04-29 09:24:19 +0000 | [diff] [blame] | 159 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 160 | return EXIT_SUCCESS; |
Glenn L McGrath | b72a735 | 2002-12-10 00:17:22 +0000 | [diff] [blame] | 161 | } |