blob: badd4a841656e4c1fd0b02c7b1b1d872f74493e6 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathb72a7352002-12-10 00:17:22 +00005 */
6
Glenn L McGrathb72a7352002-12-10 00:17:22 +00007#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +02008#include "bb_archive.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
28 /* There can be padding before archive header */
29 data_align(archive_handle, 4);
30
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000031 size = full_read(archive_handle->src_fd, cpio_header, 110);
32 if (size == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000033 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000034 }
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000035 if (size != 110) {
36 bb_error_msg_and_die("short read");
37 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +000038 archive_handle->offset += 110;
39
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010040 if (!is_prefixed_with(&cpio_header[0], "07070")
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000041 || (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 Vlasenkoa46dd892008-07-12 09:20:44 +000046 if (sscanf(cpio_header + 6,
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000047 "%8x" "%8x" "%8x" "%8x"
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000048 "%8x" "%8x" "%8x" /*maj,min:*/ "%*16c"
49 /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum: "%*8c"*/,
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000050 &inode, &mode, &uid, &gid,
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000051 &nlink, &mtime, &size,
52 &major, &minor, &namesize) != 10)
53 bb_error_msg_and_die("damaged cpio file");
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000054 file_header->mode = mode;
Aaro Koskinen2735bc02015-10-16 17:24:46 +020055 /* "cpio -R USER:GRP" support: */
56 if (archive_handle->cpio__owner.uid != (uid_t)-1L)
57 uid = archive_handle->cpio__owner.uid;
58 if (archive_handle->cpio__owner.gid != (gid_t)-1L)
59 gid = archive_handle->cpio__owner.gid;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000060 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 Vlasenkoa46dd892008-07-12 09:20:44 +000065 namesize &= 0x1fff; /* paranoia: limit names to 8k chars */
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);
Denys Vlasenkoaf1c8e82010-01-05 04:43:21 +010069 if (file_header->name[0] == '/') {
70 /* Testcase: echo /etc/hosts | cpio -pvd /tmp
71 * Without this code, it tries to unpack /etc/hosts
72 * into "/etc/hosts", not "etc/hosts".
73 */
74 char *p = file_header->name;
75 do p++; while (*p == '/');
76 overlapping_strcpy(file_header->name, p);
77 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +000078 archive_handle->offset += namesize;
79
80 /* Update offset amount and skip padding before file contents */
81 data_align(archive_handle, 4);
82
Aaro Koskinen2735bc02015-10-16 17:24:46 +020083 if (strcmp(file_header->name, cpio_TRAILER) == 0) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000084 /* Always round up. ">> 9" divides by 512 */
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +010085 archive_handle->cpio__blocks = (uoff_t)(archive_handle->offset + 511) >> 9;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000086 goto create_hardlinks;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000087 }
88
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000089 file_header->link_target = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000090 if (S_ISLNK(file_header->mode)) {
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000091 file_header->size &= 0x1fff; /* paranoia: limit names to 8k chars */
Denis Vlasenko75103842007-06-20 14:49:47 +000092 file_header->link_target = xzalloc(file_header->size + 1);
93 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000094 archive_handle->offset += file_header->size;
95 file_header->size = 0; /* Stop possible seeks in future */
96 }
Denis Vlasenko1af00ed2008-04-05 02:44:30 +000097
98// TODO: data_extract_all can't deal with hardlinks to non-files...
Denis Vlasenkoa46dd892008-07-12 09:20:44 +000099// when fixed, change S_ISREG to !S_ISDIR here
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000100
101 if (nlink > 1 && S_ISREG(file_header->mode)) {
102 hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
103 new->inode = inode;
104 new->mode = mode ;
105 new->mtime = mtime;
106 new->uid = uid ;
107 new->gid = gid ;
108 strcpy(new->name, file_header->name);
109 /* Put file on a linked list for later */
110 if (size == 0) {
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100111 new->next = archive_handle->cpio__hardlinks_to_create;
112 archive_handle->cpio__hardlinks_to_create = new;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000113 return EXIT_SUCCESS; /* Skip this one */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000114 /* TODO: this breaks cpio -t (it does not show hardlinks) */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000115 }
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100116 new->next = archive_handle->cpio__created_hardlinks;
117 archive_handle->cpio__created_hardlinks = new;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000118 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000119 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000120
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000121 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000122 archive_handle->action_data(archive_handle);
Denys Vlasenko93ac7d82010-01-09 19:56:15 +0100123//TODO: run "echo /etc/hosts | cpio -pv /tmp" twice. On 2nd run:
124//cpio: etc/hosts not created: newer or same age file exists
125//etc/hosts <-- should NOT show it
126//2 blocks <-- should say "0 blocks"
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000127 archive_handle->action_header(file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000128 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000129 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000130 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000131
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000132 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000133
Denis Vlasenko75103842007-06-20 14:49:47 +0000134 free(file_header->link_target);
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000135 free(file_header->name);
136 file_header->link_target = NULL;
137 file_header->name = NULL;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000138
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000139 return EXIT_SUCCESS;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000140
141 create_hardlinks:
142 free(file_header->link_target);
143 free(file_header->name);
144
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100145 while (archive_handle->cpio__hardlinks_to_create) {
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000146 hardlinks_t *cur;
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100147 hardlinks_t *make_me = archive_handle->cpio__hardlinks_to_create;
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000148
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100149 archive_handle->cpio__hardlinks_to_create = make_me->next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000150
151 memset(file_header, 0, sizeof(*file_header));
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000152 file_header->mtime = make_me->mtime;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000153 file_header->name = make_me->name;
154 file_header->mode = make_me->mode;
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000155 file_header->uid = make_me->uid;
156 file_header->gid = make_me->gid;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000157 /*file_header->size = 0;*/
Denis Vlasenkoa46dd892008-07-12 09:20:44 +0000158 /*file_header->link_target = NULL;*/
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000159
160 /* Try to find a file we are hardlinked to */
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100161 cur = archive_handle->cpio__created_hardlinks;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000162 while (cur) {
163 /* TODO: must match maj/min too! */
164 if (cur->inode == make_me->inode) {
165 file_header->link_target = cur->name;
166 /* link_target != NULL, size = 0: "I am a hardlink" */
167 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
168 archive_handle->action_data(archive_handle);
169 free(make_me);
170 goto next_link;
171 }
Denis Vlasenko2b407b12008-07-11 21:42:12 +0000172 cur = cur->next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000173 }
174 /* Oops... no file with such inode was created... do it now
175 * (happens when hardlinked files are empty (zero length)) */
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000176 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
177 archive_handle->action_data(archive_handle);
178 /* Move to the list of created hardlinked files */
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100179 make_me->next = archive_handle->cpio__created_hardlinks;
180 archive_handle->cpio__created_hardlinks = make_me;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000181 next_link: ;
182 }
183
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100184 while (archive_handle->cpio__created_hardlinks) {
185 hardlinks_t *p = archive_handle->cpio__created_hardlinks;
186 archive_handle->cpio__created_hardlinks = p->next;
Denis Vlasenko1af00ed2008-04-05 02:44:30 +0000187 free(p);
188 }
189
190 return EXIT_FAILURE; /* "No more files to process" */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000191}