get_header_cpio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright 2002 Laurence Anderson
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. #include "libbb.h"
  8. #include "bb_archive.h"
  9. typedef struct hardlinks_t {
  10. struct hardlinks_t *next;
  11. int inode; /* TODO: must match maj/min too! */
  12. int mode ;
  13. int mtime; /* These three are useful only in corner case */
  14. int uid ; /* of hardlinks with zero size body */
  15. int gid ;
  16. char name[1];
  17. } hardlinks_t;
  18. char FAST_FUNC get_header_cpio(archive_handle_t *archive_handle)
  19. {
  20. file_header_t *file_header = archive_handle->file_header;
  21. char cpio_header[111];
  22. int namesize;
  23. int major, minor, nlink, mode, inode;
  24. unsigned size, uid, gid, mtime;
  25. /* There can be padding before archive header */
  26. data_align(archive_handle, 4);
  27. size = full_read(archive_handle->src_fd, cpio_header, 110);
  28. if (size == 0) {
  29. goto create_hardlinks;
  30. }
  31. if (size != 110) {
  32. bb_simple_error_msg_and_die("short read");
  33. }
  34. archive_handle->offset += 110;
  35. if (!is_prefixed_with(&cpio_header[0], "07070")
  36. || (cpio_header[5] != '1' && cpio_header[5] != '2')
  37. ) {
  38. bb_simple_error_msg_and_die("unsupported cpio format, use newc or crc");
  39. }
  40. cpio_header[110] = '\0'; /* sscanf may call strlen which may break without this */
  41. if (sscanf(cpio_header + 6,
  42. "%8x" "%8x" "%8x" "%8x"
  43. "%8x" "%8x" "%8x" /*maj,min:*/ "%*16c"
  44. /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum: "%*8c"*/,
  45. &inode, &mode, &uid, &gid,
  46. &nlink, &mtime, &size,
  47. &major, &minor, &namesize) != 10)
  48. bb_simple_error_msg_and_die("damaged cpio file");
  49. file_header->mode = mode;
  50. /* "cpio -R USER:GRP" support: */
  51. if (archive_handle->cpio__owner.uid != (uid_t)-1L)
  52. uid = archive_handle->cpio__owner.uid;
  53. if (archive_handle->cpio__owner.gid != (gid_t)-1L)
  54. gid = archive_handle->cpio__owner.gid;
  55. file_header->uid = uid;
  56. file_header->gid = gid;
  57. file_header->mtime = mtime;
  58. file_header->size = size;
  59. namesize &= 0x1fff; /* paranoia: limit names to 8k chars */
  60. file_header->name = xzalloc(namesize + 1);
  61. /* Read in filename */
  62. xread(archive_handle->src_fd, file_header->name, namesize);
  63. if (file_header->name[0] == '/') {
  64. /* Testcase: echo /etc/hosts | cpio -pvd /tmp
  65. * Without this code, it tries to unpack /etc/hosts
  66. * into "/etc/hosts", not "etc/hosts".
  67. */
  68. char *p = file_header->name;
  69. do p++; while (*p == '/');
  70. overlapping_strcpy(file_header->name, p);
  71. }
  72. archive_handle->offset += namesize;
  73. /* Update offset amount and skip padding before file contents */
  74. data_align(archive_handle, 4);
  75. if (strcmp(file_header->name, cpio_TRAILER) == 0) {
  76. /* Always round up. ">> 9" divides by 512 */
  77. archive_handle->cpio__blocks = (uoff_t)(archive_handle->offset + 511) >> 9;
  78. goto create_hardlinks;
  79. }
  80. file_header->link_target = NULL;
  81. if (S_ISLNK(file_header->mode)) {
  82. file_header->size &= 0x1fff; /* paranoia: limit names to 8k chars */
  83. file_header->link_target = xzalloc(file_header->size + 1);
  84. xread(archive_handle->src_fd, file_header->link_target, file_header->size);
  85. archive_handle->offset += file_header->size;
  86. file_header->size = 0; /* Stop possible seeks in future */
  87. }
  88. // TODO: data_extract_all can't deal with hardlinks to non-files...
  89. // when fixed, change S_ISREG to !S_ISDIR here
  90. if (nlink > 1 && S_ISREG(file_header->mode)) {
  91. hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
  92. new->inode = inode;
  93. new->mode = mode ;
  94. new->mtime = mtime;
  95. new->uid = uid ;
  96. new->gid = gid ;
  97. strcpy(new->name, file_header->name);
  98. /* Put file on a linked list for later */
  99. if (size == 0) {
  100. new->next = archive_handle->cpio__hardlinks_to_create;
  101. archive_handle->cpio__hardlinks_to_create = new;
  102. return EXIT_SUCCESS; /* Skip this one */
  103. /* TODO: this breaks cpio -t (it does not show hardlinks) */
  104. }
  105. new->next = archive_handle->cpio__created_hardlinks;
  106. archive_handle->cpio__created_hardlinks = new;
  107. }
  108. file_header->device = makedev(major, minor);
  109. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  110. archive_handle->action_data(archive_handle);
  111. //TODO: run "echo /etc/hosts | cpio -pv /tmp" twice. On 2nd run:
  112. //cpio: etc/hosts not created: newer or same age file exists
  113. //etc/hosts <-- should NOT show it
  114. //2 blocks <-- should say "0 blocks"
  115. archive_handle->action_header(file_header);
  116. } else {
  117. data_skip(archive_handle);
  118. }
  119. archive_handle->offset += file_header->size;
  120. free(file_header->link_target);
  121. free(file_header->name);
  122. file_header->link_target = NULL;
  123. file_header->name = NULL;
  124. return EXIT_SUCCESS;
  125. create_hardlinks:
  126. free(file_header->link_target);
  127. free(file_header->name);
  128. while (archive_handle->cpio__hardlinks_to_create) {
  129. hardlinks_t *cur;
  130. hardlinks_t *make_me = archive_handle->cpio__hardlinks_to_create;
  131. archive_handle->cpio__hardlinks_to_create = make_me->next;
  132. memset(file_header, 0, sizeof(*file_header));
  133. file_header->mtime = make_me->mtime;
  134. file_header->name = make_me->name;
  135. file_header->mode = make_me->mode;
  136. file_header->uid = make_me->uid;
  137. file_header->gid = make_me->gid;
  138. /*file_header->size = 0;*/
  139. /*file_header->link_target = NULL;*/
  140. /* Try to find a file we are hardlinked to */
  141. cur = archive_handle->cpio__created_hardlinks;
  142. while (cur) {
  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);
  149. free(make_me);
  150. goto next_link;
  151. }
  152. cur = cur->next;
  153. }
  154. /* Oops... no file with such inode was created... do it now
  155. * (happens when hardlinked files are empty (zero length)) */
  156. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
  157. archive_handle->action_data(archive_handle);
  158. /* Move to the list of created hardlinked files */
  159. make_me->next = archive_handle->cpio__created_hardlinks;
  160. archive_handle->cpio__created_hardlinks = make_me;
  161. next_link: ;
  162. }
  163. while (archive_handle->cpio__created_hardlinks) {
  164. hardlinks_t *p = archive_handle->cpio__created_hardlinks;
  165. archive_handle->cpio__created_hardlinks = p->next;
  166. free(p);
  167. }
  168. return EXIT_FAILURE; /* "No more files to process" */
  169. }