incremen.c 44 KB

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