blob: dd48cb09bb0a6fa13aaaf6795a3c98849b7bb2a7 [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 {
11 file_header_t *entry;
12 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;
20 file_header_t *file_header = archive_handle->file_header;
21 char cpio_header[110];
22 int namesize;
23 char dummy[16];
24 int major, minor, nlink, inode;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000025
Glenn L McGrathb72a7352002-12-10 00:17:22 +000026 if (pending_hardlinks) { /* Deal with any pending hardlinks */
27 hardlinks_t *tmp;
28 hardlinks_t *oldtmp;
29
30 tmp = saved_hardlinks;
31 oldtmp = NULL;
32
33 while (tmp) {
Denis Vlasenko6d655be2006-09-06 19:02:46 +000034 bb_error_msg_and_die("need to fix this");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000035 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
36 file_header = tmp->entry;
37 if (oldtmp) {
38 oldtmp->next = tmp->next; /* Remove item from linked list */
39 } else {
40 saved_hardlinks = tmp->next;
41 }
42 free(tmp);
43 continue;
44 }
45 oldtmp = tmp;
46 tmp = tmp->next;
47 }
48 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
49 }
50
51 /* There can be padding before archive header */
52 data_align(archive_handle, 4);
53
Eric Andersend78aea82006-01-30 18:00:02 +000054 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +000055 return(EXIT_FAILURE);
56 }
57 archive_handle->offset += 110;
58
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000059 if ((strncmp(&cpio_header[0], "07070", 5) != 0) || ((cpio_header[5] != '1') && (cpio_header[5] != '2'))) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000060 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000061 }
62
Eric Andersenf4b273c2002-12-11 21:45:08 +000063 {
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000064 unsigned long tmpsize;
65 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
Eric Andersenc7bda1c2004-03-15 08:29:22 +000066 dummy, &inode, (unsigned int*)&file_header->mode,
Eric Andersenf4b273c2002-12-11 21:45:08 +000067 (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
68 &nlink, &file_header->mtime, &tmpsize,
69 dummy, &major, &minor, &namesize, dummy);
Denis Vlasenkocba9ef52006-10-10 21:00:47 +000070 file_header->size = tmpsize;
Eric Andersenf4b273c2002-12-11 21:45:08 +000071 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +000072
Rob Landley1ec5b292006-05-29 07:42:02 +000073 file_header->name = (char *) xzalloc(namesize + 1);
Rob Landley53437472006-07-16 08:14:35 +000074 /* Read in filename */
75 xread(archive_handle->src_fd, file_header->name, namesize);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000076 archive_handle->offset += namesize;
77
78 /* Update offset amount and skip padding before file contents */
79 data_align(archive_handle, 4);
80
81 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
82 printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
83 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
84 hardlinks_t *tmp = saved_hardlinks;
85 hardlinks_t *oldtmp = NULL;
86 while (tmp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000088 oldtmp = tmp;
89 tmp = tmp->next;
90 free (oldtmp->entry->name);
91 free (oldtmp->entry);
92 free (oldtmp);
93 }
94 saved_hardlinks = NULL;
95 pending_hardlinks = 0;
96 }
97 return(EXIT_FAILURE);
98 }
99
100 if (S_ISLNK(file_header->mode)) {
Rob Landley1ec5b292006-05-29 07:42:02 +0000101 file_header->link_name = (char *) xzalloc(file_header->size + 1);
Rob Landley53437472006-07-16 08:14:35 +0000102 xread(archive_handle->src_fd, file_header->link_name, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000103 archive_handle->offset += file_header->size;
104 file_header->size = 0; /* Stop possible seeks in future */
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000105 } else {
106 file_header->link_name = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000107 }
108 if (nlink > 1 && !S_ISDIR(file_header->mode)) {
109 if (file_header->size == 0) { /* Put file on a linked list for later */
110 hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
111 new->next = saved_hardlinks;
112 new->inode = inode;
113 new->entry = file_header;
114 saved_hardlinks = new;
115 return(EXIT_SUCCESS); // Skip this one
116 } else { /* Found the file with data in */
117 hardlinks_t *tmp = saved_hardlinks;
118 pending_hardlinks = 1;
119 while (tmp) {
120 if (tmp->inode == inode) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000121 tmp->entry->link_name = xstrdup(file_header->name);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000122 nlink--;
123 }
124 tmp = tmp->next;
125 }
126 if (nlink > 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000128 }
129 }
130 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000131 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000132
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000133 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000134 archive_handle->action_data(archive_handle);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000135 archive_handle->action_header(archive_handle->file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000136 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000137 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000138 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000139
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000140 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000141
142 free(file_header->link_name);
143
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000144 return (EXIT_SUCCESS);
145}