blob: 96be4b5ac82685e5294824dcfd1c39cad4b3aef1 [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
10typedef struct hardlinks_s {
Glenn L McGrathb72a7352002-12-10 00:17:22 +000011 struct hardlinks_s *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
Eric Andersend78aea82006-01-30 18:00:02 +000040 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000041 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000042 }
43 archive_handle->offset += 110;
44
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000045 if (strncmp(&cpio_header[0], "07070", 5) != 0
46 || (cpio_header[5] != '1' && cpio_header[5] != '2')
47 ) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000048 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000049 }
50
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000051 sscanf(cpio_header + 6,
52 "%8x" "%8x" "%8x" "%8x"
53 "%8x" "%8x" "%8x" /*maj,min:*/ "%16c"
54 /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum:*/ "%8c",
55 &inode, &mode, &uid, &gid,
56 &nlink, &mtime, &size, dummy,
57 &major, &minor, &namesize, dummy);
58 file_header->mode = mode;
59 file_header->uid = uid;
60 file_header->gid = gid;
61 file_header->mtime = mtime;
62 file_header->size = size;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000063
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000064 file_header->name = xzalloc(namesize + 1);
Rob Landley53437472006-07-16 08:14:35 +000065 /* Read in filename */
66 xread(archive_handle->src_fd, file_header->name, namesize);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000067 archive_handle->offset += namesize;
68
69 /* Update offset amount and skip padding before file contents */
70 data_align(archive_handle, 4);
71
72 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000073 /* Always round up. ">> 9" divides by 512 */
74 printf("%"OFF_FMT"u blocks\n", (archive_handle->offset + 511) >> 9);
75 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000076 }
77
78 if (S_ISLNK(file_header->mode)) {
Denis Vlasenko75103842007-06-20 14:49:47 +000079 file_header->link_target = xzalloc(file_header->size + 1);
80 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000081 archive_handle->offset += file_header->size;
82 file_header->size = 0; /* Stop possible seeks in future */
Glenn L McGrathfaa35462004-04-29 09:24:19 +000083 } else {
Denis Vlasenko75103842007-06-20 14:49:47 +000084 file_header->link_target = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000085 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000086
87// TODO: data_extract_all can't deal with hardlinks to non-files...
88// (should be !S_ISDIR instead of S_ISREG here)
89
90 if (nlink > 1 && S_ISREG(file_header->mode)) {
91 hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
92 new->inode = inode;
93 new->mode = mode ;
94 new->mtime = mtime;
95 new->uid = uid ;
96 new->gid = gid ;
97 strcpy(new->name, file_header->name);
98 /* Put file on a linked list for later */
99 if (size == 0) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000100 new->next = saved_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000101 saved_hardlinks = new;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000102 return EXIT_SUCCESS; /* Skip this one */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000103 /* TODO: this breaks cpio -t (it does not show hardlinks) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000104 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000105 new->next = saved_hardlinks_created;
106 saved_hardlinks_created = new;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000107 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000108 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000109
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000110 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000111 archive_handle->action_data(archive_handle);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000112 archive_handle->action_header(file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000113 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000114 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000115 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000116
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000117 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000118
Denis Vlasenko75103842007-06-20 14:49:47 +0000119 free(file_header->link_target);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000120 free(file_header->name);
121 file_header->link_target = NULL;
122 file_header->name = NULL;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000123
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000124 return EXIT_SUCCESS;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000125
126 create_hardlinks:
127 free(file_header->link_target);
128 free(file_header->name);
129
130 while (saved_hardlinks) {
131 hardlinks_t *cur;
132 hardlinks_t *make_me = saved_hardlinks;
133 saved_hardlinks = make_me->next;
134
135 memset(file_header, 0, sizeof(*file_header));
136 file_header->name = make_me->name;
137 file_header->mode = make_me->mode;
138 /*file_header->size = 0;*/
139
140 /* Try to find a file we are hardlinked to */
141 cur = saved_hardlinks_created;
142 while (cur) {
143 /* TODO: must match maj/min too! */
144 if (cur->inode == make_me->inode) {
145 file_header->link_target = cur->name;
146 /* link_target != NULL, size = 0: "I am a hardlink" */
147 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
148 archive_handle->action_data(archive_handle);
149 free(make_me);
150 goto next_link;
151 }
152 }
153 /* Oops... no file with such inode was created... do it now
154 * (happens when hardlinked files are empty (zero length)) */
155 file_header->mtime = make_me->mtime;
156 file_header->uid = make_me->uid ;
157 file_header->gid = make_me->gid ;
158 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
159 archive_handle->action_data(archive_handle);
160 /* Move to the list of created hardlinked files */
161 make_me->next = saved_hardlinks_created;
162 saved_hardlinks_created = make_me;
163 next_link: ;
164 }
165
166 while (saved_hardlinks_created) {
167 hardlinks_t *p = saved_hardlinks_created;
168 saved_hardlinks_created = p->next;
169 free(p);
170 }
171
172 return EXIT_FAILURE; /* "No more files to process" */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000173}