1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright 2002 Laurence Anderson
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6  */
7 #include "libbb.h"
8 #include "bb_archive.h"
9 
10 typedef struct hardlinks_t {
11 	struct hardlinks_t *next;
12 	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];
18 } hardlinks_t;
19 
get_header_cpio(archive_handle_t * archive_handle)20 char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
21 {
22 	file_header_t *file_header = archive_handle->file_header;
23 	char cpio_header[111];
24 	int namesize;
25 	int major, minor, nlink, mode, inode;
26 	unsigned size, uid, gid, mtime;
27 
28 	/* There can be padding before archive header */
29 	data_align(archive_handle, 4);
30 
31 	size = full_read(archive_handle->src_fd, cpio_header, 110);
32 	if (size == 0) {
33 		goto create_hardlinks;
34 	}
35 	if (size != 110) {
36 		bb_simple_error_msg_and_die("short read");
37 	}
38 	archive_handle->offset += 110;
39 
40 	if (!is_prefixed_with(&cpio_header[0], "07070")
41 	 || (cpio_header[5] != '1' && cpio_header[5] != '2')
42 	) {
43 		bb_simple_error_msg_and_die("unsupported cpio format, use newc or crc");
44 	}
45 
46 	cpio_header[110] = '\0'; /* sscanf may call strlen which may break without this */
47 	if (sscanf(cpio_header + 6,
48 			"%8x" "%8x" "%8x" "%8x"
49 			"%8x" "%8x" "%8x" /*maj,min:*/ "%*16c"
50 			/*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum: "%*8c"*/,
51 			&inode, &mode, &uid, &gid,
52 			&nlink, &mtime, &size,
53 			&major, &minor, &namesize) != 10)
54 		bb_simple_error_msg_and_die("damaged cpio file");
55 	file_header->mode = mode;
56 	/* "cpio -R USER:GRP" support: */
57 	if (archive_handle->cpio__owner.uid != (uid_t)-1L)
58 		uid = archive_handle->cpio__owner.uid;
59 	if (archive_handle->cpio__owner.gid != (gid_t)-1L)
60 		gid = archive_handle->cpio__owner.gid;
61 	file_header->uid = uid;
62 	file_header->gid = gid;
63 	file_header->mtime = mtime;
64 	file_header->size = size;
65 
66 	namesize &= 0x1fff; /* paranoia: limit names to 8k chars */
67 	file_header->name = xzalloc(namesize + 1);
68 	/* Read in filename */
69 	xread(archive_handle->src_fd, file_header->name, namesize);
70 	if (file_header->name[0] == '/') {
71 		/* Testcase: echo /etc/hosts | cpio -pvd /tmp
72 		 * Without this code, it tries to unpack /etc/hosts
73 		 * into "/etc/hosts", not "etc/hosts".
74 		 */
75 		char *p = file_header->name;
76 		do p++; while (*p == '/');
77 		overlapping_strcpy(file_header->name, p);
78 	}
79 	archive_handle->offset += namesize;
80 
81 	/* Update offset amount and skip padding before file contents */
82 	data_align(archive_handle, 4);
83 
84 	if (strcmp(file_header->name, cpio_TRAILER) == 0) {
85 		/* Always round up. ">> 9" divides by 512 */
86 		archive_handle->cpio__blocks = (uoff_t)(archive_handle->offset + 511) >> 9;
87 		goto create_hardlinks;
88 	}
89 
90 	file_header->link_target = NULL;
91 	if (S_ISLNK(file_header->mode)) {
92 		file_header->size &= 0x1fff; /* paranoia: limit names to 8k chars */
93 		file_header->link_target = xzalloc(file_header->size + 1);
94 		xread(archive_handle->src_fd, file_header->link_target, file_header->size);
95 		archive_handle->offset += file_header->size;
96 		file_header->size = 0; /* Stop possible seeks in future */
97 	}
98 
99 // TODO: data_extract_all can't deal with hardlinks to non-files...
100 // when fixed, change S_ISREG to !S_ISDIR here
101 
102 	if (nlink > 1 && S_ISREG(file_header->mode)) {
103 		hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
104 		new->inode = inode;
105 		new->mode  = mode ;
106 		new->mtime = mtime;
107 		new->uid   = uid  ;
108 		new->gid   = gid  ;
109 		strcpy(new->name, file_header->name);
110 		/* Put file on a linked list for later */
111 		if (size == 0) {
112 			new->next = archive_handle->cpio__hardlinks_to_create;
113 			archive_handle->cpio__hardlinks_to_create = new;
114 			return EXIT_SUCCESS; /* Skip this one */
115 			/* TODO: this breaks cpio -t (it does not show hardlinks) */
116 		}
117 		new->next = archive_handle->cpio__created_hardlinks;
118 		archive_handle->cpio__created_hardlinks = new;
119 	}
120 	file_header->device = makedev(major, minor);
121 
122 	if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
123 		archive_handle->action_data(archive_handle);
124 //TODO: run "echo /etc/hosts | cpio -pv /tmp" twice. On 2nd run:
125 //cpio: etc/hosts not created: newer or same age file exists
126 //etc/hosts  <-- should NOT show it
127 //2 blocks <-- should say "0 blocks"
128 		archive_handle->action_header(file_header);
129 	} else {
130 		data_skip(archive_handle);
131 	}
132 
133 	archive_handle->offset += file_header->size;
134 
135 	free(file_header->link_target);
136 	free(file_header->name);
137 	file_header->link_target = NULL;
138 	file_header->name = NULL;
139 
140 	return EXIT_SUCCESS;
141 
142  create_hardlinks:
143 	free(file_header->link_target);
144 	free(file_header->name);
145 
146 	while (archive_handle->cpio__hardlinks_to_create) {
147 		hardlinks_t *cur;
148 		hardlinks_t *make_me = archive_handle->cpio__hardlinks_to_create;
149 
150 		archive_handle->cpio__hardlinks_to_create = make_me->next;
151 
152 		memset(file_header, 0, sizeof(*file_header));
153 		file_header->mtime = make_me->mtime;
154 		file_header->name = make_me->name;
155 		file_header->mode = make_me->mode;
156 		file_header->uid = make_me->uid;
157 		file_header->gid = make_me->gid;
158 		/*file_header->size = 0;*/
159 		/*file_header->link_target = NULL;*/
160 
161 		/* Try to find a file we are hardlinked to */
162 		cur = archive_handle->cpio__created_hardlinks;
163 		while (cur) {
164 			/* TODO: must match maj/min too! */
165 			if (cur->inode == make_me->inode) {
166 				file_header->link_target = cur->name;
167 				 /* link_target != NULL, size = 0: "I am a hardlink" */
168 				if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
169 					archive_handle->action_data(archive_handle);
170 				free(make_me);
171 				goto next_link;
172 			}
173 			cur = cur->next;
174 		}
175 		/* Oops... no file with such inode was created... do it now
176 		 * (happens when hardlinked files are empty (zero length)) */
177 		if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
178 			archive_handle->action_data(archive_handle);
179 		/* Move to the list of created hardlinked files */
180 		make_me->next = archive_handle->cpio__created_hardlinks;
181 		archive_handle->cpio__created_hardlinks = make_me;
182  next_link: ;
183 	}
184 
185 	while (archive_handle->cpio__created_hardlinks) {
186 		hardlinks_t *p = archive_handle->cpio__created_hardlinks;
187 		archive_handle->cpio__created_hardlinks = p->next;
188 		free(p);
189 	}
190 
191 	return EXIT_FAILURE; /* "No more files to process" */
192 }
193