blob: 4ed18c68f3eac7009e16fa192afd9518530d062a [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{
22 static hardlinks_t *saved_hardlinks = NULL;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000023 static hardlinks_t *saved_hardlinks_created = NULL;
Denis Vlasenkoa80b4a02007-06-21 12:41:59 +000024
Glenn L McGrathb72a7352002-12-10 00:17:22 +000025 file_header_t *file_header = archive_handle->file_header;
26 char cpio_header[110];
Glenn L McGrathb72a7352002-12-10 00:17:22 +000027 char dummy[16];
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000028 int namesize;
29 int major, minor, nlink, mode, inode;
30 unsigned size, uid, gid, mtime;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000031
32 /* There can be padding before archive header */
33 data_align(archive_handle, 4);
34
Eric Andersend78aea82006-01-30 18:00:02 +000035 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000036 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000037 }
38 archive_handle->offset += 110;
39
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000040 if (strncmp(&cpio_header[0], "07070", 5) != 0
41 || (cpio_header[5] != '1' && cpio_header[5] != '2')
42 ) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000043 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000044 }
45
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000046 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 McGrathb72a7352002-12-10 00:17:22 +000058
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000059 file_header->name = xzalloc(namesize + 1);
Rob Landley53437472006-07-16 08:14:35 +000060 /* Read in filename */
61 xread(archive_handle->src_fd, file_header->name, namesize);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000062 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 Vlasenko1af00ed2008-04-05 02:44:30 +000068 /* 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 McGrathb72a7352002-12-10 00:17:22 +000071 }
72
73 if (S_ISLNK(file_header->mode)) {
Denis Vlasenko75103842007-06-20 14:49:47 +000074 file_header->link_target = xzalloc(file_header->size + 1);
75 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000076 archive_handle->offset += file_header->size;
77 file_header->size = 0; /* Stop possible seeks in future */
Glenn L McGrathfaa35462004-04-29 09:24:19 +000078 } else {
Denis Vlasenko75103842007-06-20 14:49:47 +000079 file_header->link_target = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000080 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000081
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 McGrathb72a7352002-12-10 00:17:22 +000095 new->next = saved_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000096 saved_hardlinks = new;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000097 return EXIT_SUCCESS; /* Skip this one */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000098 /* TODO: this breaks cpio -t (it does not show hardlinks) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +000099 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000100 new->next = saved_hardlinks_created;
101 saved_hardlinks_created = new;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000102 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000103 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000104
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000105 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000106 archive_handle->action_data(archive_handle);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000107 archive_handle->action_header(file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000108 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000109 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000110 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000111
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000112 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000113
Denis Vlasenko75103842007-06-20 14:49:47 +0000114 free(file_header->link_target);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000115 free(file_header->name);
116 file_header->link_target = NULL;
117 file_header->name = NULL;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000118
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000119 return EXIT_SUCCESS;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000120
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 McGrathb72a7352002-12-10 00:17:22 +0000168}