28c743589878760abebb6cc97c4b045b62364dcb
[oweals/busybox.git] / archival / libunarchive / get_header_cpio.c
1 /* vi: set sw=4 ts=4: */
2 /* Copyright 2002 Laurence Anderson
3  *
4  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/sysmacros.h>     /* major() and minor() */
12 #include "unarchive.h"
13 #include "libbb.h"
14
15 typedef struct hardlinks_s {
16         file_header_t *entry;
17         int inode;
18         struct hardlinks_s *next;
19 } hardlinks_t;
20
21 char get_header_cpio(archive_handle_t *archive_handle)
22 {
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];
27         int namesize;
28         char dummy[16];
29         int major, minor, nlink, inode;
30
31         if (pending_hardlinks) { /* Deal with any pending hardlinks */
32                 hardlinks_t *tmp;
33                 hardlinks_t *oldtmp;
34
35                 tmp = saved_hardlinks;
36                 oldtmp = NULL;
37
38                 while (tmp) {
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;
42                                 if (oldtmp) {
43                                         oldtmp->next = tmp->next; /* Remove item from linked list */
44                                 } else {
45                                         saved_hardlinks = tmp->next;
46                                 }
47                                 free(tmp);
48                                 continue;
49                         }
50                         oldtmp = tmp;
51                         tmp = tmp->next;
52                 }
53                 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
54         }
55
56         /* There can be padding before archive header */
57         data_align(archive_handle, 4);
58
59         if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
60                 return(EXIT_FAILURE);
61         }
62         archive_handle->offset += 110;
63
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");
66         }
67
68         {
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;
76         }
77
78         file_header->name = (char *) xzalloc(namesize + 1);
79         /* Read in filename */
80         xread(archive_handle->src_fd, file_header->name, namesize);
81         archive_handle->offset += namesize;
82
83         /* Update offset amount and skip padding before file contents */
84         data_align(archive_handle, 4);
85
86         if (strcmp(file_header->name, "TRAILER!!!") == 0) {
87                 printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
88                 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
89                         hardlinks_t *tmp = saved_hardlinks;
90                         hardlinks_t *oldtmp = NULL;
91                         while (tmp) {
92                                 bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
93                                 oldtmp = tmp;
94                                 tmp = tmp->next;
95                                 free (oldtmp->entry->name);
96                                 free (oldtmp->entry);
97                                 free (oldtmp);
98                         }
99                         saved_hardlinks = NULL;
100                         pending_hardlinks = 0;
101                 }
102                 return(EXIT_FAILURE);
103         }
104
105         if (S_ISLNK(file_header->mode)) {
106                 file_header->link_name = (char *) xzalloc(file_header->size + 1);
107                 xread(archive_handle->src_fd, file_header->link_name, file_header->size);
108                 archive_handle->offset += file_header->size;
109                 file_header->size = 0; /* Stop possible seeks in future */
110         } else {
111                 file_header->link_name = NULL;
112         }
113         if (nlink > 1 && !S_ISDIR(file_header->mode)) {
114                 if (file_header->size == 0) { /* Put file on a linked list for later */
115                         hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
116                         new->next = saved_hardlinks;
117                         new->inode = inode;
118                         new->entry = file_header;
119                         saved_hardlinks = new;
120                         return(EXIT_SUCCESS); // Skip this one
121                 } else { /* Found the file with data in */
122                         hardlinks_t *tmp = saved_hardlinks;
123                         pending_hardlinks = 1;
124                         while (tmp) {
125                                 if (tmp->inode == inode) {
126                                         tmp->entry->link_name = bb_xstrdup(file_header->name);
127                                         nlink--;
128                                 }
129                                 tmp = tmp->next;
130                         }
131                         if (nlink > 1) {
132                                 bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
133                         }
134                 }
135         }
136         file_header->device = makedev(major, minor);
137
138         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
139                 archive_handle->action_data(archive_handle);
140                 archive_handle->action_header(archive_handle->file_header);
141         } else {
142                 data_skip(archive_handle);
143         }
144
145         archive_handle->offset += file_header->size;
146
147         free(file_header->link_name);
148
149         return (EXIT_SUCCESS);
150 }