get_header_cpio.c 6.0 KB

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