incremen.c 45 KB

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