incremen.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /* GNU dump extensions to tar.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007 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 <mkdtemp.h>
  19. #include <quotearg.h>
  20. #include "common.h"
  21. /* Incremental dump specialities. */
  22. /* Which child files to save under a directory. */
  23. enum children
  24. {
  25. NO_CHILDREN,
  26. CHANGED_CHILDREN,
  27. ALL_CHILDREN
  28. };
  29. #define DIRF_INIT 0x0001 /* directory structure is initialized
  30. (procdir called at least once) */
  31. #define DIRF_NFS 0x0002 /* directory is mounted on nfs */
  32. #define DIRF_FOUND 0x0004 /* directory is found on fs */
  33. #define DIRF_NEW 0x0008 /* directory is new (not found
  34. in the previous dump) */
  35. #define DIRF_RENAMED 0x0010 /* directory is renamed */
  36. #define DIR_IS_INITED(d) ((d)->flags & DIRF_INIT)
  37. #define DIR_IS_NFS(d) ((d)->flags & DIRF_NFS)
  38. #define DIR_IS_FOUND(d) ((d)->flags & DIRF_FOUND)
  39. #define DIR_IS_NEW(d) ((d)->flags & DIRF_NEW)
  40. #define DIR_IS_RENAMED(d) ((d)->flags & DIRF_RENAMED)
  41. #define DIR_SET_FLAG(d,f) (d)->flags |= (f)
  42. #define DIR_CLEAR_FLAG(d,f) (d)->flags &= ~(f)
  43. /* Directory attributes. */
  44. struct directory
  45. {
  46. struct timespec mtime; /* Modification time */
  47. dev_t device_number; /* device number for directory */
  48. ino_t inode_number; /* inode number for directory */
  49. char *contents; /* Directory contents */
  50. char *icontents; /* Initial contents if the directory was
  51. rescanned */
  52. enum children children; /* What to save under this directory */
  53. unsigned flags; /* See DIRF_ macros above */
  54. struct directory *orig; /* If the directory was renamed, points to
  55. the original directory structure */
  56. char name[1]; /* file name of directory */
  57. };
  58. static Hash_table *directory_table;
  59. static Hash_table *directory_meta_table;
  60. #if HAVE_ST_FSTYPE_STRING
  61. static char const nfs_string[] = "nfs";
  62. # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
  63. #else
  64. # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
  65. # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
  66. #endif
  67. /* Calculate the hash of a directory. */
  68. static size_t
  69. hash_directory_name (void const *entry, size_t n_buckets)
  70. {
  71. struct directory const *directory = entry;
  72. return hash_string (directory->name, n_buckets);
  73. }
  74. /* Compare two directories for equality of their names. */
  75. static bool
  76. compare_directory_names (void const *entry1, void const *entry2)
  77. {
  78. struct directory const *directory1 = entry1;
  79. struct directory const *directory2 = entry2;
  80. return strcmp (directory1->name, directory2->name) == 0;
  81. }
  82. static size_t
  83. hash_directory_meta (void const *entry, size_t n_buckets)
  84. {
  85. struct directory const *directory = entry;
  86. /* FIXME: Work out a better algorytm */
  87. return (directory->device_number + directory->inode_number) % n_buckets;
  88. }
  89. /* Compare two directories for equality of their device and inode numbers. */
  90. static bool
  91. compare_directory_meta (void const *entry1, void const *entry2)
  92. {
  93. struct directory const *directory1 = entry1;
  94. struct directory const *directory2 = entry2;
  95. return directory1->device_number == directory2->device_number
  96. && directory1->inode_number == directory2->inode_number;
  97. }
  98. /* Make a directory entry for given NAME */
  99. static struct directory *
  100. make_directory (const char *name)
  101. {
  102. size_t namelen = strlen (name);
  103. size_t size = offsetof (struct directory, name) + namelen + 1;
  104. struct directory *directory = xmalloc (size);
  105. directory->contents = directory->icontents = NULL;
  106. directory->orig = NULL;
  107. directory->flags = false;
  108. strcpy (directory->name, name);
  109. if (ISSLASH (directory->name[namelen-1]))
  110. directory->name[namelen-1] = 0;
  111. return directory;
  112. }
  113. /* Create and link a new directory entry for directory NAME, having a
  114. device number DEV and an inode number INO, with NFS indicating
  115. whether it is an NFS device and FOUND indicating whether we have
  116. found that the directory exists. */
  117. static struct directory *
  118. note_directory (char const *name, struct timespec mtime,
  119. dev_t dev, ino_t ino, bool nfs, bool found, char *contents)
  120. {
  121. struct directory *directory = make_directory (name);
  122. directory->mtime = mtime;
  123. directory->device_number = dev;
  124. directory->inode_number = ino;
  125. directory->children = CHANGED_CHILDREN;
  126. if (nfs)
  127. DIR_SET_FLAG (directory, DIRF_NFS);
  128. if (found)
  129. DIR_SET_FLAG (directory, DIRF_FOUND);
  130. if (contents)
  131. {
  132. size_t size = dumpdir_size (contents);
  133. directory->contents = xmalloc (size);
  134. memcpy (directory->contents, contents, size);
  135. }
  136. else
  137. directory->contents = NULL;
  138. if (! ((directory_table
  139. || (directory_table = hash_initialize (0, 0,
  140. hash_directory_name,
  141. compare_directory_names, 0)))
  142. && hash_insert (directory_table, directory)))
  143. xalloc_die ();
  144. if (! ((directory_meta_table
  145. || (directory_meta_table = hash_initialize (0, 0,
  146. hash_directory_meta,
  147. compare_directory_meta,
  148. 0)))
  149. && hash_insert (directory_meta_table, directory)))
  150. xalloc_die ();
  151. return directory;
  152. }
  153. /* Return a directory entry for a given file NAME, or zero if none found. */
  154. static struct directory *
  155. find_directory (const char *name)
  156. {
  157. if (! directory_table)
  158. return 0;
  159. else
  160. {
  161. struct directory *dir = make_directory (name);
  162. struct directory *ret = hash_lookup (directory_table, dir);
  163. free (dir);
  164. return ret;
  165. }
  166. }
  167. /* Return a directory entry for a given combination of device and inode
  168. numbers, or zero if none found. */
  169. static struct directory *
  170. find_directory_meta (dev_t dev, ino_t ino)
  171. {
  172. if (! directory_meta_table)
  173. return 0;
  174. else
  175. {
  176. struct directory *dir = make_directory ("");
  177. struct directory *ret;
  178. dir->device_number = dev;
  179. dir->inode_number = ino;
  180. ret = hash_lookup (directory_meta_table, dir);
  181. free (dir);
  182. return ret;
  183. }
  184. }
  185. void
  186. update_parent_directory (const char *name)
  187. {
  188. struct directory *directory;
  189. char *p;
  190. p = dir_name (name);
  191. directory = find_directory (p);
  192. if (directory)
  193. {
  194. struct stat st;
  195. if (deref_stat (dereference_option, p, &st) != 0)
  196. stat_diag (name);
  197. else
  198. directory->mtime = get_stat_mtime (&st);
  199. }
  200. free (p);
  201. }
  202. static struct directory *
  203. procdir (char *name_buffer, struct stat *stat_data,
  204. dev_t device,
  205. enum children children,
  206. bool verbose)
  207. {
  208. struct directory *directory;
  209. bool nfs = NFS_FILE_STAT (*stat_data);
  210. if ((directory = find_directory (name_buffer)) != NULL)
  211. {
  212. if (DIR_IS_INITED (directory))
  213. return directory;
  214. /* With NFS, the same file can have two different devices
  215. if an NFS directory is mounted in multiple locations,
  216. which is relatively common when automounting.
  217. To avoid spurious incremental redumping of
  218. directories, consider all NFS devices as equal,
  219. relying on the i-node to establish differences. */
  220. if (! (((DIR_IS_NFS (directory) & nfs)
  221. || directory->device_number == stat_data->st_dev)
  222. && directory->inode_number == stat_data->st_ino))
  223. {
  224. /* FIXME: find_directory_meta ignores nfs */
  225. struct directory *d = find_directory_meta (stat_data->st_dev,
  226. stat_data->st_ino);
  227. if (d)
  228. {
  229. if (verbose_option)
  230. WARN ((0, 0, _("%s: Directory has been renamed from %s"),
  231. quotearg_colon (name_buffer),
  232. quote_n (1, d->name)));
  233. directory->orig = d;
  234. DIR_SET_FLAG (directory, DIRF_RENAMED);
  235. directory->children = CHANGED_CHILDREN;
  236. }
  237. else
  238. {
  239. if (verbose_option)
  240. WARN ((0, 0, _("%s: Directory has been renamed"),
  241. quotearg_colon (name_buffer)));
  242. directory->children = ALL_CHILDREN;
  243. directory->device_number = stat_data->st_dev;
  244. directory->inode_number = stat_data->st_ino;
  245. }
  246. if (nfs)
  247. DIR_SET_FLAG (directory, DIRF_NFS);
  248. }
  249. else
  250. directory->children = CHANGED_CHILDREN;
  251. DIR_SET_FLAG (directory, DIRF_FOUND);
  252. }
  253. else
  254. {
  255. struct directory *d = find_directory_meta (stat_data->st_dev,
  256. stat_data->st_ino);
  257. directory = note_directory (name_buffer,
  258. get_stat_mtime(stat_data),
  259. stat_data->st_dev,
  260. stat_data->st_ino,
  261. nfs,
  262. true,
  263. NULL);
  264. if (d)
  265. {
  266. if (verbose)
  267. WARN ((0, 0, _("%s: Directory has been renamed from %s"),
  268. quotearg_colon (name_buffer),
  269. quote_n (1, d->name)));
  270. directory->orig = d;
  271. DIR_SET_FLAG (directory, DIRF_RENAMED);
  272. directory->children = CHANGED_CHILDREN;
  273. }
  274. else
  275. {
  276. DIR_SET_FLAG (directory, DIRF_NEW);
  277. if (verbose)
  278. WARN ((0, 0, _("%s: Directory is new"),
  279. quotearg_colon (name_buffer)));
  280. directory->children =
  281. (listed_incremental_option
  282. || (OLDER_STAT_TIME (*stat_data, m)
  283. || (after_date_option
  284. && OLDER_STAT_TIME (*stat_data, c))))
  285. ? ALL_CHILDREN
  286. : CHANGED_CHILDREN;
  287. }
  288. }
  289. /* If the directory is on another device and --one-file-system was given,
  290. omit it... */
  291. if (one_file_system_option && device != stat_data->st_dev
  292. /* ... except if it was explicitely given in the command line */
  293. && !is_individual_file (name_buffer))
  294. directory->children = NO_CHILDREN;
  295. else if (children == ALL_CHILDREN)
  296. directory->children = ALL_CHILDREN;
  297. DIR_SET_FLAG (directory, DIRF_INIT);
  298. return directory;
  299. }
  300. /* Locate NAME in the dumpdir array DUMP.
  301. Return pointer to the slot in the array, or NULL if not found */
  302. const char *
  303. dumpdir_locate (const char *dump, const char *name)
  304. {
  305. if (dump)
  306. while (*dump)
  307. {
  308. /* Ignore 'R' (rename) and 'X' (tempname) entries, since they break
  309. alphabetical ordering.
  310. They normally do not occur in dumpdirs from the snapshot files,
  311. but this function is also used by purge_directory, which operates
  312. on a dumpdir from the archive, hence the need for this test. */
  313. if (!strchr ("RX", *dump))
  314. {
  315. int rc = strcmp (dump + 1, name);
  316. if (rc == 0)
  317. return dump;
  318. if (rc > 1)
  319. break;
  320. }
  321. dump += strlen (dump) + 1;
  322. }
  323. return NULL;
  324. }
  325. /* Return size in bytes of the dumpdir array P */
  326. size_t
  327. dumpdir_size (const char *p)
  328. {
  329. size_t totsize = 0;
  330. while (*p)
  331. {
  332. size_t size = strlen (p) + 1;
  333. totsize += size;
  334. p += size;
  335. }
  336. return totsize + 1;
  337. }
  338. static int
  339. compare_dirnames (const void *first, const void *second)
  340. {
  341. char const *const *name1 = first;
  342. char const *const *name2 = second;
  343. return strcmp (*name1, *name2);
  344. }
  345. /* Compare dumpdir array from DIRECTORY with directory listing DIR and
  346. build a new dumpdir template.
  347. DIR must be returned by a previous call to savedir().
  348. File names in DIRECTORY->contents must be sorted
  349. alphabetically.
  350. DIRECTORY->contents is replaced with the created template. Each entry is
  351. prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
  352. void
  353. makedumpdir (struct directory *directory, const char *dir)
  354. {
  355. size_t i,
  356. dirsize, /* Number of elements in DIR */
  357. len; /* Length of DIR, including terminating nul */
  358. const char *p;
  359. char const **array;
  360. char *new_dump, *new_dump_ptr;
  361. const char *dump;
  362. if (directory->children == ALL_CHILDREN)
  363. dump = NULL;
  364. else if (DIR_IS_RENAMED (directory))
  365. dump = directory->orig->icontents ?
  366. directory->orig->icontents : directory->orig->contents;
  367. else
  368. dump = directory->contents;
  369. /* Count the size of DIR and the number of elements it contains */
  370. dirsize = 0;
  371. len = 0;
  372. for (p = dir; *p; p += strlen (p) + 1, dirsize++)
  373. len += strlen (p) + 2;
  374. len++;
  375. /* Create a sorted directory listing */
  376. array = xcalloc (dirsize, sizeof array[0]);
  377. for (i = 0, p = dir; *p; p += strlen (p) + 1, i++)
  378. array[i] = p;
  379. qsort (array, dirsize, sizeof (array[0]), compare_dirnames);
  380. /* Prepare space for new dumpdir */
  381. new_dump = xmalloc (len);
  382. new_dump_ptr = new_dump;
  383. /* Fill in the dumpdir template */
  384. for (i = 0; i < dirsize; i++)
  385. {
  386. const char *loc = dumpdir_locate (dump, array[i]);
  387. if (loc)
  388. {
  389. *new_dump_ptr++ = ' ';
  390. dump = loc + strlen (loc) + 1;
  391. }
  392. else
  393. *new_dump_ptr++ = 'Y'; /* New entry */
  394. /* Copy the file name */
  395. for (p = array[i]; (*new_dump_ptr++ = *p++); )
  396. ;
  397. }
  398. *new_dump_ptr = 0;
  399. directory->icontents = directory->contents;
  400. directory->contents = new_dump;
  401. free (array);
  402. }
  403. /* Recursively scan the given directory. */
  404. static char *
  405. scan_directory (char *dir_name, dev_t device)
  406. {
  407. char *dirp = savedir (dir_name); /* for scanning directory */
  408. char *name_buffer; /* directory, `/', and directory member */
  409. size_t name_buffer_size; /* allocated size of name_buffer, minus 2 */
  410. size_t name_length; /* used length in name_buffer */
  411. struct stat stat_data;
  412. struct directory *directory;
  413. if (! dirp)
  414. savedir_error (dir_name);
  415. name_buffer_size = strlen (dir_name) + NAME_FIELD_SIZE;
  416. name_buffer = xmalloc (name_buffer_size + 2);
  417. strcpy (name_buffer, dir_name);
  418. if (! ISSLASH (dir_name[strlen (dir_name) - 1]))
  419. strcat (name_buffer, "/");
  420. name_length = strlen (name_buffer);
  421. if (deref_stat (dereference_option, name_buffer, &stat_data))
  422. {
  423. stat_diag (name_buffer);
  424. /* FIXME: used to be
  425. children = CHANGED_CHILDREN;
  426. but changed to: */
  427. free (name_buffer);
  428. free (dirp);
  429. return NULL;
  430. }
  431. directory = procdir (name_buffer, &stat_data, device, NO_CHILDREN, false);
  432. if (dirp && directory->children != NO_CHILDREN)
  433. {
  434. char *entry; /* directory entry being scanned */
  435. size_t entrylen; /* length of directory entry */
  436. makedumpdir (directory, dirp);
  437. for (entry = directory->contents;
  438. (entrylen = strlen (entry)) != 0;
  439. entry += entrylen + 1)
  440. {
  441. if (name_buffer_size <= entrylen - 1 + name_length)
  442. {
  443. do
  444. name_buffer_size += NAME_FIELD_SIZE;
  445. while (name_buffer_size <= entrylen - 1 + name_length);
  446. name_buffer = xrealloc (name_buffer, name_buffer_size + 2);
  447. }
  448. strcpy (name_buffer + name_length, entry + 1);
  449. if (excluded_name (name_buffer))
  450. *entry = 'N';
  451. else
  452. {
  453. if (deref_stat (dereference_option, name_buffer, &stat_data))
  454. {
  455. stat_diag (name_buffer);
  456. *entry = 'N';
  457. continue;
  458. }
  459. if (S_ISDIR (stat_data.st_mode))
  460. {
  461. procdir (name_buffer, &stat_data, device,
  462. directory->children,
  463. verbose_option);
  464. *entry = 'D';
  465. }
  466. else if (one_file_system_option && device != stat_data.st_dev)
  467. *entry = 'N';
  468. else if (*entry == 'Y')
  469. /* New entry, skip further checks */;
  470. /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
  471. else if (OLDER_STAT_TIME (stat_data, m)
  472. && (!after_date_option
  473. || OLDER_STAT_TIME (stat_data, c)))
  474. *entry = 'N';
  475. else
  476. *entry = 'Y';
  477. }
  478. }
  479. }
  480. free (name_buffer);
  481. if (dirp)
  482. free (dirp);
  483. return directory->contents;
  484. }
  485. char *
  486. get_directory_contents (char *dir_name, dev_t device)
  487. {
  488. return scan_directory (dir_name, device);
  489. }
  490. static void
  491. obstack_code_rename (struct obstack *stk, char *from, char *to)
  492. {
  493. obstack_1grow (stk, 'R');
  494. obstack_grow (stk, from, strlen (from) + 1);
  495. obstack_1grow (stk, 'T');
  496. obstack_grow (stk, to, strlen (to) + 1);
  497. }
  498. static bool
  499. rename_handler (void *data, void *proc_data)
  500. {
  501. struct directory *dir = data;
  502. struct obstack *stk = proc_data;
  503. if (DIR_IS_RENAMED (dir))
  504. {
  505. struct directory *prev, *p;
  506. /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
  507. are ignored when hit by this function next time.
  508. If the chain forms a cycle, prev points to the entry DIR is renamed
  509. from. In this case it still retains DIRF_RENAMED flag, which will be
  510. cleared in the `else' branch below */
  511. for (prev = dir; prev && prev->orig != dir; prev = prev->orig)
  512. DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
  513. if (prev == NULL)
  514. {
  515. for (p = dir; p && p->orig; p = p->orig)
  516. obstack_code_rename (stk, p->orig->name, p->name);
  517. }
  518. else
  519. {
  520. char *temp_name;
  521. DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
  522. /* Break the cycle by using a temporary name for one of its
  523. elements.
  524. First, create a temp name stub entry. */
  525. temp_name = dir_name (dir->name);
  526. obstack_1grow (stk, 'X');
  527. obstack_grow (stk, temp_name, strlen (temp_name) + 1);
  528. obstack_code_rename (stk, dir->name, "");
  529. for (p = dir; p != prev; p = p->orig)
  530. obstack_code_rename (stk, p->orig->name, p->name);
  531. obstack_code_rename (stk, "", prev->name);
  532. }
  533. }
  534. return true;
  535. }
  536. const char *
  537. append_incremental_renames (const char *dump)
  538. {
  539. struct obstack stk;
  540. size_t size;
  541. if (directory_table == NULL)
  542. return dump;
  543. obstack_init (&stk);
  544. if (dump)
  545. {
  546. size = dumpdir_size (dump) - 1;
  547. obstack_grow (&stk, dump, size);
  548. }
  549. else
  550. size = 0;
  551. hash_do_for_each (directory_table, rename_handler, &stk);
  552. if (obstack_object_size (&stk) != size)
  553. {
  554. obstack_1grow (&stk, 0);
  555. dump = obstack_finish (&stk);
  556. }
  557. else
  558. obstack_free (&stk, NULL);
  559. return dump;
  560. }
  561. static FILE *listed_incremental_stream;
  562. /* Version of incremental format snapshots (directory files) used by this
  563. tar. Currently it is supposed to be a single decimal number. 0 means
  564. incremental snapshots as per tar version before 1.15.2.
  565. The current tar version supports incremental versions from
  566. 0 up to TAR_INCREMENTAL_VERSION, inclusive.
  567. It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
  568. #define TAR_INCREMENTAL_VERSION 2
  569. /* Read incremental snapshot formats 0 and 1 */
  570. static void
  571. read_incr_db_01 (int version, const char *initbuf)
  572. {
  573. int n;
  574. uintmax_t u;
  575. time_t sec;
  576. long int nsec;
  577. char *buf = 0;
  578. size_t bufsize;
  579. char *ebuf;
  580. long lineno = 1;
  581. if (version == 1)
  582. {
  583. if (getline (&buf, &bufsize, listed_incremental_stream) <= 0)
  584. {
  585. read_error (listed_incremental_option);
  586. free (buf);
  587. return;
  588. }
  589. ++lineno;
  590. }
  591. else
  592. {
  593. buf = strdup (initbuf);
  594. bufsize = strlen (buf) + 1;
  595. }
  596. sec = TYPE_MINIMUM (time_t);
  597. nsec = -1;
  598. errno = 0;
  599. u = strtoumax (buf, &ebuf, 10);
  600. if (!errno && TYPE_MAXIMUM (time_t) < u)
  601. errno = ERANGE;
  602. if (errno || buf == ebuf)
  603. ERROR ((0, errno, "%s:%ld: %s",
  604. quotearg_colon (listed_incremental_option),
  605. lineno,
  606. _("Invalid time stamp")));
  607. else
  608. {
  609. sec = u;
  610. if (version == 1 && *ebuf)
  611. {
  612. char const *buf_ns = ebuf + 1;
  613. errno = 0;
  614. u = strtoumax (buf_ns, &ebuf, 10);
  615. if (!errno && BILLION <= u)
  616. errno = ERANGE;
  617. if (errno || buf_ns == ebuf)
  618. {
  619. ERROR ((0, errno, "%s:%ld: %s",
  620. quotearg_colon (listed_incremental_option),
  621. lineno,
  622. _("Invalid time stamp")));
  623. sec = TYPE_MINIMUM (time_t);
  624. }
  625. else
  626. nsec = u;
  627. }
  628. else
  629. {
  630. /* pre-1 incremental format does not contain nanoseconds */
  631. nsec = 0;
  632. }
  633. }
  634. newer_mtime_option.tv_sec = sec;
  635. newer_mtime_option.tv_nsec = nsec;
  636. while (0 < (n = getline (&buf, &bufsize, listed_incremental_stream)))
  637. {
  638. dev_t dev;
  639. ino_t ino;
  640. bool nfs = buf[0] == '+';
  641. char *strp = buf + nfs;
  642. struct timespec mtime;
  643. lineno++;
  644. if (buf[n - 1] == '\n')
  645. buf[n - 1] = '\0';
  646. if (version == 1)
  647. {
  648. errno = 0;
  649. u = strtoumax (strp, &ebuf, 10);
  650. if (!errno && TYPE_MAXIMUM (time_t) < u)
  651. errno = ERANGE;
  652. if (errno || strp == ebuf || *ebuf != ' ')
  653. {
  654. ERROR ((0, errno, "%s:%ld: %s",
  655. quotearg_colon (listed_incremental_option), lineno,
  656. _("Invalid modification time (seconds)")));
  657. sec = (time_t) -1;
  658. }
  659. else
  660. sec = u;
  661. strp = ebuf;
  662. errno = 0;
  663. u = strtoumax (strp, &ebuf, 10);
  664. if (!errno && BILLION <= u)
  665. errno = ERANGE;
  666. if (errno || strp == ebuf || *ebuf != ' ')
  667. {
  668. ERROR ((0, errno, "%s:%ld: %s",
  669. quotearg_colon (listed_incremental_option), lineno,
  670. _("Invalid modification time (nanoseconds)")));
  671. nsec = -1;
  672. }
  673. else
  674. nsec = u;
  675. mtime.tv_sec = sec;
  676. mtime.tv_nsec = nsec;
  677. strp = ebuf;
  678. }
  679. else
  680. memset (&mtime, 0, sizeof mtime);
  681. errno = 0;
  682. u = strtoumax (strp, &ebuf, 10);
  683. if (!errno && TYPE_MAXIMUM (dev_t) < u)
  684. errno = ERANGE;
  685. if (errno || strp == ebuf || *ebuf != ' ')
  686. {
  687. ERROR ((0, errno, "%s:%ld: %s",
  688. quotearg_colon (listed_incremental_option), lineno,
  689. _("Invalid device number")));
  690. dev = (dev_t) -1;
  691. }
  692. else
  693. dev = u;
  694. strp = ebuf;
  695. errno = 0;
  696. u = strtoumax (strp, &ebuf, 10);
  697. if (!errno && TYPE_MAXIMUM (ino_t) < u)
  698. errno = ERANGE;
  699. if (errno || strp == ebuf || *ebuf != ' ')
  700. {
  701. ERROR ((0, errno, "%s:%ld: %s",
  702. quotearg_colon (listed_incremental_option), lineno,
  703. _("Invalid inode number")));
  704. ino = (ino_t) -1;
  705. }
  706. else
  707. ino = u;
  708. strp = ebuf;
  709. strp++;
  710. unquote_string (strp);
  711. note_directory (strp, mtime, dev, ino, nfs, false, NULL);
  712. }
  713. free (buf);
  714. }
  715. /* Read a nul-terminated string from FP and store it in STK.
  716. Store the number of bytes read (including nul terminator) in PCOUNT.
  717. Return the last character read or EOF on end of file. */
  718. static int
  719. read_obstack (FILE *fp, struct obstack *stk, size_t *pcount)
  720. {
  721. int c;
  722. size_t i;
  723. for (i = 0, c = getc (fp); c != EOF && c != 0; c = getc (fp), i++)
  724. obstack_1grow (stk, c);
  725. obstack_1grow (stk, 0);
  726. *pcount = i;
  727. return c;
  728. }
  729. /* Read from file FP a nul-terminated string and convert it to
  730. intmax_t. Return the resulting value in PVAL. Assume '-' has
  731. already been read.
  732. Throw a fatal error if the string cannot be converted or if the
  733. converted value is less than MIN_VAL. */
  734. static void
  735. read_negative_num (FILE *fp, intmax_t min_val, intmax_t *pval)
  736. {
  737. int c;
  738. size_t i;
  739. char buf[INT_BUFSIZE_BOUND (intmax_t)];
  740. char *ep;
  741. buf[0] = '-';
  742. for (i = 1; ISDIGIT (c = getc (fp)); i++)
  743. {
  744. if (i == sizeof buf - 1)
  745. FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
  746. buf[i] = c;
  747. }
  748. if (c < 0)
  749. {
  750. if (ferror (fp))
  751. FATAL_ERROR ((0, errno, _("Read error in snapshot file")));
  752. else
  753. FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
  754. }
  755. buf[i] = 0;
  756. errno = 0;
  757. *pval = strtoimax (buf, &ep, 10);
  758. if (c || errno || *pval < min_val)
  759. FATAL_ERROR ((0, errno, _("Unexpected field value in snapshot file")));
  760. }
  761. /* Read from file FP a nul-terminated string and convert it to
  762. uintmax_t. Return the resulting value in PVAL. Assume C has
  763. already been read.
  764. Throw a fatal error if the string cannot be converted or if the
  765. converted value exceeds MAX_VAL.
  766. Return the last character read or EOF on end of file. */
  767. static int
  768. read_unsigned_num (int c, FILE *fp, uintmax_t max_val, uintmax_t *pval)
  769. {
  770. size_t i;
  771. char buf[UINTMAX_STRSIZE_BOUND], *ep;
  772. for (i = 0; ISDIGIT (c); i++)
  773. {
  774. if (i == sizeof buf - 1)
  775. FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
  776. buf[i] = c;
  777. c = getc (fp);
  778. }
  779. if (c < 0)
  780. {
  781. if (ferror (fp))
  782. FATAL_ERROR ((0, errno, _("Read error in snapshot file")));
  783. else if (i == 0)
  784. return c;
  785. else
  786. FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
  787. }
  788. buf[i] = 0;
  789. errno = 0;
  790. *pval = strtoumax (buf, &ep, 10);
  791. if (c || errno || max_val < *pval)
  792. FATAL_ERROR ((0, errno, _("Unexpected field value in snapshot file")));
  793. return c;
  794. }
  795. /* Read from file FP a nul-terminated string and convert it to
  796. uintmax_t. Return the resulting value in PVAL.
  797. Throw a fatal error if the string cannot be converted or if the
  798. converted value exceeds MAX_VAL.
  799. Return the last character read or EOF on end of file. */
  800. static int
  801. read_num (FILE *fp, uintmax_t max_val, uintmax_t *pval)
  802. {
  803. return read_unsigned_num (getc (fp), fp, max_val, pval);
  804. }
  805. /* Read from FP two NUL-terminated strings representing a struct
  806. timespec. Return the resulting value in PVAL.
  807. Throw a fatal error if the string cannot be converted. */
  808. static void
  809. read_timespec (FILE *fp, struct timespec *pval)
  810. {
  811. int c = getc (fp);
  812. intmax_t i;
  813. uintmax_t u;
  814. if (c == '-')
  815. {
  816. read_negative_num (fp, TYPE_MINIMUM (time_t), &i);
  817. c = 0;
  818. pval->tv_sec = i;
  819. }
  820. else
  821. {
  822. c = read_unsigned_num (c, fp, TYPE_MAXIMUM (time_t), &u);
  823. pval->tv_sec = u;
  824. }
  825. if (c || read_num (fp, BILLION - 1, &u))
  826. FATAL_ERROR ((0, 0, "%s: %s",
  827. quotearg_colon (listed_incremental_option),
  828. _("Unexpected EOF in snapshot file")));
  829. pval->tv_nsec = u;
  830. }
  831. /* Read incremental snapshot format 2 */
  832. static void
  833. read_incr_db_2 ()
  834. {
  835. uintmax_t u;
  836. struct obstack stk;
  837. obstack_init (&stk);
  838. read_timespec (listed_incremental_stream, &newer_mtime_option);
  839. for (;;)
  840. {
  841. struct timespec mtime;
  842. dev_t dev;
  843. ino_t ino;
  844. bool nfs;
  845. char *name;
  846. char *content;
  847. size_t s;
  848. if (read_num (listed_incremental_stream, 1, &u))
  849. return; /* Normal return */
  850. nfs = u;
  851. read_timespec (listed_incremental_stream, &mtime);
  852. if (read_num (listed_incremental_stream, TYPE_MAXIMUM (dev_t), &u))
  853. break;
  854. dev = u;
  855. if (read_num (listed_incremental_stream, TYPE_MAXIMUM (ino_t), &u))
  856. break;
  857. ino = u;
  858. if (read_obstack (listed_incremental_stream, &stk, &s))
  859. break;
  860. name = obstack_finish (&stk);
  861. while (read_obstack (listed_incremental_stream, &stk, &s) == 0 && s > 1)
  862. ;
  863. if (getc (listed_incremental_stream) != 0)
  864. FATAL_ERROR ((0, 0, "%s: %s",
  865. quotearg_colon (listed_incremental_option),
  866. _("Missing record terminator")));
  867. content = obstack_finish (&stk);
  868. note_directory (name, mtime, dev, ino, nfs, false, content);
  869. obstack_free (&stk, content);
  870. }
  871. FATAL_ERROR ((0, 0, "%s: %s",
  872. quotearg_colon (listed_incremental_option),
  873. _("Unexpected EOF in snapshot file")));
  874. }
  875. /* Read incremental snapshot file (directory file).
  876. If the file has older incremental version, make sure that it is processed
  877. correctly and that tar will use the most conservative backup method among
  878. possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
  879. etc.) This ensures that the snapshots are updated to the recent version
  880. without any loss of data. */
  881. void
  882. read_directory_file (void)
  883. {
  884. int fd;
  885. char *buf = 0;
  886. size_t bufsize;
  887. /* Open the file for both read and write. That way, we can write
  888. it later without having to reopen it, and don't have to worry if
  889. we chdir in the meantime. */
  890. fd = open (listed_incremental_option, O_RDWR | O_CREAT, MODE_RW);
  891. if (fd < 0)
  892. {
  893. open_error (listed_incremental_option);
  894. return;
  895. }
  896. listed_incremental_stream = fdopen (fd, "r+");
  897. if (! listed_incremental_stream)
  898. {
  899. open_error (listed_incremental_option);
  900. close (fd);
  901. return;
  902. }
  903. if (0 < getline (&buf, &bufsize, listed_incremental_stream))
  904. {
  905. char *ebuf;
  906. uintmax_t incremental_version;
  907. if (strncmp (buf, PACKAGE_NAME, sizeof PACKAGE_NAME - 1) == 0)
  908. {
  909. ebuf = buf + sizeof PACKAGE_NAME - 1;
  910. if (*ebuf++ != '-')
  911. ERROR((1, 0, _("Bad incremental file format")));
  912. for (; *ebuf != '-'; ebuf++)
  913. if (!*ebuf)
  914. ERROR((1, 0, _("Bad incremental file format")));
  915. incremental_version = strtoumax (ebuf + 1, NULL, 10);
  916. }
  917. else
  918. incremental_version = 0;
  919. switch (incremental_version)
  920. {
  921. case 0:
  922. case 1:
  923. read_incr_db_01 (incremental_version, buf);
  924. break;
  925. case TAR_INCREMENTAL_VERSION:
  926. read_incr_db_2 ();
  927. break;
  928. default:
  929. ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX),
  930. incremental_version));
  931. }
  932. }
  933. if (ferror (listed_incremental_stream))
  934. read_error (listed_incremental_option);
  935. if (buf)
  936. free (buf);
  937. }
  938. /* Output incremental data for the directory ENTRY to the file DATA.
  939. Return nonzero if successful, preserving errno on write failure. */
  940. static bool
  941. write_directory_file_entry (void *entry, void *data)
  942. {
  943. struct directory const *directory = entry;
  944. FILE *fp = data;
  945. if (DIR_IS_FOUND (directory))
  946. {
  947. char buf[UINTMAX_STRSIZE_BOUND];
  948. char *s;
  949. s = DIR_IS_NFS (directory) ? "1" : "0";
  950. fwrite (s, 2, 1, fp);
  951. s = (TYPE_SIGNED (time_t)
  952. ? imaxtostr (directory->mtime.tv_sec, buf)
  953. : umaxtostr (directory->mtime.tv_sec, buf));
  954. fwrite (s, strlen (s) + 1, 1, fp);
  955. s = umaxtostr (directory->mtime.tv_nsec, buf);
  956. fwrite (s, strlen (s) + 1, 1, fp);
  957. s = umaxtostr (directory->device_number, buf);
  958. fwrite (s, strlen (s) + 1, 1, fp);
  959. s = umaxtostr (directory->inode_number, buf);
  960. fwrite (s, strlen (s) + 1, 1, fp);
  961. fwrite (directory->name, strlen (directory->name) + 1, 1, fp);
  962. if (directory->contents)
  963. {
  964. char *p;
  965. for (p = directory->contents; *p; p += strlen (p) + 1)
  966. {
  967. if (strchr ("YND", *p))
  968. fwrite (p, strlen (p) + 1, 1, fp);
  969. }
  970. }
  971. fwrite ("\0\0", 2, 1, fp);
  972. }
  973. return ! ferror (fp);
  974. }
  975. void
  976. write_directory_file (void)
  977. {
  978. FILE *fp = listed_incremental_stream;
  979. char buf[UINTMAX_STRSIZE_BOUND];
  980. char *s;
  981. if (! fp)
  982. return;
  983. if (fseek (fp, 0L, SEEK_SET) != 0)
  984. seek_error (listed_incremental_option);
  985. if (sys_truncate (fileno (fp)) != 0)
  986. truncate_error (listed_incremental_option);
  987. fprintf (fp, "%s-%s-%d\n", PACKAGE_NAME, PACKAGE_VERSION,
  988. TAR_INCREMENTAL_VERSION);
  989. s = (TYPE_SIGNED (time_t)
  990. ? imaxtostr (start_time.tv_sec, buf)
  991. : umaxtostr (start_time.tv_sec, buf));
  992. fwrite (s, strlen (s) + 1, 1, fp);
  993. s = umaxtostr (start_time.tv_nsec, buf);
  994. fwrite (s, strlen (s) + 1, 1, fp);
  995. if (! ferror (fp) && directory_table)
  996. hash_do_for_each (directory_table, write_directory_file_entry, fp);
  997. if (ferror (fp))
  998. write_error (listed_incremental_option);
  999. if (fclose (fp) != 0)
  1000. close_error (listed_incremental_option);
  1001. }
  1002. /* Restoration of incremental dumps. */
  1003. static void
  1004. get_gnu_dumpdir (struct tar_stat_info *stat_info)
  1005. {
  1006. size_t size;
  1007. size_t copied;
  1008. union block *data_block;
  1009. char *to;
  1010. char *archive_dir;
  1011. size = stat_info->stat.st_size;
  1012. archive_dir = xmalloc (size);
  1013. to = archive_dir;
  1014. set_next_block_after (current_header);
  1015. mv_begin (stat_info);
  1016. for (; size > 0; size -= copied)
  1017. {
  1018. mv_size_left (size);
  1019. data_block = find_next_block ();
  1020. if (!data_block)
  1021. ERROR ((1, 0, _("Unexpected EOF in archive")));
  1022. copied = available_space_after (data_block);
  1023. if (copied > size)
  1024. copied = size;
  1025. memcpy (to, data_block->buffer, copied);
  1026. to += copied;
  1027. set_next_block_after ((union block *)
  1028. (data_block->buffer + copied - 1));
  1029. }
  1030. mv_end ();
  1031. stat_info->dumpdir = archive_dir;
  1032. stat_info->skipped = true; /* For skip_member() and friends
  1033. to work correctly */
  1034. }
  1035. /* Return T if STAT_INFO represents a dumpdir archive member.
  1036. Note: can invalidate current_header. It happens if flush_archive()
  1037. gets called within get_gnu_dumpdir() */
  1038. bool
  1039. is_dumpdir (struct tar_stat_info *stat_info)
  1040. {
  1041. if (stat_info->is_dumpdir && !stat_info->dumpdir)
  1042. get_gnu_dumpdir (stat_info);
  1043. return stat_info->is_dumpdir;
  1044. }
  1045. static bool
  1046. dumpdir_ok (char *dumpdir)
  1047. {
  1048. char *p;
  1049. int has_tempdir = 0;
  1050. int expect = 0;
  1051. for (p = dumpdir; *p; p += strlen (p) + 1)
  1052. {
  1053. if (expect && *p != expect)
  1054. {
  1055. ERROR ((0, 0,
  1056. _("Malformed dumpdir: expected '%c' but found %#3o"),
  1057. expect, *p));
  1058. return false;
  1059. }
  1060. switch (*p)
  1061. {
  1062. case 'X':
  1063. if (has_tempdir)
  1064. {
  1065. ERROR ((0, 0,
  1066. _("Malformed dumpdir: 'X' duplicated")));
  1067. return false;
  1068. }
  1069. else
  1070. has_tempdir = 1;
  1071. break;
  1072. case 'R':
  1073. if (p[1] == 0)
  1074. {
  1075. if (!has_tempdir)
  1076. {
  1077. ERROR ((0, 0,
  1078. _("Malformed dumpdir: empty name in 'R'")));
  1079. return false;
  1080. }
  1081. else
  1082. has_tempdir = 0;
  1083. }
  1084. expect = 'T';
  1085. break;
  1086. case 'T':
  1087. if (expect != 'T')
  1088. {
  1089. ERROR ((0, 0,
  1090. _("Malformed dumpdir: 'T' not preceeded by 'R'")));
  1091. return false;
  1092. }
  1093. if (p[1] == 0 && !has_tempdir)
  1094. {
  1095. ERROR ((0, 0,
  1096. _("Malformed dumpdir: empty name in 'T'")));
  1097. return false;
  1098. }
  1099. expect = 0;
  1100. break;
  1101. case 'N':
  1102. case 'Y':
  1103. case 'D':
  1104. break;
  1105. default:
  1106. /* FIXME: bail out? */
  1107. break;
  1108. }
  1109. }
  1110. if (expect)
  1111. {
  1112. ERROR ((0, 0,
  1113. _("Malformed dumpdir: expected '%c' but found end of data"),
  1114. expect));
  1115. return false;
  1116. }
  1117. if (has_tempdir)
  1118. WARN ((0, 0, _("Malformed dumpdir: 'X' never used")));
  1119. return true;
  1120. }
  1121. /* Examine the directories under directory_name and delete any
  1122. files that were not there at the time of the back-up. */
  1123. static bool
  1124. try_purge_directory (char const *directory_name)
  1125. {
  1126. char *current_dir;
  1127. char *cur, *arc, *p;
  1128. char *temp_stub = NULL;
  1129. if (!is_dumpdir (&current_stat_info))
  1130. return false;
  1131. current_dir = savedir (directory_name);
  1132. if (!current_dir)
  1133. /* The directory doesn't exist now. It'll be created. In any
  1134. case, we don't have to delete any files out of it. */
  1135. return false;
  1136. /* Verify if dump directory is sane */
  1137. if (!dumpdir_ok (current_stat_info.dumpdir))
  1138. return false;
  1139. /* Process renames */
  1140. for (arc = current_stat_info.dumpdir; *arc; arc += strlen (arc) + 1)
  1141. {
  1142. if (*arc == 'X')
  1143. {
  1144. #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
  1145. size_t len = strlen (arc + 1);
  1146. temp_stub = xrealloc (temp_stub, len + 1 + sizeof TEMP_DIR_TEMPLATE);
  1147. memcpy (temp_stub, arc + 1, len);
  1148. temp_stub[len] = '/';
  1149. memcpy (temp_stub + len + 1, TEMP_DIR_TEMPLATE,
  1150. sizeof TEMP_DIR_TEMPLATE);
  1151. if (!mkdtemp (temp_stub))
  1152. {
  1153. ERROR ((0, errno,
  1154. _("Cannot create temporary directory using template %s"),
  1155. quote (temp_stub)));
  1156. free (temp_stub);
  1157. free (current_dir);
  1158. return false;
  1159. }
  1160. }
  1161. else if (*arc == 'R')
  1162. {
  1163. char *src, *dst;
  1164. src = arc + 1;
  1165. arc += strlen (arc) + 1;
  1166. dst = arc + 1;
  1167. if (*src == 0)
  1168. src = temp_stub;
  1169. else if (*dst == 0)
  1170. dst = temp_stub;
  1171. if (!rename_directory (src, dst))
  1172. {
  1173. free (temp_stub);
  1174. free (current_dir);
  1175. /* FIXME: Make sure purge_directory(dst) will return
  1176. immediately */
  1177. return false;
  1178. }
  1179. }
  1180. }
  1181. free (temp_stub);
  1182. /* Process deletes */
  1183. p = NULL;
  1184. for (cur = current_dir; *cur; cur += strlen (cur) + 1)
  1185. {
  1186. const char *entry;
  1187. struct stat st;
  1188. if (p)
  1189. free (p);
  1190. p = new_name (directory_name, cur);
  1191. if (deref_stat (false, p, &st))
  1192. {
  1193. if (errno != ENOENT) /* FIXME: Maybe keep a list of renamed
  1194. dirs and check it here? */
  1195. {
  1196. stat_diag (p);
  1197. WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
  1198. quotearg_colon (p)));
  1199. }
  1200. continue;
  1201. }
  1202. if (!(entry = dumpdir_locate (current_stat_info.dumpdir, cur))
  1203. || (*entry == 'D' && !S_ISDIR (st.st_mode))
  1204. || (*entry == 'Y' && S_ISDIR (st.st_mode)))
  1205. {
  1206. if (one_file_system_option && st.st_dev != root_device)
  1207. {
  1208. WARN ((0, 0,
  1209. _("%s: directory is on a different device: not purging"),
  1210. quotearg_colon (p)));
  1211. continue;
  1212. }
  1213. if (! interactive_option || confirm ("delete", p))
  1214. {
  1215. if (verbose_option)
  1216. fprintf (stdlis, _("%s: Deleting %s\n"),
  1217. program_name, quote (p));
  1218. if (! remove_any_file (p, RECURSIVE_REMOVE_OPTION))
  1219. {
  1220. int e = errno;
  1221. ERROR ((0, e, _("%s: Cannot remove"), quotearg_colon (p)));
  1222. }
  1223. }
  1224. }
  1225. }
  1226. free (p);
  1227. free (current_dir);
  1228. return true;
  1229. }
  1230. void
  1231. purge_directory (char const *directory_name)
  1232. {
  1233. if (!try_purge_directory (directory_name))
  1234. skip_member ();
  1235. }
  1236. void
  1237. list_dumpdir (char *buffer, size_t size)
  1238. {
  1239. int state = 0;
  1240. while (size)
  1241. {
  1242. switch (*buffer)
  1243. {
  1244. case 'Y':
  1245. case 'N':
  1246. case 'D':
  1247. case 'R':
  1248. case 'T':
  1249. case 'X':
  1250. fprintf (stdlis, "%c", *buffer);
  1251. if (state == 0)
  1252. {
  1253. fprintf (stdlis, " ");
  1254. state = 1;
  1255. }
  1256. buffer++;
  1257. size--;
  1258. break;
  1259. case 0:
  1260. fputc ('\n', stdlis);
  1261. buffer++;
  1262. size--;
  1263. state = 0;
  1264. break;
  1265. default:
  1266. fputc (*buffer, stdlis);
  1267. buffer++;
  1268. size--;
  1269. }
  1270. }
  1271. }