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