blob: 93f6c61aa0c633590343c07916f744b0db8d9cf3 [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];
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000024 int namesize;
25 int major, minor, nlink, mode, inode;
26 unsigned size, uid, gid, mtime;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000027
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000028#define hardlinks_to_create (*(hardlinks_t **)(&archive_handle->ah_priv[0]))
29#define created_hardlinks (*(hardlinks_t **)(&archive_handle->ah_priv[1]))
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000030// if (!archive_handle->ah_priv_inited) {
31// archive_handle->ah_priv_inited = 1;
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000032// hardlinks_to_create = NULL;
33// created_hardlinks = NULL;
Denis Vlasenkoa60936d2008-06-28 05:04:09 +000034// }
35
Glenn L McGrathb72a7352002-12-10 00:17:22 +000036 /* There can be padding before archive header */
37 data_align(archive_handle, 4);
38
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000039 size = full_read(archive_handle->src_fd, cpio_header, 110);
40 if (size == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000041 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000042 }
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000043 if (size != 110) {
44 bb_error_msg_and_die("short read");
45 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +000046 archive_handle->offset += 110;
47
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000048 if (strncmp(&cpio_header[0], "07070", 5) != 0
49 || (cpio_header[5] != '1' && cpio_header[5] != '2')
50 ) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000051 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000052 }
53
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000054 if (sscanf(cpio_header + 6,
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000055 "%8x" "%8x" "%8x" "%8x"
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000056 "%8x" "%8x" "%8x" /*maj,min:*/ "%*16c"
57 /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum: "%*8c"*/,
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000058 &inode, &mode, &uid, &gid,
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000059 &nlink, &mtime, &size,
60 &major, &minor, &namesize) != 10)
61 bb_error_msg_and_die("damaged cpio file");
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000062 file_header->mode = mode;
63 file_header->uid = uid;
64 file_header->gid = gid;
65 file_header->mtime = mtime;
66 file_header->size = size;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000067
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000068 namesize &= 0x1fff; /* paranoia: limit names to 8k chars */
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000069 file_header->name = xzalloc(namesize + 1);
Rob Landley53437472006-07-16 08:14:35 +000070 /* Read in filename */
71 xread(archive_handle->src_fd, file_header->name, namesize);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000072 archive_handle->offset += namesize;
73
74 /* Update offset amount and skip padding before file contents */
75 data_align(archive_handle, 4);
76
77 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000078 /* Always round up. ">> 9" divides by 512 */
79 printf("%"OFF_FMT"u blocks\n", (archive_handle->offset + 511) >> 9);
80 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000081 }
82
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000083 file_header->link_target = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000084 if (S_ISLNK(file_header->mode)) {
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000085 file_header->size &= 0x1fff; /* paranoia: limit names to 8k chars */
Denis Vlasenko75103842007-06-20 14:49:47 +000086 file_header->link_target = xzalloc(file_header->size + 1);
87 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000088 archive_handle->offset += file_header->size;
89 file_header->size = 0; /* Stop possible seeks in future */
90 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000091
92// TODO: data_extract_all can't deal with hardlinks to non-files...
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000093// when fixed, change S_ISREG to !S_ISDIR here
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000094
95 if (nlink > 1 && S_ISREG(file_header->mode)) {
96 hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
97 new->inode = inode;
98 new->mode = mode ;
99 new->mtime = mtime;
100 new->uid = uid ;
101 new->gid = gid ;
102 strcpy(new->name, file_header->name);
103 /* Put file on a linked list for later */
104 if (size == 0) {
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000105 new->next = hardlinks_to_create;
106 hardlinks_to_create = new;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000107 return EXIT_SUCCESS; /* Skip this one */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000108 /* TODO: this breaks cpio -t (it does not show hardlinks) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000109 }
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000110 new->next = created_hardlinks;
111 created_hardlinks = new;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000112 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000113 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000114
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000115 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000116 archive_handle->action_data(archive_handle);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000117 archive_handle->action_header(file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000118 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000119 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000120 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000121
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000122 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000123
Denis Vlasenko75103842007-06-20 14:49:47 +0000124 free(file_header->link_target);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000125 free(file_header->name);
126 file_header->link_target = NULL;
127 file_header->name = NULL;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000128
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000129 return EXIT_SUCCESS;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000130
131 create_hardlinks:
132 free(file_header->link_target);
133 free(file_header->name);
134
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000135 while (hardlinks_to_create) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000136 hardlinks_t *cur;
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000137 hardlinks_t *make_me = hardlinks_to_create;
138
139 hardlinks_to_create = make_me->next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000140
141 memset(file_header, 0, sizeof(*file_header));
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000142 file_header->mtime = make_me->mtime;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000143 file_header->name = make_me->name;
144 file_header->mode = make_me->mode;
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000145 file_header->uid = make_me->uid;
146 file_header->gid = make_me->gid;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000147 /*file_header->size = 0;*/
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000148 /*file_header->link_target = NULL;*/
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000149
150 /* Try to find a file we are hardlinked to */
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000151 cur = created_hardlinks;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000152 while (cur) {
153 /* TODO: must match maj/min too! */
154 if (cur->inode == make_me->inode) {
155 file_header->link_target = cur->name;
156 /* link_target != NULL, size = 0: "I am a hardlink" */
157 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
158 archive_handle->action_data(archive_handle);
159 free(make_me);
160 goto next_link;
161 }
Denis Vlasenko2b407b12008-07-11 21:42:12 +0000162 cur = cur->next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000163 }
164 /* Oops... no file with such inode was created... do it now
165 * (happens when hardlinked files are empty (zero length)) */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000166 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
167 archive_handle->action_data(archive_handle);
168 /* Move to the list of created hardlinked files */
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000169 make_me->next = created_hardlinks;
170 created_hardlinks = make_me;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000171 next_link: ;
172 }
173
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000174 while (created_hardlinks) {
175 hardlinks_t *p = created_hardlinks;
176 created_hardlinks = p->next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000177 free(p);
178 }
179
180 return EXIT_FAILURE; /* "No more files to process" */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000181}