Dont unlink when testing !
[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         
40         if (pending_hardlinks) { /* Deal with any pending hardlinks */
41                 hardlinks_t *tmp;
42                 hardlinks_t *oldtmp;
43
44                 tmp = saved_hardlinks;
45                 oldtmp = NULL;
46
47                 while (tmp) {
48                         bb_error_msg_and_die("need to fix this\n");
49                         if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
50                                 file_header = tmp->entry;
51                                 if (oldtmp) {
52                                         oldtmp->next = tmp->next; /* Remove item from linked list */
53                                 } else {
54                                         saved_hardlinks = tmp->next;
55                                 }
56                                 free(tmp);
57                                 continue;
58                         }
59                         oldtmp = tmp;
60                         tmp = tmp->next;
61                 }
62                 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
63         }
64
65         /* There can be padding before archive header */
66         data_align(archive_handle, 4);
67
68         if (archive_xread_all_eof(archive_handle, cpio_header, 110) == 0) {
69                 return(EXIT_FAILURE);
70         }
71         archive_handle->offset += 110;
72
73         if ((strncmp(&cpio_header[0], "07070", 5) != 0) || ((cpio_header[5] != '1') && (cpio_header[5] != '2'))) {
74                 bb_error_msg_and_die("Unsupported cpio format, use newc or crc");
75         }
76
77         {
78             unsigned long tmpsize;
79             sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
80                     dummy, &inode, (unsigned int*)&file_header->mode, 
81                     (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
82                     &nlink, &file_header->mtime, &tmpsize,
83                     dummy, &major, &minor, &namesize, dummy);
84             file_header->size = tmpsize;
85         }
86
87         file_header->name = (char *) xmalloc(namesize + 1);
88         archive_xread_all(archive_handle, file_header->name, namesize); /* Read in filename */
89         file_header->name[namesize] = '\0';
90         archive_handle->offset += namesize;
91
92         /* Update offset amount and skip padding before file contents */
93         data_align(archive_handle, 4);
94
95         if (strcmp(file_header->name, "TRAILER!!!") == 0) {
96                 printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
97                 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
98                         hardlinks_t *tmp = saved_hardlinks;
99                         hardlinks_t *oldtmp = NULL;
100                         while (tmp) {
101                                 bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
102                                 oldtmp = tmp;
103                                 tmp = tmp->next;
104                                 free (oldtmp->entry->name);
105                                 free (oldtmp->entry);
106                                 free (oldtmp);
107                         }
108                         saved_hardlinks = NULL;
109                         pending_hardlinks = 0;
110                 }
111                 return(EXIT_FAILURE);
112         }
113
114         if (S_ISLNK(file_header->mode)) {
115                 file_header->link_name = (char *) xmalloc(file_header->size + 1);
116                 archive_xread_all(archive_handle, file_header->link_name, file_header->size);
117                 file_header->link_name[file_header->size] = '\0';
118                 archive_handle->offset += file_header->size;
119                 file_header->size = 0; /* Stop possible seeks in future */
120         }
121         if (nlink > 1 && !S_ISDIR(file_header->mode)) {
122                 if (file_header->size == 0) { /* Put file on a linked list for later */
123                         hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
124                         new->next = saved_hardlinks;
125                         new->inode = inode;
126                         new->entry = file_header;
127                         saved_hardlinks = new;
128                         return(EXIT_SUCCESS); // Skip this one
129                 } else { /* Found the file with data in */
130                         hardlinks_t *tmp = saved_hardlinks;
131                         pending_hardlinks = 1;
132                         while (tmp) {
133                                 if (tmp->inode == inode) {
134                                         tmp->entry->link_name = bb_xstrdup(file_header->name);
135                                         nlink--;
136                                 }
137                                 tmp = tmp->next;
138                         }
139                         if (nlink > 1) {
140                                 bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
141                         }
142                 }
143         }
144         file_header->device = (major << 8) | minor;
145
146         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
147                 archive_handle->action_data(archive_handle);
148                 archive_handle->action_header(archive_handle->file_header);
149         } else {
150                 data_skip(archive_handle);                      
151         }
152
153         archive_handle->offset += file_header->size;
154         return (EXIT_SUCCESS);
155 }