4
0

incremen.c 42 KB

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