1 /* vi: set sw=4 ts=4: */
2 /* Copyright 2002 Laurence Anderson
4 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include <sys/sysmacros.h> /* major() and minor() */
12 #include "unarchive.h"
15 typedef struct hardlinks_s {
18 struct hardlinks_s *next;
21 char get_header_cpio(archive_handle_t *archive_handle)
23 static hardlinks_t *saved_hardlinks = NULL;
24 static unsigned short pending_hardlinks = 0;
25 file_header_t *file_header = archive_handle->file_header;
26 char cpio_header[110];
29 int major, minor, nlink, inode;
31 if (pending_hardlinks) { /* Deal with any pending hardlinks */
35 tmp = saved_hardlinks;
39 bb_error_msg_and_die("need to fix this\n");
40 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
41 file_header = tmp->entry;
43 oldtmp->next = tmp->next; /* Remove item from linked list */
45 saved_hardlinks = tmp->next;
53 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
56 /* There can be padding before archive header */
57 data_align(archive_handle, 4);
59 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
62 archive_handle->offset += 110;
64 if ((strncmp(&cpio_header[0], "07070", 5) != 0) || ((cpio_header[5] != '1') && (cpio_header[5] != '2'))) {
65 bb_error_msg_and_die("Unsupported cpio format, use newc or crc");
69 unsigned long tmpsize;
70 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
71 dummy, &inode, (unsigned int*)&file_header->mode,
72 (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
73 &nlink, &file_header->mtime, &tmpsize,
74 dummy, &major, &minor, &namesize, dummy);
75 file_header->size = tmpsize;
78 file_header->name = (char *) xzalloc(namesize + 1);
79 archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */
80 archive_handle->offset += namesize;
82 /* Update offset amount and skip padding before file contents */
83 data_align(archive_handle, 4);
85 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
86 printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
87 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
88 hardlinks_t *tmp = saved_hardlinks;
89 hardlinks_t *oldtmp = NULL;
91 bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
94 free (oldtmp->entry->name);
98 saved_hardlinks = NULL;
99 pending_hardlinks = 0;
101 return(EXIT_FAILURE);
104 if (S_ISLNK(file_header->mode)) {
105 file_header->link_name = (char *) xzalloc(file_header->size + 1);
106 archive_xread_all(archive_handle, file_header->link_name, file_header->size);
107 archive_handle->offset += file_header->size;
108 file_header->size = 0; /* Stop possible seeks in future */
110 file_header->link_name = NULL;
112 if (nlink > 1 && !S_ISDIR(file_header->mode)) {
113 if (file_header->size == 0) { /* Put file on a linked list for later */
114 hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
115 new->next = saved_hardlinks;
117 new->entry = file_header;
118 saved_hardlinks = new;
119 return(EXIT_SUCCESS); // Skip this one
120 } else { /* Found the file with data in */
121 hardlinks_t *tmp = saved_hardlinks;
122 pending_hardlinks = 1;
124 if (tmp->inode == inode) {
125 tmp->entry->link_name = bb_xstrdup(file_header->name);
131 bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
135 file_header->device = makedev(major, minor);
137 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
138 archive_handle->action_data(archive_handle);
139 archive_handle->action_header(archive_handle->file_header);
141 data_skip(archive_handle);
144 archive_handle->offset += file_header->size;
146 free(file_header->link_name);
148 return (EXIT_SUCCESS);