1
2
3
4
5
6
7#include "libbb.h"
8#include "unarchive.h"
9
10typedef struct hardlinks_t {
11 struct hardlinks_t *next;
12 int inode;
13 int mode ;
14 int mtime;
15 int uid ;
16 int gid ;
17 char name[1];
18} hardlinks_t;
19
20char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
21{
22 file_header_t *file_header = archive_handle->file_header;
23 char cpio_header[110];
24 int namesize;
25 int major, minor, nlink, mode, inode;
26 unsigned size, uid, gid, mtime;
27
28
29 data_align(archive_handle, 4);
30
31 size = full_read(archive_handle->src_fd, cpio_header, 110);
32 if (size == 0) {
33 goto create_hardlinks;
34 }
35 if (size != 110) {
36 bb_error_msg_and_die("short read");
37 }
38 archive_handle->offset += 110;
39
40 if (strncmp(&cpio_header[0], "07070", 5) != 0
41 || (cpio_header[5] != '1' && cpio_header[5] != '2')
42 ) {
43 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
44 }
45
46 if (sscanf(cpio_header + 6,
47 "%8x" "%8x" "%8x" "%8x"
48 "%8x" "%8x" "%8x" "%*16c"
49 "%8x" "%8x" "%8x" ,
50 &inode, &mode, &uid, &gid,
51 &nlink, &mtime, &size,
52 &major, &minor, &namesize) != 10)
53 bb_error_msg_and_die("damaged cpio file");
54 file_header->mode = mode;
55 file_header->uid = uid;
56 file_header->gid = gid;
57 file_header->mtime = mtime;
58 file_header->size = size;
59
60 namesize &= 0x1fff;
61 file_header->name = xzalloc(namesize + 1);
62
63 xread(archive_handle->src_fd, file_header->name, namesize);
64 if (file_header->name[0] == '/') {
65
66
67
68
69 char *p = file_header->name;
70 do p++; while (*p == '/');
71 overlapping_strcpy(file_header->name, p);
72 }
73 archive_handle->offset += namesize;
74
75
76 data_align(archive_handle, 4);
77
78 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
79
80 archive_handle->cpio__blocks = (uoff_t)(archive_handle->offset + 511) >> 9;
81 goto create_hardlinks;
82 }
83
84 file_header->link_target = NULL;
85 if (S_ISLNK(file_header->mode)) {
86 file_header->size &= 0x1fff;
87 file_header->link_target = xzalloc(file_header->size + 1);
88 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
89 archive_handle->offset += file_header->size;
90 file_header->size = 0;
91 }
92
93
94
95
96 if (nlink > 1 && S_ISREG(file_header->mode)) {
97 hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
98 new->inode = inode;
99 new->mode = mode ;
100 new->mtime = mtime;
101 new->uid = uid ;
102 new->gid = gid ;
103 strcpy(new->name, file_header->name);
104
105 if (size == 0) {
106 new->next = archive_handle->cpio__hardlinks_to_create;
107 archive_handle->cpio__hardlinks_to_create = new;
108 return EXIT_SUCCESS;
109
110 }
111 new->next = archive_handle->cpio__created_hardlinks;
112 archive_handle->cpio__created_hardlinks = new;
113 }
114 file_header->device = makedev(major, minor);
115
116 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
117 archive_handle->action_data(archive_handle);
118
119
120
121
122 archive_handle->action_header(file_header);
123 } else {
124 data_skip(archive_handle);
125 }
126
127 archive_handle->offset += file_header->size;
128
129 free(file_header->link_target);
130 free(file_header->name);
131 file_header->link_target = NULL;
132 file_header->name = NULL;
133
134 return EXIT_SUCCESS;
135
136 create_hardlinks:
137 free(file_header->link_target);
138 free(file_header->name);
139
140 while (archive_handle->cpio__hardlinks_to_create) {
141 hardlinks_t *cur;
142 hardlinks_t *make_me = archive_handle->cpio__hardlinks_to_create;
143
144 archive_handle->cpio__hardlinks_to_create = make_me->next;
145
146 memset(file_header, 0, sizeof(*file_header));
147 file_header->mtime = make_me->mtime;
148 file_header->name = make_me->name;
149 file_header->mode = make_me->mode;
150 file_header->uid = make_me->uid;
151 file_header->gid = make_me->gid;
152
153
154
155
156 cur = archive_handle->cpio__created_hardlinks;
157 while (cur) {
158
159 if (cur->inode == make_me->inode) {
160 file_header->link_target = cur->name;
161
162 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
163 archive_handle->action_data(archive_handle);
164 free(make_me);
165 goto next_link;
166 }
167 cur = cur->next;
168 }
169
170
171 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
172 archive_handle->action_data(archive_handle);
173
174 make_me->next = archive_handle->cpio__created_hardlinks;
175 archive_handle->cpio__created_hardlinks = make_me;
176 next_link: ;
177 }
178
179 while (archive_handle->cpio__created_hardlinks) {
180 hardlinks_t *p = archive_handle->cpio__created_hardlinks;
181 archive_handle->cpio__created_hardlinks = p->next;
182 free(p);
183 }
184
185 return EXIT_FAILURE;
186}
187