4
0

incremen.c 45 KB

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