update.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /* Update a tar archive.
  2. Copyright (C) 1988, 1992 Free Software Foundation
  3. This file is part of GNU Tar.
  4. GNU Tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Tar; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* JF implement the 'r' 'u' and 'A' options for tar. */
  16. /* The 'A' option is my own invention: It means that the file-names are
  17. tar files, and they should simply be appended to the end of the archive.
  18. No attempt is made to block the reads from the args; if they're on raw
  19. tape or something like that, it'll probably lose. . . */
  20. #include <sys/types.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #ifndef STDC_HEADERS
  24. extern int errno;
  25. #endif
  26. #ifdef HAVE_SYS_MTIO_H
  27. #include <sys/ioctl.h>
  28. #include <sys/mtio.h>
  29. #endif
  30. #ifdef BSD42
  31. #include <sys/file.h>
  32. #else
  33. #ifndef V7
  34. #include <fcntl.h>
  35. #endif
  36. #endif
  37. #ifndef __MSDOS__
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #endif
  41. #define STDIN 0
  42. #define STDOUT 1
  43. #include "tar.h"
  44. #include "port.h"
  45. #include "rmt.h"
  46. int time_to_start_writing = 0; /* We've hit the end of the old stuff,
  47. and its time to start writing new stuff
  48. to the tape. This involves seeking
  49. back one block and re-writing the current
  50. block (which has been changed). */
  51. char *output_start; /* Pointer to where we started to write in
  52. the first block we write out. This is used
  53. if we can't backspace the output and have
  54. to null out the first part of the block */
  55. extern void skip_file ();
  56. extern void skip_extended_headers ();
  57. extern union record *head;
  58. extern struct stat hstat;
  59. void append_file ();
  60. void close_archive ();
  61. int confirm ();
  62. void decode_header ();
  63. void fl_read ();
  64. void fl_write ();
  65. void flush_archive ();
  66. int move_arch ();
  67. struct name *name_scan ();
  68. char *name_from_list ();
  69. void name_expand ();
  70. void name_gather ();
  71. void names_notfound ();
  72. void open_archive ();
  73. int read_header ();
  74. void reset_eof ();
  75. void write_block ();
  76. void write_eot ();
  77. /* Implement the 'r' (add files to end of archive), and 'u' (add files to
  78. end of archive if they arent there, or are more up to date than the
  79. version in the archive.) commands.*/
  80. void
  81. update_archive ()
  82. {
  83. int found_end = 0;
  84. int status = 3;
  85. int prev_status;
  86. char *p;
  87. struct name *name;
  88. extern void dump_file ();
  89. name_gather ();
  90. if (cmd_mode == CMD_UPDATE)
  91. name_expand ();
  92. open_archive (2); /* Open for updating */
  93. do
  94. {
  95. prev_status = status;
  96. status = read_header ();
  97. switch (status)
  98. {
  99. case EOF:
  100. found_end = 1;
  101. break;
  102. case 0: /* A bad record */
  103. userec (head);
  104. switch (prev_status)
  105. {
  106. case 3:
  107. msg ("This doesn't look like a tar archive.");
  108. /* FALL THROUGH */
  109. case 2:
  110. case 1:
  111. msg ("Skipping to next header");
  112. case 0:
  113. break;
  114. }
  115. break;
  116. /* A good record */
  117. case 1:
  118. /* printf("File %s\n",head->header.name); */
  119. /* head->header.name[NAMSIZ-1]='\0'; */
  120. if (cmd_mode == CMD_UPDATE && (name = name_scan (current_file_name)))
  121. {
  122. /* struct stat hstat; */
  123. struct stat nstat;
  124. int head_standard;
  125. decode_header (head, &hstat, &head_standard, 0);
  126. if (stat (current_file_name, &nstat) < 0)
  127. {
  128. msg_perror ("can't stat %s:", current_file_name);
  129. }
  130. else
  131. {
  132. if (hstat.st_mtime >= nstat.st_mtime)
  133. name->found++;
  134. }
  135. }
  136. userec (head);
  137. if (head->header.isextended)
  138. skip_extended_headers ();
  139. skip_file ((long) hstat.st_size);
  140. break;
  141. case 2:
  142. ar_record = head;
  143. found_end = 1;
  144. break;
  145. }
  146. }
  147. while (!found_end);
  148. reset_eof ();
  149. time_to_start_writing = 1;
  150. output_start = ar_record->charptr;
  151. while (p = name_from_list ())
  152. {
  153. if (f_confirm && !confirm ("add", p))
  154. continue;
  155. if (cmd_mode == CMD_CAT)
  156. append_file (p);
  157. else
  158. dump_file (p, -1, 1);
  159. }
  160. write_eot ();
  161. close_archive ();
  162. names_notfound ();
  163. }
  164. /* Catenate file p to the archive without creating a header for it. It had
  165. better be a tar file or the archive is screwed */
  166. void
  167. append_file (p)
  168. char *p;
  169. {
  170. int fd;
  171. struct stat statbuf;
  172. long bytes_left;
  173. union record *start;
  174. long bufsiz, count;
  175. if (0 != stat (p, &statbuf) || (fd = open (p, O_RDONLY | O_BINARY)) < 0)
  176. {
  177. msg_perror ("can't open file %s", p);
  178. errors++;
  179. return;
  180. }
  181. bytes_left = statbuf.st_size;
  182. while (bytes_left > 0)
  183. {
  184. start = findrec ();
  185. bufsiz = endofrecs ()->charptr - start->charptr;
  186. if (bytes_left < bufsiz)
  187. {
  188. bufsiz = bytes_left;
  189. count = bufsiz % RECORDSIZE;
  190. if (count)
  191. bzero (start->charptr + bytes_left, (int) (RECORDSIZE - count));
  192. }
  193. count = read (fd, start->charptr, bufsiz);
  194. if (count < 0)
  195. {
  196. msg_perror ("read error at byte %ld reading %d bytes in file %s", statbuf.st_size - bytes_left, bufsiz, p);
  197. exit (EX_ARGSBAD); /* FOO */
  198. }
  199. bytes_left -= count;
  200. userec (start + (count - 1) / RECORDSIZE);
  201. if (count != bufsiz)
  202. {
  203. msg ("%s: file shrunk by %d bytes, yark!", p, bytes_left);
  204. abort ();
  205. }
  206. }
  207. (void) close (fd);
  208. }
  209. #ifdef DONTDEF
  210. bprint (fp, buf, num)
  211. FILE *fp;
  212. char *buf;
  213. {
  214. int c;
  215. if (num == 0 || num == -1)
  216. return;
  217. fputs (" '", fp);
  218. while (num--)
  219. {
  220. c = *buf++;
  221. if (c == '\\')
  222. fputs ("\\\\", fp);
  223. else if (c >= ' ' && c <= '~')
  224. putc (c, fp);
  225. else
  226. switch (c)
  227. {
  228. case '\n':
  229. fputs ("\\n", fp);
  230. break;
  231. case '\r':
  232. fputs ("\\r", fp);
  233. break;
  234. case '\b':
  235. fputs ("\\b", fp);
  236. break;
  237. case '\0':
  238. /* fputs("\\-",fp); */
  239. break;
  240. default:
  241. fprintf (fp, "\\%03o", c);
  242. break;
  243. }
  244. }
  245. fputs ("'\n", fp);
  246. }
  247. #endif
  248. int number_of_blocks_read = 0;
  249. int number_of_new_records = 0;
  250. int number_of_records_needed = 0;
  251. union record *new_block = 0;
  252. union record *save_block = 0;
  253. void
  254. junk_archive ()
  255. {
  256. int found_stuff = 0;
  257. int status = 3;
  258. int prev_status;
  259. struct name *name;
  260. /* int dummy_head; */
  261. int number_of_records_to_skip = 0;
  262. int number_of_records_to_keep = 0;
  263. int number_of_kept_records_in_block;
  264. int sub_status;
  265. extern int write_archive_to_stdout;
  266. /* fprintf(stderr,"Junk files\n"); */
  267. name_gather ();
  268. open_archive (2);
  269. while (!found_stuff)
  270. {
  271. prev_status = status;
  272. status = read_header ();
  273. switch (status)
  274. {
  275. case EOF:
  276. found_stuff = 1;
  277. break;
  278. case 0:
  279. userec (head);
  280. switch (prev_status)
  281. {
  282. case 3:
  283. msg ("This doesn't look like a tar archive.");
  284. /* FALL THROUGH */
  285. case 2:
  286. case 1:
  287. msg ("Skipping to next header");
  288. /* FALL THROUGH */
  289. case 0:
  290. break;
  291. }
  292. break;
  293. case 1:
  294. /* head->header.name[NAMSIZ-1] = '\0'; */
  295. /* fprintf(stderr,"file %s\n",head->header.name); */
  296. if ((name = name_scan (current_file_name)) == (struct name *) 0)
  297. {
  298. userec (head);
  299. /* fprintf(stderr,"Skip %ld\n",(long)(hstat.st_size)); */
  300. if (head->header.isextended)
  301. skip_extended_headers ();
  302. skip_file ((long) (hstat.st_size));
  303. break;
  304. }
  305. name->found = 1;
  306. found_stuff = 2;
  307. break;
  308. case 2:
  309. found_stuff = 1;
  310. break;
  311. }
  312. }
  313. /* fprintf(stderr,"Out of first loop\n"); */
  314. if (found_stuff != 2)
  315. {
  316. write_eot ();
  317. close_archive ();
  318. names_notfound ();
  319. return;
  320. }
  321. if (write_archive_to_stdout)
  322. write_archive_to_stdout = 0;
  323. new_block = (union record *) malloc (blocksize);
  324. if (new_block == 0)
  325. {
  326. msg ("Can't allocate secondary block of %d bytes", blocksize);
  327. exit (EX_SYSTEM);
  328. }
  329. /* Save away records before this one in this block */
  330. number_of_new_records = ar_record - ar_block;
  331. number_of_records_needed = blocking - number_of_new_records;
  332. if (number_of_new_records)
  333. bcopy ((void *) ar_block, (void *) new_block, (number_of_new_records) * RECORDSIZE);
  334. /* fprintf(stderr,"Saved %d recs, need %d more\n",number_of_new_records,number_of_records_needed); */
  335. userec (head);
  336. if (head->header.isextended)
  337. skip_extended_headers ();
  338. skip_file ((long) (hstat.st_size));
  339. found_stuff = 0;
  340. /* goto flush_file; */
  341. for (;;)
  342. {
  343. /* Fill in a block */
  344. /* another_file: */
  345. if (ar_record == ar_last)
  346. {
  347. /* fprintf(stderr,"New block\n"); */
  348. flush_archive ();
  349. number_of_blocks_read++;
  350. }
  351. sub_status = read_header ();
  352. /* fprintf(stderr,"Header type %d\n",sub_status); */
  353. if (sub_status == 2 && f_ignorez)
  354. {
  355. userec (head);
  356. continue;
  357. }
  358. if (sub_status == EOF || sub_status == 2)
  359. {
  360. found_stuff = 1;
  361. bzero (new_block[number_of_new_records].charptr, RECORDSIZE * number_of_records_needed);
  362. number_of_new_records += number_of_records_needed;
  363. number_of_records_needed = 0;
  364. write_block (0);
  365. break;
  366. }
  367. if (sub_status == 0)
  368. {
  369. msg ("Deleting non-header from archive.");
  370. userec (head);
  371. continue;
  372. }
  373. /* Found another header. Yipee! */
  374. /* head->header.name[NAMSIZ-1] = '\0'; */
  375. /* fprintf(stderr,"File %s ",head->header.name); */
  376. if (name = name_scan (current_file_name))
  377. {
  378. name->found = 1;
  379. /* fprintf(stderr,"Flush it\n"); */
  380. /* flush_file: */
  381. /* decode_header(head,&hstat,&dummy_head,0); */
  382. userec (head);
  383. number_of_records_to_skip = (hstat.st_size + RECORDSIZE - 1) / RECORDSIZE;
  384. /* fprintf(stderr,"Flushing %d recs from %s\n",number_of_records_to_skip,head->header.name); */
  385. while (ar_last - ar_record <= number_of_records_to_skip)
  386. {
  387. /* fprintf(stderr,"Block: %d <= %d ",ar_last-ar_record,number_of_records_to_skip); */
  388. number_of_records_to_skip -= (ar_last - ar_record);
  389. flush_archive ();
  390. number_of_blocks_read++;
  391. /* fprintf(stderr,"Block %d left\n",number_of_records_to_skip); */
  392. }
  393. ar_record += number_of_records_to_skip;
  394. /* fprintf(stderr,"Final %d\n",number_of_records_to_skip); */
  395. number_of_records_to_skip = 0;
  396. continue;
  397. }
  398. /* copy_header: */
  399. new_block[number_of_new_records] = *head;
  400. number_of_new_records++;
  401. number_of_records_needed--;
  402. number_of_records_to_keep = (hstat.st_size + RECORDSIZE - 1) / RECORDSIZE;
  403. userec (head);
  404. if (number_of_records_needed == 0)
  405. write_block (1);
  406. /* copy_data: */
  407. number_of_kept_records_in_block = ar_last - ar_record;
  408. if (number_of_kept_records_in_block > number_of_records_to_keep)
  409. number_of_kept_records_in_block = number_of_records_to_keep;
  410. /* fprintf(stderr,"Need %d kept_in %d keep %d\n",blocking,number_of_kept_records_in_block,number_of_records_to_keep); */
  411. while (number_of_records_to_keep)
  412. {
  413. int n;
  414. if (ar_record == ar_last)
  415. {
  416. /* fprintf(stderr,"Flush. . .\n"); */
  417. fl_read ();
  418. number_of_blocks_read++;
  419. ar_record = ar_block;
  420. number_of_kept_records_in_block = blocking;
  421. if (number_of_kept_records_in_block > number_of_records_to_keep)
  422. number_of_kept_records_in_block = number_of_records_to_keep;
  423. }
  424. n = number_of_kept_records_in_block;
  425. if (n > number_of_records_needed)
  426. n = number_of_records_needed;
  427. /* fprintf(stderr,"Copying %d\n",n); */
  428. bcopy ((void *) ar_record, (void *) (new_block + number_of_new_records), n * RECORDSIZE);
  429. number_of_new_records += n;
  430. number_of_records_needed -= n;
  431. ar_record += n;
  432. number_of_records_to_keep -= n;
  433. number_of_kept_records_in_block -= n;
  434. /* fprintf(stderr,"Now new %d need %d keep %d keep_in %d rec %d/%d\n",
  435. number_of_new_records,number_of_records_needed,number_of_records_to_keep,
  436. number_of_kept_records_in_block,ar_record-ar_block,ar_last-ar_block); */
  437. if (number_of_records_needed == 0)
  438. {
  439. write_block (1);
  440. }
  441. }
  442. }
  443. write_eot ();
  444. close_archive ();
  445. names_notfound ();
  446. }
  447. void
  448. write_block (f)
  449. int f;
  450. {
  451. /* fprintf(stderr,"Write block\n"); */
  452. /* We've filled out a block. Write it out. */
  453. /* Backspace back to where we started. . . */
  454. if (archive != STDIN)
  455. (void) move_arch (-(number_of_blocks_read + 1));
  456. save_block = ar_block;
  457. ar_block = new_block;
  458. if (archive == STDIN)
  459. archive = STDOUT;
  460. fl_write ();
  461. if (archive == STDOUT)
  462. archive = STDIN;
  463. ar_block = save_block;
  464. if (f)
  465. {
  466. /* Move the tape head back to where we were */
  467. if (archive != STDIN)
  468. (void) move_arch (number_of_blocks_read);
  469. number_of_blocks_read--;
  470. }
  471. number_of_records_needed = blocking;
  472. number_of_new_records = 0;
  473. }
  474. /* Move archive descriptor by n blocks worth. If n is positive we move
  475. forward, else we move negative. If its a tape, MTIOCTOP had better
  476. work. If its something else, we try to seek on it. If we can't
  477. seek, we lose! */
  478. int
  479. move_arch (n)
  480. int n;
  481. {
  482. long cur;
  483. #ifdef MTIOCTOP
  484. struct mtop t;
  485. int er;
  486. if (n > 0)
  487. {
  488. t.mt_op = MTFSR;
  489. t.mt_count = n;
  490. }
  491. else
  492. {
  493. t.mt_op = MTBSR;
  494. t.mt_count = -n;
  495. }
  496. if ((er = rmtioctl (archive, MTIOCTOP, &t)) >= 0)
  497. return 1;
  498. if (errno == EIO && (er = rmtioctl (archive, MTIOCTOP, &t)) >= 0)
  499. return 1;
  500. #endif
  501. cur = rmtlseek (archive, 0L, 1);
  502. cur += blocksize * n;
  503. /* fprintf(stderr,"Fore to %x\n",cur); */
  504. if (rmtlseek (archive, cur, 0) != cur)
  505. {
  506. /* Lseek failed. Try a different method */
  507. msg ("Couldn't re-position archive file.");
  508. exit (EX_BADARCH);
  509. }
  510. return 3;
  511. }