Contents of /trunk/mkinitrd-magellan/busybox/archival/libunarchive/get_header_cpio.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 4452 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 4452 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
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 "libbb.h" |
8 | #include "unarchive.h" |
9 | |
10 | typedef struct hardlinks_s { |
11 | char *name; |
12 | int inode; |
13 | struct hardlinks_s *next; |
14 | } hardlinks_t; |
15 | |
16 | char get_header_cpio(archive_handle_t *archive_handle) |
17 | { |
18 | static hardlinks_t *saved_hardlinks = NULL; |
19 | static unsigned short pending_hardlinks = 0; |
20 | static int inode; |
21 | file_header_t *file_header = archive_handle->file_header; |
22 | char cpio_header[110]; |
23 | int namesize; |
24 | char dummy[16]; |
25 | int major, minor, nlink; |
26 | |
27 | if (pending_hardlinks) { /* Deal with any pending hardlinks */ |
28 | hardlinks_t *tmp, *oldtmp; |
29 | |
30 | tmp = saved_hardlinks; |
31 | oldtmp = NULL; |
32 | |
33 | file_header->link_name = file_header->name; |
34 | file_header->size = 0; |
35 | |
36 | while (tmp) { |
37 | if (tmp->inode != inode) { |
38 | tmp = tmp->next; |
39 | continue; |
40 | } |
41 | |
42 | file_header->name = tmp->name; |
43 | |
44 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
45 | archive_handle->action_data(archive_handle); |
46 | archive_handle->action_header(archive_handle->file_header); |
47 | } |
48 | |
49 | pending_hardlinks--; |
50 | |
51 | oldtmp = tmp; |
52 | tmp = tmp->next; |
53 | free(oldtmp->name); |
54 | free(oldtmp); |
55 | if (oldtmp == saved_hardlinks) |
56 | saved_hardlinks = tmp; |
57 | } |
58 | |
59 | file_header->name = file_header->link_name; |
60 | |
61 | if (pending_hardlinks > 1) { |
62 | bb_error_msg("error resolving hardlink: archive made by GNU cpio 2.0-2.2?"); |
63 | } |
64 | |
65 | /* No more pending hardlinks, read next file entry */ |
66 | pending_hardlinks = 0; |
67 | } |
68 | |
69 | /* There can be padding before archive header */ |
70 | data_align(archive_handle, 4); |
71 | |
72 | if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) { |
73 | return EXIT_FAILURE; |
74 | } |
75 | archive_handle->offset += 110; |
76 | |
77 | if (strncmp(&cpio_header[0], "07070", 5) != 0 |
78 | || (cpio_header[5] != '1' && cpio_header[5] != '2') |
79 | ) { |
80 | bb_error_msg_and_die("unsupported cpio format, use newc or crc"); |
81 | } |
82 | |
83 | { |
84 | unsigned long tmpsize; |
85 | sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c", |
86 | dummy, &inode, (unsigned int*)&file_header->mode, |
87 | (unsigned int*)&file_header->uid, (unsigned int*)&file_header->gid, |
88 | &nlink, &file_header->mtime, &tmpsize, |
89 | dummy, &major, &minor, &namesize, dummy); |
90 | file_header->size = tmpsize; |
91 | } |
92 | |
93 | free(file_header->name); |
94 | file_header->name = xzalloc(namesize + 1); |
95 | /* Read in filename */ |
96 | xread(archive_handle->src_fd, file_header->name, namesize); |
97 | archive_handle->offset += namesize; |
98 | |
99 | /* Update offset amount and skip padding before file contents */ |
100 | data_align(archive_handle, 4); |
101 | |
102 | if (strcmp(file_header->name, "TRAILER!!!") == 0) { |
103 | /* Always round up */ |
104 | printf("%d blocks\n", (int) (archive_handle->offset % 512 ? |
105 | archive_handle->offset / 512 + 1 : |
106 | archive_handle->offset / 512 |
107 | )); |
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 | bb_error_msg("%s not created: cannot resolve hardlink", tmp->name); |
113 | oldtmp = tmp; |
114 | tmp = tmp->next; |
115 | free(oldtmp->name); |
116 | free(oldtmp); |
117 | } |
118 | saved_hardlinks = NULL; |
119 | pending_hardlinks = 0; |
120 | } |
121 | return EXIT_FAILURE; |
122 | } |
123 | |
124 | if (S_ISLNK(file_header->mode)) { |
125 | file_header->link_name = xzalloc(file_header->size + 1); |
126 | xread(archive_handle->src_fd, file_header->link_name, file_header->size); |
127 | archive_handle->offset += file_header->size; |
128 | file_header->size = 0; /* Stop possible seeks in future */ |
129 | } else { |
130 | file_header->link_name = NULL; |
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 | /* name current allocated, freed later */ |
138 | new->name = file_header->name; |
139 | file_header->name = NULL; |
140 | saved_hardlinks = new; |
141 | return EXIT_SUCCESS; /* Skip this one */ |
142 | } |
143 | /* Found the file with data in */ |
144 | pending_hardlinks = nlink; |
145 | } |
146 | file_header->device = makedev(major, minor); |
147 | |
148 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
149 | archive_handle->action_data(archive_handle); |
150 | archive_handle->action_header(archive_handle->file_header); |
151 | } else { |
152 | data_skip(archive_handle); |
153 | } |
154 | |
155 | archive_handle->offset += file_header->size; |
156 | |
157 | free(file_header->link_name); |
158 | |
159 | return EXIT_SUCCESS; |
160 | } |