1 /* vi: set sw=4 ts=4: */
3 * Mini cpio implementation for busybox
5 * Copyright (C) 2001 by Glenn McGrath
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Only supports new ASCII and CRC formats
31 #include "unarchive.h"
34 typedef struct hardlinks_s {
37 struct hardlinks_s *next;
40 extern int cpio_main(int argc, char **argv)
42 archive_handle_t *archive_handle;
46 archive_handle = init_handle();
47 archive_handle->src_fd = fileno(stdin);
48 archive_handle->action_header = header_list;
50 while ((opt = getopt(argc, argv, "idmuvtF:")) != -1) {
52 case 'i': /* extract */
53 archive_handle->action_data = data_extract_all;
55 case 'd': /* create _leading_ directories */
56 archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
58 case 'm': /* preserve modification time */
59 archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
61 case 'v': /* verbosly list files */
62 archive_handle->action_header = header_verbose_list;
64 case 'u': /* unconditional */
65 archive_handle->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
67 case 't': /* list files */
68 archive_handle->action_header = header_list;
71 archive_handle->src_fd = xopen(optarg, O_RDONLY);
78 while (optind < argc) {
79 archive_handle->filter = filter_accept_list;
80 archive_handle->accept = add_to_list(archive_handle->accept, argv[optind]);
85 static hardlinks_t *saved_hardlinks = NULL;
86 static unsigned short pending_hardlinks = 0;
87 file_header_t *file_header = archive_handle->file_header;
88 char cpio_header[110];
91 int major, minor, nlink, inode;
94 if (pending_hardlinks) { /* Deal with any pending hardlinks */
98 tmp = saved_hardlinks;
102 error_msg_and_die("need to fix this\n");
103 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
104 file_header = tmp->entry;
106 oldtmp->next = tmp->next; /* Remove item from linked list */
108 saved_hardlinks = tmp->next;
116 pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
119 /* There can be padding before archive header */
120 archive_handle->offset += data_align(archive_handle->src_fd, archive_handle->offset, 4);
122 if (xread_all_eof(archive_handle->src_fd, cpio_header, 110) == 0) {
123 return(EXIT_FAILURE);
125 archive_handle->offset += 110;
127 if (strncmp(&cpio_header[0], "07070", 5) != 0) {
128 printf("cpio header is %x-%x-%x-%x-%x\n",
134 error_msg_and_die("Unsupported cpio format");
137 if ((cpio_header[5] != '1') && (cpio_header[5] != '2')) {
138 error_msg_and_die("Unsupported cpio format, use newc or crc");
141 sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
142 dummy, &inode, (unsigned int*)&file_header->mode,
143 (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid,
144 &nlink, &file_header->mtime, &file_header->size,
145 dummy, &major, &minor, &namesize, dummy);
147 file_header->name = (char *) xmalloc(namesize + 1);
148 xread(archive_handle->src_fd, file_header->name, namesize); /* Read in filename */
149 file_header->name[namesize] = '\0';
150 archive_handle->offset += namesize;
152 /* Update offset amount and skip padding before file contents */
153 archive_handle->offset += data_align(archive_handle->src_fd, archive_handle->offset, 4);
155 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
156 printf("%d blocks\n", (int) (archive_handle->offset % 512 ? (archive_handle->offset / 512) + 1 : archive_handle->offset / 512)); /* Always round up */
157 if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
158 hardlinks_t *tmp = saved_hardlinks;
159 hardlinks_t *oldtmp = NULL;
161 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
164 free (oldtmp->entry->name);
165 free (oldtmp->entry);
168 saved_hardlinks = NULL;
169 pending_hardlinks = 0;
171 return(EXIT_FAILURE);
174 if (S_ISLNK(file_header->mode)) {
175 file_header->link_name = (char *) xmalloc(file_header->size + 1);
176 xread(archive_handle->src_fd, file_header->link_name, file_header->size);
177 file_header->link_name[file_header->size] = '\0';
178 archive_handle->offset += file_header->size;
179 file_header->size = 0; /* Stop possible seeks in future */
181 if (nlink > 1 && !S_ISDIR(file_header->mode)) {
182 if (file_header->size == 0) { /* Put file on a linked list for later */
183 hardlinks_t *new = xmalloc(sizeof(hardlinks_t));
184 new->next = saved_hardlinks;
186 new->entry = file_header;
187 saved_hardlinks = new;
189 } else { /* Found the file with data in */
190 hardlinks_t *tmp = saved_hardlinks;
191 pending_hardlinks = 1;
193 if (tmp->inode == inode) {
194 tmp->entry->link_name = xstrdup(file_header->name);
200 error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
204 file_header->device = (major << 8) | minor;
206 extract_flag = FALSE;
207 if (archive_handle->filter(archive_handle->accept, archive_handle->reject, file_header->name) == EXIT_SUCCESS) {
212 /* Check if the file already exists */
213 if (lstat (file_header->name, &statbuf) == 0) {
214 if ((archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) || (statbuf.st_mtime < file_header->mtime)) {
215 /* Remove file if flag set or its older than the file to be extracted */
216 if (unlink(file_header->name) == -1) {
217 perror_msg_and_die("Couldnt remove old file");
220 if (! archive_handle->flags & ARCHIVE_EXTRACT_QUIET) {
221 error_msg("%s not created: newer or same age file exists", file_header->name);
223 extract_flag = FALSE;
226 archive_handle->action_header(file_header);
229 archive_handle->action_header(file_header);
231 archive_handle->action_data(archive_handle);
233 data_skip(archive_handle);
235 archive_handle->offset += file_header->size;
238 return(EXIT_SUCCESS);