1 /* GNU dump extensions to tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 /* Incremental dump specialities. */
27 /* Which child files to save under a directory. */
35 #define DIRF_INIT 0x0001 /* directory structure is initialized
36 (procdir called at least once) */
37 #define DIRF_NFS 0x0002 /* directory is mounted on nfs */
38 #define DIRF_FOUND 0x0004 /* directory is found on fs */
39 #define DIRF_NEW 0x0008 /* directory is new (not found
40 in the previous dump) */
41 #define DIRF_RENAMED 0x0010 /* directory is renamed */
43 #define DIR_IS_INITED(d) ((d)->flags & DIRF_INIT)
44 #define DIR_IS_NFS(d) ((d)->flags & DIRF_NFS)
45 #define DIR_IS_FOUND(d) ((d)->flags & DIRF_FOUND)
46 #define DIR_IS_NEW(d) ((d)->flags & DIRF_NEW)
47 #define DIR_IS_RENAMED(d) ((d)->flags & DIRF_RENAMED)
49 #define DIR_SET_FLAG(d,f) (d)->flags |= (f)
50 #define DIR_CLEAR_FLAG(d,f) (d)->flags &= ~(f)
52 /* Directory attributes. */
55 struct timespec mtime
; /* Modification time */
56 dev_t device_number
; /* device number for directory */
57 ino_t inode_number
; /* inode number for directory */
58 char *contents
; /* Directory contents */
59 char *icontents
; /* Initial contents if the directory was
61 enum children children
; /* What to save under this directory */
62 unsigned flags
; /* See DIRF_ macros above */
63 struct directory
*orig
; /* If the directory was renamed, points to
64 the original directory structure */
65 const char *tagfile
; /* Tag file, if the directory falls under
66 exclusion_tag_under */
67 char name
[1]; /* file name of directory */
70 static Hash_table
*directory_table
;
71 static Hash_table
*directory_meta_table
;
73 #if HAVE_ST_FSTYPE_STRING
74 static char const nfs_string
[] = "nfs";
75 # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
77 # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
78 # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
81 /* Calculate the hash of a directory. */
83 hash_directory_name (void const *entry
, size_t n_buckets
)
85 struct directory
const *directory
= entry
;
86 return hash_string (directory
->name
, n_buckets
);
89 /* Compare two directories for equality of their names. */
91 compare_directory_names (void const *entry1
, void const *entry2
)
93 struct directory
const *directory1
= entry1
;
94 struct directory
const *directory2
= entry2
;
95 return strcmp (directory1
->name
, directory2
->name
) == 0;
99 hash_directory_meta (void const *entry
, size_t n_buckets
)
101 struct directory
const *directory
= entry
;
102 /* FIXME: Work out a better algorytm */
103 return (directory
->device_number
+ directory
->inode_number
) % n_buckets
;
106 /* Compare two directories for equality of their device and inode numbers. */
108 compare_directory_meta (void const *entry1
, void const *entry2
)
110 struct directory
const *directory1
= entry1
;
111 struct directory
const *directory2
= entry2
;
112 return directory1
->device_number
== directory2
->device_number
113 && directory1
->inode_number
== directory2
->inode_number
;
116 /* Make a directory entry for given NAME */
117 static struct directory
*
118 make_directory (const char *name
)
120 size_t namelen
= strlen (name
);
121 size_t size
= offsetof (struct directory
, name
) + namelen
+ 1;
122 struct directory
*directory
= xmalloc (size
);
123 directory
->contents
= directory
->icontents
= NULL
;
124 directory
->orig
= NULL
;
125 directory
->flags
= false;
126 strcpy (directory
->name
, name
);
127 if (ISSLASH (directory
->name
[namelen
-1]))
128 directory
->name
[namelen
-1] = 0;
129 directory
->tagfile
= NULL
;
133 /* Create and link a new directory entry for directory NAME, having a
134 device number DEV and an inode number INO, with NFS indicating
135 whether it is an NFS device and FOUND indicating whether we have
136 found that the directory exists. */
137 static struct directory
*
138 note_directory (char const *name
, struct timespec mtime
,
139 dev_t dev
, ino_t ino
, bool nfs
, bool found
, char *contents
)
141 struct directory
*directory
= make_directory (name
);
143 directory
->mtime
= mtime
;
144 directory
->device_number
= dev
;
145 directory
->inode_number
= ino
;
146 directory
->children
= CHANGED_CHILDREN
;
148 DIR_SET_FLAG (directory
, DIRF_NFS
);
150 DIR_SET_FLAG (directory
, DIRF_FOUND
);
153 size_t size
= dumpdir_size (contents
);
154 directory
->contents
= xmalloc (size
);
155 memcpy (directory
->contents
, contents
, size
);
158 directory
->contents
= NULL
;
160 if (! ((directory_table
161 || (directory_table
= hash_initialize (0, 0,
163 compare_directory_names
, 0)))
164 && hash_insert (directory_table
, directory
)))
167 if (! ((directory_meta_table
168 || (directory_meta_table
= hash_initialize (0, 0,
170 compare_directory_meta
,
172 && hash_insert (directory_meta_table
, directory
)))
178 /* Return a directory entry for a given file NAME, or zero if none found. */
179 static struct directory
*
180 find_directory (const char *name
)
182 if (! directory_table
)
186 struct directory
*dir
= make_directory (name
);
187 struct directory
*ret
= hash_lookup (directory_table
, dir
);
193 /* Return a directory entry for a given combination of device and inode
194 numbers, or zero if none found. */
195 static struct directory
*
196 find_directory_meta (dev_t dev
, ino_t ino
)
198 if (! directory_meta_table
)
202 struct directory
*dir
= make_directory ("");
203 struct directory
*ret
;
204 dir
->device_number
= dev
;
205 dir
->inode_number
= ino
;
206 ret
= hash_lookup (directory_meta_table
, dir
);
213 update_parent_directory (const char *name
)
215 struct directory
*directory
;
219 directory
= find_directory (p
);
223 if (deref_stat (dereference_option
, p
, &st
) != 0)
226 directory
->mtime
= get_stat_mtime (&st
);
231 static struct directory
*
232 procdir (char *name_buffer
, struct stat
*stat_data
,
234 enum children children
,
238 struct directory
*directory
;
239 bool nfs
= NFS_FILE_STAT (*stat_data
);
241 if ((directory
= find_directory (name_buffer
)) != NULL
)
243 if (DIR_IS_INITED (directory
))
246 /* With NFS, the same file can have two different devices
247 if an NFS directory is mounted in multiple locations,
248 which is relatively common when automounting.
249 To avoid spurious incremental redumping of
250 directories, consider all NFS devices as equal,
251 relying on the i-node to establish differences. */
253 if (! (((DIR_IS_NFS (directory
) & nfs
)
254 || directory
->device_number
== stat_data
->st_dev
)
255 && directory
->inode_number
== stat_data
->st_ino
))
257 /* FIXME: find_directory_meta ignores nfs */
258 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
263 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
264 quotearg_colon (name_buffer
),
265 quote_n (1, d
->name
)));
267 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
268 directory
->children
= CHANGED_CHILDREN
;
273 WARN ((0, 0, _("%s: Directory has been renamed"),
274 quotearg_colon (name_buffer
)));
275 directory
->children
= ALL_CHILDREN
;
276 directory
->device_number
= stat_data
->st_dev
;
277 directory
->inode_number
= stat_data
->st_ino
;
280 DIR_SET_FLAG (directory
, DIRF_NFS
);
283 directory
->children
= CHANGED_CHILDREN
;
285 DIR_SET_FLAG (directory
, DIRF_FOUND
);
289 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
292 directory
= note_directory (name_buffer
,
293 get_stat_mtime(stat_data
),
303 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
304 quotearg_colon (name_buffer
),
305 quote_n (1, d
->name
)));
307 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
308 directory
->children
= CHANGED_CHILDREN
;
312 DIR_SET_FLAG (directory
, DIRF_NEW
);
314 WARN ((0, 0, _("%s: Directory is new"),
315 quotearg_colon (name_buffer
)));
316 directory
->children
=
317 (listed_incremental_option
318 || (OLDER_STAT_TIME (*stat_data
, m
)
319 || (after_date_option
320 && OLDER_STAT_TIME (*stat_data
, c
))))
326 /* If the directory is on another device and --one-file-system was given,
328 if (one_file_system_option
&& device
!= stat_data
->st_dev
329 /* ... except if it was explicitely given in the command line */
330 && !is_individual_file (name_buffer
))
331 directory
->children
= NO_CHILDREN
;
332 else if (children
== ALL_CHILDREN
)
333 directory
->children
= ALL_CHILDREN
;
335 DIR_SET_FLAG (directory
, DIRF_INIT
);
338 const char *tag_file_name
;
341 switch (check_exclusion_tags (name_buffer
, &tag_file_name
))
343 case exclusion_tag_all
:
344 /* This warning can be duplicated by code in dump_file0, but only
345 in case when the topmost directory being archived contains
347 exclusion_tag_warning (name_buffer
, tag_file_name
,
348 _("directory not dumped"));
351 directory
->children
= NO_CHILDREN
;
354 case exclusion_tag_contents
:
355 exclusion_tag_warning (name_buffer
, tag_file_name
,
356 _("contents not dumped"));
357 directory
->children
= NO_CHILDREN
;
360 case exclusion_tag_under
:
361 exclusion_tag_warning (name_buffer
, tag_file_name
,
362 _("contents not dumped"));
363 directory
->tagfile
= tag_file_name
;
366 case exclusion_tag_none
:
374 /* Locate NAME in the dumpdir array DUMP.
375 Return pointer to the slot in the array, or NULL if not found */
377 dumpdir_locate (const char *dump
, const char *name
)
382 /* Ignore 'R' (rename) and 'X' (tempname) entries, since they break
383 alphabetical ordering.
384 They normally do not occur in dumpdirs from the snapshot files,
385 but this function is also used by purge_directory, which operates
386 on a dumpdir from the archive, hence the need for this test. */
387 if (!strchr ("RX", *dump
))
389 int rc
= strcmp (dump
+ 1, name
);
395 dump
+= strlen (dump
) + 1;
400 /* Return size in bytes of the dumpdir array P */
402 dumpdir_size (const char *p
)
408 size_t size
= strlen (p
) + 1;
416 compare_dirnames (const void *first
, const void *second
)
418 char const *const *name1
= first
;
419 char const *const *name2
= second
;
420 return strcmp (*name1
, *name2
);
423 /* Compare dumpdir array from DIRECTORY with directory listing DIR and
424 build a new dumpdir template.
426 DIR must be returned by a previous call to savedir().
428 File names in DIRECTORY->contents must be sorted
431 DIRECTORY->contents is replaced with the created template. Each entry is
432 prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
435 makedumpdir (struct directory
*directory
, const char *dir
)
438 dirsize
, /* Number of elements in DIR */
439 len
; /* Length of DIR, including terminating nul */
442 char *new_dump
, *new_dump_ptr
;
445 if (directory
->children
== ALL_CHILDREN
)
447 else if (DIR_IS_RENAMED (directory
))
448 dump
= directory
->orig
->icontents
?
449 directory
->orig
->icontents
: directory
->orig
->contents
;
451 dump
= directory
->contents
;
453 /* Count the size of DIR and the number of elements it contains */
456 for (p
= dir
; *p
; p
+= strlen (p
) + 1, dirsize
++)
457 len
+= strlen (p
) + 2;
460 /* Create a sorted directory listing */
461 array
= xcalloc (dirsize
, sizeof array
[0]);
462 for (i
= 0, p
= dir
; *p
; p
+= strlen (p
) + 1, i
++)
465 qsort (array
, dirsize
, sizeof (array
[0]), compare_dirnames
);
467 /* Prepare space for new dumpdir */
468 new_dump
= xmalloc (len
);
469 new_dump_ptr
= new_dump
;
471 /* Fill in the dumpdir template */
472 for (i
= 0; i
< dirsize
; i
++)
474 const char *loc
= dumpdir_locate (dump
, array
[i
]);
477 if (directory
->tagfile
)
478 *new_dump_ptr
= strcmp (directory
->tagfile
, array
[i
]) == 0 ?
483 dump
= loc
+ strlen (loc
) + 1;
485 else if (directory
->tagfile
)
486 *new_dump_ptr
++ = strcmp (directory
->tagfile
, array
[i
]) == 0 ?
489 *new_dump_ptr
++ = 'Y'; /* New entry */
491 /* Copy the file name */
492 for (p
= array
[i
]; (*new_dump_ptr
++ = *p
++); )
496 directory
->icontents
= directory
->contents
;
497 directory
->contents
= new_dump
;
501 /* Recursively scan the given directory. */
503 scan_directory (char *dir
, dev_t device
)
505 char *dirp
= savedir (dir
); /* for scanning directory */
506 char *name_buffer
; /* directory, `/', and directory member */
507 size_t name_buffer_size
; /* allocated size of name_buffer, minus 2 */
508 size_t name_length
; /* used length in name_buffer */
509 struct stat stat_data
;
510 struct directory
*directory
;
515 name_buffer_size
= strlen (dir
) + NAME_FIELD_SIZE
;
516 name_buffer
= xmalloc (name_buffer_size
+ 2);
517 strcpy (name_buffer
, dir
);
518 if (! ISSLASH (dir
[strlen (dir
) - 1]))
519 strcat (name_buffer
, "/");
520 name_length
= strlen (name_buffer
);
522 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
524 stat_diag (name_buffer
);
526 children = CHANGED_CHILDREN;
533 directory
= procdir (name_buffer
, &stat_data
, device
, NO_CHILDREN
, false,
536 if (dirp
&& directory
->children
!= NO_CHILDREN
)
538 char *entry
; /* directory entry being scanned */
539 size_t entrylen
; /* length of directory entry */
541 makedumpdir (directory
, dirp
);
543 for (entry
= directory
->contents
;
544 (entrylen
= strlen (entry
)) != 0;
545 entry
+= entrylen
+ 1)
547 if (name_buffer_size
<= entrylen
- 1 + name_length
)
550 name_buffer_size
+= NAME_FIELD_SIZE
;
551 while (name_buffer_size
<= entrylen
- 1 + name_length
);
552 name_buffer
= xrealloc (name_buffer
, name_buffer_size
+ 2);
554 strcpy (name_buffer
+ name_length
, entry
+ 1);
556 if (*entry
== 'I') /* Ignored entry */
558 else if (excluded_name (name_buffer
))
562 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
564 stat_diag (name_buffer
);
569 if (S_ISDIR (stat_data
.st_mode
))
572 procdir (name_buffer
, &stat_data
, device
,
574 verbose_option
, entry
);
577 else if (one_file_system_option
&& device
!= stat_data
.st_dev
)
580 else if (*entry
== 'Y')
581 /* New entry, skip further checks */;
583 /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
585 else if (OLDER_STAT_TIME (stat_data
, m
)
586 && (!after_date_option
587 || OLDER_STAT_TIME (stat_data
, c
)))
599 return directory
->contents
;
603 get_directory_contents (char *dir
, dev_t device
)
605 return scan_directory (dir
, device
);
610 obstack_code_rename (struct obstack
*stk
, char *from
, char *to
)
612 obstack_1grow (stk
, 'R');
613 obstack_grow (stk
, from
, strlen (from
) + 1);
614 obstack_1grow (stk
, 'T');
615 obstack_grow (stk
, to
, strlen (to
) + 1);
619 rename_handler (void *data
, void *proc_data
)
621 struct directory
*dir
= data
;
622 struct obstack
*stk
= proc_data
;
624 if (DIR_IS_RENAMED (dir
))
626 struct directory
*prev
, *p
;
628 /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
629 are ignored when hit by this function next time.
630 If the chain forms a cycle, prev points to the entry DIR is renamed
631 from. In this case it still retains DIRF_RENAMED flag, which will be
632 cleared in the `else' branch below */
633 for (prev
= dir
; prev
&& prev
->orig
!= dir
; prev
= prev
->orig
)
634 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
638 for (p
= dir
; p
&& p
->orig
; p
= p
->orig
)
639 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
645 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
647 /* Break the cycle by using a temporary name for one of its
649 First, create a temp name stub entry. */
650 temp_name
= dir_name (dir
->name
);
651 obstack_1grow (stk
, 'X');
652 obstack_grow (stk
, temp_name
, strlen (temp_name
) + 1);
654 obstack_code_rename (stk
, dir
->name
, "");
656 for (p
= dir
; p
!= prev
; p
= p
->orig
)
657 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
659 obstack_code_rename (stk
, "", prev
->name
);
666 append_incremental_renames (const char *dump
)
671 if (directory_table
== NULL
)
677 size
= dumpdir_size (dump
) - 1;
678 obstack_grow (&stk
, dump
, size
);
683 hash_do_for_each (directory_table
, rename_handler
, &stk
);
684 if (obstack_object_size (&stk
) != size
)
686 obstack_1grow (&stk
, 0);
687 dump
= obstack_finish (&stk
);
690 obstack_free (&stk
, NULL
);
696 static FILE *listed_incremental_stream
;
698 /* Version of incremental format snapshots (directory files) used by this
699 tar. Currently it is supposed to be a single decimal number. 0 means
700 incremental snapshots as per tar version before 1.15.2.
702 The current tar version supports incremental versions from
703 0 up to TAR_INCREMENTAL_VERSION, inclusive.
704 It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
706 #define TAR_INCREMENTAL_VERSION 2
708 /* Read incremental snapshot formats 0 and 1 */
710 read_incr_db_01 (int version
, const char *initbuf
)
723 if (getline (&buf
, &bufsize
, listed_incremental_stream
) <= 0)
725 read_error (listed_incremental_option
);
733 buf
= strdup (initbuf
);
734 bufsize
= strlen (buf
) + 1;
737 sec
= TYPE_MINIMUM (time_t);
740 u
= strtoumax (buf
, &ebuf
, 10);
741 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
743 if (errno
|| buf
== ebuf
)
744 ERROR ((0, errno
, "%s:%ld: %s",
745 quotearg_colon (listed_incremental_option
),
747 _("Invalid time stamp")));
752 if (version
== 1 && *ebuf
)
754 char const *buf_ns
= ebuf
+ 1;
756 u
= strtoumax (buf_ns
, &ebuf
, 10);
757 if (!errno
&& BILLION
<= u
)
759 if (errno
|| buf_ns
== ebuf
)
761 ERROR ((0, errno
, "%s:%ld: %s",
762 quotearg_colon (listed_incremental_option
),
764 _("Invalid time stamp")));
765 sec
= TYPE_MINIMUM (time_t);
772 /* pre-1 incremental format does not contain nanoseconds */
776 newer_mtime_option
.tv_sec
= sec
;
777 newer_mtime_option
.tv_nsec
= nsec
;
780 while (0 < (n
= getline (&buf
, &bufsize
, listed_incremental_stream
)))
784 bool nfs
= buf
[0] == '+';
785 char *strp
= buf
+ nfs
;
786 struct timespec mtime
;
790 if (buf
[n
- 1] == '\n')
796 u
= strtoumax (strp
, &ebuf
, 10);
797 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
799 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
801 ERROR ((0, errno
, "%s:%ld: %s",
802 quotearg_colon (listed_incremental_option
), lineno
,
803 _("Invalid modification time (seconds)")));
811 u
= strtoumax (strp
, &ebuf
, 10);
812 if (!errno
&& BILLION
<= u
)
814 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
816 ERROR ((0, errno
, "%s:%ld: %s",
817 quotearg_colon (listed_incremental_option
), lineno
,
818 _("Invalid modification time (nanoseconds)")));
824 mtime
.tv_nsec
= nsec
;
828 memset (&mtime
, 0, sizeof mtime
);
831 u
= strtoumax (strp
, &ebuf
, 10);
832 if (!errno
&& TYPE_MAXIMUM (dev_t
) < u
)
834 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
836 ERROR ((0, errno
, "%s:%ld: %s",
837 quotearg_colon (listed_incremental_option
), lineno
,
838 _("Invalid device number")));
846 u
= strtoumax (strp
, &ebuf
, 10);
847 if (!errno
&& TYPE_MAXIMUM (ino_t
) < u
)
849 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
851 ERROR ((0, errno
, "%s:%ld: %s",
852 quotearg_colon (listed_incremental_option
), lineno
,
853 _("Invalid inode number")));
861 unquote_string (strp
);
862 note_directory (strp
, mtime
, dev
, ino
, nfs
, false, NULL
);
867 /* Read a nul-terminated string from FP and store it in STK.
868 Store the number of bytes read (including nul terminator) in PCOUNT.
870 Return the last character read or EOF on end of file. */
872 read_obstack (FILE *fp
, struct obstack
*stk
, size_t *pcount
)
877 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
878 obstack_1grow (stk
, c
);
879 obstack_1grow (stk
, 0);
885 /* Read from file FP a nul-terminated string and convert it to
886 intmax_t. Return the resulting value in PVAL. Assume '-' has
889 Throw a fatal error if the string cannot be converted or if the
890 converted value is less than MIN_VAL. */
893 read_negative_num (FILE *fp
, intmax_t min_val
, intmax_t *pval
)
897 char buf
[INT_BUFSIZE_BOUND (intmax_t)];
901 for (i
= 1; ISDIGIT (c
= getc (fp
)); i
++)
903 if (i
== sizeof buf
- 1)
904 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
911 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
913 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
918 *pval
= strtoimax (buf
, &ep
, 10);
919 if (c
|| errno
|| *pval
< min_val
)
920 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
923 /* Read from file FP a nul-terminated string and convert it to
924 uintmax_t. Return the resulting value in PVAL. Assume C has
927 Throw a fatal error if the string cannot be converted or if the
928 converted value exceeds MAX_VAL.
930 Return the last character read or EOF on end of file. */
933 read_unsigned_num (int c
, FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
936 char buf
[UINTMAX_STRSIZE_BOUND
], *ep
;
938 for (i
= 0; ISDIGIT (c
); i
++)
940 if (i
== sizeof buf
- 1)
941 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
949 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
953 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
958 *pval
= strtoumax (buf
, &ep
, 10);
959 if (c
|| errno
|| max_val
< *pval
)
960 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
964 /* Read from file FP a nul-terminated string and convert it to
965 uintmax_t. Return the resulting value in PVAL.
967 Throw a fatal error if the string cannot be converted or if the
968 converted value exceeds MAX_VAL.
970 Return the last character read or EOF on end of file. */
973 read_num (FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
975 return read_unsigned_num (getc (fp
), fp
, max_val
, pval
);
978 /* Read from FP two NUL-terminated strings representing a struct
979 timespec. Return the resulting value in PVAL.
981 Throw a fatal error if the string cannot be converted. */
984 read_timespec (FILE *fp
, struct timespec
*pval
)
992 read_negative_num (fp
, TYPE_MINIMUM (time_t), &i
);
998 c
= read_unsigned_num (c
, fp
, TYPE_MAXIMUM (time_t), &u
);
1002 if (c
|| read_num (fp
, BILLION
- 1, &u
))
1003 FATAL_ERROR ((0, 0, "%s: %s",
1004 quotearg_colon (listed_incremental_option
),
1005 _("Unexpected EOF in snapshot file")));
1009 /* Read incremental snapshot format 2 */
1016 obstack_init (&stk
);
1018 read_timespec (listed_incremental_stream
, &newer_mtime_option
);
1022 struct timespec mtime
;
1030 if (read_num (listed_incremental_stream
, 1, &u
))
1031 return; /* Normal return */
1035 read_timespec (listed_incremental_stream
, &mtime
);
1037 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (dev_t
), &u
))
1041 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (ino_t
), &u
))
1045 if (read_obstack (listed_incremental_stream
, &stk
, &s
))
1048 name
= obstack_finish (&stk
);
1050 while (read_obstack (listed_incremental_stream
, &stk
, &s
) == 0 && s
> 1)
1052 if (getc (listed_incremental_stream
) != 0)
1053 FATAL_ERROR ((0, 0, "%s: %s",
1054 quotearg_colon (listed_incremental_option
),
1055 _("Missing record terminator")));
1057 content
= obstack_finish (&stk
);
1058 note_directory (name
, mtime
, dev
, ino
, nfs
, false, content
);
1059 obstack_free (&stk
, content
);
1061 FATAL_ERROR ((0, 0, "%s: %s",
1062 quotearg_colon (listed_incremental_option
),
1063 _("Unexpected EOF in snapshot file")));
1066 /* Read incremental snapshot file (directory file).
1067 If the file has older incremental version, make sure that it is processed
1068 correctly and that tar will use the most conservative backup method among
1069 possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
1070 etc.) This ensures that the snapshots are updated to the recent version
1071 without any loss of data. */
1073 read_directory_file (void)
1079 /* Open the file for both read and write. That way, we can write
1080 it later without having to reopen it, and don't have to worry if
1081 we chdir in the meantime. */
1082 fd
= open (listed_incremental_option
, O_RDWR
| O_CREAT
, MODE_RW
);
1085 open_error (listed_incremental_option
);
1089 listed_incremental_stream
= fdopen (fd
, "r+");
1090 if (! listed_incremental_stream
)
1092 open_error (listed_incremental_option
);
1097 if (0 < getline (&buf
, &bufsize
, listed_incremental_stream
))
1100 uintmax_t incremental_version
;
1102 if (strncmp (buf
, PACKAGE_NAME
, sizeof PACKAGE_NAME
- 1) == 0)
1104 ebuf
= buf
+ sizeof PACKAGE_NAME
- 1;
1106 ERROR((1, 0, _("Bad incremental file format")));
1107 for (; *ebuf
!= '-'; ebuf
++)
1109 ERROR((1, 0, _("Bad incremental file format")));
1111 incremental_version
= strtoumax (ebuf
+ 1, NULL
, 10);
1114 incremental_version
= 0;
1116 switch (incremental_version
)
1120 read_incr_db_01 (incremental_version
, buf
);
1123 case TAR_INCREMENTAL_VERSION
:
1128 ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX
),
1129 incremental_version
));
1134 if (ferror (listed_incremental_stream
))
1135 read_error (listed_incremental_option
);
1140 /* Output incremental data for the directory ENTRY to the file DATA.
1141 Return nonzero if successful, preserving errno on write failure. */
1143 write_directory_file_entry (void *entry
, void *data
)
1145 struct directory
const *directory
= entry
;
1148 if (DIR_IS_FOUND (directory
))
1150 char buf
[UINTMAX_STRSIZE_BOUND
];
1153 s
= DIR_IS_NFS (directory
) ? "1" : "0";
1154 fwrite (s
, 2, 1, fp
);
1155 s
= (TYPE_SIGNED (time_t)
1156 ? imaxtostr (directory
->mtime
.tv_sec
, buf
)
1157 : umaxtostr (directory
->mtime
.tv_sec
, buf
));
1158 fwrite (s
, strlen (s
) + 1, 1, fp
);
1159 s
= umaxtostr (directory
->mtime
.tv_nsec
, buf
);
1160 fwrite (s
, strlen (s
) + 1, 1, fp
);
1161 s
= umaxtostr (directory
->device_number
, buf
);
1162 fwrite (s
, strlen (s
) + 1, 1, fp
);
1163 s
= umaxtostr (directory
->inode_number
, buf
);
1164 fwrite (s
, strlen (s
) + 1, 1, fp
);
1166 fwrite (directory
->name
, strlen (directory
->name
) + 1, 1, fp
);
1167 if (directory
->contents
)
1170 for (p
= directory
->contents
; *p
; p
+= strlen (p
) + 1)
1172 if (strchr ("YND", *p
))
1173 fwrite (p
, strlen (p
) + 1, 1, fp
);
1176 fwrite ("\0\0", 2, 1, fp
);
1179 return ! ferror (fp
);
1183 write_directory_file (void)
1185 FILE *fp
= listed_incremental_stream
;
1186 char buf
[UINTMAX_STRSIZE_BOUND
];
1192 if (fseek (fp
, 0L, SEEK_SET
) != 0)
1193 seek_error (listed_incremental_option
);
1194 if (sys_truncate (fileno (fp
)) != 0)
1195 truncate_error (listed_incremental_option
);
1197 fprintf (fp
, "%s-%s-%d\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1198 TAR_INCREMENTAL_VERSION
);
1200 s
= (TYPE_SIGNED (time_t)
1201 ? imaxtostr (start_time
.tv_sec
, buf
)
1202 : umaxtostr (start_time
.tv_sec
, buf
));
1203 fwrite (s
, strlen (s
) + 1, 1, fp
);
1204 s
= umaxtostr (start_time
.tv_nsec
, buf
);
1205 fwrite (s
, strlen (s
) + 1, 1, fp
);
1207 if (! ferror (fp
) && directory_table
)
1208 hash_do_for_each (directory_table
, write_directory_file_entry
, fp
);
1211 write_error (listed_incremental_option
);
1212 if (fclose (fp
) != 0)
1213 close_error (listed_incremental_option
);
1217 /* Restoration of incremental dumps. */
1220 get_gnu_dumpdir (struct tar_stat_info
*stat_info
)
1224 union block
*data_block
;
1228 size
= stat_info
->stat
.st_size
;
1230 archive_dir
= xmalloc (size
);
1233 set_next_block_after (current_header
);
1234 mv_begin (stat_info
);
1236 for (; size
> 0; size
-= copied
)
1238 mv_size_left (size
);
1239 data_block
= find_next_block ();
1241 ERROR ((1, 0, _("Unexpected EOF in archive")));
1242 copied
= available_space_after (data_block
);
1245 memcpy (to
, data_block
->buffer
, copied
);
1247 set_next_block_after ((union block
*)
1248 (data_block
->buffer
+ copied
- 1));
1253 stat_info
->dumpdir
= archive_dir
;
1254 stat_info
->skipped
= true; /* For skip_member() and friends
1255 to work correctly */
1258 /* Return T if STAT_INFO represents a dumpdir archive member.
1259 Note: can invalidate current_header. It happens if flush_archive()
1260 gets called within get_gnu_dumpdir() */
1262 is_dumpdir (struct tar_stat_info
*stat_info
)
1264 if (stat_info
->is_dumpdir
&& !stat_info
->dumpdir
)
1265 get_gnu_dumpdir (stat_info
);
1266 return stat_info
->is_dumpdir
;
1270 dumpdir_ok (char *dumpdir
)
1273 int has_tempdir
= 0;
1276 for (p
= dumpdir
; *p
; p
+= strlen (p
) + 1)
1278 if (expect
&& *p
!= expect
)
1281 _("Malformed dumpdir: expected '%c' but found %#3o"),
1291 _("Malformed dumpdir: 'X' duplicated")));
1304 _("Malformed dumpdir: empty name in 'R'")));
1317 _("Malformed dumpdir: 'T' not preceeded by 'R'")));
1320 if (p
[1] == 0 && !has_tempdir
)
1323 _("Malformed dumpdir: empty name in 'T'")));
1335 /* FIXME: bail out? */
1343 _("Malformed dumpdir: expected '%c' but found end of data"),
1349 WARN ((0, 0, _("Malformed dumpdir: 'X' never used")));
1354 /* Examine the directories under directory_name and delete any
1355 files that were not there at the time of the back-up. */
1357 try_purge_directory (char const *directory_name
)
1360 char *cur
, *arc
, *p
;
1361 char *temp_stub
= NULL
;
1363 if (!is_dumpdir (¤t_stat_info
))
1366 current_dir
= savedir (directory_name
);
1369 /* The directory doesn't exist now. It'll be created. In any
1370 case, we don't have to delete any files out of it. */
1373 /* Verify if dump directory is sane */
1374 if (!dumpdir_ok (current_stat_info
.dumpdir
))
1377 /* Process renames */
1378 for (arc
= current_stat_info
.dumpdir
; *arc
; arc
+= strlen (arc
) + 1)
1382 #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
1383 size_t len
= strlen (arc
+ 1);
1384 temp_stub
= xrealloc (temp_stub
, len
+ 1 + sizeof TEMP_DIR_TEMPLATE
);
1385 memcpy (temp_stub
, arc
+ 1, len
);
1386 temp_stub
[len
] = '/';
1387 memcpy (temp_stub
+ len
+ 1, TEMP_DIR_TEMPLATE
,
1388 sizeof TEMP_DIR_TEMPLATE
);
1389 if (!mkdtemp (temp_stub
))
1392 _("Cannot create temporary directory using template %s"),
1393 quote (temp_stub
)));
1399 else if (*arc
== 'R')
1403 arc
+= strlen (arc
) + 1;
1411 if (!rename_directory (src
, dst
))
1415 /* FIXME: Make sure purge_directory(dst) will return
1424 /* Process deletes */
1426 for (cur
= current_dir
; *cur
; cur
+= strlen (cur
) + 1)
1432 p
= new_name (directory_name
, cur
);
1434 if (deref_stat (false, p
, &st
))
1436 if (errno
!= ENOENT
) /* FIXME: Maybe keep a list of renamed
1437 dirs and check it here? */
1440 WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
1441 quotearg_colon (p
)));
1446 if (!(entry
= dumpdir_locate (current_stat_info
.dumpdir
, cur
))
1447 || (*entry
== 'D' && !S_ISDIR (st
.st_mode
))
1448 || (*entry
== 'Y' && S_ISDIR (st
.st_mode
)))
1450 if (one_file_system_option
&& st
.st_dev
!= root_device
)
1453 _("%s: directory is on a different device: not purging"),
1454 quotearg_colon (p
)));
1458 if (! interactive_option
|| confirm ("delete", p
))
1461 fprintf (stdlis
, _("%s: Deleting %s\n"),
1462 program_name
, quote (p
));
1463 if (! remove_any_file (p
, RECURSIVE_REMOVE_OPTION
))
1466 ERROR ((0, e
, _("%s: Cannot remove"), quotearg_colon (p
)));
1478 purge_directory (char const *directory_name
)
1480 if (!try_purge_directory (directory_name
))
1485 list_dumpdir (char *buffer
, size_t size
)
1498 fprintf (stdlis
, "%c", *buffer
);
1501 fprintf (stdlis
, " ");
1509 fputc ('\n', stdlis
);
1516 fputc (*buffer
, stdlis
);