1 /* GNU dump extensions to tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006 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 2, 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. */
27 /* Incremental dump specialities. */
29 /* Which child files to save under a directory. */
37 #define DIRF_INIT 0x0001 /* directory structure is initialized
38 (procdir called at least once) */
39 #define DIRF_NFS 0x0002 /* directory is mounted on nfs */
40 #define DIRF_FOUND 0x0004 /* directory is found on fs */
41 #define DIRF_NEW 0x0008 /* directory is new (not found
42 in the previous dump) */
43 #define DIRF_RENAMED 0x0010 /* directory is renamed */
45 #define DIR_IS_INITED(d) ((d)->flags & DIRF_INIT)
46 #define DIR_IS_NFS(d) ((d)->flags & DIRF_NFS)
47 #define DIR_IS_FOUND(d) ((d)->flags & DIRF_FOUND)
48 #define DIR_IS_NEW(d) ((d)->flags & DIRF_NEW)
49 #define DIR_IS_RENAMED(d) ((d)->flags & DIRF_RENAMED)
51 #define DIR_SET_FLAG(d,f) (d)->flags |= (f)
52 #define DIR_CLEAR_FLAG(d,f) (d)->flags &= ~(f)
54 /* Directory attributes. */
57 struct timespec mtime
; /* Modification time */
58 dev_t device_number
; /* device number for directory */
59 ino_t inode_number
; /* inode number for directory */
60 char *contents
; /* Directory contents */
61 char *icontents
; /* Initial contents if the directory was
63 enum children children
; /* What to save under this directory */
64 unsigned flags
; /* See DIRF_ macros above */
65 struct directory
*orig
; /* If the directory was renamed, points to
66 the original directory structure */
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;
132 /* Create and link a new directory entry for directory NAME, having a
133 device number DEV and an inode number INO, with NFS indicating
134 whether it is an NFS device and FOUND indicating whether we have
135 found that the directory exists. */
136 static struct directory
*
137 note_directory (char const *name
, struct timespec mtime
,
138 dev_t dev
, ino_t ino
, bool nfs
, bool found
, char *contents
)
140 struct directory
*directory
= make_directory (name
);
142 directory
->mtime
= mtime
;
143 directory
->device_number
= dev
;
144 directory
->inode_number
= ino
;
145 directory
->children
= CHANGED_CHILDREN
;
147 DIR_SET_FLAG (directory
, DIRF_NFS
);
149 DIR_SET_FLAG (directory
, DIRF_FOUND
);
152 size_t size
= dumpdir_size (contents
);
153 directory
->contents
= xmalloc (size
);
154 memcpy (directory
->contents
, contents
, size
);
157 directory
->contents
= NULL
;
159 if (! ((directory_table
160 || (directory_table
= hash_initialize (0, 0,
162 compare_directory_names
, 0)))
163 && hash_insert (directory_table
, directory
)))
166 if (! ((directory_meta_table
167 || (directory_meta_table
= hash_initialize (0, 0,
169 compare_directory_meta
,
171 && hash_insert (directory_meta_table
, directory
)))
177 /* Return a directory entry for a given file NAME, or zero if none found. */
178 static struct directory
*
179 find_directory (const char *name
)
181 if (! directory_table
)
185 struct directory
*dir
= make_directory (name
);
186 struct directory
*ret
= hash_lookup (directory_table
, dir
);
192 /* Return a directory entry for a given combination of device and inode
193 numbers, or zero if none found. */
194 static struct directory
*
195 find_directory_meta (dev_t dev
, ino_t ino
)
197 if (! directory_meta_table
)
201 struct directory
*dir
= make_directory ("");
202 struct directory
*ret
;
203 dir
->device_number
= dev
;
204 dir
->inode_number
= ino
;
205 ret
= hash_lookup (directory_meta_table
, dir
);
212 update_parent_directory (const char *name
)
214 struct directory
*directory
;
218 directory
= find_directory (p
);
222 if (deref_stat (dereference_option
, p
, &st
) != 0)
225 directory
->mtime
= get_stat_mtime (&st
);
230 static struct directory
*
231 procdir (char *name_buffer
, struct stat
*stat_data
,
233 enum children children
,
236 struct directory
*directory
;
237 bool nfs
= NFS_FILE_STAT (*stat_data
);
239 if ((directory
= find_directory (name_buffer
)) != NULL
)
241 if (DIR_IS_INITED (directory
))
244 /* With NFS, the same file can have two different devices
245 if an NFS directory is mounted in multiple locations,
246 which is relatively common when automounting.
247 To avoid spurious incremental redumping of
248 directories, consider all NFS devices as equal,
249 relying on the i-node to establish differences. */
251 if (! (((DIR_IS_NFS (directory
) & nfs
)
252 || directory
->device_number
== stat_data
->st_dev
)
253 && directory
->inode_number
== stat_data
->st_ino
))
255 /* FIXME: find_directory_meta ignores nfs */
256 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
261 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
262 quotearg_colon (name_buffer
),
263 quote_n (1, d
->name
)));
265 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
266 directory
->children
= CHANGED_CHILDREN
;
271 WARN ((0, 0, _("%s: Directory has been renamed"),
272 quotearg_colon (name_buffer
)));
273 directory
->children
= ALL_CHILDREN
;
274 directory
->device_number
= stat_data
->st_dev
;
275 directory
->inode_number
= stat_data
->st_ino
;
278 DIR_SET_FLAG (directory
, DIRF_NFS
);
281 directory
->children
= CHANGED_CHILDREN
;
283 DIR_SET_FLAG (directory
, DIRF_FOUND
);
287 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
290 directory
= note_directory (name_buffer
,
291 get_stat_mtime(stat_data
),
301 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
302 quotearg_colon (name_buffer
),
303 quote_n (1, d
->name
)));
305 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
306 directory
->children
= CHANGED_CHILDREN
;
310 DIR_SET_FLAG (directory
, DIRF_NEW
);
312 WARN ((0, 0, _("%s: Directory is new"),
313 quotearg_colon (name_buffer
)));
314 directory
->children
=
315 (listed_incremental_option
316 || (OLDER_STAT_TIME (*stat_data
, m
)
317 || (after_date_option
318 && OLDER_STAT_TIME (*stat_data
, c
))))
324 /* If the directory is on another device and --one-file-system was given,
326 if (one_file_system_option
&& device
!= stat_data
->st_dev
327 /* ... except if it was explicitely given in the command line */
328 && !is_individual_file (name_buffer
))
329 directory
->children
= NO_CHILDREN
;
330 else if (children
== ALL_CHILDREN
)
331 directory
->children
= ALL_CHILDREN
;
333 DIR_SET_FLAG (directory
, DIRF_INIT
);
338 /* Locate NAME in the dumpdir array DUMP.
339 Return pointer to the slot in the array, or NULL if not found */
341 dumpdir_locate (const char *dump
, const char *name
)
346 /* Ignore 'R' (rename) and 'X' (tempname) entries, since they break
347 alphabetical ordering.
348 They normally do not occur in dumpdirs from the snapshot files,
349 but this function is also used by purge_directory, which operates
350 on a dumpdir from the archive, hence the need for this test. */
351 if (!strchr ("RX", *dump
))
353 int rc
= strcmp (dump
+ 1, name
);
359 dump
+= strlen (dump
) + 1;
364 /* Return size in bytes of the dumpdir array P */
366 dumpdir_size (const char *p
)
372 size_t size
= strlen (p
) + 1;
380 compare_dirnames (const void *first
, const void *second
)
382 char const *const *name1
= first
;
383 char const *const *name2
= second
;
384 return strcmp (*name1
, *name2
);
387 /* Compare dumpdir array from DIRECTORY with directory listing DIR and
388 build a new dumpdir template.
390 DIR must be returned by a previous call to savedir().
392 File names in DIRECTORY->contents must be sorted
395 DIRECTORY->contents is replaced with the created template. Each entry is
396 prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
399 makedumpdir (struct directory
*directory
, const char *dir
)
402 dirsize
, /* Number of elements in DIR */
403 len
; /* Length of DIR, including terminating nul */
406 char *new_dump
, *new_dump_ptr
;
409 if (directory
->children
== ALL_CHILDREN
)
411 else if (DIR_IS_RENAMED (directory
))
412 dump
= directory
->orig
->icontents
?
413 directory
->orig
->icontents
: directory
->orig
->contents
;
415 dump
= directory
->contents
;
417 /* Count the size of DIR and the number of elements it contains */
420 for (p
= dir
; *p
; p
+= strlen (p
) + 1, dirsize
++)
421 len
+= strlen (p
) + 2;
424 /* Create a sorted directory listing */
425 array
= xcalloc (dirsize
, sizeof array
[0]);
426 for (i
= 0, p
= dir
; *p
; p
+= strlen (p
) + 1, i
++)
429 qsort (array
, dirsize
, sizeof (array
[0]), compare_dirnames
);
431 /* Prepare space for new dumpdir */
432 new_dump
= xmalloc (len
);
433 new_dump_ptr
= new_dump
;
435 /* Fill in the dumpdir template */
436 for (i
= 0; i
< dirsize
; i
++)
438 const char *loc
= dumpdir_locate (dump
, array
[i
]);
441 *new_dump_ptr
++ = ' ';
442 dump
= loc
+ strlen (loc
) + 1;
445 *new_dump_ptr
++ = 'Y'; /* New entry */
447 /* Copy the file name */
448 for (p
= array
[i
]; (*new_dump_ptr
++ = *p
++); )
452 directory
->icontents
= directory
->contents
;
453 directory
->contents
= new_dump
;
457 /* Recursively scan the given directory. */
459 scan_directory (char *dir_name
, dev_t device
)
461 char *dirp
= savedir (dir_name
); /* for scanning directory */
462 char *name_buffer
; /* directory, `/', and directory member */
463 size_t name_buffer_size
; /* allocated size of name_buffer, minus 2 */
464 size_t name_length
; /* used length in name_buffer */
465 struct stat stat_data
;
466 struct directory
*directory
;
469 savedir_error (dir_name
);
471 name_buffer_size
= strlen (dir_name
) + NAME_FIELD_SIZE
;
472 name_buffer
= xmalloc (name_buffer_size
+ 2);
473 strcpy (name_buffer
, dir_name
);
474 if (! ISSLASH (dir_name
[strlen (dir_name
) - 1]))
475 strcat (name_buffer
, "/");
476 name_length
= strlen (name_buffer
);
478 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
480 stat_diag (name_buffer
);
482 children = CHANGED_CHILDREN;
489 directory
= procdir (name_buffer
, &stat_data
, device
, NO_CHILDREN
, false);
491 if (dirp
&& directory
->children
!= NO_CHILDREN
)
493 char *entry
; /* directory entry being scanned */
494 size_t entrylen
; /* length of directory entry */
496 makedumpdir (directory
, dirp
);
498 for (entry
= directory
->contents
;
499 (entrylen
= strlen (entry
)) != 0;
500 entry
+= entrylen
+ 1)
502 if (name_buffer_size
<= entrylen
- 1 + name_length
)
505 name_buffer_size
+= NAME_FIELD_SIZE
;
506 while (name_buffer_size
<= entrylen
- 1 + name_length
);
507 name_buffer
= xrealloc (name_buffer
, name_buffer_size
+ 2);
509 strcpy (name_buffer
+ name_length
, entry
+ 1);
511 if (excluded_name (name_buffer
))
515 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
517 stat_diag (name_buffer
);
522 if (S_ISDIR (stat_data
.st_mode
))
524 procdir (name_buffer
, &stat_data
, device
,
530 else if (one_file_system_option
&& device
!= stat_data
.st_dev
)
533 else if (*entry
== 'Y')
534 /* New entry, skip further checks */;
536 /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
538 else if (OLDER_STAT_TIME (stat_data
, m
)
539 && (!after_date_option
540 || OLDER_STAT_TIME (stat_data
, c
)))
552 return directory
->contents
;
556 get_directory_contents (char *dir_name
, dev_t device
)
558 return scan_directory (dir_name
, device
);
563 obstack_code_rename (struct obstack
*stk
, char *from
, char *to
)
565 obstack_1grow (stk
, 'R');
566 obstack_grow (stk
, from
, strlen (from
) + 1);
567 obstack_1grow (stk
, 'T');
568 obstack_grow (stk
, to
, strlen (to
) + 1);
572 rename_handler (void *data
, void *proc_data
)
574 struct directory
*dir
= data
;
575 struct obstack
*stk
= proc_data
;
577 if (DIR_IS_RENAMED (dir
))
579 struct directory
*prev
, *p
;
581 /* Detect eventual cycles and clear DIRF_RENAMED flag, so this entries
582 are ignored when hit by this function next time.
583 If the chain forms a cycle, prev points to the entry DIR is renamed
584 from. In this case it still retains DIRF_RENAMED flag, which will be
585 cleared in the `else' branch below */
586 for (prev
= dir
; prev
&& prev
->orig
!= dir
; prev
= prev
->orig
)
587 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
591 for (p
= dir
; p
&& p
->orig
; p
= p
->orig
)
592 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
598 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
600 /* Break the cycle by using a temporary name for one of its
602 First, create a temp name stub entry. */
603 temp_name
= dir_name (dir
->name
);
604 obstack_1grow (stk
, 'X');
605 obstack_grow (stk
, temp_name
, strlen (temp_name
) + 1);
607 obstack_code_rename (stk
, dir
->name
, "");
609 for (p
= dir
; p
!= prev
; p
= p
->orig
)
610 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
612 obstack_code_rename (stk
, "", prev
->name
);
619 append_incremental_renames (const char *dump
)
624 if (directory_table
== NULL
)
630 size
= dumpdir_size (dump
) - 1;
631 obstack_grow (&stk
, dump
, size
);
636 hash_do_for_each (directory_table
, rename_handler
, &stk
);
637 if (obstack_object_size (&stk
) != size
)
639 obstack_1grow (&stk
, 0);
640 dump
= obstack_finish (&stk
);
643 obstack_free (&stk
, NULL
);
649 static FILE *listed_incremental_stream
;
651 /* Version of incremental format snapshots (directory files) used by this
652 tar. Currently it is supposed to be a single decimal number. 0 means
653 incremental snapshots as per tar version before 1.15.2.
655 The current tar version supports incremental versions from
656 0 up to TAR_INCREMENTAL_VERSION, inclusive.
657 It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
659 #define TAR_INCREMENTAL_VERSION 2
661 /* Read incremental snapshot formats 0 and 1 */
663 read_incr_db_01 (int version
, const char *initbuf
)
676 if (getline (&buf
, &bufsize
, listed_incremental_stream
) <= 0)
678 read_error (listed_incremental_option
);
686 buf
= strdup (initbuf
);
687 bufsize
= strlen (buf
) + 1;
690 sec
= TYPE_MINIMUM (time_t);
693 u
= strtoumax (buf
, &ebuf
, 10);
694 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
696 if (errno
|| buf
== ebuf
)
697 ERROR ((0, errno
, "%s:%ld: %s",
698 quotearg_colon (listed_incremental_option
),
700 _("Invalid time stamp")));
705 if (version
== 1 && *ebuf
)
707 char const *buf_ns
= ebuf
+ 1;
709 u
= strtoumax (buf_ns
, &ebuf
, 10);
710 if (!errno
&& BILLION
<= u
)
712 if (errno
|| buf_ns
== ebuf
)
714 ERROR ((0, errno
, "%s:%ld: %s",
715 quotearg_colon (listed_incremental_option
),
717 _("Invalid time stamp")));
718 sec
= TYPE_MINIMUM (time_t);
725 /* pre-1 incremental format does not contain nanoseconds */
729 newer_mtime_option
.tv_sec
= sec
;
730 newer_mtime_option
.tv_nsec
= nsec
;
733 while (0 < (n
= getline (&buf
, &bufsize
, listed_incremental_stream
)))
737 bool nfs
= buf
[0] == '+';
738 char *strp
= buf
+ nfs
;
739 struct timespec mtime
;
743 if (buf
[n
- 1] == '\n')
749 u
= strtoumax (strp
, &ebuf
, 10);
750 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
752 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
754 ERROR ((0, errno
, "%s:%ld: %s",
755 quotearg_colon (listed_incremental_option
), lineno
,
756 _("Invalid modification time (seconds)")));
764 u
= strtoumax (strp
, &ebuf
, 10);
765 if (!errno
&& BILLION
<= u
)
767 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
769 ERROR ((0, errno
, "%s:%ld: %s",
770 quotearg_colon (listed_incremental_option
), lineno
,
771 _("Invalid modification time (nanoseconds)")));
777 mtime
.tv_nsec
= nsec
;
781 memset (&mtime
, 0, sizeof mtime
);
784 u
= strtoumax (strp
, &ebuf
, 10);
785 if (!errno
&& TYPE_MAXIMUM (dev_t
) < u
)
787 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
789 ERROR ((0, errno
, "%s:%ld: %s",
790 quotearg_colon (listed_incremental_option
), lineno
,
791 _("Invalid device number")));
799 u
= strtoumax (strp
, &ebuf
, 10);
800 if (!errno
&& TYPE_MAXIMUM (ino_t
) < u
)
802 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
804 ERROR ((0, errno
, "%s:%ld: %s",
805 quotearg_colon (listed_incremental_option
), lineno
,
806 _("Invalid inode number")));
814 unquote_string (strp
);
815 note_directory (strp
, mtime
, dev
, ino
, nfs
, false, NULL
);
820 /* Read a nul-terminated string from FP and store it in STK.
821 Store the number of bytes read (including nul terminator) in PCOUNT.
823 Return the last character read or EOF on end of file. */
825 read_obstack (FILE *fp
, struct obstack
*stk
, size_t *pcount
)
830 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
831 obstack_1grow (stk
, c
);
832 obstack_1grow (stk
, 0);
838 /* Read from file FP a nul-terminated string and convert it to
839 intmax_t. Return the resulting value in PVAL. Assume '-' has
842 Throw a fatal error if the string cannot be converted or if the
843 converted value is less than MIN_VAL. */
846 read_negative_num (FILE *fp
, intmax_t min_val
, intmax_t *pval
)
850 char buf
[INT_BUFSIZE_BOUND (intmax_t)];
854 for (i
= 1; ISDIGIT (c
= getc (fp
)); i
++)
856 if (i
== sizeof buf
- 1)
857 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
864 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
866 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
871 *pval
= strtoimax (buf
, &ep
, 10);
872 if (c
|| errno
|| *pval
< min_val
)
873 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
876 /* Read from file FP a nul-terminated string and convert it to
877 uintmax_t. Return the resulting value in PVAL. Assume C has
880 Throw a fatal error if the string cannot be converted or if the
881 converted value exceeds MAX_VAL.
883 Return the last character read or EOF on end of file. */
886 read_unsigned_num (int c
, FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
889 char buf
[UINTMAX_STRSIZE_BOUND
], *ep
;
891 for (i
= 0; ISDIGIT (c
); i
++)
893 if (i
== sizeof buf
- 1)
894 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
902 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
906 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
911 *pval
= strtoumax (buf
, &ep
, 10);
912 if (c
|| errno
|| max_val
< *pval
)
913 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
917 /* Read from file FP a nul-terminated string and convert it to
918 uintmax_t. Return the resulting value in PVAL.
920 Throw a fatal error if the string cannot be converted or if the
921 converted value exceeds MAX_VAL.
923 Return the last character read or EOF on end of file. */
926 read_num (FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
928 return read_unsigned_num (getc (fp
), fp
, max_val
, pval
);
931 /* Read from FP two NUL-terminated strings representing a struct
932 timespec. Return the resulting value in PVAL.
934 Throw a fatal error if the string cannot be converted. */
937 read_timespec (FILE *fp
, struct timespec
*pval
)
945 read_negative_num (fp
, TYPE_MINIMUM (time_t), &i
);
951 c
= read_unsigned_num (c
, fp
, TYPE_MAXIMUM (time_t), &u
);
955 if (c
|| read_num (fp
, BILLION
- 1, &u
))
956 FATAL_ERROR ((0, 0, "%s: %s",
957 quotearg_colon (listed_incremental_option
),
958 _("Unexpected EOF in snapshot file")));
962 /* Read incremental snapshot format 2 */
971 read_timespec (listed_incremental_stream
, &newer_mtime_option
);
975 struct timespec mtime
;
983 if (read_num (listed_incremental_stream
, 1, &u
))
984 return; /* Normal return */
988 read_timespec (listed_incremental_stream
, &mtime
);
990 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (dev_t
), &u
))
994 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (ino_t
), &u
))
998 if (read_obstack (listed_incremental_stream
, &stk
, &s
))
1001 name
= obstack_finish (&stk
);
1003 while (read_obstack (listed_incremental_stream
, &stk
, &s
) == 0 && s
> 1)
1005 if (getc (listed_incremental_stream
) != 0)
1006 FATAL_ERROR ((0, 0, "%s: %s",
1007 quotearg_colon (listed_incremental_option
),
1008 _("Missing record terminator")));
1010 content
= obstack_finish (&stk
);
1011 note_directory (name
, mtime
, dev
, ino
, nfs
, false, content
);
1012 obstack_free (&stk
, content
);
1014 FATAL_ERROR ((0, 0, "%s: %s",
1015 quotearg_colon (listed_incremental_option
),
1016 _("Unexpected EOF in snapshot file")));
1019 /* Read incremental snapshot file (directory file).
1020 If the file has older incremental version, make sure that it is processed
1021 correctly and that tar will use the most conservative backup method among
1022 possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
1023 etc.) This ensures that the snapshots are updated to the recent version
1024 without any loss of data. */
1026 read_directory_file (void)
1032 /* Open the file for both read and write. That way, we can write
1033 it later without having to reopen it, and don't have to worry if
1034 we chdir in the meantime. */
1035 fd
= open (listed_incremental_option
, O_RDWR
| O_CREAT
, MODE_RW
);
1038 open_error (listed_incremental_option
);
1042 listed_incremental_stream
= fdopen (fd
, "r+");
1043 if (! listed_incremental_stream
)
1045 open_error (listed_incremental_option
);
1050 if (0 < getline (&buf
, &bufsize
, listed_incremental_stream
))
1053 uintmax_t incremental_version
;
1055 if (strncmp (buf
, PACKAGE_NAME
, sizeof PACKAGE_NAME
- 1) == 0)
1057 ebuf
= buf
+ sizeof PACKAGE_NAME
- 1;
1059 ERROR((1, 0, _("Bad incremental file format")));
1060 for (; *ebuf
!= '-'; ebuf
++)
1062 ERROR((1, 0, _("Bad incremental file format")));
1064 incremental_version
= strtoumax (ebuf
+ 1, NULL
, 10);
1067 incremental_version
= 0;
1069 switch (incremental_version
)
1073 read_incr_db_01 (incremental_version
, buf
);
1076 case TAR_INCREMENTAL_VERSION
:
1081 ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX
),
1082 incremental_version
));
1087 if (ferror (listed_incremental_stream
))
1088 read_error (listed_incremental_option
);
1093 /* Output incremental data for the directory ENTRY to the file DATA.
1094 Return nonzero if successful, preserving errno on write failure. */
1096 write_directory_file_entry (void *entry
, void *data
)
1098 struct directory
const *directory
= entry
;
1101 if (DIR_IS_FOUND (directory
))
1103 char buf
[UINTMAX_STRSIZE_BOUND
];
1106 s
= DIR_IS_NFS (directory
) ? "1" : "0";
1107 fwrite (s
, 2, 1, fp
);
1108 s
= (TYPE_SIGNED (time_t)
1109 ? imaxtostr (directory
->mtime
.tv_sec
, buf
)
1110 : umaxtostr (directory
->mtime
.tv_sec
, buf
));
1111 fwrite (s
, strlen (s
) + 1, 1, fp
);
1112 s
= umaxtostr (directory
->mtime
.tv_nsec
, buf
);
1113 fwrite (s
, strlen (s
) + 1, 1, fp
);
1114 s
= umaxtostr (directory
->device_number
, buf
);
1115 fwrite (s
, strlen (s
) + 1, 1, fp
);
1116 s
= umaxtostr (directory
->inode_number
, buf
);
1117 fwrite (s
, strlen (s
) + 1, 1, fp
);
1119 fwrite (directory
->name
, strlen (directory
->name
) + 1, 1, fp
);
1120 if (directory
->contents
)
1123 for (p
= directory
->contents
; *p
; p
+= strlen (p
) + 1)
1125 if (strchr ("YND", *p
))
1126 fwrite (p
, strlen (p
) + 1, 1, fp
);
1129 fwrite ("\0\0", 2, 1, fp
);
1132 return ! ferror (fp
);
1136 write_directory_file (void)
1138 FILE *fp
= listed_incremental_stream
;
1139 char buf
[UINTMAX_STRSIZE_BOUND
];
1145 if (fseek (fp
, 0L, SEEK_SET
) != 0)
1146 seek_error (listed_incremental_option
);
1147 if (sys_truncate (fileno (fp
)) != 0)
1148 truncate_error (listed_incremental_option
);
1150 fprintf (fp
, "%s-%s-%d\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1151 TAR_INCREMENTAL_VERSION
);
1153 s
= (TYPE_SIGNED (time_t)
1154 ? imaxtostr (start_time
.tv_sec
, buf
)
1155 : umaxtostr (start_time
.tv_sec
, buf
));
1156 fwrite (s
, strlen (s
) + 1, 1, fp
);
1157 s
= umaxtostr (start_time
.tv_nsec
, buf
);
1158 fwrite (s
, strlen (s
) + 1, 1, fp
);
1160 if (! ferror (fp
) && directory_table
)
1161 hash_do_for_each (directory_table
, write_directory_file_entry
, fp
);
1164 write_error (listed_incremental_option
);
1165 if (fclose (fp
) != 0)
1166 close_error (listed_incremental_option
);
1170 /* Restoration of incremental dumps. */
1173 get_gnu_dumpdir (struct tar_stat_info
*stat_info
)
1177 union block
*data_block
;
1181 size
= stat_info
->stat
.st_size
;
1183 archive_dir
= xmalloc (size
);
1186 set_next_block_after (current_header
);
1187 mv_begin (stat_info
);
1189 for (; size
> 0; size
-= copied
)
1191 mv_size_left (size
);
1192 data_block
= find_next_block ();
1194 ERROR ((1, 0, _("Unexpected EOF in archive")));
1195 copied
= available_space_after (data_block
);
1198 memcpy (to
, data_block
->buffer
, copied
);
1200 set_next_block_after ((union block
*)
1201 (data_block
->buffer
+ copied
- 1));
1206 stat_info
->dumpdir
= archive_dir
;
1207 stat_info
->skipped
= true; /* For skip_member() and friends
1208 to work correctly */
1211 /* Return T if STAT_INFO represents a dumpdir archive member.
1212 Note: can invalidate current_header. It happens if flush_archive()
1213 gets called within get_gnu_dumpdir() */
1215 is_dumpdir (struct tar_stat_info
*stat_info
)
1217 if (stat_info
->is_dumpdir
&& !stat_info
->dumpdir
)
1218 get_gnu_dumpdir (stat_info
);
1219 return stat_info
->is_dumpdir
;
1223 dumpdir_ok (char *dumpdir
)
1226 int has_tempdir
= 0;
1229 for (p
= dumpdir
; *p
; p
+= strlen (p
) + 1)
1231 if (expect
&& *p
!= expect
)
1234 _("Malformed dumpdir: expected '%c' but found %#3o"),
1244 _("Malformed dumpdir: 'X' duplicated")));
1257 _("Malformed dumpdir: empty name in 'R'")));
1270 _("Malformed dumpdir: 'T' not preceeded by 'R'")));
1273 if (p
[1] == 0 && !has_tempdir
)
1276 _("Malformed dumpdir: empty name in 'T'")));
1288 /* FIXME: bail out? */
1296 _("Malformed dumpdir: expected '%c' but found end of data"),
1302 WARN ((0, 0, _("Malformed dumpdir: 'X' never used")));
1307 /* Examine the directories under directory_name and delete any
1308 files that were not there at the time of the back-up. */
1310 try_purge_directory (char const *directory_name
)
1313 char *cur
, *arc
, *p
;
1314 char *temp_stub
= NULL
;
1316 if (!is_dumpdir (¤t_stat_info
))
1319 current_dir
= savedir (directory_name
);
1322 /* The directory doesn't exist now. It'll be created. In any
1323 case, we don't have to delete any files out of it. */
1326 /* Verify if dump directory is sane */
1327 if (!dumpdir_ok (current_stat_info
.dumpdir
))
1330 /* Process renames */
1331 for (arc
= current_stat_info
.dumpdir
; *arc
; arc
+= strlen (arc
) + 1)
1335 #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
1336 size_t len
= strlen (arc
+ 1);
1337 temp_stub
= xrealloc (temp_stub
, len
+ 1 + sizeof TEMP_DIR_TEMPLATE
);
1338 memcpy (temp_stub
, arc
+ 1, len
);
1339 temp_stub
[len
] = '/';
1340 memcpy (temp_stub
+ len
+ 1, TEMP_DIR_TEMPLATE
,
1341 sizeof TEMP_DIR_TEMPLATE
);
1342 if (!mkdtemp (temp_stub
))
1345 _("Cannot create temporary directory using template %s"),
1346 quote (temp_stub
)));
1352 else if (*arc
== 'R')
1356 arc
+= strlen (arc
) + 1;
1364 if (!rename_directory (src
, dst
))
1368 /* FIXME: Make sure purge_directory(dst) will return
1377 /* Process deletes */
1379 for (cur
= current_dir
; *cur
; cur
+= strlen (cur
) + 1)
1385 p
= new_name (directory_name
, cur
);
1387 if (deref_stat (false, p
, &st
))
1389 if (errno
!= ENOENT
) /* FIXME: Maybe keep a list of renamed
1390 dirs and check it here? */
1393 WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
1394 quotearg_colon (p
)));
1399 if (!(entry
= dumpdir_locate (current_stat_info
.dumpdir
, cur
))
1400 || (*entry
== 'D' && !S_ISDIR (st
.st_mode
))
1401 || (*entry
== 'Y' && S_ISDIR (st
.st_mode
)))
1403 if (one_file_system_option
&& st
.st_dev
!= root_device
)
1406 _("%s: directory is on a different device: not purging"),
1407 quotearg_colon (p
)));
1411 if (! interactive_option
|| confirm ("delete", p
))
1414 fprintf (stdlis
, _("%s: Deleting %s\n"),
1415 program_name
, quote (p
));
1416 if (! remove_any_file (p
, RECURSIVE_REMOVE_OPTION
))
1419 ERROR ((0, e
, _("%s: Cannot remove"), quotearg_colon (p
)));
1431 purge_directory (char const *directory_name
)
1433 if (!try_purge_directory (directory_name
))
1438 list_dumpdir (char *buffer
, size_t size
)
1450 fprintf (stdlis
, "%c ", *buffer
);
1456 fputc ('\n', stdlis
);
1462 fputc (*buffer
, stdlis
);