get_header_cpio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "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 (strncmp(&cpio_header[0], "07070", 5) != 0
  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. file_header->uid = uid;
  49. file_header->gid = gid;
  50. file_header->mtime = mtime;
  51. file_header->size = size;
  52. namesize &= 0x1fff; /* paranoia: limit names to 8k chars */
  53. file_header->name = xzalloc(namesize + 1);
  54. /* Read in filename */
  55. xread(archive_handle->src_fd, file_header->name, namesize);
  56. if (file_header->name[0] == '/') {
  57. /* Testcase: echo /etc/hosts | cpio -pvd /tmp
  58. * Without this code, it tries to unpack /etc/hosts
  59. * into "/etc/hosts", not "etc/hosts".
  60. */
  61. char *p = file_header->name;
  62. do p++; while (*p == '/');
  63. overlapping_strcpy(file_header->name, p);
  64. }
  65. archive_handle->offset += namesize;
  66. /* Update offset amount and skip padding before file contents */
  67. data_align(archive_handle, 4);
  68. if (strcmp(file_header->name, "TRAILER!!!") == 0) {
  69. /* Always round up. ">> 9" divides by 512 */
  70. archive_handle->cpio__blocks = (uoff_t)(archive_handle->offset + 511) >> 9;
  71. goto create_hardlinks;
  72. }
  73. file_header->link_target = NULL;
  74. if (S_ISLNK(file_header->mode)) {
  75. file_header->size &= 0x1fff; /* paranoia: limit names to 8k chars */
  76. file_header->link_target = xzalloc(file_header->size + 1);
  77. xread(archive_handle->src_fd, file_header->link_target, file_header->size);
  78. archive_handle->offset += file_header->size;
  79. file_header->size = 0; /* Stop possible seeks in future */
  80. }
  81. // TODO: data_extract_all can't deal with hardlinks to non-files...
  82. // when fixed, change S_ISREG to !S_ISDIR here
  83. if (nlink > 1 && S_ISREG(file_header->mode)) {
  84. hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
  85. new->inode = inode;
  86. new->mode = mode ;
  87. new->mtime = mtime;
  88. new->uid = uid ;
  89. new->gid = gid ;
  90. strcpy(new->name, file_header->name);
  91. /* Put file on a linked list for later */
  92. if (size == 0) {
  93. new->next = archive_handle->cpio__hardlinks_to_create;
  94. archive_handle->cpio__hardlinks_to_create = new;
  95. return EXIT_SUCCESS; /* Skip this one */
  96. /* TODO: this breaks cpio -t (it does not show hardlinks) */
  97. }
  98. new->next = archive_handle->cpio__created_hardlinks;
  99. archive_handle->cpio__created_hardlinks = new;
  100. }
  101. file_header->device = makedev(major, minor);
  102. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  103. archive_handle->action_data(archive_handle);
  104. //TODO: run "echo /etc/hosts | cpio -pv /tmp" twice. On 2nd run:
  105. //cpio: etc/hosts not created: newer or same age file exists
  106. //etc/hosts <-- should NOT show it
  107. //2 blocks <-- should say "0 blocks"
  108. archive_handle->action_header(file_header);
  109. } else {
  110. data_skip(archive_handle);
  111. }
  112. archive_handle->offset += file_header->size;
  113. free(file_header->link_target);
  114. free(file_header->name);
  115. file_header->link_target = NULL;
  116. file_header->name = NULL;
  117. return EXIT_SUCCESS;
  118. create_hardlinks:
  119. free(file_header->link_target);
  120. free(file_header->name);
  121. while (archive_handle->cpio__hardlinks_to_create) {
  122. hardlinks_t *cur;
  123. hardlinks_t *make_me = archive_handle->cpio__hardlinks_to_create;
  124. archive_handle->cpio__hardlinks_to_create = make_me->next;
  125. memset(file_header, 0, sizeof(*file_header));
  126. file_header->mtime = make_me->mtime;
  127. file_header->name = make_me->name;
  128. file_header->mode = make_me->mode;
  129. file_header->uid = make_me->uid;
  130. file_header->gid = make_me->gid;
  131. /*file_header->size = 0;*/
  132. /*file_header->link_target = NULL;*/
  133. /* Try to find a file we are hardlinked to */
  134. cur = archive_handle->cpio__created_hardlinks;
  135. while (cur) {
  136. /* TODO: must match maj/min too! */
  137. if (cur->inode == make_me->inode) {
  138. file_header->link_target = cur->name;
  139. /* link_target != NULL, size = 0: "I am a hardlink" */
  140. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
  141. archive_handle->action_data(archive_handle);
  142. free(make_me);
  143. goto next_link;
  144. }
  145. cur = cur->next;
  146. }
  147. /* Oops... no file with such inode was created... do it now
  148. * (happens when hardlinked files are empty (zero length)) */
  149. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
  150. archive_handle->action_data(archive_handle);
  151. /* Move to the list of created hardlinked files */
  152. make_me->next = archive_handle->cpio__created_hardlinks;
  153. archive_handle->cpio__created_hardlinks = make_me;
  154. next_link: ;
  155. }
  156. while (archive_handle->cpio__created_hardlinks) {
  157. hardlinks_t *p = archive_handle->cpio__created_hardlinks;
  158. archive_handle->cpio__created_hardlinks = p->next;
  159. free(p);
  160. }
  161. return EXIT_FAILURE; /* "No more files to process" */
  162. }