incremen.c 14 KB

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