update.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Update a tar archive.
  2. Copyright (C) 1988, 1992, 1994, 1996, 1997, 1999, 2000, 2001 Free
  3. Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Implement the 'r', 'u' and 'A' options for tar. 'A' means that the
  16. file names are tar files, and they should simply be appended to the end
  17. of the archive. No attempt is made to record the reads from the args; if
  18. they're on raw tape or something like that, it'll probably lose... */
  19. #include "system.h"
  20. #include <quotearg.h>
  21. #include "common.h"
  22. /* FIXME: This module should not directly handle the following variable,
  23. instead, this should be done in buffer.c only. */
  24. extern union block *current_block;
  25. /* We've hit the end of the old stuff, and its time to start writing new
  26. stuff to the tape. This involves seeking back one record and
  27. re-writing the current record (which has been changed). */
  28. int time_to_start_writing;
  29. /* Pointer to where we started to write in the first record we write out.
  30. This is used if we can't backspace the output and have to null out the
  31. first part of the record. */
  32. char *output_start;
  33. /* Catenate file PATH to the archive without creating a header for it.
  34. It had better be a tar file or the archive is screwed. */
  35. static void
  36. append_file (char *path)
  37. {
  38. int handle = open (path, O_RDONLY | O_BINARY);
  39. struct stat stat_data;
  40. if (handle < 0)
  41. {
  42. open_error (path);
  43. return;
  44. }
  45. if (fstat (handle, &stat_data) != 0)
  46. stat_error (path);
  47. else
  48. {
  49. off_t bytes_left = stat_data.st_size;
  50. while (bytes_left > 0)
  51. {
  52. union block *start = find_next_block ();
  53. size_t buffer_size = available_space_after (start);
  54. ssize_t status;
  55. char buf[UINTMAX_STRSIZE_BOUND];
  56. if (bytes_left < buffer_size)
  57. {
  58. buffer_size = bytes_left;
  59. status = buffer_size % BLOCKSIZE;
  60. if (status)
  61. memset (start->buffer + bytes_left, 0, BLOCKSIZE - status);
  62. }
  63. status = safe_read (handle, start->buffer, buffer_size);
  64. if (status < 0)
  65. read_fatal_details (path, stat_data.st_size - bytes_left,
  66. buffer_size);
  67. if (status == 0)
  68. FATAL_ERROR ((0, 0, _("%s: File shrank by %s bytes"),
  69. quotearg_colon (path),
  70. STRINGIFY_BIGINT (bytes_left, buf)));
  71. bytes_left -= status;
  72. set_next_block_after (start + (status - 1) / BLOCKSIZE);
  73. }
  74. }
  75. if (close (handle) != 0)
  76. close_error (path);
  77. }
  78. /* Implement the 'r' (add files to end of archive), and 'u' (add files
  79. to end of archive if they aren't there, or are more up to date than
  80. the version in the archive) commands. */
  81. void
  82. update_archive (void)
  83. {
  84. enum read_header previous_status = HEADER_STILL_UNREAD;
  85. int found_end = 0;
  86. name_gather ();
  87. open_archive (ACCESS_UPDATE);
  88. while (!found_end)
  89. {
  90. enum read_header status = read_header (0);
  91. switch (status)
  92. {
  93. case HEADER_STILL_UNREAD:
  94. abort ();
  95. case HEADER_SUCCESS:
  96. {
  97. struct name *name;
  98. if (subcommand_option == UPDATE_SUBCOMMAND
  99. && (name = name_scan (current_file_name), name))
  100. {
  101. struct stat s;
  102. enum archive_format unused;
  103. decode_header (current_header, &current_stat, &unused, 0);
  104. chdir_do (name->change_dir);
  105. if (deref_stat (dereference_option, current_file_name, &s) == 0
  106. && s.st_mtime <= current_stat.st_mtime)
  107. add_avoided_name (current_file_name);
  108. }
  109. skip_member ();
  110. break;
  111. }
  112. case HEADER_ZERO_BLOCK:
  113. current_block = current_header;
  114. found_end = 1;
  115. break;
  116. case HEADER_END_OF_FILE:
  117. found_end = 1;
  118. break;
  119. case HEADER_FAILURE:
  120. set_next_block_after (current_header);
  121. switch (previous_status)
  122. {
  123. case HEADER_STILL_UNREAD:
  124. WARN ((0, 0, _("This does not look like a tar archive")));
  125. /* Fall through. */
  126. case HEADER_SUCCESS:
  127. case HEADER_ZERO_BLOCK:
  128. ERROR ((0, 0, _("Skipping to next header")));
  129. /* Fall through. */
  130. case HEADER_FAILURE:
  131. break;
  132. case HEADER_END_OF_FILE:
  133. abort ();
  134. }
  135. break;
  136. }
  137. previous_status = status;
  138. }
  139. reset_eof ();
  140. time_to_start_writing = 1;
  141. output_start = current_block->buffer;
  142. {
  143. char *path;
  144. while (path = name_from_list (), path)
  145. {
  146. if (excluded_name (path))
  147. continue;
  148. if (interactive_option && !confirm ("add", path))
  149. continue;
  150. if (subcommand_option == CAT_SUBCOMMAND)
  151. append_file (path);
  152. else
  153. dump_file (path, 1, (dev_t) 0);
  154. }
  155. }
  156. write_eot ();
  157. close_archive ();
  158. names_notfound ();
  159. }