1 /* vi: set sw=4 ts=4: */
2 /* Copyright 2002 Laurence Anderson
4 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 typedef struct hardlinks_s {
11 struct hardlinks_s *next;
12 int inode; /* TODO: must match maj/min too! */
14 int mtime; /* These three are useful only in corner case */
15 int uid ; /* of hardlinks with zero size body */
20 char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
22 file_header_t *file_header = archive_handle->file_header;
23 char cpio_header[110];
26 int major, minor, nlink, mode, inode;
27 unsigned size, uid, gid, mtime;
29 #define saved_hardlinks (*(hardlinks_t **)(&archive_handle->ah_priv[0]))
30 #define saved_hardlinks_created (*(hardlinks_t **)(&archive_handle->ah_priv[1]))
31 // if (!archive_handle->ah_priv_inited) {
32 // archive_handle->ah_priv_inited = 1;
33 // saved_hardlinks = NULL;
34 // saved_hardlinks_created = NULL;
37 /* There can be padding before archive header */
38 data_align(archive_handle, 4);
40 if (archive_xread_all_eof(archive_handle, (unsigned char*)cpio_header, 110) == 0) {
41 goto create_hardlinks;
43 archive_handle->offset += 110;
45 if (strncmp(&cpio_header[0], "07070", 5) != 0
46 || (cpio_header[5] != '1' && cpio_header[5] != '2')
48 bb_error_msg_and_die("unsupported cpio format, use newc or crc");
51 sscanf(cpio_header + 6,
52 "%8x" "%8x" "%8x" "%8x"
53 "%8x" "%8x" "%8x" /*maj,min:*/ "%16c"
54 /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum:*/ "%8c",
55 &inode, &mode, &uid, &gid,
56 &nlink, &mtime, &size, dummy,
57 &major, &minor, &namesize, dummy);
58 file_header->mode = mode;
59 file_header->uid = uid;
60 file_header->gid = gid;
61 file_header->mtime = mtime;
62 file_header->size = size;
64 file_header->name = xzalloc(namesize + 1);
65 /* Read in filename */
66 xread(archive_handle->src_fd, file_header->name, namesize);
67 archive_handle->offset += namesize;
69 /* Update offset amount and skip padding before file contents */
70 data_align(archive_handle, 4);
72 if (strcmp(file_header->name, "TRAILER!!!") == 0) {
73 /* Always round up. ">> 9" divides by 512 */
74 printf("%"OFF_FMT"u blocks\n", (archive_handle->offset + 511) >> 9);
75 goto create_hardlinks;
78 if (S_ISLNK(file_header->mode)) {
79 file_header->link_target = xzalloc(file_header->size + 1);
80 xread(archive_handle->src_fd, file_header->link_target, file_header->size);
81 archive_handle->offset += file_header->size;
82 file_header->size = 0; /* Stop possible seeks in future */
84 file_header->link_target = NULL;
87 // TODO: data_extract_all can't deal with hardlinks to non-files...
88 // (should be !S_ISDIR instead of S_ISREG here)
90 if (nlink > 1 && S_ISREG(file_header->mode)) {
91 hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
97 strcpy(new->name, file_header->name);
98 /* Put file on a linked list for later */
100 new->next = saved_hardlinks;
101 saved_hardlinks = new;
102 return EXIT_SUCCESS; /* Skip this one */
103 /* TODO: this breaks cpio -t (it does not show hardlinks) */
105 new->next = saved_hardlinks_created;
106 saved_hardlinks_created = new;
108 file_header->device = makedev(major, minor);
110 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
111 archive_handle->action_data(archive_handle);
112 archive_handle->action_header(file_header);
114 data_skip(archive_handle);
117 archive_handle->offset += file_header->size;
119 free(file_header->link_target);
120 free(file_header->name);
121 file_header->link_target = NULL;
122 file_header->name = NULL;
127 free(file_header->link_target);
128 free(file_header->name);
130 while (saved_hardlinks) {
132 hardlinks_t *make_me = saved_hardlinks;
133 saved_hardlinks = make_me->next;
135 memset(file_header, 0, sizeof(*file_header));
136 file_header->name = make_me->name;
137 file_header->mode = make_me->mode;
138 /*file_header->size = 0;*/
140 /* Try to find a file we are hardlinked to */
141 cur = saved_hardlinks_created;
143 /* TODO: must match maj/min too! */
144 if (cur->inode == make_me->inode) {
145 file_header->link_target = cur->name;
146 /* link_target != NULL, size = 0: "I am a hardlink" */
147 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
148 archive_handle->action_data(archive_handle);
153 /* Oops... no file with such inode was created... do it now
154 * (happens when hardlinked files are empty (zero length)) */
155 file_header->mtime = make_me->mtime;
156 file_header->uid = make_me->uid ;
157 file_header->gid = make_me->gid ;
158 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
159 archive_handle->action_data(archive_handle);
160 /* Move to the list of created hardlinked files */
161 make_me->next = saved_hardlinks_created;
162 saved_hardlinks_created = make_me;
166 while (saved_hardlinks_created) {
167 hardlinks_t *p = saved_hardlinks_created;
168 saved_hardlinks_created = p->next;
172 return EXIT_FAILURE; /* "No more files to process" */