1 /* Copyright 2002 Laurence Anderson
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 #include <sys/sysmacros.h> /* major() and minor() */
11 #include "unarchive.h"
14 typedef struct hardlinks_s {
17 struct hardlinks_s *next;
20 char get_header_cpio(archive_handle_t *archive_handle)
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];
28 int major, minor, nlink, inode;
30 if (pending_hardlinks) { /* Deal with any pending hardlinks */
34 tmp = saved_hardlinks;
38 bb_error_msg_and_die("need to fix this\n");
39 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
40 file_header = tmp->entry;
42 oldtmp->next = tmp->next; /* Remove item from linked list */
44 saved_hardlinks = tmp->next;
52 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
55 /* There can be padding before archive header */
56 data_align(archive_handle, 4);
58 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
61 archive_handle->offset += 110;
63 if ((strncmp(&cpio_header[0], "07070", 5) != 0) || ((cpio_header[5] != '1') && (cpio_header[5] != '2'))) {
64 bb_error_msg_and_die("Unsupported cpio format, use newc or crc");
68 unsigned long tmpsize;
69 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
70 dummy, &inode, (unsigned int*)&file_header->mode,
71 (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;
77 file_header->name = (char *) xzalloc(namesize + 1);
78 archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */
79 archive_handle->offset += namesize;
81 /* Update offset amount and skip padding before file contents */
82 data_align(archive_handle, 4);
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;
90 bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
93 free (oldtmp->entry->name);
97 saved_hardlinks = NULL;
98 pending_hardlinks = 0;
100 return(EXIT_FAILURE);
103 if (S_ISLNK(file_header->mode)) {
104 file_header->link_name = (char *) xzalloc(file_header->size + 1);
105 archive_xread_all(archive_handle, file_header->link_name, file_header->size);
106 archive_handle->offset += file_header->size;
107 file_header->size = 0; /* Stop possible seeks in future */
109 file_header->link_name = NULL;
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;
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;
123 if (tmp->inode == inode) {
124 tmp->entry->link_name = bb_xstrdup(file_header->name);
130 bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
134 file_header->device = makedev(major, minor);
136 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
137 archive_handle->action_data(archive_handle);
138 archive_handle->action_header(archive_handle->file_header);
140 data_skip(archive_handle);
143 archive_handle->offset += file_header->size;
145 free(file_header->link_name);
147 return (EXIT_SUCCESS);