ea0857840d2b10c110dbc4789a0004ce9149f539
[oweals/busybox.git] / archival / libunarchive / get_header_cpio.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or
5  *  (at your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful,
8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *  GNU Library General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include "unarchive.h"
22 #include "libbb.h"
23
24 typedef struct hardlinks_s {
25         file_header_t *entry;
26         int inode;
27         struct hardlinks_s *next;
28 } hardlinks_t;
29
30 extern char get_header_cpio(archive_handle_t *archive_handle)
31 {
32         static hardlinks_t *saved_hardlinks = NULL;
33         static unsigned short pending_hardlinks = 0;
34         file_header_t *file_header = archive_handle->file_header;
35         char cpio_header[110];
36         int namesize;
37         char dummy[16];
38         int major, minor, nlink, inode;
39         char extract_flag;
40         
41         if (pending_hardlinks) { /* Deal with any pending hardlinks */
42                 hardlinks_t *tmp;
43                 hardlinks_t *oldtmp;
44
45                 tmp = saved_hardlinks;
46                 oldtmp = NULL;
47
48                 while (tmp) {
49                         error_msg_and_die("need to fix this\n");
50                         if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
51                                 file_header = tmp->entry;
52                                 if (oldtmp) {
53                                         oldtmp->next = tmp->next; /* Remove item from linked list */
54                                 } else {
55                                         saved_hardlinks = tmp->next;
56                                 }
57                                 free(tmp);
58                                 continue;
59                         }
60                         oldtmp = tmp;
61                         tmp = tmp->next;
62                 }
63                 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
64         }
65
66         /* There can be padding before archive header */
67         data_align(archive_handle, 4);
68
69         if (archive_xread_all_eof(archive_handle, cpio_header, 110) == 0) {
70                 return(EXIT_FAILURE);
71         }
72         archive_handle->offset += 110;
73
74         if (strncmp(&cpio_header[0], "07070", 5) != 0) {
75                 printf("cpio header is %x-%x-%x-%x-%x\n",
76                         cpio_header[0],
77                         cpio_header[1],
78                         cpio_header[2],
79                         cpio_header[3],
80                         cpio_header[4]);
81                 error_msg_and_die("Unsupported cpio format");
82         }
83                 
84         if ((cpio_header[5] != '1') && (cpio_header[5] != '2')) {
85                 error_msg_and_die("Unsupported cpio format, use newc or crc");
86         }
87
88         {
89             unsigned long tmpsize;
90             sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
91                     dummy, &inode, (unsigned int*)&file_header->mode, 
92                     (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
93                     &nlink, &file_header->mtime, &tmpsize,
94                     dummy, &major, &minor, &namesize, dummy);
95             file_header->size = tmpsize;
96         }
97
98         file_header->name = (char *) xmalloc(namesize + 1);
99         archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */
100         file_header->name[namesize] = '\0';
101         archive_handle->offset += namesize;
102
103         /* Update offset amount and skip padding before file contents */
104         data_align(archive_handle, 4);
105
106         if (strcmp(file_header->name, "TRAILER!!!") == 0) {
107                 printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
108                 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
109                         hardlinks_t *tmp = saved_hardlinks;
110                         hardlinks_t *oldtmp = NULL;
111                         while (tmp) {
112                                 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
113                                 oldtmp = tmp;
114                                 tmp = tmp->next;
115                                 free (oldtmp->entry->name);
116                                 free (oldtmp->entry);
117                                 free (oldtmp);
118                         }
119                         saved_hardlinks = NULL;
120                         pending_hardlinks = 0;
121                 }
122                 return(EXIT_FAILURE);
123         }
124
125         if (S_ISLNK(file_header->mode)) {
126                 file_header->link_name = (char *) xmalloc(file_header->size + 1);
127                 archive_xread_all(archive_handle, file_header->link_name, file_header->size);
128                 file_header->link_name[file_header->size] = '\0';
129                 archive_handle->offset += file_header->size;
130                 file_header->size = 0; /* Stop possible seeks in future */
131         }
132         if (nlink > 1 && !S_ISDIR(file_header->mode)) {
133                 if (file_header->size == 0) { /* Put file on a linked list for later */
134                         hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
135                         new->next = saved_hardlinks;
136                         new->inode = inode;
137                         new->entry = file_header;
138                         saved_hardlinks = new;
139                         return(EXIT_SUCCESS); // Skip this one
140                 } else { /* Found the file with data in */
141                         hardlinks_t *tmp = saved_hardlinks;
142                         pending_hardlinks = 1;
143                         while (tmp) {
144                                 if (tmp->inode == inode) {
145                                         tmp->entry->link_name = xstrdup(file_header->name);
146                                         nlink--;
147                                 }
148                                 tmp = tmp->next;
149                         }
150                         if (nlink > 1) {
151                                 error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
152                         }
153                 }
154         }
155         file_header->device = (major << 8) | minor;
156
157         extract_flag = FALSE;
158         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
159                 struct stat statbuf;
160
161                 extract_flag = TRUE;
162
163                 /* Check if the file already exists */
164                 if (lstat (file_header->name, &statbuf) == 0) {
165                         if ((archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) || (statbuf.st_mtime < file_header->mtime)) {
166                                 /* Remove file if flag set or its older than the file to be extracted */
167                                 if (unlink(file_header->name) == -1) {
168                                         perror_msg_and_die("Couldnt remove old file");
169                                 }
170                         } else {
171                                 if (! archive_handle->flags & ARCHIVE_EXTRACT_QUIET) {
172                                         error_msg("%s not created: newer or same age file exists", file_header->name);
173                                 }
174                                 extract_flag = FALSE;
175                         }
176                 }
177                 archive_handle->action_header(file_header);
178         }
179
180         archive_handle->action_header(file_header);
181         if (extract_flag) {
182                 archive_handle->action_data(archive_handle);
183         } else {
184                 data_skip(archive_handle);
185         }
186         archive_handle->offset += file_header->size;
187         return (EXIT_SUCCESS);
188 }
189