4
0

incremen.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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. WARNOPT (WARN_RENAME_DIRECTORY,
  395. (0, 0,
  396. _("%s: Directory has been renamed from %s"),
  397. quotearg_colon (name_buffer),
  398. quote_n (1, d->name)));
  399. directory->orig = d;
  400. DIR_SET_FLAG (directory, DIRF_RENAMED);
  401. dirlist_replace_prefix (d->name, name_buffer);
  402. }
  403. directory->children = CHANGED_CHILDREN;
  404. }
  405. else
  406. {
  407. if (verbose_option)
  408. WARNOPT (WARN_RENAME_DIRECTORY,
  409. (0, 0, _("%s: Directory has been renamed"),
  410. quotearg_colon (name_buffer)));
  411. directory->children = ALL_CHILDREN;
  412. directory->device_number = stat_data->st_dev;
  413. directory->inode_number = stat_data->st_ino;
  414. }
  415. if (nfs)
  416. DIR_SET_FLAG (directory, DIRF_NFS);
  417. }
  418. else
  419. directory->children = CHANGED_CHILDREN;
  420. DIR_SET_FLAG (directory, DIRF_FOUND);
  421. }
  422. else
  423. {
  424. struct directory *d = find_directory_meta (stat_data->st_dev,
  425. stat_data->st_ino);
  426. directory = note_directory (name_buffer,
  427. get_stat_mtime(stat_data),
  428. stat_data->st_dev,
  429. stat_data->st_ino,
  430. nfs,
  431. true,
  432. NULL);
  433. if (d)
  434. {
  435. if (strcmp (d->name, name_buffer))
  436. {
  437. if (flag & PD_VERBOSE)
  438. WARNOPT (WARN_RENAME_DIRECTORY,
  439. (0, 0, _("%s: Directory has been renamed from %s"),
  440. quotearg_colon (name_buffer),
  441. quote_n (1, d->name)));
  442. directory->orig = d;
  443. DIR_SET_FLAG (directory, DIRF_RENAMED);
  444. dirlist_replace_prefix (d->name, name_buffer);
  445. }
  446. directory->children = CHANGED_CHILDREN;
  447. }
  448. else
  449. {
  450. DIR_SET_FLAG (directory, DIRF_NEW);
  451. if (flag & PD_VERBOSE)
  452. WARNOPT (WARN_NEW_DIRECTORY,
  453. (0, 0, _("%s: Directory is new"),
  454. quotearg_colon (name_buffer)));
  455. directory->children =
  456. (listed_incremental_option
  457. || (OLDER_STAT_TIME (*stat_data, m)
  458. || (after_date_option
  459. && OLDER_STAT_TIME (*stat_data, c))))
  460. ? ALL_CHILDREN
  461. : CHANGED_CHILDREN;
  462. }
  463. }
  464. /* If the directory is on another device and --one-file-system was given,
  465. omit it... */
  466. if (one_file_system_option && device != stat_data->st_dev
  467. /* ... except if it was explicitely given in the command line */
  468. && !is_individual_file (name_buffer))
  469. directory->children = NO_CHILDREN;
  470. else if (flag & PD_FORCE_CHILDREN)
  471. {
  472. directory->children = PD_CHILDREN(flag);
  473. if (directory->children == NO_CHILDREN)
  474. *entry = 'N';
  475. }
  476. DIR_SET_FLAG (directory, DIRF_INIT);
  477. if (directory->children != NO_CHILDREN)
  478. {
  479. const char *tag_file_name;
  480. switch (check_exclusion_tags (name_buffer, &tag_file_name))
  481. {
  482. case exclusion_tag_all:
  483. /* This warning can be duplicated by code in dump_file0, but only
  484. in case when the topmost directory being archived contains
  485. an exclusion tag. */
  486. exclusion_tag_warning (name_buffer, tag_file_name,
  487. _("directory not dumped"));
  488. if (entry)
  489. *entry = 'N';
  490. directory->children = NO_CHILDREN;
  491. break;
  492. case exclusion_tag_contents:
  493. exclusion_tag_warning (name_buffer, tag_file_name,
  494. _("contents not dumped"));
  495. directory->children = NO_CHILDREN;
  496. break;
  497. case exclusion_tag_under:
  498. exclusion_tag_warning (name_buffer, tag_file_name,
  499. _("contents not dumped"));
  500. directory->tagfile = tag_file_name;
  501. break;
  502. case exclusion_tag_none:
  503. break;
  504. }
  505. }
  506. return directory;
  507. }
  508. /* Compare dumpdir array from DIRECTORY with directory listing DIR and
  509. build a new dumpdir template.
  510. DIR must be returned by a previous call to savedir().
  511. File names in DIRECTORY->dump->contents must be sorted
  512. alphabetically.
  513. DIRECTORY->dump is replaced with the created template. Each entry is
  514. prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
  515. void
  516. makedumpdir (struct directory *directory, const char *dir)
  517. {
  518. size_t i,
  519. dirsize, /* Number of elements in DIR */
  520. len; /* Length of DIR, including terminating nul */
  521. const char *p;
  522. char const **array;
  523. char *new_dump, *new_dump_ptr;
  524. struct dumpdir *dump;
  525. if (directory->children == ALL_CHILDREN)
  526. dump = NULL;
  527. else if (DIR_IS_RENAMED (directory))
  528. dump = directory->orig->idump ?
  529. directory->orig->idump : directory->orig->dump;
  530. else
  531. dump = directory->dump;
  532. /* Count the size of DIR and the number of elements it contains */
  533. dirsize = 0;
  534. len = 0;
  535. for (p = dir; *p; p += strlen (p) + 1, dirsize++)
  536. len += strlen (p) + 2;
  537. len++;
  538. /* Create a sorted directory listing */
  539. array = xcalloc (dirsize, sizeof array[0]);
  540. for (i = 0, p = dir; *p; p += strlen (p) + 1, i++)
  541. array[i] = p;
  542. qsort (array, dirsize, sizeof (array[0]), compare_dirnames);
  543. /* Prepare space for new dumpdir */
  544. new_dump = xmalloc (len);
  545. new_dump_ptr = new_dump;
  546. /* Fill in the dumpdir template */
  547. for (i = 0; i < dirsize; i++)
  548. {
  549. const char *loc = dumpdir_locate (dump, array[i]);
  550. if (loc)
  551. {
  552. if (directory->tagfile)
  553. *new_dump_ptr = strcmp (directory->tagfile, array[i]) == 0 ?
  554. ' ' : 'I';
  555. else
  556. *new_dump_ptr = ' ';
  557. new_dump_ptr++;
  558. }
  559. else if (directory->tagfile)
  560. *new_dump_ptr++ = strcmp (directory->tagfile, array[i]) == 0 ?
  561. ' ' : 'I';
  562. else
  563. *new_dump_ptr++ = 'Y'; /* New entry */
  564. /* Copy the file name */
  565. for (p = array[i]; (*new_dump_ptr++ = *p++); )
  566. ;
  567. }
  568. *new_dump_ptr = 0;
  569. directory->idump = directory->dump;
  570. directory->dump = dumpdir_create0 (new_dump, NULL);
  571. free (array);
  572. }
  573. /* Recursively scan the given directory. */
  574. static const char *
  575. scan_directory (char *dir, dev_t device)
  576. {
  577. char *dirp = savedir (dir); /* for scanning directory */
  578. char *name_buffer; /* directory, `/', and directory member */
  579. size_t name_buffer_size; /* allocated size of name_buffer, minus 2 */
  580. size_t name_length; /* used length in name_buffer */
  581. struct stat stat_data;
  582. struct directory *directory;
  583. if (! dirp)
  584. savedir_error (dir);
  585. name_buffer_size = strlen (dir) + NAME_FIELD_SIZE;
  586. name_buffer = xmalloc (name_buffer_size + 2);
  587. strcpy (name_buffer, dir);
  588. if (! ISSLASH (dir[strlen (dir) - 1]))
  589. strcat (name_buffer, "/");
  590. name_length = strlen (name_buffer);
  591. if (deref_stat (dereference_option, name_buffer, &stat_data))
  592. {
  593. stat_diag (name_buffer);
  594. /* FIXME: used to be
  595. children = CHANGED_CHILDREN;
  596. but changed to: */
  597. free (name_buffer);
  598. free (dirp);
  599. return NULL;
  600. }
  601. directory = procdir (name_buffer, &stat_data, device, 0, NULL);
  602. if (dirp && directory->children != NO_CHILDREN)
  603. {
  604. char *entry; /* directory entry being scanned */
  605. size_t entrylen; /* length of directory entry */
  606. dumpdir_iter_t itr;
  607. makedumpdir (directory, dirp);
  608. for (entry = dumpdir_first (directory->dump, 1, &itr);
  609. entry;
  610. entry = dumpdir_next (itr))
  611. {
  612. entrylen = strlen (entry);
  613. if (name_buffer_size <= entrylen - 1 + name_length)
  614. {
  615. do
  616. name_buffer_size += NAME_FIELD_SIZE;
  617. while (name_buffer_size <= entrylen - 1 + name_length);
  618. name_buffer = xrealloc (name_buffer, name_buffer_size + 2);
  619. }
  620. strcpy (name_buffer + name_length, entry + 1);
  621. if (*entry == 'I') /* Ignored entry */
  622. *entry = 'N';
  623. else if (excluded_name (name_buffer))
  624. *entry = 'N';
  625. else
  626. {
  627. if (deref_stat (dereference_option, name_buffer, &stat_data))
  628. {
  629. stat_diag (name_buffer);
  630. *entry = 'N';
  631. continue;
  632. }
  633. if (S_ISDIR (stat_data.st_mode))
  634. {
  635. int pd_flag = (verbose_option ? PD_VERBOSE : 0);
  636. if (!recursion_option)
  637. pd_flag |= PD_FORCE_CHILDREN | NO_CHILDREN;
  638. else if (directory->children == ALL_CHILDREN)
  639. pd_flag |= PD_FORCE_CHILDREN | ALL_CHILDREN;
  640. *entry = 'D';
  641. procdir (name_buffer, &stat_data, device, pd_flag, entry);
  642. }
  643. else if (one_file_system_option && device != stat_data.st_dev)
  644. *entry = 'N';
  645. else if (*entry == 'Y')
  646. /* New entry, skip further checks */;
  647. /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
  648. else if (OLDER_STAT_TIME (stat_data, m)
  649. && (!after_date_option
  650. || OLDER_STAT_TIME (stat_data, c)))
  651. *entry = 'N';
  652. else
  653. *entry = 'Y';
  654. }
  655. }
  656. free (itr);
  657. }
  658. free (name_buffer);
  659. if (dirp)
  660. free (dirp);
  661. return directory->dump ? directory->dump->contents : NULL;
  662. }
  663. const char *
  664. get_directory_contents (char *dir, dev_t device)
  665. {
  666. return scan_directory (dir, device);
  667. }
  668. static void
  669. obstack_code_rename (struct obstack *stk, char *from, char *to)
  670. {
  671. char *s;
  672. s = from[0] == 0 ? from :
  673. safer_name_suffix (from, false, absolute_names_option);
  674. obstack_1grow (stk, 'R');
  675. obstack_grow (stk, s, strlen (s) + 1);
  676. s = to[0] == 0 ? to:
  677. safer_name_suffix (to, false, absolute_names_option);
  678. obstack_1grow (stk, 'T');
  679. obstack_grow (stk, s, strlen (s) + 1);
  680. }
  681. static void
  682. store_rename (struct directory *dir, struct obstack *stk)
  683. {
  684. if (DIR_IS_RENAMED (dir))
  685. {
  686. struct directory *prev, *p;
  687. /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
  688. are ignored when hit by this function next time.
  689. If the chain forms a cycle, prev points to the entry DIR is renamed
  690. from. In this case it still retains DIRF_RENAMED flag, which will be
  691. cleared in the `else' branch below */
  692. for (prev = dir; prev && prev->orig != dir; prev = prev->orig)
  693. DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
  694. if (prev == NULL)
  695. {
  696. for (p = dir; p && p->orig; p = p->orig)
  697. obstack_code_rename (stk, p->orig->name, p->name);
  698. }
  699. else
  700. {
  701. char *temp_name;
  702. DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
  703. /* Break the cycle by using a temporary name for one of its
  704. elements.
  705. First, create a temp name stub entry. */
  706. temp_name = dir_name (dir->name);
  707. obstack_1grow (stk, 'X');
  708. obstack_grow (stk, temp_name, strlen (temp_name) + 1);
  709. obstack_code_rename (stk, dir->name, "");
  710. for (p = dir; p != prev; p = p->orig)
  711. obstack_code_rename (stk, p->orig->name, p->name);
  712. obstack_code_rename (stk, "", prev->name);
  713. }
  714. }
  715. }
  716. const char *
  717. append_incremental_renames (const char *dump)
  718. {
  719. struct obstack stk;
  720. size_t size;
  721. struct directory *dp;
  722. if (dirhead == NULL)
  723. return dump;
  724. obstack_init (&stk);
  725. if (dump)
  726. {
  727. size = dumpdir_size (dump) - 1;
  728. obstack_grow (&stk, dump, size);
  729. }
  730. else
  731. size = 0;
  732. for (dp = dirhead; dp; dp = dp->next)
  733. store_rename (dp, &stk);
  734. if (obstack_object_size (&stk) != size)
  735. {
  736. obstack_1grow (&stk, 0);
  737. dump = obstack_finish (&stk);
  738. }
  739. else
  740. obstack_free (&stk, NULL);
  741. return dump;
  742. }
  743. static FILE *listed_incremental_stream;
  744. /* Version of incremental format snapshots (directory files) used by this
  745. tar. Currently it is supposed to be a single decimal number. 0 means
  746. incremental snapshots as per tar version before 1.15.2.
  747. The current tar version supports incremental versions from
  748. 0 up to TAR_INCREMENTAL_VERSION, inclusive.
  749. It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
  750. #define TAR_INCREMENTAL_VERSION 2
  751. /* Read incremental snapshot formats 0 and 1 */
  752. static void
  753. read_incr_db_01 (int version, const char *initbuf)
  754. {
  755. int n;
  756. uintmax_t u;
  757. time_t sec;
  758. long int nsec;
  759. char *buf = 0;
  760. size_t bufsize;
  761. char *ebuf;
  762. long lineno = 1;
  763. if (version == 1)
  764. {
  765. if (getline (&buf, &bufsize, listed_incremental_stream) <= 0)
  766. {
  767. read_error (listed_incremental_option);
  768. free (buf);
  769. return;
  770. }
  771. ++lineno;
  772. }
  773. else
  774. {
  775. buf = strdup (initbuf);
  776. bufsize = strlen (buf) + 1;
  777. }
  778. sec = TYPE_MINIMUM (time_t);
  779. nsec = -1;
  780. errno = 0;
  781. u = strtoumax (buf, &ebuf, 10);
  782. if (!errno && TYPE_MAXIMUM (time_t) < u)
  783. errno = ERANGE;
  784. if (errno || buf == ebuf)
  785. ERROR ((0, errno, "%s:%ld: %s",
  786. quotearg_colon (listed_incremental_option),
  787. lineno,
  788. _("Invalid time stamp")));
  789. else
  790. {
  791. sec = u;
  792. if (version == 1 && *ebuf)
  793. {
  794. char const *buf_ns = ebuf + 1;
  795. errno = 0;
  796. u = strtoumax (buf_ns, &ebuf, 10);
  797. if (!errno && BILLION <= u)
  798. errno = ERANGE;
  799. if (errno || buf_ns == ebuf)
  800. {
  801. ERROR ((0, errno, "%s:%ld: %s",
  802. quotearg_colon (listed_incremental_option),
  803. lineno,
  804. _("Invalid time stamp")));
  805. sec = TYPE_MINIMUM (time_t);
  806. }
  807. else
  808. nsec = u;
  809. }
  810. else
  811. {
  812. /* pre-1 incremental format does not contain nanoseconds */
  813. nsec = 0;
  814. }
  815. }
  816. newer_mtime_option.tv_sec = sec;
  817. newer_mtime_option.tv_nsec = nsec;
  818. while (0 < (n = getline (&buf, &bufsize, listed_incremental_stream)))
  819. {
  820. dev_t dev;
  821. ino_t ino;
  822. bool nfs = buf[0] == '+';
  823. char *strp = buf + nfs;
  824. struct timespec mtime;
  825. lineno++;
  826. if (buf[n - 1] == '\n')
  827. buf[n - 1] = '\0';
  828. if (version == 1)
  829. {
  830. errno = 0;
  831. u = strtoumax (strp, &ebuf, 10);
  832. if (!errno && TYPE_MAXIMUM (time_t) < u)
  833. errno = ERANGE;
  834. if (errno || strp == ebuf || *ebuf != ' ')
  835. {
  836. ERROR ((0, errno, "%s:%ld: %s",
  837. quotearg_colon (listed_incremental_option), lineno,
  838. _("Invalid modification time (seconds)")));
  839. sec = (time_t) -1;
  840. }
  841. else
  842. sec = u;
  843. strp = ebuf;
  844. errno = 0;
  845. u = strtoumax (strp, &ebuf, 10);
  846. if (!errno && BILLION <= u)
  847. errno = ERANGE;
  848. if (errno || strp == ebuf || *ebuf != ' ')
  849. {
  850. ERROR ((0, errno, "%s:%ld: %s",
  851. quotearg_colon (listed_incremental_option), lineno,
  852. _("Invalid modification time (nanoseconds)")));
  853. nsec = -1;
  854. }
  855. else
  856. nsec = u;
  857. mtime.tv_sec = sec;
  858. mtime.tv_nsec = nsec;
  859. strp = ebuf;
  860. }
  861. else
  862. memset (&mtime, 0, sizeof mtime);
  863. errno = 0;
  864. u = strtoumax (strp, &ebuf, 10);
  865. if (!errno && TYPE_MAXIMUM (dev_t) < u)
  866. errno = ERANGE;
  867. if (errno || strp == ebuf || *ebuf != ' ')
  868. {
  869. ERROR ((0, errno, "%s:%ld: %s",
  870. quotearg_colon (listed_incremental_option), lineno,
  871. _("Invalid device number")));
  872. dev = (dev_t) -1;
  873. }
  874. else
  875. dev = u;
  876. strp = ebuf;
  877. errno = 0;
  878. u = strtoumax (strp, &ebuf, 10);
  879. if (!errno && TYPE_MAXIMUM (ino_t) < u)
  880. errno = ERANGE;
  881. if (errno || strp == ebuf || *ebuf != ' ')
  882. {
  883. ERROR ((0, errno, "%s:%ld: %s",
  884. quotearg_colon (listed_incremental_option), lineno,
  885. _("Invalid inode number")));
  886. ino = (ino_t) -1;
  887. }
  888. else
  889. ino = u;
  890. strp = ebuf;
  891. strp++;
  892. unquote_string (strp);
  893. note_directory (strp, mtime, dev, ino, nfs, false, NULL);
  894. }
  895. free (buf);
  896. }
  897. /* Read a nul-terminated string from FP and store it in STK.
  898. Store the number of bytes read (including nul terminator) in PCOUNT.
  899. Return the last character read or EOF on end of file. */
  900. static int
  901. read_obstack (FILE *fp, struct obstack *stk, size_t *pcount)
  902. {
  903. int c;
  904. size_t i;
  905. for (i = 0, c = getc (fp); c != EOF && c != 0; c = getc (fp), i++)
  906. obstack_1grow (stk, c);
  907. obstack_1grow (stk, 0);
  908. *pcount = i;
  909. return c;
  910. }
  911. /* Read from file FP a nul-terminated string and convert it to
  912. intmax_t. Return the resulting value in PVAL. Assume '-' has
  913. already been read.
  914. Throw a fatal error if the string cannot be converted or if the
  915. converted value is less than MIN_VAL. */
  916. static void
  917. read_negative_num (FILE *fp, intmax_t min_val, intmax_t *pval)
  918. {
  919. int c;
  920. size_t i;
  921. char buf[INT_BUFSIZE_BOUND (intmax_t)];
  922. char *ep;
  923. buf[0] = '-';
  924. for (i = 1; ISDIGIT (c = getc (fp)); i++)
  925. {
  926. if (i == sizeof buf - 1)
  927. FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
  928. buf[i] = c;
  929. }
  930. if (c < 0)
  931. {
  932. if (ferror (fp))
  933. FATAL_ERROR ((0, errno, _("Read error in snapshot file")));
  934. else
  935. FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
  936. }
  937. buf[i] = 0;
  938. errno = 0;
  939. *pval = strtoimax (buf, &ep, 10);
  940. if (c || errno || *pval < min_val)
  941. FATAL_ERROR ((0, errno, _("Unexpected field value in snapshot file")));
  942. }
  943. /* Read from file FP a nul-terminated string and convert it to
  944. uintmax_t. Return the resulting value in PVAL. Assume C has
  945. already been read.
  946. Throw a fatal error if the string cannot be converted or if the
  947. converted value exceeds MAX_VAL.
  948. Return the last character read or EOF on end of file. */
  949. static int
  950. read_unsigned_num (int c, FILE *fp, uintmax_t max_val, uintmax_t *pval)
  951. {
  952. size_t i;
  953. char buf[UINTMAX_STRSIZE_BOUND], *ep;
  954. for (i = 0; ISDIGIT (c); i++)
  955. {
  956. if (i == sizeof buf - 1)
  957. FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
  958. buf[i] = c;
  959. c = getc (fp);
  960. }
  961. if (c < 0)
  962. {
  963. if (ferror (fp))
  964. FATAL_ERROR ((0, errno, _("Read error in snapshot file")));
  965. else if (i == 0)
  966. return c;
  967. else
  968. FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
  969. }
  970. buf[i] = 0;
  971. errno = 0;
  972. *pval = strtoumax (buf, &ep, 10);
  973. if (c || errno || max_val < *pval)
  974. FATAL_ERROR ((0, errno, _("Unexpected field value in snapshot file")));
  975. return c;
  976. }
  977. /* Read from file FP a nul-terminated string and convert it to
  978. uintmax_t. Return the resulting value in PVAL.
  979. Throw a fatal error if the string cannot be converted or if the
  980. converted value exceeds MAX_VAL.
  981. Return the last character read or EOF on end of file. */
  982. static int
  983. read_num (FILE *fp, uintmax_t max_val, uintmax_t *pval)
  984. {
  985. return read_unsigned_num (getc (fp), fp, max_val, pval);
  986. }
  987. /* Read from FP two NUL-terminated strings representing a struct
  988. timespec. Return the resulting value in PVAL.
  989. Throw a fatal error if the string cannot be converted. */
  990. static void
  991. read_timespec (FILE *fp, struct timespec *pval)
  992. {
  993. int c = getc (fp);
  994. intmax_t i;
  995. uintmax_t u;
  996. if (c == '-')
  997. {
  998. read_negative_num (fp, TYPE_MINIMUM (time_t), &i);
  999. c = 0;
  1000. pval->tv_sec = i;
  1001. }
  1002. else
  1003. {
  1004. c = read_unsigned_num (c, fp, TYPE_MAXIMUM (time_t), &u);
  1005. pval->tv_sec = u;
  1006. }
  1007. if (c || read_num (fp, BILLION - 1, &u))
  1008. FATAL_ERROR ((0, 0, "%s: %s",
  1009. quotearg_colon (listed_incremental_option),
  1010. _("Unexpected EOF in snapshot file")));
  1011. pval->tv_nsec = u;
  1012. }
  1013. /* Read incremental snapshot format 2 */
  1014. static void
  1015. read_incr_db_2 ()
  1016. {
  1017. uintmax_t u;
  1018. struct obstack stk;
  1019. obstack_init (&stk);
  1020. read_timespec (listed_incremental_stream, &newer_mtime_option);
  1021. for (;;)
  1022. {
  1023. struct timespec mtime;
  1024. dev_t dev;
  1025. ino_t ino;
  1026. bool nfs;
  1027. char *name;
  1028. char *content;
  1029. size_t s;
  1030. if (read_num (listed_incremental_stream, 1, &u))
  1031. return; /* Normal return */
  1032. nfs = u;
  1033. read_timespec (listed_incremental_stream, &mtime);
  1034. if (read_num (listed_incremental_stream, TYPE_MAXIMUM (dev_t), &u))
  1035. break;
  1036. dev = u;
  1037. if (read_num (listed_incremental_stream, TYPE_MAXIMUM (ino_t), &u))
  1038. break;
  1039. ino = u;
  1040. if (read_obstack (listed_incremental_stream, &stk, &s))
  1041. break;
  1042. name = obstack_finish (&stk);
  1043. while (read_obstack (listed_incremental_stream, &stk, &s) == 0 && s > 1)
  1044. ;
  1045. if (getc (listed_incremental_stream) != 0)
  1046. FATAL_ERROR ((0, 0, "%s: %s",
  1047. quotearg_colon (listed_incremental_option),
  1048. _("Missing record terminator")));
  1049. content = obstack_finish (&stk);
  1050. note_directory (name, mtime, dev, ino, nfs, false, content);
  1051. obstack_free (&stk, content);
  1052. }
  1053. FATAL_ERROR ((0, 0, "%s: %s",
  1054. quotearg_colon (listed_incremental_option),
  1055. _("Unexpected EOF in snapshot file")));
  1056. }
  1057. /* Read incremental snapshot file (directory file).
  1058. If the file has older incremental version, make sure that it is processed
  1059. correctly and that tar will use the most conservative backup method among
  1060. possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
  1061. etc.) This ensures that the snapshots are updated to the recent version
  1062. without any loss of data. */
  1063. void
  1064. read_directory_file (void)
  1065. {
  1066. int fd;
  1067. char *buf = 0;
  1068. size_t bufsize;
  1069. /* Open the file for both read and write. That way, we can write
  1070. it later without having to reopen it, and don't have to worry if
  1071. we chdir in the meantime. */
  1072. fd = open (listed_incremental_option, O_RDWR | O_CREAT, MODE_RW);
  1073. if (fd < 0)
  1074. {
  1075. open_error (listed_incremental_option);
  1076. return;
  1077. }
  1078. listed_incremental_stream = fdopen (fd, "r+");
  1079. if (! listed_incremental_stream)
  1080. {
  1081. open_error (listed_incremental_option);
  1082. close (fd);
  1083. return;
  1084. }
  1085. if (0 < getline (&buf, &bufsize, listed_incremental_stream))
  1086. {
  1087. char *ebuf;
  1088. uintmax_t incremental_version;
  1089. if (strncmp (buf, PACKAGE_NAME, sizeof PACKAGE_NAME - 1) == 0)
  1090. {
  1091. ebuf = buf + sizeof PACKAGE_NAME - 1;
  1092. if (*ebuf++ != '-')
  1093. ERROR((1, 0, _("Bad incremental file format")));
  1094. for (; *ebuf != '-'; ebuf++)
  1095. if (!*ebuf)
  1096. ERROR((1, 0, _("Bad incremental file format")));
  1097. incremental_version = strtoumax (ebuf + 1, NULL, 10);
  1098. }
  1099. else
  1100. incremental_version = 0;
  1101. switch (incremental_version)
  1102. {
  1103. case 0:
  1104. case 1:
  1105. read_incr_db_01 (incremental_version, buf);
  1106. break;
  1107. case TAR_INCREMENTAL_VERSION:
  1108. read_incr_db_2 ();
  1109. break;
  1110. default:
  1111. ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX),
  1112. incremental_version));
  1113. }
  1114. }
  1115. if (ferror (listed_incremental_stream))
  1116. read_error (listed_incremental_option);
  1117. if (buf)
  1118. free (buf);
  1119. }
  1120. /* Output incremental data for the directory ENTRY to the file DATA.
  1121. Return nonzero if successful, preserving errno on write failure. */
  1122. static bool
  1123. write_directory_file_entry (void *entry, void *data)
  1124. {
  1125. struct directory const *directory = entry;
  1126. FILE *fp = data;
  1127. if (DIR_IS_FOUND (directory))
  1128. {
  1129. char buf[UINTMAX_STRSIZE_BOUND];
  1130. char *s;
  1131. s = DIR_IS_NFS (directory) ? "1" : "0";
  1132. fwrite (s, 2, 1, fp);
  1133. s = (TYPE_SIGNED (time_t)
  1134. ? imaxtostr (directory->mtime.tv_sec, buf)
  1135. : umaxtostr (directory->mtime.tv_sec, buf));
  1136. fwrite (s, strlen (s) + 1, 1, fp);
  1137. s = umaxtostr (directory->mtime.tv_nsec, buf);
  1138. fwrite (s, strlen (s) + 1, 1, fp);
  1139. s = umaxtostr (directory->device_number, buf);
  1140. fwrite (s, strlen (s) + 1, 1, fp);
  1141. s = umaxtostr (directory->inode_number, buf);
  1142. fwrite (s, strlen (s) + 1, 1, fp);
  1143. fwrite (directory->name, strlen (directory->name) + 1, 1, fp);
  1144. if (directory->dump)
  1145. {
  1146. const char *p;
  1147. dumpdir_iter_t itr;
  1148. for (p = dumpdir_first (directory->dump, 0, &itr);
  1149. p;
  1150. p = dumpdir_next (itr))
  1151. fwrite (p, strlen (p) + 1, 1, fp);
  1152. free (itr);
  1153. }
  1154. fwrite ("\0\0", 2, 1, fp);
  1155. }
  1156. return ! ferror (fp);
  1157. }
  1158. void
  1159. write_directory_file (void)
  1160. {
  1161. FILE *fp = listed_incremental_stream;
  1162. char buf[UINTMAX_STRSIZE_BOUND];
  1163. char *s;
  1164. if (! fp)
  1165. return;
  1166. if (fseek (fp, 0L, SEEK_SET) != 0)
  1167. seek_error (listed_incremental_option);
  1168. if (sys_truncate (fileno (fp)) != 0)
  1169. truncate_error (listed_incremental_option);
  1170. fprintf (fp, "%s-%s-%d\n", PACKAGE_NAME, PACKAGE_VERSION,
  1171. TAR_INCREMENTAL_VERSION);
  1172. s = (TYPE_SIGNED (time_t)
  1173. ? imaxtostr (start_time.tv_sec, buf)
  1174. : umaxtostr (start_time.tv_sec, buf));
  1175. fwrite (s, strlen (s) + 1, 1, fp);
  1176. s = umaxtostr (start_time.tv_nsec, buf);
  1177. fwrite (s, strlen (s) + 1, 1, fp);
  1178. if (! ferror (fp) && directory_table)
  1179. hash_do_for_each (directory_table, write_directory_file_entry, fp);
  1180. if (ferror (fp))
  1181. write_error (listed_incremental_option);
  1182. if (fclose (fp) != 0)
  1183. close_error (listed_incremental_option);
  1184. }
  1185. /* Restoration of incremental dumps. */
  1186. static void
  1187. get_gnu_dumpdir (struct tar_stat_info *stat_info)
  1188. {
  1189. size_t size;
  1190. size_t copied;
  1191. union block *data_block;
  1192. char *to;
  1193. char *archive_dir;
  1194. size = stat_info->stat.st_size;
  1195. archive_dir = xmalloc (size);
  1196. to = archive_dir;
  1197. set_next_block_after (current_header);
  1198. mv_begin (stat_info);
  1199. for (; size > 0; size -= copied)
  1200. {
  1201. mv_size_left (size);
  1202. data_block = find_next_block ();
  1203. if (!data_block)
  1204. ERROR ((1, 0, _("Unexpected EOF in archive")));
  1205. copied = available_space_after (data_block);
  1206. if (copied > size)
  1207. copied = size;
  1208. memcpy (to, data_block->buffer, copied);
  1209. to += copied;
  1210. set_next_block_after ((union block *)
  1211. (data_block->buffer + copied - 1));
  1212. }
  1213. mv_end ();
  1214. stat_info->dumpdir = archive_dir;
  1215. stat_info->skipped = true; /* For skip_member() and friends
  1216. to work correctly */
  1217. }
  1218. /* Return T if STAT_INFO represents a dumpdir archive member.
  1219. Note: can invalidate current_header. It happens if flush_archive()
  1220. gets called within get_gnu_dumpdir() */
  1221. bool
  1222. is_dumpdir (struct tar_stat_info *stat_info)
  1223. {
  1224. if (stat_info->is_dumpdir && !stat_info->dumpdir)
  1225. get_gnu_dumpdir (stat_info);
  1226. return stat_info->is_dumpdir;
  1227. }
  1228. static bool
  1229. dumpdir_ok (char *dumpdir)
  1230. {
  1231. char *p;
  1232. int has_tempdir = 0;
  1233. int expect = 0;
  1234. for (p = dumpdir; *p; p += strlen (p) + 1)
  1235. {
  1236. if (expect && *p != expect)
  1237. {
  1238. ERROR ((0, 0,
  1239. _("Malformed dumpdir: expected '%c' but found %#3o"),
  1240. expect, *p));
  1241. return false;
  1242. }
  1243. switch (*p)
  1244. {
  1245. case 'X':
  1246. if (has_tempdir)
  1247. {
  1248. ERROR ((0, 0,
  1249. _("Malformed dumpdir: 'X' duplicated")));
  1250. return false;
  1251. }
  1252. else
  1253. has_tempdir = 1;
  1254. break;
  1255. case 'R':
  1256. if (p[1] == 0)
  1257. {
  1258. if (!has_tempdir)
  1259. {
  1260. ERROR ((0, 0,
  1261. _("Malformed dumpdir: empty name in 'R'")));
  1262. return false;
  1263. }
  1264. else
  1265. has_tempdir = 0;
  1266. }
  1267. expect = 'T';
  1268. break;
  1269. case 'T':
  1270. if (expect != 'T')
  1271. {
  1272. ERROR ((0, 0,
  1273. _("Malformed dumpdir: 'T' not preceeded by 'R'")));
  1274. return false;
  1275. }
  1276. if (p[1] == 0 && !has_tempdir)
  1277. {
  1278. ERROR ((0, 0,
  1279. _("Malformed dumpdir: empty name in 'T'")));
  1280. return false;
  1281. }
  1282. expect = 0;
  1283. break;
  1284. case 'N':
  1285. case 'Y':
  1286. case 'D':
  1287. break;
  1288. default:
  1289. /* FIXME: bail out? */
  1290. break;
  1291. }
  1292. }
  1293. if (expect)
  1294. {
  1295. ERROR ((0, 0,
  1296. _("Malformed dumpdir: expected '%c' but found end of data"),
  1297. expect));
  1298. return false;
  1299. }
  1300. if (has_tempdir)
  1301. WARNOPT (WARN_BAD_DUMPDIR,
  1302. (0, 0, _("Malformed dumpdir: 'X' never used")));
  1303. return true;
  1304. }
  1305. /* Examine the directories under directory_name and delete any
  1306. files that were not there at the time of the back-up. */
  1307. static bool
  1308. try_purge_directory (char const *directory_name)
  1309. {
  1310. char *current_dir;
  1311. char *cur, *arc, *p;
  1312. char *temp_stub = NULL;
  1313. struct dumpdir *dump;
  1314. if (!is_dumpdir (&current_stat_info))
  1315. return false;
  1316. current_dir = savedir (directory_name);
  1317. if (!current_dir)
  1318. /* The directory doesn't exist now. It'll be created. In any
  1319. case, we don't have to delete any files out of it. */
  1320. return false;
  1321. /* Verify if dump directory is sane */
  1322. if (!dumpdir_ok (current_stat_info.dumpdir))
  1323. return false;
  1324. /* Process renames */
  1325. for (arc = current_stat_info.dumpdir; *arc; arc += strlen (arc) + 1)
  1326. {
  1327. if (*arc == 'X')
  1328. {
  1329. #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
  1330. size_t len = strlen (arc + 1);
  1331. temp_stub = xrealloc (temp_stub, len + 1 + sizeof TEMP_DIR_TEMPLATE);
  1332. memcpy (temp_stub, arc + 1, len);
  1333. temp_stub[len] = '/';
  1334. memcpy (temp_stub + len + 1, TEMP_DIR_TEMPLATE,
  1335. sizeof TEMP_DIR_TEMPLATE);
  1336. if (!mkdtemp (temp_stub))
  1337. {
  1338. ERROR ((0, errno,
  1339. _("Cannot create temporary directory using template %s"),
  1340. quote (temp_stub)));
  1341. free (temp_stub);
  1342. free (current_dir);
  1343. return false;
  1344. }
  1345. }
  1346. else if (*arc == 'R')
  1347. {
  1348. char *src, *dst;
  1349. src = arc + 1;
  1350. arc += strlen (arc) + 1;
  1351. dst = arc + 1;
  1352. /* Ensure that neither source nor destination are absolute file
  1353. names (unless permitted by -P option), and that they do not
  1354. contain dubious parts (e.g. ../).
  1355. This is an extra safety precaution. Besides, it might be
  1356. necessary to extract from archives created with tar versions
  1357. prior to 1.19. */
  1358. if (*src)
  1359. src = safer_name_suffix (src, false, absolute_names_option);
  1360. if (*dst)
  1361. dst = safer_name_suffix (dst, false, absolute_names_option);
  1362. if (*src == 0)
  1363. src = temp_stub;
  1364. else if (*dst == 0)
  1365. dst = temp_stub;
  1366. if (!rename_directory (src, dst))
  1367. {
  1368. free (temp_stub);
  1369. free (current_dir);
  1370. /* FIXME: Make sure purge_directory(dst) will return
  1371. immediately */
  1372. return false;
  1373. }
  1374. }
  1375. }
  1376. free (temp_stub);
  1377. /* Process deletes */
  1378. dump = dumpdir_create (current_stat_info.dumpdir);
  1379. p = NULL;
  1380. for (cur = current_dir; *cur; cur += strlen (cur) + 1)
  1381. {
  1382. const char *entry;
  1383. struct stat st;
  1384. if (p)
  1385. free (p);
  1386. p = new_name (directory_name, cur);
  1387. if (deref_stat (false, p, &st))
  1388. {
  1389. if (errno != ENOENT) /* FIXME: Maybe keep a list of renamed
  1390. dirs and check it here? */
  1391. {
  1392. stat_diag (p);
  1393. WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
  1394. quotearg_colon (p)));
  1395. }
  1396. continue;
  1397. }
  1398. if (!(entry = dumpdir_locate (dump, cur))
  1399. || (*entry == 'D' && !S_ISDIR (st.st_mode))
  1400. || (*entry == 'Y' && S_ISDIR (st.st_mode)))
  1401. {
  1402. if (one_file_system_option && st.st_dev != root_device)
  1403. {
  1404. WARN ((0, 0,
  1405. _("%s: directory is on a different device: not purging"),
  1406. quotearg_colon (p)));
  1407. continue;
  1408. }
  1409. if (! interactive_option || confirm ("delete", p))
  1410. {
  1411. if (verbose_option)
  1412. fprintf (stdlis, _("%s: Deleting %s\n"),
  1413. program_name, quote (p));
  1414. if (! remove_any_file (p, RECURSIVE_REMOVE_OPTION))
  1415. {
  1416. int e = errno;
  1417. ERROR ((0, e, _("%s: Cannot remove"), quotearg_colon (p)));
  1418. }
  1419. }
  1420. }
  1421. }
  1422. free (p);
  1423. dumpdir_free (dump);
  1424. free (current_dir);
  1425. return true;
  1426. }
  1427. void
  1428. purge_directory (char const *directory_name)
  1429. {
  1430. if (!try_purge_directory (directory_name))
  1431. skip_member ();
  1432. }
  1433. void
  1434. list_dumpdir (char *buffer, size_t size)
  1435. {
  1436. int state = 0;
  1437. while (size)
  1438. {
  1439. switch (*buffer)
  1440. {
  1441. case 'Y':
  1442. case 'N':
  1443. case 'D':
  1444. case 'R':
  1445. case 'T':
  1446. case 'X':
  1447. fprintf (stdlis, "%c", *buffer);
  1448. if (state == 0)
  1449. {
  1450. fprintf (stdlis, " ");
  1451. state = 1;
  1452. }
  1453. buffer++;
  1454. size--;
  1455. break;
  1456. case 0:
  1457. fputc ('\n', stdlis);
  1458. buffer++;
  1459. size--;
  1460. state = 0;
  1461. break;
  1462. default:
  1463. fputc (*buffer, stdlis);
  1464. buffer++;
  1465. size--;
  1466. }
  1467. }
  1468. }