blob: 307d2a66d9049968f9bfc5396123570311518146 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley1ec5b292006-05-29 07:42:02 +00002/* Copyright 2002 Laurence Anderson
Glenn L McGrathb72a7352002-12-10 00:17:22 +00003 *
Rob Landley1ec5b292006-05-29 07:42:02 +00004 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathb72a7352002-12-10 00:17:22 +00005 */
6
Glenn L McGrathb72a7352002-12-10 00:17:22 +00007#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +00008#include "unarchive.h"
Glenn L McGrathb72a7352002-12-10 00:17:22 +00009
Denis Vlasenko2b407b12008-07-11 21:42:12 +000010typedef struct hardlinks_t {
11 struct hardlinks_t *next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000012 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 McGrathb72a7352002-12-10 00:17:22 +000018} hardlinks_t;
19
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
Glenn L McGrathb72a7352002-12-10 00:17:22 +000021{
Glenn L McGrathb72a7352002-12-10 00:17:22 +000022 file_header_t *file_header = archive_handle->file_header;
23 char cpio_header[110];
Glenn L McGrathb72a7352002-12-10 00:17:22 +000024 char dummy[16];
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000025 int namesize;
26 int major, minor, nlink, mode, inode;
27 unsigned size, uid, gid, mtime;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000028
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000029#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 McGrathb72a7352002-12-10 00:17:22 +000037 /* There can be padding before archive header */
38 data_align(archive_handle, 4);
39
Denis Vlasenkoee1b3b12008-07-11 22:20:59 +000040//TODO: this function is used only here, make it static?
Eric Andersend78aea82006-01-30 18:00:02 +000041 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000042 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000043 }
44 archive_handle->offset += 110;
45
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000046 if (strncmp(&cpio_header[0], "07070", 5) != 0
47 || (cpio_header[5] != '1' && cpio_header[5] != '2')
48 ) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000049 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000050 }
51
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000052 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 McGrathb72a7352002-12-10 00:17:22 +000064
Denis Vlasenkoee1b3b12008-07-11 22:20:59 +000065 namesize &= 0x1fff; /* paranoia: names can't be that long */
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000066 file_header->name = xzalloc(namesize + 1);
Rob Landley53437472006-07-16 08:14:35 +000067 /* Read in filename */
68 xread(archive_handle->src_fd, file_header->name, namesize);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000069 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 Vlasenko1af00ed2008-04-05 02:44:30 +000075 /* 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 McGrathb72a7352002-12-10 00:17:22 +000078 }
79
80 if (S_ISLNK(file_header->mode)) {
Denis Vlasenko75103842007-06-20 14:49:47 +000081 file_header->link_target = xzalloc(file_header->size + 1);
82 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000083 archive_handle->offset += file_header->size;
84 file_header->size = 0; /* Stop possible seeks in future */
Glenn L McGrathfaa35462004-04-29 09:24:19 +000085 } else {
Denis Vlasenko75103842007-06-20 14:49:47 +000086 file_header->link_target = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000087 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000088
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 McGrathb72a7352002-12-10 00:17:22 +0000102 new->next = saved_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000103 saved_hardlinks = new;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000104 return EXIT_SUCCESS; /* Skip this one */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000105 /* TODO: this breaks cpio -t (it does not show hardlinks) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000106 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000107 new->next = saved_hardlinks_created;
108 saved_hardlinks_created = new;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000109 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000110 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000111
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000112 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000113 archive_handle->action_data(archive_handle);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000114 archive_handle->action_header(file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000115 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000116 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000117 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000118
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000119 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000120
Denis Vlasenko75103842007-06-20 14:49:47 +0000121 free(file_header->link_target);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000122 free(file_header->name);
123 file_header->link_target = NULL;
124 file_header->name = NULL;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000125
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000126 return EXIT_SUCCESS;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000127
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 Vlasenko2b407b12008-07-11 21:42:12 +0000154 cur = cur->next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000155 }
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 McGrathb72a7352002-12-10 00:17:22 +0000176}