blob: f54b5af69b9dc3f726b213452748b54500e8b70c [file] [log] [blame]
Rob Landley1ec5b292006-05-29 07:42:02 +00001/* Copyright 2002 Laurence Anderson
Glenn L McGrathb72a7352002-12-10 00:17:22 +00002 *
Rob Landley1ec5b292006-05-29 07:42:02 +00003 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrathb72a7352002-12-10 00:17:22 +00004 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
Eric Andersen4f807a82004-07-26 09:11:12 +000010#include <sys/sysmacros.h> /* major() and minor() */
Glenn L McGrathb72a7352002-12-10 00:17:22 +000011#include "unarchive.h"
12#include "libbb.h"
13
14typedef struct hardlinks_s {
15 file_header_t *entry;
16 int inode;
17 struct hardlinks_s *next;
18} hardlinks_t;
19
Rob Landleydfba7412006-03-06 20:47:33 +000020char get_header_cpio(archive_handle_t *archive_handle)
Glenn L McGrathb72a7352002-12-10 00:17:22 +000021{
22 static hardlinks_t *saved_hardlinks = NULL;
23 static unsigned short pending_hardlinks = 0;
24 file_header_t *file_header = archive_handle->file_header;
25 char cpio_header[110];
26 int namesize;
27 char dummy[16];
28 int major, minor, nlink, inode;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029
Glenn L McGrathb72a7352002-12-10 00:17:22 +000030 if (pending_hardlinks) { /* Deal with any pending hardlinks */
31 hardlinks_t *tmp;
32 hardlinks_t *oldtmp;
33
34 tmp = saved_hardlinks;
35 oldtmp = NULL;
36
37 while (tmp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 bb_error_msg_and_die("need to fix this\n");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000039 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
40 file_header = tmp->entry;
41 if (oldtmp) {
42 oldtmp->next = tmp->next; /* Remove item from linked list */
43 } else {
44 saved_hardlinks = tmp->next;
45 }
46 free(tmp);
47 continue;
48 }
49 oldtmp = tmp;
50 tmp = tmp->next;
51 }
52 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
53 }
54
55 /* There can be padding before archive header */
56 data_align(archive_handle, 4);
57
Eric Andersend78aea82006-01-30 18:00:02 +000058 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +000059 return(EXIT_FAILURE);
60 }
61 archive_handle->offset += 110;
62
Glenn L McGrath4cee66d2003-08-28 19:12:23 +000063 if ((strncmp(&cpio_header[0], "07070", 5) != 0) || ((cpio_header[5] != '1') && (cpio_header[5] != '2'))) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 bb_error_msg_and_die("Unsupported cpio format, use newc or crc");
Glenn L McGrathb72a7352002-12-10 00:17:22 +000065 }
66
Eric Andersenf4b273c2002-12-11 21:45:08 +000067 {
68 unsigned long tmpsize;
69 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
Eric Andersenc7bda1c2004-03-15 08:29:22 +000070 dummy, &inode, (unsigned int*)&file_header->mode,
Eric Andersenf4b273c2002-12-11 21:45:08 +000071 (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
72 &nlink, &file_header->mtime, &tmpsize,
73 dummy, &major, &minor, &namesize, dummy);
74 file_header->size = tmpsize;
75 }
Glenn L McGrathb72a7352002-12-10 00:17:22 +000076
Rob Landley1ec5b292006-05-29 07:42:02 +000077 file_header->name = (char *) xzalloc(namesize + 1);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000078 archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */
Glenn L McGrathb72a7352002-12-10 00:17:22 +000079 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, "TRAILER!!!") == 0) {
85 printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
86 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
87 hardlinks_t *tmp = saved_hardlinks;
88 hardlinks_t *oldtmp = NULL;
89 while (tmp) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
Glenn L McGrathb72a7352002-12-10 00:17:22 +000091 oldtmp = tmp;
92 tmp = tmp->next;
93 free (oldtmp->entry->name);
94 free (oldtmp->entry);
95 free (oldtmp);
96 }
97 saved_hardlinks = NULL;
98 pending_hardlinks = 0;
99 }
100 return(EXIT_FAILURE);
101 }
102
103 if (S_ISLNK(file_header->mode)) {
Rob Landley1ec5b292006-05-29 07:42:02 +0000104 file_header->link_name = (char *) xzalloc(file_header->size + 1);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000105 archive_xread_all(archive_handle, file_header->link_name, file_header->size);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000106 archive_handle->offset += file_header->size;
107 file_header->size = 0; /* Stop possible seeks in future */
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000108 } else {
109 file_header->link_name = NULL;
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000110 }
111 if (nlink > 1 && !S_ISDIR(file_header->mode)) {
112 if (file_header->size == 0) { /* Put file on a linked list for later */
113 hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
114 new->next = saved_hardlinks;
115 new->inode = inode;
116 new->entry = file_header;
117 saved_hardlinks = new;
118 return(EXIT_SUCCESS); // Skip this one
119 } else { /* Found the file with data in */
120 hardlinks_t *tmp = saved_hardlinks;
121 pending_hardlinks = 1;
122 while (tmp) {
123 if (tmp->inode == inode) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 tmp->entry->link_name = bb_xstrdup(file_header->name);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000125 nlink--;
126 }
127 tmp = tmp->next;
128 }
129 if (nlink > 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 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 +0000131 }
132 }
133 }
Eric Andersen4f807a82004-07-26 09:11:12 +0000134 file_header->device = makedev(major, minor);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000135
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000136 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000137 archive_handle->action_data(archive_handle);
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000138 archive_handle->action_header(archive_handle->file_header);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000139 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000140 data_skip(archive_handle);
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000141 }
Glenn L McGrath4cee66d2003-08-28 19:12:23 +0000142
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000143 archive_handle->offset += file_header->size;
Glenn L McGrathfaa35462004-04-29 09:24:19 +0000144
145 free(file_header->link_name);
146
Glenn L McGrathb72a7352002-12-10 00:17:22 +0000147 return (EXIT_SUCCESS);
148}