get_header_cpio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* vi: set sw=4 ts=4: */
  2. /* Copyright 2002 Laurence Anderson
  3. *
  4. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  5. */
  6. #include "libbb.h"
  7. #include "unarchive.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. #define hardlinks_to_create (*(hardlinks_t **)(&archive_handle->ah_priv[0]))
  25. #define created_hardlinks (*(hardlinks_t **)(&archive_handle->ah_priv[1]))
  26. #define block_count (archive_handle->ah_priv[2])
  27. // if (!archive_handle->ah_priv_inited) {
  28. // archive_handle->ah_priv_inited = 1;
  29. // hardlinks_to_create = NULL;
  30. // created_hardlinks = NULL;
  31. // }
  32. /* There can be padding before archive header */
  33. data_align(archive_handle, 4);
  34. size = full_read(archive_handle->src_fd, cpio_header, 110);
  35. if (size == 0) {
  36. goto create_hardlinks;
  37. }
  38. if (size != 110) {
  39. bb_error_msg_and_die("short read");
  40. }
  41. archive_handle->offset += 110;
  42. if (strncmp(&cpio_header[0], "07070", 5) != 0
  43. || (cpio_header[5] != '1' && cpio_header[5] != '2')
  44. ) {
  45. bb_error_msg_and_die("unsupported cpio format, use newc or crc");
  46. }
  47. if (sscanf(cpio_header + 6,
  48. "%8x" "%8x" "%8x" "%8x"
  49. "%8x" "%8x" "%8x" /*maj,min:*/ "%*16c"
  50. /*rmaj,rmin:*/"%8x" "%8x" "%8x" /*chksum: "%*8c"*/,
  51. &inode, &mode, &uid, &gid,
  52. &nlink, &mtime, &size,
  53. &major, &minor, &namesize) != 10)
  54. bb_error_msg_and_die("damaged cpio file");
  55. file_header->mode = mode;
  56. file_header->uid = uid;
  57. file_header->gid = gid;
  58. file_header->mtime = mtime;
  59. file_header->size = size;
  60. namesize &= 0x1fff; /* paranoia: limit names to 8k chars */
  61. file_header->name = xzalloc(namesize + 1);
  62. /* Read in filename */
  63. xread(archive_handle->src_fd, file_header->name, namesize);
  64. archive_handle->offset += namesize;
  65. /* Update offset amount and skip padding before file contents */
  66. data_align(archive_handle, 4);
  67. if (strcmp(file_header->name, "TRAILER!!!") == 0) {
  68. /* Always round up. ">> 9" divides by 512 */
  69. block_count = (void*)(ptrdiff_t) ((archive_handle->offset + 511) >> 9);
  70. goto create_hardlinks;
  71. }
  72. file_header->link_target = NULL;
  73. if (S_ISLNK(file_header->mode)) {
  74. file_header->size &= 0x1fff; /* paranoia: limit names to 8k chars */
  75. file_header->link_target = xzalloc(file_header->size + 1);
  76. xread(archive_handle->src_fd, file_header->link_target, file_header->size);
  77. archive_handle->offset += file_header->size;
  78. file_header->size = 0; /* Stop possible seeks in future */
  79. }
  80. // TODO: data_extract_all can't deal with hardlinks to non-files...
  81. // when fixed, change S_ISREG to !S_ISDIR here
  82. if (nlink > 1 && S_ISREG(file_header->mode)) {
  83. hardlinks_t *new = xmalloc(sizeof(*new) + namesize);
  84. new->inode = inode;
  85. new->mode = mode ;
  86. new->mtime = mtime;
  87. new->uid = uid ;
  88. new->gid = gid ;
  89. strcpy(new->name, file_header->name);
  90. /* Put file on a linked list for later */
  91. if (size == 0) {
  92. new->next = hardlinks_to_create;
  93. hardlinks_to_create = new;
  94. return EXIT_SUCCESS; /* Skip this one */
  95. /* TODO: this breaks cpio -t (it does not show hardlinks) */
  96. }
  97. new->next = created_hardlinks;
  98. created_hardlinks = new;
  99. }
  100. file_header->device = makedev(major, minor);
  101. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
  102. archive_handle->action_data(archive_handle);
  103. archive_handle->action_header(file_header);
  104. } else {
  105. data_skip(archive_handle);
  106. }
  107. archive_handle->offset += file_header->size;
  108. free(file_header->link_target);
  109. free(file_header->name);
  110. file_header->link_target = NULL;
  111. file_header->name = NULL;
  112. return EXIT_SUCCESS;
  113. create_hardlinks:
  114. free(file_header->link_target);
  115. free(file_header->name);
  116. while (hardlinks_to_create) {
  117. hardlinks_t *cur;
  118. hardlinks_t *make_me = hardlinks_to_create;
  119. hardlinks_to_create = make_me->next;
  120. memset(file_header, 0, sizeof(*file_header));
  121. file_header->mtime = make_me->mtime;
  122. file_header->name = make_me->name;
  123. file_header->mode = make_me->mode;
  124. file_header->uid = make_me->uid;
  125. file_header->gid = make_me->gid;
  126. /*file_header->size = 0;*/
  127. /*file_header->link_target = NULL;*/
  128. /* Try to find a file we are hardlinked to */
  129. cur = created_hardlinks;
  130. while (cur) {
  131. /* TODO: must match maj/min too! */
  132. if (cur->inode == make_me->inode) {
  133. file_header->link_target = cur->name;
  134. /* link_target != NULL, size = 0: "I am a hardlink" */
  135. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
  136. archive_handle->action_data(archive_handle);
  137. free(make_me);
  138. goto next_link;
  139. }
  140. cur = cur->next;
  141. }
  142. /* Oops... no file with such inode was created... do it now
  143. * (happens when hardlinked files are empty (zero length)) */
  144. if (archive_handle->filter(archive_handle) == EXIT_SUCCESS)
  145. archive_handle->action_data(archive_handle);
  146. /* Move to the list of created hardlinked files */
  147. make_me->next = created_hardlinks;
  148. created_hardlinks = make_me;
  149. next_link: ;
  150. }
  151. while (created_hardlinks) {
  152. hardlinks_t *p = created_hardlinks;
  153. created_hardlinks = p->next;
  154. free(p);
  155. }
  156. return EXIT_FAILURE; /* "No more files to process" */
  157. }