blob: 724368be28e7aa808d01854a5ecd64be895a9d47 [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 {
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000011 char *name;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000012 int inode;
13 struct hardlinks_s *next;
14} hardlinks_t;
15
Rob Landleydfba7412006-03-06 20:47:33 +000016char get_header_cpio(archive_handle_t *archive_handle)
Glenn L McGrathb72a7352002-12-10 00:17:22 +000017{
18 static hardlinks_t *saved_hardlinks = NULL;
19 static unsigned short pending_hardlinks = 0;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000020 static int inode;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000021 file_header_t *file_header = archive_handle->file_header;
22 char cpio_header[110];
23 int namesize;
24 char dummy[16];
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000025 int major, minor, nlink;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000026
Glenn L McGrathb72a7352002-12-10 00:17:22 +000027 if (pending_hardlinks) { /* Deal with any pending hardlinks */
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000028 hardlinks_t *tmp, *oldtmp;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000029
30 tmp = saved_hardlinks;
31 oldtmp = NULL;
32
Denis Vlasenko75103842007-06-20 14:49:47 +000033 file_header->link_target = file_header->name;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000034 file_header->size = 0;
35
Glenn L McGrathb72a7352002-12-10 00:17:22 +000036 while (tmp) {
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000037 if (tmp->inode != inode) {
38 tmp = tmp->next;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000039 continue;
40 }
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000041
42 file_header->name = tmp->name;
43
44 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
45 archive_handle->action_data(archive_handle);
46 archive_handle->action_header(archive_handle->file_header);
47 }
48
49 pending_hardlinks--;
50
Glenn L McGrathb72a7352002-12-10 00:17:22 +000051 oldtmp = tmp;
52 tmp = tmp->next;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000053 free(oldtmp->name);
54 free(oldtmp);
55 if (oldtmp == saved_hardlinks)
56 saved_hardlinks = tmp;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000057 }
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000058
Denis Vlasenko75103842007-06-20 14:49:47 +000059 file_header->name = file_header->link_target;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000060
61 if (pending_hardlinks > 1) {
62 bb_error_msg("error resolving hardlink: archive made by GNU cpio 2.0-2.2?");
63 }
64
65 /* No more pending hardlinks, read next file entry */
66 pending_hardlinks = 0;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000067 }
68
69 /* There can be padding before archive header */
70 data_align(archive_handle, 4);
71
Eric Andersend78aea82006-01-30 18:00:02 +000072 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000073 return EXIT_FAILURE;
Glenn L McGrathb72a7352002-12-10 00:17:22 +000074 }
75 archive_handle->offset += 110;
76
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000077 if (strncmp(&cpio_header[0], "07070", 5) != 0
78 || (cpio_header[5] != '1' && cpio_header[5] != '2')
79 ) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000080 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000081 }
82
Eric Andersenf4b273c2002-12-11 21:45:08 +000083 {
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000084 unsigned long tmpsize;
85 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
Eric Andersenc7bda1c2004-03-15 08:29:22 +000086 dummy, &inode, (unsigned int*)&file_header->mode,
Eric Andersenf4b273c2002-12-11 21:45:08 +000087 (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
88 &nlink, &file_header->mtime, &tmpsize,
89 dummy, &major, &minor, &namesize, dummy);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000090 file_header->size = tmpsize;
Eric Andersenf4b273c2002-12-11 21:45:08 +000091 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +000092
Denis Vlasenko3eb91c22006-11-21 00:55:46 +000093 free(file_header->name);
94 file_header->name = xzalloc(namesize + 1);
Rob Landley53437472006-07-16 08:14:35 +000095 /* Read in filename */
96 xread(archive_handle->src_fd, file_header->name, namesize);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000097 archive_handle->offset += namesize;
98
99 /* Update offset amount and skip padding before file contents */
100 data_align(archive_handle, 4);
101
102 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000103 /* Always round up */
104 printf("%d blocks\n", (int) (archive_handle->offset % 512 ?
105 archive_handle->offset / 512 + 1 :
Denis Vlasenko150f4022007-01-13 21:06:21 +0000106 archive_handle->offset / 512
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000107 ));
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000108 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
109 hardlinks_t *tmp = saved_hardlinks;
110 hardlinks_t *oldtmp = NULL;
111 while (tmp) {
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000112 bb_error_msg("%s not created: cannot resolve hardlink", tmp->name);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000113 oldtmp = tmp;
114 tmp = tmp->next;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000115 free(oldtmp->name);
116 free(oldtmp);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000117 }
118 saved_hardlinks = NULL;
119 pending_hardlinks = 0;
120 }
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000121 return EXIT_FAILURE;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000122 }
123
124 if (S_ISLNK(file_header->mode)) {
Denis Vlasenko75103842007-06-20 14:49:47 +0000125 file_header->link_target = xzalloc(file_header->size + 1);
126 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000127 archive_handle->offset += file_header->size;
128 file_header->size = 0; /* Stop possible seeks in future */
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000129 } else {
Denis Vlasenko75103842007-06-20 14:49:47 +0000130 file_header->link_target = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000131 }
132 if (nlink > 1 && !S_ISDIR(file_header->mode)) {
133 if (file_header->size == 0) { /* Put file on a linked list for later */
134 hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
135 new->next = saved_hardlinks;
136 new->inode = inode;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000137 /* name current allocated, freed later */
138 new->name = file_header->name;
139 file_header->name = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000140 saved_hardlinks = new;
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000141 return EXIT_SUCCESS; /* Skip this one */
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000142 }
Denis Vlasenko3eb91c22006-11-21 00:55:46 +0000143 /* Found the file with data in */
144 pending_hardlinks = nlink;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000145 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000146 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000147
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000148 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000149 archive_handle->action_data(archive_handle);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000150 archive_handle->action_header(archive_handle->file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000151 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000152 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000153 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000154
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000155 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000156
Denis Vlasenko75103842007-06-20 14:49:47 +0000157 free(file_header->link_target);
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000158
Denis Vlasenkod9e15f22006-11-27 16:49:55 +0000159 return EXIT_SUCCESS;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000160}