incremen.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /* GNU dump extensions to tar.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <system.h>
  16. #include <getline.h>
  17. #include <hash.h>
  18. #include <quotearg.h>
  19. #include "common.h"
  20. /* Incremental dump specialities. */
  21. /* Which child files to save under a directory. */
  22. enum children
  23. {
  24. NO_CHILDREN,
  25. CHANGED_CHILDREN,
  26. ALL_CHILDREN
  27. };
  28. #define DIRF_INIT 0x0001 /* directory structure is initialized
  29. (procdir called at least once) */
  30. #define DIRF_NFS 0x0002 /* directory is mounted on nfs */
  31. #define DIRF_FOUND 0x0004 /* directory is found on fs */
  32. #define DIRF_NEW 0x0008 /* directory is new (not found
  33. in the previous dump) */
  34. #define DIRF_RENAMED 0x0010 /* directory is renamed */
  35. #define DIR_IS_INITED(d) ((d)->flags & DIRF_INIT)
  36. #define DIR_IS_NFS(d) ((d)->flags & DIRF_NFS)
  37. #define DIR_IS_FOUND(d) ((d)->flags & DIRF_FOUND)
  38. #define DIR_IS_NEW(d) ((d)->flags & DIRF_NEW)
  39. #define DIR_IS_RENAMED(d) ((d)->flags & DIRF_RENAMED)
  40. #define DIR_SET_FLAG(d,f) (d)->flags |= (f)
  41. #define DIR_CLEAR_FLAG(d,f) (d)->flags &= ~(f)
  42. /* Directory attributes. */
  43. struct directory
  44. {
  45. struct timespec mtime; /* Modification time */
  46. dev_t device_number; /* device number for directory */
  47. ino_t inode_number; /* inode number for directory */
  48. char *contents; /* Directory contents */
  49. char *icontents; /* Initial contents if the directory was
  50. rescanned */
  51. enum children children; /* What to save under this directory */
  52. unsigned flags; /* See DIRF_ macros above */
  53. struct directory *orig; /* If the directory was renamed, points to
  54. the original directory structure */
  55. char name[1]; /* file name of directory */
  56. };
  57. static Hash_table *directory_table;
  58. static Hash_table *directory_meta_table;
  59. #if HAVE_ST_FSTYPE_STRING
  60. static char const nfs_string[] = "nfs";
  61. # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
  62. #else
  63. # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
  64. # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
  65. #endif
  66. /* Calculate the hash of a directory. */
  67. static size_t
  68. hash_directory_name (void const *entry, size_t n_buckets)
  69. {
  70. struct directory const *directory = entry;
  71. return hash_string (directory->name, n_buckets);
  72. }
  73. /* Compare two directories for equality of their names. */
  74. static bool
  75. compare_directory_names (void const *entry1, void const *entry2)
  76. {
  77. struct directory const *directory1 = entry1;
  78. struct directory const *directory2 = entry2;
  79. return strcmp (directory1->name, directory2->name) == 0;
  80. }
  81. static size_t
  82. hash_directory_meta (void const *entry, size_t n_buckets)
  83. {
  84. struct directory const *directory = entry;
  85. /* FIXME: Work out a better algorytm */
  86. return (directory->device_number + directory->inode_number) % n_buckets;
  87. }
  88. /* Compare two directories for equality of their device and inode numbers. */
  89. static bool
  90. compare_directory_meta (void const *entry1, void const *entry2)
  91. {
  92. struct directory const *directory1 = entry1;
  93. struct directory const *directory2 = entry2;
  94. return directory1->device_number == directory2->device_number
  95. && directory1->inode_number == directory2->inode_number;
  96. }
  97. /* Make a directory entry for given NAME */
  98. static struct directory *
  99. make_directory (const char *name)
  100. {
  101. size_t namelen = strlen (name);
  102. size_t size = offsetof (struct directory, name) + namelen + 1;
  103. struct directory *directory = xmalloc (size);
  104. directory->contents = directory->icontents = NULL;
  105. directory->orig = NULL;
  106. directory->flags = false;
  107. strcpy (directory->name, name);
  108. if (ISSLASH (directory->name[namelen-1]))
  109. directory->name[namelen-1] = 0;
  110. return directory;
  111. }
  112. /* Create and link a new directory entry for directory NAME, having a
  113. device number DEV and an inode number INO, with NFS indicating
  114. whether it is an NFS device and FOUND indicating whether we have
  115. found that the directory exists. */
  116. static struct directory *
  117. note_directory (char const *name, struct timespec mtime,
  118. dev_t dev, ino_t ino, bool nfs, bool found, char *contents)
  119. {
  120. struct directory *directory = make_directory (name);
  121. directory->mtime = mtime;
  122. directory->device_number = dev;
  123. directory->inode_number = ino;
  124. directory->children = CHANGED_CHILDREN;
  125. if (nfs)
  126. DIR_SET_FLAG (directory, DIRF_NFS);
  127. if (found)
  128. DIR_SET_FLAG (directory, DIRF_FOUND);
  129. if (contents)
  130. {
  131. size_t size = dumpdir_size (contents);
  132. directory->contents = xmalloc (size);
  133. memcpy (directory->contents, contents, size);
  134. }
  135. else
  136. directory->contents = NULL;
  137. if (! ((directory_table
  138. || (directory_table = hash_initialize (0, 0,
  139. hash_directory_name,
  140. compare_directory_names, 0)))
  141. && hash_insert (directory_table, directory)))
  142. xalloc_die ();
  143. if (! ((directory_meta_table
  144. || (directory_meta_table = hash_initialize (0, 0,
  145. hash_directory_meta,
  146. compare_directory_meta,
  147. 0)))
  148. && hash_insert (directory_meta_table, directory)))
  149. xalloc_die ();
  150. return directory;
  151. }
  152. /* Return a directory entry for a given file NAME, or zero if none found. */
  153. static struct directory *
  154. find_directory (const char *name)
  155. {
  156. if (! directory_table)
  157. return 0;
  158. else
  159. {
  160. struct directory *dir = make_directory (name);
  161. struct directory *ret = hash_lookup (directory_table, dir);
  162. free (dir);
  163. return ret;
  164. }
  165. }
  166. /* Return a directory entry for a given combination of device and inode
  167. numbers, or zero if none found. */
  168. static struct directory *
  169. find_directory_meta (dev_t dev, ino_t ino)
  170. {
  171. if (! directory_meta_table)
  172. return 0;
  173. else
  174. {
  175. struct directory *dir = make_directory ("");
  176. struct directory *ret;
  177. dir->device_number = dev;
  178. dir->inode_number = ino;
  179. ret = hash_lookup (directory_meta_table, dir);
  180. free (dir);
  181. return ret;
  182. }
  183. }
  184. void
  185. update_parent_directory (const char *name)
  186. {
  187. struct directory *directory;
  188. char *p, *name_buffer;
  189. p = dir_name (name);
  190. directory = find_directory (p);
  191. if (directory)
  192. {
  193. struct stat st;
  194. if (deref_stat (dereference_option, p, &st) != 0)
  195. stat_diag (name);
  196. else
  197. directory->mtime = get_stat_mtime (&st);
  198. }
  199. free (p);
  200. }
  201. static struct directory *
  202. procdir (char *name_buffer, struct stat *stat_data,
  203. dev_t device,
  204. enum children children,
  205. bool verbose)
  206. {
  207. struct directory *directory;
  208. bool nfs = NFS_FILE_STAT (*stat_data);
  209. struct name *np;
  210. if ((directory = find_directory (name_buffer)) != NULL)
  211. {
  212. if (DIR_IS_INITED (directory))
  213. return directory;
  214. /* With NFS, the same file can have two different devices
  215. if an NFS directory is mounted in multiple locations,
  216. which is relatively common when automounting.
  217. To avoid spurious incremental redumping of
  218. directories, consider all NFS devices as equal,
  219. relying on the i-node to establish differences. */
  220. if (! (((DIR_IS_NFS (directory) & nfs)
  221. || directory->device_number == stat_data->st_dev)
  222. && directory->inode_number == stat_data->st_ino))
  223. {
  224. /* FIXME: find_directory_meta ignores nfs */
  225. struct directory *d = find_directory_meta (stat_data->st_dev,
  226. stat_data->st_ino);
  227. if (d)
  228. {
  229. if (verbose_option)
  230. WARN ((0, 0, _("%s: Directory has been renamed from %s"),
  231. quotearg_colon (name_buffer),
  232. quote_n (1, d->name)));
  233. directory->orig = d;
  234. DIR_SET_FLAG (directory, DIRF_RENAMED);
  235. directory->children = CHANGED_CHILDREN;
  236. }
  237. else
  238. {
  239. if (verbose_option)
  240. WARN ((0, 0, _("%s: Directory has been renamed"),
  241. quotearg_colon (name_buffer)));
  242. directory->children = ALL_CHILDREN;
  243. directory->device_number = stat_data->st_dev;
  244. directory->inode_number = stat_data->st_ino;
  245. }
  246. if (nfs)
  247. DIR_SET_FLAG (directory, DIRF_NFS);
  248. }
  249. else
  250. directory->children = CHANGED_CHILDREN;
  251. DIR_SET_FLAG (directory, DIRF_FOUND);
  252. }
  253. else
  254. {
  255. struct directory *d = find_directory_meta (stat_data->st_dev,
  256. stat_data->st_ino);
  257. directory = note_directory (name_buffer,
  258. get_stat_mtime(stat_data),
  259. stat_data->st_dev,
  260. stat_data->st_ino,
  261. nfs,
  262. true,
  263. NULL);
  264. if (d)
  265. {
  266. if (verbose)
  267. WARN ((0, 0, _("%s: Directory has been renamed from %s"),
  268. quotearg_colon (name_buffer),
  269. quote_n (1, d->name)));
  270. directory->orig = d;
  271. DIR_SET_FLAG (directory, DIRF_RENAMED);
  272. directory->children = CHANGED_CHILDREN;
  273. }
  274. else
  275. {
  276. DIR_SET_FLAG (directory, DIRF_NEW);
  277. if (verbose)
  278. WARN ((0, 0, _("%s: Directory is new"),
  279. quotearg_colon (name_buffer)));
  280. directory->children =
  281. (listed_incremental_option
  282. || (OLDER_STAT_TIME (*stat_data, m)
  283. || (after_date_option
  284. && OLDER_STAT_TIME (*stat_data, c))))
  285. ? ALL_CHILDREN
  286. : CHANGED_CHILDREN;
  287. }
  288. }
  289. /* If the directory is on another device and --one-file-system was given,
  290. omit it... */
  291. if (one_file_system_option && device != stat_data->st_dev
  292. /* ... except if it was explicitely given in the command line */
  293. && !((np = name_scan (name_buffer, true)) && np->explicit))
  294. directory->children = NO_CHILDREN;
  295. else if (children == ALL_CHILDREN)
  296. directory->children = ALL_CHILDREN;
  297. DIR_SET_FLAG (directory, DIRF_INIT);
  298. return directory;
  299. }
  300. /* Locate NAME in the dumpdir array DUMP.
  301. Return pointer to the slot in the array, or NULL if not found */
  302. const char *
  303. dumpdir_locate (const char *dump, const char *name)
  304. {
  305. if (dump)
  306. while (*dump)
  307. {
  308. /* Ignore 'R' (rename) entries, since they break alphabetical ordering.
  309. They normally do not occur in dumpdirs from the snapshot files,
  310. but this function is also used by purge_directory, which operates
  311. on a dumpdir from the archive, hence the need for this test. */
  312. if (*dump != 'R')
  313. {
  314. int rc = strcmp (dump + 1, name);
  315. if (rc == 0)
  316. return dump;
  317. if (rc > 1)
  318. break;
  319. }
  320. dump += strlen (dump) + 1;
  321. }
  322. return NULL;
  323. }
  324. /* Return size in bytes of the dumpdir array P */
  325. size_t
  326. dumpdir_size (const char *p)
  327. {
  328. size_t totsize = 0;
  329. while (*p)
  330. {
  331. size_t size = strlen (p) + 1;
  332. totsize += size;
  333. p += size;
  334. }
  335. return totsize + 1;
  336. }
  337. static int
  338. compare_dirnames (const void *first, const void *second)
  339. {
  340. return strcmp (*(const char**)first, *(const char**)second);
  341. }
  342. /* Compare dumpdir array from DIRECTORY with directory listing DIR and
  343. build a new dumpdir template.
  344. DIR must be returned by a previous call to savedir().
  345. File names in DIRECTORY->contents must be sorted
  346. alphabetically.
  347. DIRECTORY->contents is replaced with the created template. Each entry is
  348. prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
  349. void
  350. makedumpdir (struct directory *directory, const char *dir)
  351. {
  352. size_t i,
  353. dirsize, /* Number of elements in DIR */
  354. len; /* Length of DIR, including terminating nul */
  355. const char *p;
  356. char const **array;
  357. char *new_dump, *new_dump_ptr;
  358. const char *dump;
  359. if (directory->children == ALL_CHILDREN)
  360. dump = NULL;
  361. else if (DIR_IS_RENAMED (directory))
  362. dump = directory->orig->icontents ?
  363. directory->orig->icontents : directory->orig->contents;
  364. else
  365. dump = directory->contents;
  366. /* Count the size of DIR and the number of elements it contains */
  367. dirsize = 0;
  368. len = 0;
  369. for (p = dir; *p; p += strlen (p) + 1, dirsize++)
  370. len += strlen (p) + 2;
  371. len++;
  372. /* Create a sorted directory listing */
  373. array = xcalloc (dirsize, sizeof array[0]);
  374. for (i = 0, p = dir; *p; p += strlen (p) + 1, i++)
  375. array[i] = p;
  376. qsort (array, dirsize, sizeof (array[0]), compare_dirnames);
  377. /* Prepare space for new dumpdir */
  378. new_dump = xmalloc (len);
  379. new_dump_ptr = new_dump;
  380. /* Fill in the dumpdir template */
  381. for (i = 0; i < dirsize; i++)
  382. {
  383. const char *loc = dumpdir_locate (dump, array[i]);
  384. if (loc)
  385. {
  386. *new_dump_ptr++ = ' ';
  387. dump = loc + strlen (loc) + 1;
  388. }
  389. else
  390. *new_dump_ptr++ = 'Y'; /* New entry */
  391. /* Copy the file name */
  392. for (p = array[i]; (*new_dump_ptr++ = *p++); )
  393. ;
  394. }
  395. *new_dump_ptr = 0;
  396. directory->icontents = directory->contents;
  397. directory->contents = new_dump;
  398. free (array);
  399. }
  400. /* Recursively scan the given directory. */
  401. static char *
  402. scan_directory (char *dir_name, dev_t device)
  403. {
  404. char *dirp = savedir (dir_name); /* for scanning directory */
  405. char *name_buffer; /* directory, `/', and directory member */
  406. size_t name_buffer_size; /* allocated size of name_buffer, minus 2 */
  407. size_t name_length; /* used length in name_buffer */
  408. struct stat stat_data;
  409. struct directory *directory;
  410. if (! dirp)
  411. savedir_error (dir_name);
  412. name_buffer_size = strlen (dir_name) + NAME_FIELD_SIZE;
  413. name_buffer = xmalloc (name_buffer_size + 2);
  414. strcpy (name_buffer, dir_name);
  415. if (! ISSLASH (dir_name[strlen (dir_name) - 1]))
  416. strcat (name_buffer, "/");
  417. name_length = strlen (name_buffer);
  418. if (deref_stat (dereference_option, name_buffer, &stat_data))
  419. {
  420. stat_diag (name_buffer);
  421. /* FIXME: used to be
  422. children = CHANGED_CHILDREN;
  423. but changed to: */
  424. free (name_buffer);
  425. free (dirp);
  426. return NULL;
  427. }
  428. directory = procdir (name_buffer, &stat_data, device, NO_CHILDREN, false);
  429. if (dirp && directory->children != NO_CHILDREN)
  430. {
  431. char *entry; /* directory entry being scanned */
  432. size_t entrylen; /* length of directory entry */
  433. makedumpdir (directory, dirp);
  434. for (entry = directory->contents;
  435. (entrylen = strlen (entry)) != 0;
  436. entry += entrylen + 1)
  437. {
  438. if (name_buffer_size <= entrylen - 1 + name_length)
  439. {
  440. do
  441. name_buffer_size += NAME_FIELD_SIZE;
  442. while (name_buffer_size <= entrylen - 1 + name_length);
  443. name_buffer = xrealloc (name_buffer, name_buffer_size + 2);
  444. }
  445. strcpy (name_buffer + name_length, entry + 1);
  446. if (excluded_name (name_buffer))
  447. *entry = 'N';
  448. else
  449. {
  450. if (deref_stat (dereference_option, name_buffer, &stat_data))
  451. {
  452. stat_diag (name_buffer);
  453. *entry = 'N';
  454. continue;
  455. }
  456. if (S_ISDIR (stat_data.st_mode))
  457. {
  458. procdir (name_buffer, &stat_data, device,
  459. directory->children,
  460. verbose_option);
  461. *entry = 'D';
  462. }
  463. else if (one_file_system_option && device != stat_data.st_dev)
  464. *entry = 'N';
  465. else if (*entry == 'Y')
  466. /* New entry, skip further checks */;
  467. /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
  468. else if (OLDER_STAT_TIME (stat_data, m)
  469. && (!after_date_option
  470. || OLDER_STAT_TIME (stat_data, c)))
  471. *entry = 'N';
  472. else
  473. *entry = 'Y';
  474. }
  475. }
  476. }
  477. free (name_buffer);
  478. if (dirp)
  479. free (dirp);
  480. return directory->contents;
  481. }
  482. char *
  483. get_directory_contents (char *dir_name, dev_t device)
  484. {
  485. return scan_directory (dir_name, device);
  486. }
  487. static bool
  488. try_pos (char *name, int pos, const char *dumpdir)
  489. {
  490. int i;
  491. static char namechars[] =
  492. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  493. if (pos > 0)
  494. for (i = 0; i < sizeof namechars; i++)
  495. {
  496. name[pos] = namechars[i];
  497. if (!dumpdir_locate (dumpdir, name)
  498. || try_pos (name, pos-1, dumpdir))
  499. return true;
  500. }
  501. return false;
  502. }
  503. static bool
  504. create_temp_name (char *name, const char *dumpdir)
  505. {
  506. size_t pos = strlen (name) - 6;
  507. return try_pos (name + pos, 5, dumpdir);
  508. }
  509. char *
  510. make_tmp_dir_name (const char *name)
  511. {
  512. char *dirname = dir_name (name);
  513. char *tmp_name = NULL;
  514. struct directory *dir = find_directory (dirname);
  515. tmp_name = new_name (dirname, "000000");
  516. if (!create_temp_name (tmp_name, dir ? dir->contents : NULL))
  517. {
  518. free (tmp_name);
  519. tmp_name = NULL;
  520. }
  521. free (dirname);
  522. return tmp_name;
  523. }
  524. static void
  525. obstack_code_rename (struct obstack *stk, char *from, char *to)
  526. {
  527. obstack_1grow (stk, 'R');
  528. obstack_grow (stk, from, strlen (from) + 1);
  529. obstack_1grow (stk, 'T');
  530. obstack_grow (stk, to, strlen (to) + 1);
  531. }
  532. static bool
  533. rename_handler (void *data, void *proc_data)
  534. {
  535. struct directory *dir = data;
  536. struct obstack *stk = proc_data;
  537. if (DIR_IS_RENAMED (dir))
  538. {
  539. struct directory *prev, *p;
  540. /* Detect eventual cycles and clear DIRF_RENAMED flag, so this entries
  541. are ignored when hit by this function next time.
  542. If the chain forms a cycle, prev points to the entry DIR is renamed
  543. from. In this case it still retains DIRF_RENAMED flag, which will be
  544. cleared in the `else' branch below */
  545. for (prev = dir; prev && prev->orig != dir; prev = prev->orig)
  546. DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
  547. if (prev == NULL)
  548. {
  549. for (p = dir; p && p->orig; p = p->orig)
  550. obstack_code_rename (stk, p->orig->name, p->name);
  551. }
  552. else
  553. {
  554. char *temp_name;
  555. DIR_CLEAR_FLAG (prev, DIRF_RENAMED);
  556. /* Break the cycle by using a temporary name for one of its
  557. elements.
  558. FIXME: Leave the choice of the name to the extractor. */
  559. temp_name = make_tmp_dir_name (dir->name);
  560. obstack_code_rename (stk, dir->name, temp_name);
  561. for (p = dir; p != prev; p = p->orig)
  562. obstack_code_rename (stk, p->orig->name, p->name);
  563. obstack_code_rename (stk, temp_name, prev->name);
  564. }
  565. }
  566. return true;
  567. }
  568. const char *
  569. append_incremental_renames (const char *dump)
  570. {
  571. struct obstack stk;
  572. size_t size;
  573. if (directory_table == NULL)
  574. return dump;
  575. obstack_init (&stk);
  576. if (dump)
  577. {
  578. size = dumpdir_size (dump) - 1;
  579. obstack_grow (&stk, dump, size);
  580. }
  581. else
  582. size = 0;
  583. hash_do_for_each (directory_table, rename_handler, &stk);
  584. if (obstack_object_size (&stk) != size)
  585. {
  586. obstack_1grow (&stk, 0);
  587. dump = obstack_finish (&stk);
  588. }
  589. else
  590. obstack_free (&stk, NULL);
  591. return dump;
  592. }
  593. static FILE *listed_incremental_stream;
  594. /* Version of incremental format snapshots (directory files) used by this
  595. tar. Currently it is supposed to be a single decimal number. 0 means
  596. incremental snapshots as per tar version before 1.15.2.
  597. The current tar version supports incremental versions from
  598. 0 up to TAR_INCREMENTAL_VERSION, inclusive.
  599. It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
  600. #define TAR_INCREMENTAL_VERSION 2
  601. /* Read incremental snapshot formats 0 and 1 */
  602. static void
  603. read_incr_db_01 (int version, const char *initbuf)
  604. {
  605. int n;
  606. uintmax_t u;
  607. time_t t = u;
  608. char *buf = 0;
  609. size_t bufsize;
  610. char *ebuf;
  611. long lineno = 1;
  612. if (version == 1)
  613. {
  614. if (getline (&buf, &bufsize, listed_incremental_stream) <= 0)
  615. {
  616. read_error (listed_incremental_option);
  617. free (buf);
  618. return;
  619. }
  620. ++lineno;
  621. }
  622. else
  623. {
  624. buf = strdup (initbuf);
  625. bufsize = strlen (buf) + 1;
  626. }
  627. t = u = (errno = 0, strtoumax (buf, &ebuf, 10));
  628. if (buf == ebuf || (u == 0 && errno == EINVAL))
  629. ERROR ((0, 0, "%s:%ld: %s",
  630. quotearg_colon (listed_incremental_option),
  631. lineno,
  632. _("Invalid time stamp")));
  633. else if (t != u)
  634. ERROR ((0, 0, "%s:%ld: %s",
  635. quotearg_colon (listed_incremental_option),
  636. lineno,
  637. _("Time stamp out of range")));
  638. else if (version == 1)
  639. {
  640. newer_mtime_option.tv_sec = t;
  641. t = u = (errno = 0, strtoumax (buf, &ebuf, 10));
  642. if (buf == ebuf || (u == 0 && errno == EINVAL))
  643. ERROR ((0, 0, "%s:%ld: %s",
  644. quotearg_colon (listed_incremental_option),
  645. lineno,
  646. _("Invalid time stamp")));
  647. else if (t != u)
  648. ERROR ((0, 0, "%s:%ld: %s",
  649. quotearg_colon (listed_incremental_option),
  650. lineno,
  651. _("Time stamp out of range")));
  652. newer_mtime_option.tv_nsec = t;
  653. }
  654. else
  655. {
  656. /* pre-1 incremental format does not contain nanoseconds */
  657. newer_mtime_option.tv_sec = t;
  658. newer_mtime_option.tv_nsec = 0;
  659. }
  660. while (0 < (n = getline (&buf, &bufsize, listed_incremental_stream)))
  661. {
  662. dev_t dev;
  663. ino_t ino;
  664. bool nfs = buf[0] == '+';
  665. char *strp = buf + nfs;
  666. struct timespec mtime;
  667. lineno++;
  668. if (buf[n - 1] == '\n')
  669. buf[n - 1] = '\0';
  670. if (version == 1)
  671. {
  672. errno = 0;
  673. mtime.tv_sec = u = strtoumax (strp, &ebuf, 10);
  674. if (!isspace (*ebuf))
  675. ERROR ((0, 0, "%s:%ld: %s",
  676. quotearg_colon (listed_incremental_option), lineno,
  677. _("Invalid modification time (seconds)")));
  678. else if (mtime.tv_sec != u)
  679. ERROR ((0, 0, "%s:%ld: %s",
  680. quotearg_colon (listed_incremental_option), lineno,
  681. _("Modification time (seconds) out of range")));
  682. strp = ebuf;
  683. errno = 0;
  684. mtime.tv_nsec = u = strtoumax (strp, &ebuf, 10);
  685. if (!isspace (*ebuf))
  686. ERROR ((0, 0, "%s:%ld: %s",
  687. quotearg_colon (listed_incremental_option), lineno,
  688. _("Invalid modification time (nanoseconds)")));
  689. else if (mtime.tv_nsec != u)
  690. ERROR ((0, 0, "%s:%ld: %s",
  691. quotearg_colon (listed_incremental_option), lineno,
  692. _("Modification time (nanoseconds) out of range")));
  693. strp = ebuf;
  694. }
  695. else
  696. memset (&mtime, 0, sizeof mtime);
  697. errno = 0;
  698. dev = u = strtoumax (strp, &ebuf, 10);
  699. if (!isspace (*ebuf))
  700. ERROR ((0, 0, "%s:%ld: %s",
  701. quotearg_colon (listed_incremental_option), lineno,
  702. _("Invalid device number")));
  703. else if (dev != u)
  704. ERROR ((0, 0, "%s:%ld: %s",
  705. quotearg_colon (listed_incremental_option), lineno,
  706. _("Device number out of range")));
  707. strp = ebuf;
  708. errno = 0;
  709. ino = u = strtoumax (strp, &ebuf, 10);
  710. if (!isspace (*ebuf))
  711. ERROR ((0, 0, "%s:%ld: %s",
  712. quotearg_colon (listed_incremental_option), lineno,
  713. _("Invalid inode number")));
  714. else if (ino != u)
  715. ERROR ((0, 0, "%s:%ld: %s",
  716. quotearg_colon (listed_incremental_option), lineno,
  717. _("Inode number out of range")));
  718. strp = ebuf;
  719. strp++;
  720. unquote_string (strp);
  721. note_directory (strp, mtime, dev, ino, nfs, false, NULL);
  722. }
  723. free (buf);
  724. }
  725. /* Read a nul-terminated string from FP and store it in STK.
  726. Store the number of bytes read (including nul terminator) in PCOUNT.
  727. Return the last character read or EOF on end of file. */
  728. static int
  729. read_obstack (FILE *fp, struct obstack *stk, size_t *pcount)
  730. {
  731. int c;
  732. size_t i;
  733. for (i = 0, c = getc (fp); c != EOF && c != 0; c = getc (fp), i++)
  734. obstack_1grow (stk, c);
  735. obstack_1grow (stk, 0);
  736. *pcount = i;
  737. return c;
  738. }
  739. /* Read from file FP a nul-terminated string and convert it to
  740. uintmax_t. Return the resulting value in PVAL.
  741. Throw fatal error if the string cannot be converted.
  742. Return the last character read or EOF on end of file. */
  743. static int
  744. read_num (FILE *fp, uintmax_t *pval)
  745. {
  746. int c;
  747. size_t i;
  748. char buf[UINTMAX_STRSIZE_BOUND], *ep;
  749. for (i = 0, c = getc (fp); c != EOF && c != 0; c = getc (fp), i++)
  750. {
  751. if (i == sizeof buf - 1)
  752. FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
  753. buf[i] = c;
  754. }
  755. buf[i] = 0;
  756. *pval = strtoumax (buf, &ep, 10);
  757. if (*ep)
  758. FATAL_ERROR ((0, 0, _("Unexpected field value in snapshot file")));
  759. return c;
  760. }
  761. /* Read incremental snapshot format 2 */
  762. static void
  763. read_incr_db_2 ()
  764. {
  765. uintmax_t u;
  766. struct obstack stk;
  767. obstack_init (&stk);
  768. if (read_num (listed_incremental_stream, &u))
  769. FATAL_ERROR ((0, 0, "%s: %s",
  770. quotearg_colon (listed_incremental_option),
  771. _("Error reading time stamp")));
  772. newer_mtime_option.tv_sec = u;
  773. if (newer_mtime_option.tv_sec != u)
  774. FATAL_ERROR ((0, 0, "%s: %s",
  775. quotearg_colon (listed_incremental_option),
  776. _("Time stamp out of range")));
  777. if (read_num (listed_incremental_stream, &u))
  778. FATAL_ERROR ((0, 0, "%s: %s",
  779. quotearg_colon (listed_incremental_option),
  780. _("Error reading time stamp")));
  781. newer_mtime_option.tv_nsec = u;
  782. if (newer_mtime_option.tv_nsec != u)
  783. FATAL_ERROR ((0, 0, "%s: %s",
  784. quotearg_colon (listed_incremental_option),
  785. _("Time stamp out of range")));
  786. for (;;)
  787. {
  788. struct timespec mtime;
  789. dev_t dev;
  790. ino_t ino;
  791. bool nfs;
  792. char *name;
  793. char *content;
  794. size_t s;
  795. if (read_num (listed_incremental_stream, &u))
  796. return; /* Normal return */
  797. nfs = u;
  798. if (read_num (listed_incremental_stream, &u))
  799. break;
  800. mtime.tv_sec = u;
  801. if (mtime.tv_sec != u)
  802. FATAL_ERROR ((0, 0, "%s: %s",
  803. quotearg_colon (listed_incremental_option),
  804. _("Modification time (seconds) out of range")));
  805. if (read_num (listed_incremental_stream, &u))
  806. break;
  807. mtime.tv_nsec = u;
  808. if (mtime.tv_nsec != u)
  809. FATAL_ERROR ((0, 0, "%s: %s",
  810. quotearg_colon (listed_incremental_option),
  811. _("Modification time (nanoseconds) out of range")));
  812. if (read_num (listed_incremental_stream, &u))
  813. break;
  814. dev = u;
  815. if (dev != u)
  816. FATAL_ERROR ((0, 0, "%s: %s",
  817. quotearg_colon (listed_incremental_option),
  818. _("Device number out of range")));
  819. if (read_num (listed_incremental_stream, &u))
  820. break;
  821. ino = u;
  822. if (ino != u)
  823. FATAL_ERROR ((0, 0, "%s: %s",
  824. quotearg_colon (listed_incremental_option),
  825. _("Inode number out of range")));
  826. if (read_obstack (listed_incremental_stream, &stk, &s))
  827. break;
  828. name = obstack_finish (&stk);
  829. while (read_obstack (listed_incremental_stream, &stk, &s) == 0 && s > 1)
  830. ;
  831. if (getc (listed_incremental_stream) != 0)
  832. FATAL_ERROR ((0, 0, "%s: %s",
  833. quotearg_colon (listed_incremental_option),
  834. _("Missing record terminator")));
  835. content = obstack_finish (&stk);
  836. note_directory (name, mtime, dev, ino, nfs, false, content);
  837. obstack_free (&stk, content);
  838. }
  839. FATAL_ERROR ((0, 0, "%s: %s",
  840. quotearg_colon (listed_incremental_option),
  841. _("Unexpected EOF")));
  842. }
  843. /* Read incremental snapshot file (directory file).
  844. If the file has older incremental version, make sure that it is processed
  845. correctly and that tar will use the most conservative backup method among
  846. possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
  847. etc.) This ensures that the snapshots are updated to the recent version
  848. without any loss of data. */
  849. void
  850. read_directory_file (void)
  851. {
  852. int fd;
  853. char *buf = 0;
  854. size_t bufsize;
  855. long lineno = 1;
  856. /* Open the file for both read and write. That way, we can write
  857. it later without having to reopen it, and don't have to worry if
  858. we chdir in the meantime. */
  859. fd = open (listed_incremental_option, O_RDWR | O_CREAT, MODE_RW);
  860. if (fd < 0)
  861. {
  862. open_error (listed_incremental_option);
  863. return;
  864. }
  865. listed_incremental_stream = fdopen (fd, "r+");
  866. if (! listed_incremental_stream)
  867. {
  868. open_error (listed_incremental_option);
  869. close (fd);
  870. return;
  871. }
  872. if (0 < getline (&buf, &bufsize, listed_incremental_stream))
  873. {
  874. char *ebuf;
  875. int incremental_version;
  876. if (strncmp (buf, PACKAGE_NAME, sizeof PACKAGE_NAME - 1) == 0)
  877. {
  878. ebuf = buf + sizeof PACKAGE_NAME - 1;
  879. if (*ebuf++ != '-')
  880. ERROR((1, 0, _("Bad incremental file format")));
  881. for (; *ebuf != '-'; ebuf++)
  882. if (!*ebuf)
  883. ERROR((1, 0, _("Bad incremental file format")));
  884. incremental_version = (errno = 0, strtoumax (ebuf+1, &ebuf, 10));
  885. }
  886. else
  887. incremental_version = 0;
  888. switch (incremental_version)
  889. {
  890. case 0:
  891. case 1:
  892. read_incr_db_01 (incremental_version, buf);
  893. break;
  894. case TAR_INCREMENTAL_VERSION:
  895. read_incr_db_2 ();
  896. break;
  897. default:
  898. ERROR ((1, 0, _("Unsupported incremental format version: %d"),
  899. incremental_version));
  900. }
  901. }
  902. if (ferror (listed_incremental_stream))
  903. read_error (listed_incremental_option);
  904. if (buf)
  905. free (buf);
  906. }
  907. /* Output incremental data for the directory ENTRY to the file DATA.
  908. Return nonzero if successful, preserving errno on write failure. */
  909. static bool
  910. write_directory_file_entry (void *entry, void *data)
  911. {
  912. struct directory const *directory = entry;
  913. FILE *fp = data;
  914. if (DIR_IS_FOUND (directory))
  915. {
  916. char buf[UINTMAX_STRSIZE_BOUND];
  917. char *s;
  918. s = DIR_IS_NFS (directory) ? "1" : "0";
  919. fwrite (s, 2, 1, fp);
  920. s = umaxtostr (directory->mtime.tv_sec, buf);
  921. fwrite (s, strlen (s) + 1, 1, fp);
  922. s = umaxtostr (directory->mtime.tv_nsec, buf);
  923. fwrite (s, strlen (s) + 1, 1, fp);
  924. s = umaxtostr (directory->device_number, buf);
  925. fwrite (s, strlen (s) + 1, 1, fp);
  926. s = umaxtostr (directory->inode_number, buf);
  927. fwrite (s, strlen (s) + 1, 1, fp);
  928. fwrite (directory->name, strlen (directory->name) + 1, 1, fp);
  929. if (directory->contents)
  930. {
  931. char *p;
  932. for (p = directory->contents; *p; p += strlen (p) + 1)
  933. {
  934. if (strchr ("YND", *p))
  935. fwrite (p, strlen (p) + 1, 1, fp);
  936. }
  937. }
  938. fwrite ("\0\0", 2, 1, fp);
  939. }
  940. return ! ferror (fp);
  941. }
  942. void
  943. write_directory_file (void)
  944. {
  945. FILE *fp = listed_incremental_stream;
  946. char buf[UINTMAX_STRSIZE_BOUND];
  947. char *s;
  948. if (! fp)
  949. return;
  950. if (fseek (fp, 0L, SEEK_SET) != 0)
  951. seek_error (listed_incremental_option);
  952. if (sys_truncate (fileno (fp)) != 0)
  953. truncate_error (listed_incremental_option);
  954. fprintf (fp, "%s-%s-%d\n", PACKAGE_NAME, PACKAGE_VERSION,
  955. TAR_INCREMENTAL_VERSION);
  956. s = umaxtostr (start_time.tv_sec, buf);
  957. fwrite (s, strlen (s) + 1, 1, fp);
  958. s = umaxtostr (start_time.tv_nsec, buf);
  959. fwrite (s, strlen (s) + 1, 1, fp);
  960. if (! ferror (fp) && directory_table)
  961. hash_do_for_each (directory_table, write_directory_file_entry, fp);
  962. if (ferror (fp))
  963. write_error (listed_incremental_option);
  964. if (fclose (fp) != 0)
  965. close_error (listed_incremental_option);
  966. }
  967. /* Restoration of incremental dumps. */
  968. static void
  969. get_gnu_dumpdir (struct tar_stat_info *stat_info)
  970. {
  971. size_t size;
  972. size_t copied;
  973. union block *data_block;
  974. char *to;
  975. char *archive_dir;
  976. size = stat_info->stat.st_size;
  977. archive_dir = xmalloc (size);
  978. to = archive_dir;
  979. set_next_block_after (current_header);
  980. mv_begin (stat_info);
  981. for (; size > 0; size -= copied)
  982. {
  983. mv_size_left (size);
  984. data_block = find_next_block ();
  985. if (!data_block)
  986. ERROR ((1, 0, _("Unexpected EOF in archive")));
  987. copied = available_space_after (data_block);
  988. if (copied > size)
  989. copied = size;
  990. memcpy (to, data_block->buffer, copied);
  991. to += copied;
  992. set_next_block_after ((union block *)
  993. (data_block->buffer + copied - 1));
  994. }
  995. mv_end ();
  996. stat_info->dumpdir = archive_dir;
  997. stat_info->skipped = true; /* For skip_member() and friends
  998. to work correctly */
  999. }
  1000. /* Return T if STAT_INFO represents a dumpdir archive member.
  1001. Note: can invalidate current_header. It happens if flush_archive()
  1002. gets called within get_gnu_dumpdir() */
  1003. bool
  1004. is_dumpdir (struct tar_stat_info *stat_info)
  1005. {
  1006. if (stat_info->is_dumpdir && !stat_info->dumpdir)
  1007. get_gnu_dumpdir (stat_info);
  1008. return stat_info->is_dumpdir;
  1009. }
  1010. /* Examine the directories under directory_name and delete any
  1011. files that were not there at the time of the back-up. */
  1012. void
  1013. purge_directory (char const *directory_name)
  1014. {
  1015. char *current_dir;
  1016. char *cur, *arc, *p;
  1017. if (!is_dumpdir (&current_stat_info))
  1018. {
  1019. skip_member ();
  1020. return;
  1021. }
  1022. current_dir = savedir (directory_name);
  1023. if (!current_dir)
  1024. {
  1025. /* The directory doesn't exist now. It'll be created. In any
  1026. case, we don't have to delete any files out of it. */
  1027. skip_member ();
  1028. return;
  1029. }
  1030. /* Process renames */
  1031. for (arc = current_stat_info.dumpdir; *arc; arc += strlen (arc) + 1)
  1032. {
  1033. if (*arc == 'R')
  1034. {
  1035. char *src, *dst;
  1036. src = arc + 1;
  1037. arc += strlen (arc) + 1;
  1038. dst = arc + 1;
  1039. if (!rename_directory (src, dst))
  1040. {
  1041. free (current_dir);
  1042. /* FIXME: Make sure purge_directory(dst) will return
  1043. immediately */
  1044. return;
  1045. }
  1046. }
  1047. }
  1048. /* Process deletes */
  1049. p = NULL;
  1050. for (cur = current_dir; *cur; cur += strlen (cur) + 1)
  1051. {
  1052. const char *entry;
  1053. struct stat st;
  1054. if (p)
  1055. free (p);
  1056. p = new_name (directory_name, cur);
  1057. if (!(entry = dumpdir_locate (current_stat_info.dumpdir, cur))
  1058. || (*entry == 'D' && S_ISDIR (st.st_mode))
  1059. || (*entry == 'Y' && !S_ISDIR (st.st_mode)))
  1060. {
  1061. if (deref_stat (false, p, &st))
  1062. {
  1063. if (errno != ENOENT) /* FIXME: Maybe keep a list of renamed
  1064. dirs and check it here? */
  1065. {
  1066. stat_diag (p);
  1067. WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
  1068. quotearg_colon (p)));
  1069. }
  1070. continue;
  1071. }
  1072. else if (one_file_system_option && st.st_dev != root_device)
  1073. {
  1074. WARN ((0, 0,
  1075. _("%s: directory is on a different device: not purging"),
  1076. quotearg_colon (p)));
  1077. continue;
  1078. }
  1079. if (! interactive_option || confirm ("delete", p))
  1080. {
  1081. if (verbose_option)
  1082. fprintf (stdlis, _("%s: Deleting %s\n"),
  1083. program_name, quote (p));
  1084. if (! remove_any_file (p, RECURSIVE_REMOVE_OPTION))
  1085. {
  1086. int e = errno;
  1087. ERROR ((0, e, _("%s: Cannot remove"), quotearg_colon (p)));
  1088. }
  1089. }
  1090. }
  1091. }
  1092. free (p);
  1093. free (current_dir);
  1094. }
  1095. void
  1096. list_dumpdir (char *buffer, size_t size)
  1097. {
  1098. while (size)
  1099. {
  1100. switch (*buffer)
  1101. {
  1102. case 'Y':
  1103. case 'N':
  1104. case 'D':
  1105. case 'R':
  1106. case 'T':
  1107. fprintf (stdlis, "%c ", *buffer);
  1108. buffer++;
  1109. size--;
  1110. break;
  1111. case 0:
  1112. fputc ('\n', stdlis);
  1113. buffer++;
  1114. size--;
  1115. break;
  1116. default:
  1117. fputc (*buffer, stdlis);
  1118. buffer++;
  1119. size--;
  1120. }
  1121. }
  1122. }