get_header_cpio.c 6.0 KB

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