4
0

incremen.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* GNU dump extensions to tar.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005 Free 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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <system.h>
  16. #include <getline.h>
  17. #include <hash.h>
  18. #include <quotearg.h>
  19. #include "common.h"
  20. /* Incremental dump specialities. */
  21. /* Which child files to save under a directory. */
  22. enum children {NO_CHILDREN, CHANGED_CHILDREN, ALL_CHILDREN};
  23. /* Directory attributes. */
  24. struct directory
  25. {
  26. dev_t device_number; /* device number for directory */
  27. ino_t inode_number; /* inode number for directory */
  28. enum children children;
  29. bool nfs;
  30. bool found;
  31. char name[1]; /* file name of directory */
  32. };
  33. static Hash_table *directory_table;
  34. #if HAVE_ST_FSTYPE_STRING
  35. static char const nfs_string[] = "nfs";
  36. # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
  37. #else
  38. # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
  39. # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
  40. #endif
  41. /* Calculate the hash of a directory. */
  42. static size_t
  43. hash_directory (void const *entry, size_t n_buckets)
  44. {
  45. struct directory const *directory = entry;
  46. return hash_string (directory->name, n_buckets);
  47. }
  48. /* Compare two directories for equality. */
  49. static bool
  50. compare_directories (void const *entry1, void const *entry2)
  51. {
  52. struct directory const *directory1 = entry1;
  53. struct directory const *directory2 = entry2;
  54. return strcmp (directory1->name, directory2->name) == 0;
  55. }
  56. /* Create and link a new directory entry for directory NAME, having a
  57. device number DEV and an inode number INO, with NFS indicating
  58. whether it is an NFS device and FOUND indicating whether we have
  59. found that the directory exists. */
  60. static struct directory *
  61. note_directory (char const *name, dev_t dev, ino_t ino, bool nfs, bool found)
  62. {
  63. size_t size = offsetof (struct directory, name) + strlen (name) + 1;
  64. struct directory *directory = xmalloc (size);
  65. directory->device_number = dev;
  66. directory->inode_number = ino;
  67. directory->children = CHANGED_CHILDREN;
  68. directory->nfs = nfs;
  69. directory->found = found;
  70. strcpy (directory->name, name);
  71. if (! ((directory_table
  72. || (directory_table = hash_initialize (0, 0, hash_directory,
  73. compare_directories, 0)))
  74. && hash_insert (directory_table, directory)))
  75. xalloc_die ();
  76. return directory;
  77. }
  78. /* Return a directory entry for a given file NAME, or zero if none found. */
  79. static struct directory *
  80. find_directory (char *name)
  81. {
  82. if (! directory_table)
  83. return 0;
  84. else
  85. {
  86. size_t size = offsetof (struct directory, name) + strlen (name) + 1;
  87. struct directory *dir = alloca (size);
  88. strcpy (dir->name, name);
  89. return hash_lookup (directory_table, dir);
  90. }
  91. }
  92. static int
  93. compare_dirents (const void *first, const void *second)
  94. {
  95. return strcmp ((*(char *const *) first) + 1,
  96. (*(char *const *) second) + 1);
  97. }
  98. /* Recursively scan the given directory. */
  99. static void
  100. scan_directory (struct obstack *stk, char *dir_name, dev_t device)
  101. {
  102. char *dirp = savedir (dir_name); /* for scanning directory */
  103. char const *entry; /* directory entry being scanned */
  104. size_t entrylen; /* length of directory entry */
  105. char *name_buffer; /* directory, `/', and directory member */
  106. size_t name_buffer_size; /* allocated size of name_buffer, minus 2 */
  107. size_t name_length; /* used length in name_buffer */
  108. struct directory *directory; /* for checking if already seen */
  109. enum children children;
  110. if (! dirp)
  111. {
  112. savedir_error (dir_name);
  113. }
  114. errno = 0;
  115. name_buffer_size = strlen (dir_name) + NAME_FIELD_SIZE;
  116. name_buffer = xmalloc (name_buffer_size + 2);
  117. strcpy (name_buffer, dir_name);
  118. if (! ISSLASH (dir_name[strlen (dir_name) - 1]))
  119. strcat (name_buffer, "/");
  120. name_length = strlen (name_buffer);
  121. directory = find_directory (dir_name);
  122. children = directory ? directory->children : CHANGED_CHILDREN;
  123. if (dirp && children != NO_CHILDREN)
  124. for (entry = dirp;
  125. (entrylen = strlen (entry)) != 0;
  126. entry += entrylen + 1)
  127. {
  128. if (name_buffer_size <= entrylen + name_length)
  129. {
  130. do
  131. name_buffer_size += NAME_FIELD_SIZE;
  132. while (name_buffer_size <= entrylen + name_length);
  133. name_buffer = xrealloc (name_buffer, name_buffer_size + 2);
  134. }
  135. strcpy (name_buffer + name_length, entry);
  136. if (excluded_name (name_buffer))
  137. obstack_1grow (stk, 'N');
  138. else
  139. {
  140. struct stat stat_data;
  141. if (deref_stat (dereference_option, name_buffer, &stat_data))
  142. {
  143. stat_diag (name_buffer);
  144. continue;
  145. }
  146. if (S_ISDIR (stat_data.st_mode))
  147. {
  148. bool nfs = NFS_FILE_STAT (stat_data);
  149. if ((directory = find_directory (name_buffer)) != NULL)
  150. {
  151. /* With NFS, the same file can have two different devices
  152. if an NFS directory is mounted in multiple locations,
  153. which is relatively common when automounting.
  154. To avoid spurious incremental redumping of
  155. directories, consider all NFS devices as equal,
  156. relying on the i-node to establish differences. */
  157. if (! (((directory->nfs & nfs)
  158. || directory->device_number == stat_data.st_dev)
  159. && directory->inode_number == stat_data.st_ino))
  160. {
  161. if (verbose_option)
  162. WARN ((0, 0, _("%s: Directory has been renamed"),
  163. quotearg_colon (name_buffer)));
  164. directory->children = ALL_CHILDREN;
  165. directory->nfs = nfs;
  166. directory->device_number = stat_data.st_dev;
  167. directory->inode_number = stat_data.st_ino;
  168. }
  169. directory->found = 1;
  170. }
  171. else
  172. {
  173. if (verbose_option)
  174. WARN ((0, 0, _("%s: Directory is new"),
  175. quotearg_colon (name_buffer)));
  176. directory = note_directory (name_buffer,
  177. stat_data.st_dev,
  178. stat_data.st_ino, nfs, 1);
  179. directory->children =
  180. ((listed_incremental_option
  181. || OLDER_STAT_TIME (stat_data, m)
  182. || (after_date_option
  183. && OLDER_STAT_TIME (stat_data, c)))
  184. ? ALL_CHILDREN
  185. : CHANGED_CHILDREN);
  186. }
  187. if (one_file_system_option && device != stat_data.st_dev)
  188. directory->children = NO_CHILDREN;
  189. else if (children == ALL_CHILDREN)
  190. directory->children = ALL_CHILDREN;
  191. obstack_1grow (stk, 'D');
  192. }
  193. else if (one_file_system_option && device != stat_data.st_dev)
  194. obstack_1grow (stk, 'N');
  195. #ifdef S_ISHIDDEN
  196. else if (S_ISHIDDEN (stat_data.st_mode))
  197. {
  198. obstack_1grow (stk, 'D');
  199. obstack_grow (stk, entry, entrylen);
  200. obstack_grow (stk, "A", 2);
  201. continue;
  202. }
  203. #endif
  204. else
  205. if (children == CHANGED_CHILDREN
  206. && OLDER_STAT_TIME (stat_data, m)
  207. && (!after_date_option || OLDER_STAT_TIME (stat_data, c)))
  208. obstack_1grow (stk, 'N');
  209. else
  210. obstack_1grow (stk, 'Y');
  211. }
  212. obstack_grow (stk, entry, entrylen + 1);
  213. }
  214. obstack_grow (stk, "\000\000", 2);
  215. free (name_buffer);
  216. if (dirp)
  217. free (dirp);
  218. }
  219. /* Sort the contents of the obstack, and convert it to the char * */
  220. static char *
  221. sort_obstack (struct obstack *stk)
  222. {
  223. char *pointer = obstack_finish (stk);
  224. size_t counter;
  225. char *cursor;
  226. char *buffer;
  227. char **array;
  228. char **array_cursor;
  229. counter = 0;
  230. for (cursor = pointer; *cursor; cursor += strlen (cursor) + 1)
  231. counter++;
  232. if (!counter)
  233. return NULL;
  234. array = obstack_alloc (stk, sizeof (char *) * (counter + 1));
  235. array_cursor = array;
  236. for (cursor = pointer; *cursor; cursor += strlen (cursor) + 1)
  237. *array_cursor++ = cursor;
  238. *array_cursor = 0;
  239. qsort (array, counter, sizeof (char *), compare_dirents);
  240. buffer = xmalloc (cursor - pointer + 2);
  241. cursor = buffer;
  242. for (array_cursor = array; *array_cursor; array_cursor++)
  243. {
  244. char *string = *array_cursor;
  245. while ((*cursor++ = *string++))
  246. continue;
  247. }
  248. *cursor = '\0';
  249. return buffer;
  250. }
  251. char *
  252. get_directory_contents (char *dir_name, dev_t device)
  253. {
  254. struct obstack stk;
  255. char *buffer;
  256. obstack_init (&stk);
  257. scan_directory (&stk, dir_name, device);
  258. buffer = sort_obstack (&stk);
  259. obstack_free (&stk, NULL);
  260. return buffer;
  261. }
  262. static FILE *listed_incremental_stream;
  263. void
  264. read_directory_file (void)
  265. {
  266. int fd;
  267. FILE *fp;
  268. char *buf = 0;
  269. size_t bufsize;
  270. /* Open the file for both read and write. That way, we can write
  271. it later without having to reopen it, and don't have to worry if
  272. we chdir in the meantime. */
  273. fd = open (listed_incremental_option, O_RDWR | O_CREAT, MODE_RW);
  274. if (fd < 0)
  275. {
  276. open_error (listed_incremental_option);
  277. return;
  278. }
  279. fp = fdopen (fd, "r+");
  280. if (! fp)
  281. {
  282. open_error (listed_incremental_option);
  283. close (fd);
  284. return;
  285. }
  286. listed_incremental_stream = fp;
  287. if (0 < getline (&buf, &bufsize, fp))
  288. {
  289. char *ebuf;
  290. int n;
  291. long lineno = 1;
  292. unsigned long u = (errno = 0, strtoul (buf, &ebuf, 10));
  293. time_t t = u;
  294. if (buf == ebuf || (u == 0 && errno == EINVAL))
  295. ERROR ((0, 0, "%s:1: %s", quotearg_colon (listed_incremental_option),
  296. _("Invalid time stamp")));
  297. else if (t != u || (u == -1 && errno == ERANGE))
  298. ERROR ((0, 0, "%s:1: %s", quotearg_colon (listed_incremental_option),
  299. _("Time stamp out of range")));
  300. else
  301. {
  302. newer_mtime_option.tv_sec = t;
  303. newer_mtime_option.tv_nsec = 0;
  304. }
  305. while (0 < (n = getline (&buf, &bufsize, fp)))
  306. {
  307. dev_t dev;
  308. ino_t ino;
  309. bool nfs = buf[0] == '+';
  310. char *strp = buf + nfs;
  311. lineno++;
  312. if (buf[n - 1] == '\n')
  313. buf[n - 1] = '\0';
  314. errno = 0;
  315. dev = u = strtoul (strp, &ebuf, 10);
  316. if (strp == ebuf || (u == 0 && errno == EINVAL))
  317. ERROR ((0, 0, "%s:%ld: %s",
  318. quotearg_colon (listed_incremental_option), lineno,
  319. _("Invalid device number")));
  320. else if (dev != u || (u == -1 && errno == ERANGE))
  321. ERROR ((0, 0, "%s:%ld: %s",
  322. quotearg_colon (listed_incremental_option), lineno,
  323. _("Device number out of range")));
  324. strp = ebuf;
  325. errno = 0;
  326. ino = u = strtoul (strp, &ebuf, 10);
  327. if (strp == ebuf || (u == 0 && errno == EINVAL))
  328. ERROR ((0, 0, "%s:%ld: %s",
  329. quotearg_colon (listed_incremental_option), lineno,
  330. _("Invalid inode number")));
  331. else if (ino != u || (u == -1 && errno == ERANGE))
  332. ERROR ((0, 0, "%s:%ld: %s",
  333. quotearg_colon (listed_incremental_option), lineno,
  334. _("Inode number out of range")));
  335. strp = ebuf;
  336. strp++;
  337. unquote_string (strp);
  338. note_directory (strp, dev, ino, nfs, 0);
  339. }
  340. }
  341. if (ferror (fp))
  342. read_error (listed_incremental_option);
  343. if (buf)
  344. free (buf);
  345. }
  346. /* Output incremental data for the directory ENTRY to the file DATA.
  347. Return nonzero if successful, preserving errno on write failure. */
  348. static bool
  349. write_directory_file_entry (void *entry, void *data)
  350. {
  351. struct directory const *directory = entry;
  352. FILE *fp = data;
  353. if (directory->found)
  354. {
  355. int e;
  356. char *str = quote_copy_string (directory->name);
  357. fprintf (fp, "+%lu %lu %s\n" + ! directory->nfs,
  358. (unsigned long) directory->device_number,
  359. (unsigned long) directory->inode_number,
  360. str ? str : directory->name);
  361. e = errno;
  362. if (str)
  363. free (str);
  364. errno = e;
  365. }
  366. return ! ferror (fp);
  367. }
  368. void
  369. write_directory_file (void)
  370. {
  371. FILE *fp = listed_incremental_stream;
  372. if (! fp)
  373. return;
  374. if (fseek (fp, 0L, SEEK_SET) != 0)
  375. seek_error (listed_incremental_option);
  376. if (sys_truncate (fileno (fp)) != 0)
  377. truncate_error (listed_incremental_option);
  378. fprintf (fp, "%lu\n", (unsigned long) start_time);
  379. if (! ferror (fp) && directory_table)
  380. hash_do_for_each (directory_table, write_directory_file_entry, fp);
  381. if (ferror (fp))
  382. write_error (listed_incremental_option);
  383. if (fclose (fp) != 0)
  384. close_error (listed_incremental_option);
  385. }
  386. /* Restoration of incremental dumps. */
  387. /* Examine the directories under directory_name and delete any
  388. files that were not there at the time of the back-up. */
  389. void
  390. purge_directory (char const *directory_name)
  391. {
  392. char *archive_dir;
  393. char *current_dir;
  394. char *cur, *arc;
  395. size_t size;
  396. size_t copied;
  397. union block *data_block;
  398. char *to;
  399. current_dir = savedir (directory_name);
  400. if (!current_dir)
  401. {
  402. /* The directory doesn't exist now. It'll be created. In any
  403. case, we don't have to delete any files out of it. */
  404. skip_member ();
  405. return;
  406. }
  407. size = current_stat_info.stat.st_size;
  408. if (size != current_stat_info.stat.st_size)
  409. xalloc_die ();
  410. archive_dir = xmalloc (size);
  411. to = archive_dir;
  412. for (; size > 0; size -= copied)
  413. {
  414. data_block = find_next_block ();
  415. if (!data_block)
  416. {
  417. ERROR ((0, 0, _("Unexpected EOF in archive")));
  418. break; /* FIXME: What happens then? */
  419. }
  420. copied = available_space_after (data_block);
  421. if (copied > size)
  422. copied = size;
  423. memcpy (to, data_block->buffer, copied);
  424. to += copied;
  425. set_next_block_after ((union block *)
  426. (data_block->buffer + copied - 1));
  427. }
  428. for (cur = current_dir; *cur; cur += strlen (cur) + 1)
  429. {
  430. for (arc = archive_dir; *arc; arc += strlen (arc) + 1)
  431. {
  432. arc++;
  433. if (!strcmp (arc, cur))
  434. break;
  435. }
  436. if (*arc == '\0')
  437. {
  438. struct stat st;
  439. char *p = new_name (directory_name, cur);
  440. if (deref_stat (true, p, &st))
  441. {
  442. stat_diag (p);
  443. WARN((0, 0, _("%s: Not purging directory: unable to stat"),
  444. quotearg_colon (p)));
  445. continue;
  446. }
  447. else if (one_file_system_option && st.st_dev != root_device)
  448. {
  449. WARN((0, 0,
  450. _("%s: directory is on a different device: not purging"),
  451. quotearg_colon (p)));
  452. continue;
  453. }
  454. if (! interactive_option || confirm ("delete", p))
  455. {
  456. if (verbose_option)
  457. fprintf (stdlis, _("%s: Deleting %s\n"),
  458. program_name, quote (p));
  459. if (! remove_any_file (p, RECURSIVE_REMOVE_OPTION))
  460. {
  461. int e = errno;
  462. ERROR ((0, e, _("%s: Cannot remove"), quotearg_colon (p)));
  463. }
  464. }
  465. free (p);
  466. }
  467. }
  468. free (current_dir);
  469. free (archive_dir);
  470. }