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. */
26 /* Incremental dump specialities. */
28 /* Which child files to save under a directory. */
36 #define DIRF_INIT 0x0001 /* directory structure is initialized
37 (procdir called at least once) */
38 #define DIRF_NFS 0x0002 /* directory is mounted on nfs */
39 #define DIRF_FOUND 0x0004 /* directory is found on fs */
40 #define DIRF_NEW 0x0008 /* directory is new (not found
41 in the previous dump) */
42 #define DIRF_RENAMED 0x0010 /* directory is renamed */
44 #define DIR_IS_INITED(d) ((d)->flags & DIRF_INIT)
45 #define DIR_IS_NFS(d) ((d)->flags & DIRF_NFS)
46 #define DIR_IS_FOUND(d) ((d)->flags & DIRF_FOUND)
47 #define DIR_IS_NEW(d) ((d)->flags & DIRF_NEW)
48 #define DIR_IS_RENAMED(d) ((d)->flags & DIRF_RENAMED)
50 #define DIR_SET_FLAG(d,f) (d)->flags |= (f)
51 #define DIR_CLEAR_FLAG(d,f) (d)->flags &= ~(f)
53 /* Directory attributes. */
56 struct timespec mtime
; /* Modification time */
57 dev_t device_number
; /* device number for directory */
58 ino_t inode_number
; /* inode number for directory */
59 char *contents
; /* Directory contents */
60 char *icontents
; /* Initial contents if the directory was
62 enum children children
; /* What to save under this directory */
63 unsigned flags
; /* See DIRF_ macros above */
64 struct directory
*orig
; /* If the directory was renamed, points to
65 the original directory structure */
66 char name
[1]; /* file name of directory */
69 static Hash_table
*directory_table
;
70 static Hash_table
*directory_meta_table
;
72 #if HAVE_ST_FSTYPE_STRING
73 static char const nfs_string
[] = "nfs";
74 # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
76 # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
77 # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
80 /* Calculate the hash of a directory. */
82 hash_directory_name (void const *entry
, size_t n_buckets
)
84 struct directory
const *directory
= entry
;
85 return hash_string (directory
->name
, n_buckets
);
88 /* Compare two directories for equality of their names. */
90 compare_directory_names (void const *entry1
, void const *entry2
)
92 struct directory
const *directory1
= entry1
;
93 struct directory
const *directory2
= entry2
;
94 return strcmp (directory1
->name
, directory2
->name
) == 0;
98 hash_directory_meta (void const *entry
, size_t n_buckets
)
100 struct directory
const *directory
= entry
;
101 /* FIXME: Work out a better algorytm */
102 return (directory
->device_number
+ directory
->inode_number
) % n_buckets
;
105 /* Compare two directories for equality of their device and inode numbers. */
107 compare_directory_meta (void const *entry1
, void const *entry2
)
109 struct directory
const *directory1
= entry1
;
110 struct directory
const *directory2
= entry2
;
111 return directory1
->device_number
== directory2
->device_number
112 && directory1
->inode_number
== directory2
->inode_number
;
115 /* Make a directory entry for given NAME */
116 static struct directory
*
117 make_directory (const char *name
)
119 size_t namelen
= strlen (name
);
120 size_t size
= offsetof (struct directory
, name
) + namelen
+ 1;
121 struct directory
*directory
= xmalloc (size
);
122 strcpy (directory
->name
, name
);
123 if (ISSLASH (directory
->name
[namelen
-1]))
124 directory
->name
[namelen
-1] = 0;
125 directory
->flags
= false;
129 /* Create and link a new directory entry for directory NAME, having a
130 device number DEV and an inode number INO, with NFS indicating
131 whether it is an NFS device and FOUND indicating whether we have
132 found that the directory exists. */
133 static struct directory
*
134 note_directory (char const *name
, struct timespec mtime
,
135 dev_t dev
, ino_t ino
, bool nfs
, bool found
, char *contents
)
137 struct directory
*directory
= make_directory (name
);
139 directory
->mtime
= mtime
;
140 directory
->device_number
= dev
;
141 directory
->inode_number
= ino
;
142 directory
->children
= CHANGED_CHILDREN
;
144 DIR_SET_FLAG (directory
, DIRF_NFS
);
146 DIR_SET_FLAG (directory
, DIRF_FOUND
);
149 size_t size
= dumpdir_size (contents
);
150 directory
->contents
= xmalloc (size
);
151 memcpy (directory
->contents
, contents
, size
);
154 directory
->contents
= NULL
;
156 if (! ((directory_table
157 || (directory_table
= hash_initialize (0, 0,
159 compare_directory_names
, 0)))
160 && hash_insert (directory_table
, directory
)))
163 if (! ((directory_meta_table
164 || (directory_meta_table
= hash_initialize (0, 0,
166 compare_directory_meta
,
168 && hash_insert (directory_meta_table
, directory
)))
174 /* Return a directory entry for a given file NAME, or zero if none found. */
175 static struct directory
*
176 find_directory (const char *name
)
178 if (! directory_table
)
182 struct directory
*dir
= make_directory (name
);
183 struct directory
*ret
= hash_lookup (directory_table
, dir
);
189 /* Return a directory entry for a given combination of device and inode
190 numbers, or zero if none found. */
191 static struct directory
*
192 find_directory_meta (dev_t dev
, ino_t ino
)
194 if (! directory_meta_table
)
198 struct directory
*dir
= make_directory ("");
199 struct directory
*ret
;
200 dir
->device_number
= dev
;
201 dir
->inode_number
= ino
;
202 ret
= hash_lookup (directory_meta_table
, dir
);
209 update_parent_directory (const char *name
)
211 struct directory
*directory
;
212 char *p
, *name_buffer
;
215 directory
= find_directory (p
);
219 if (deref_stat (dereference_option
, p
, &st
) != 0)
222 directory
->mtime
= get_stat_mtime (&st
);
227 static struct directory
*
228 procdir (char *name_buffer
, struct stat
*stat_data
,
230 enum children children
,
233 struct directory
*directory
;
234 bool nfs
= NFS_FILE_STAT (*stat_data
);
237 if ((directory
= find_directory (name_buffer
)) != NULL
)
239 if (DIR_IS_INITED (directory
))
242 /* With NFS, the same file can have two different devices
243 if an NFS directory is mounted in multiple locations,
244 which is relatively common when automounting.
245 To avoid spurious incremental redumping of
246 directories, consider all NFS devices as equal,
247 relying on the i-node to establish differences. */
249 if (! (((DIR_IS_NFS (directory
) & nfs
)
250 || directory
->device_number
== stat_data
->st_dev
)
251 && directory
->inode_number
== stat_data
->st_ino
))
253 /* FIXME: find_directory_meta ignores nfs */
254 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
259 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
260 quotearg_colon (name_buffer
),
261 quote_n (1, d
->name
)));
263 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
264 directory
->children
= CHANGED_CHILDREN
;
269 WARN ((0, 0, _("%s: Directory has been renamed"),
270 quotearg_colon (name_buffer
)));
271 directory
->children
= ALL_CHILDREN
;
272 directory
->device_number
= stat_data
->st_dev
;
273 directory
->inode_number
= stat_data
->st_ino
;
276 DIR_SET_FLAG (directory
, DIRF_NFS
);
279 directory
->children
= CHANGED_CHILDREN
;
281 DIR_SET_FLAG (directory
, DIRF_FOUND
);
285 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
288 directory
= note_directory (name_buffer
,
289 get_stat_mtime(stat_data
),
299 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
300 quotearg_colon (name_buffer
),
301 quote_n (1, d
->name
)));
303 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
304 directory
->children
= CHANGED_CHILDREN
;
308 DIR_SET_FLAG (directory
, DIRF_NEW
);
310 WARN ((0, 0, _("%s: Directory is new"),
311 quotearg_colon (name_buffer
)));
312 directory
->children
=
313 (listed_incremental_option
314 || (OLDER_STAT_TIME (*stat_data
, m
)
315 || (after_date_option
316 && OLDER_STAT_TIME (*stat_data
, c
))))
322 /* If the directory is on another device and --one-file-system was given,
324 if (one_file_system_option
&& device
!= stat_data
->st_dev
325 /* ... except if it was explicitely given in the command line */
326 && !((np
= name_scan (name_buffer
, true)) && np
->explicit))
327 directory
->children
= NO_CHILDREN
;
328 else if (children
== ALL_CHILDREN
)
329 directory
->children
= ALL_CHILDREN
;
331 DIR_SET_FLAG (directory
, DIRF_INIT
);
336 /* Locate NAME in the dumpdir array DUMP.
337 Return pointer to the slot in the array, or NULL if not found */
339 dumpdir_locate (const char *dump
, const char *name
)
344 /* Ignore 'R' (rename) entries, since they break alphabetical ordering.
345 They normally do not occur in dumpdirs from the snapshot files,
346 but this function is also used by purge_directory, which operates
347 on a dumpdir from the archive, hence the need for this test. */
350 int rc
= strcmp (dump
+ 1, name
);
356 dump
+= strlen (dump
) + 1;
361 /* Return size in bytes of the dumpdir array P */
363 dumpdir_size (const char *p
)
369 size_t size
= strlen (p
) + 1;
377 compare_dirnames (const void *first
, const void *second
)
379 return strcmp (*(const char**)first
, *(const char**)second
);
382 /* Compare dumpdir array from DIRECTORY with directory listing DIR and
383 build a new dumpdir template.
385 DIR must be returned by a previous call to savedir().
387 File names in DIRECTORY->contents must be sorted
390 DIRECTORY->contents is replaced with the created template. Each entry is
391 prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
394 makedumpdir (struct directory
*directory
, const char *dir
)
397 dirsize
, /* Number of elements in DIR */
398 len
; /* Length of DIR, including terminating nul */
401 char *new_dump
, *new_dump_ptr
;
404 if (directory
->children
== ALL_CHILDREN
)
406 else if (DIR_IS_RENAMED (directory
))
407 dump
= directory
->orig
->icontents
?
408 directory
->orig
->icontents
: directory
->orig
->contents
;
410 dump
= directory
->contents
;
412 /* Count the size of DIR and the number of elements it contains */
415 for (p
= dir
; *p
; p
+= strlen (p
) + 1, dirsize
++)
416 len
+= strlen (p
) + 2;
419 /* Create a sorted directory listing */
420 array
= xcalloc (dirsize
, sizeof array
[0]);
421 for (i
= 0, p
= dir
; *p
; p
+= strlen (p
) + 1, i
++)
424 qsort (array
, dirsize
, sizeof (array
[0]), compare_dirnames
);
426 /* Prepare space for new dumpdir */
427 new_dump
= xmalloc (len
);
428 new_dump_ptr
= new_dump
;
430 /* Fill in the dumpdir template */
431 for (i
= 0; i
< dirsize
; i
++)
433 const char *loc
= dumpdir_locate (dump
, array
[i
]);
436 *new_dump_ptr
++ = ' ';
437 dump
= loc
+ strlen (loc
) + 1;
440 *new_dump_ptr
++ = 'Y'; /* New entry */
442 /* Copy the file name */
443 for (p
= array
[i
]; (*new_dump_ptr
++ = *p
++); )
447 directory
->icontents
= directory
->contents
;
448 directory
->contents
= new_dump
;
452 /* Recursively scan the given directory. */
454 scan_directory (char *dir_name
, dev_t device
)
456 char *dirp
= savedir (dir_name
); /* for scanning directory */
457 char *name_buffer
; /* directory, `/', and directory member */
458 size_t name_buffer_size
; /* allocated size of name_buffer, minus 2 */
459 size_t name_length
; /* used length in name_buffer */
460 struct stat stat_data
;
461 struct directory
*directory
;
464 savedir_error (dir_name
);
466 name_buffer_size
= strlen (dir_name
) + NAME_FIELD_SIZE
;
467 name_buffer
= xmalloc (name_buffer_size
+ 2);
468 strcpy (name_buffer
, dir_name
);
469 if (! ISSLASH (dir_name
[strlen (dir_name
) - 1]))
470 strcat (name_buffer
, "/");
471 name_length
= strlen (name_buffer
);
473 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
475 stat_diag (name_buffer
);
477 children = CHANGED_CHILDREN;
484 directory
= procdir (name_buffer
, &stat_data
, device
, NO_CHILDREN
, false);
486 if (dirp
&& directory
->children
!= NO_CHILDREN
)
488 char *entry
; /* directory entry being scanned */
489 size_t entrylen
; /* length of directory entry */
491 makedumpdir (directory
, dirp
);
493 for (entry
= directory
->contents
;
494 (entrylen
= strlen (entry
)) != 0;
495 entry
+= entrylen
+ 1)
497 if (name_buffer_size
<= entrylen
- 1 + name_length
)
500 name_buffer_size
+= NAME_FIELD_SIZE
;
501 while (name_buffer_size
<= entrylen
- 1 + name_length
);
502 name_buffer
= xrealloc (name_buffer
, name_buffer_size
+ 2);
504 strcpy (name_buffer
+ name_length
, entry
+ 1);
506 if (excluded_name (name_buffer
))
510 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
512 stat_diag (name_buffer
);
517 if (S_ISDIR (stat_data
.st_mode
))
519 procdir (name_buffer
, &stat_data
, device
,
525 else if (one_file_system_option
&& device
!= stat_data
.st_dev
)
528 else if (*entry
== 'Y')
529 /* New entry, skip further checks */;
531 /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
533 else if (OLDER_STAT_TIME (stat_data
, m
)
534 && (!after_date_option
535 || OLDER_STAT_TIME (stat_data
, c
)))
547 return directory
->contents
;
551 get_directory_contents (char *dir_name
, dev_t device
)
553 return scan_directory (dir_name
, device
);
558 try_pos (char *name
, int pos
, const char *dumpdir
)
561 static char namechars
[] =
562 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
565 for (i
= 0; i
< sizeof namechars
; i
++)
567 name
[pos
] = namechars
[i
];
568 if (!dumpdir_locate (dumpdir
, name
)
569 || try_pos (name
, pos
-1, dumpdir
))
577 create_temp_name (char *name
, const char *dumpdir
)
579 size_t pos
= strlen (name
) - 6;
580 return try_pos (name
+ pos
, 5, dumpdir
);
584 make_tmp_dir_name (const char *name
)
586 char *dirname
= dir_name (name
);
587 char *tmp_name
= NULL
;
588 struct directory
*dir
= find_directory (dirname
);
590 tmp_name
= new_name (dirname
, "000000");
591 if (!create_temp_name (tmp_name
, dir
? dir
->contents
: NULL
))
601 obstack_code_rename (struct obstack
*stk
, char *from
, char *to
)
603 obstack_1grow (stk
, 'R');
604 obstack_grow (stk
, from
, strlen (from
) + 1);
605 obstack_1grow (stk
, 'T');
606 obstack_grow (stk
, to
, strlen (to
) + 1);
610 rename_handler (void *data
, void *proc_data
)
612 struct directory
*dir
= data
;
613 struct obstack
*stk
= proc_data
;
615 if (DIR_IS_RENAMED (dir
))
617 struct directory
*prev
, *p
;
619 /* Detect eventual cycles and clear DIRF_RENAMED flag, so this entries
620 are ignored when hit by this function next time.
621 If the chain forms a cycle, prev points to the entry DIR is renamed
622 from. In this case it still retains DIRF_RENAMED flag, which will be
623 cleared in the `else' branch below */
624 for (prev
= dir
; prev
&& prev
->orig
!= dir
; prev
= prev
->orig
)
625 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
629 for (p
= dir
; p
&& p
->orig
; p
= p
->orig
)
630 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
636 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
638 /* Break the cycle by using a temporary name for one of its
640 FIXME: Leave the choice of the name to the extractor. */
641 temp_name
= make_tmp_dir_name (dir
->name
);
642 obstack_code_rename (stk
, dir
->name
, temp_name
);
644 for (p
= dir
; p
!= prev
; p
= p
->orig
)
645 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
647 obstack_code_rename (stk
, temp_name
, prev
->name
);
654 append_incremental_renames (const char *dump
)
659 if (directory_table
== NULL
)
665 size
= dumpdir_size (dump
) - 1;
666 obstack_grow (&stk
, dump
, size
);
671 hash_do_for_each (directory_table
, rename_handler
, &stk
);
672 if (obstack_object_size (&stk
) != size
)
674 obstack_1grow (&stk
, 0);
675 dump
= obstack_finish (&stk
);
678 obstack_free (&stk
, NULL
);
684 static FILE *listed_incremental_stream
;
686 /* Version of incremental format snapshots (directory files) used by this
687 tar. Currently it is supposed to be a single decimal number. 0 means
688 incremental snapshots as per tar version before 1.15.2.
690 The current tar version supports incremental versions from
691 0 up to TAR_INCREMENTAL_VERSION, inclusive.
692 It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
694 #define TAR_INCREMENTAL_VERSION 2
696 /* Read incremental snapshot formats 0 and 1 */
698 read_incr_db_01 (int version
, const char *initbuf
)
710 if (getline (&buf
, &bufsize
, listed_incremental_stream
) <= 0)
712 read_error (listed_incremental_option
);
720 buf
= strdup (initbuf
);
721 bufsize
= strlen (buf
) + 1;
724 t
= u
= (errno
= 0, strtoumax (buf
, &ebuf
, 10));
725 if (buf
== ebuf
|| (u
== 0 && errno
== EINVAL
))
726 ERROR ((0, 0, "%s:%ld: %s",
727 quotearg_colon (listed_incremental_option
),
729 _("Invalid time stamp")));
731 ERROR ((0, 0, "%s:%ld: %s",
732 quotearg_colon (listed_incremental_option
),
734 _("Time stamp out of range")));
735 else if (version
== 1)
737 newer_mtime_option
.tv_sec
= t
;
739 t
= u
= (errno
= 0, strtoumax (buf
, &ebuf
, 10));
740 if (buf
== ebuf
|| (u
== 0 && errno
== EINVAL
))
741 ERROR ((0, 0, "%s:%ld: %s",
742 quotearg_colon (listed_incremental_option
),
744 _("Invalid time stamp")));
746 ERROR ((0, 0, "%s:%ld: %s",
747 quotearg_colon (listed_incremental_option
),
749 _("Time stamp out of range")));
750 newer_mtime_option
.tv_nsec
= t
;
754 /* pre-1 incremental format does not contain nanoseconds */
755 newer_mtime_option
.tv_sec
= t
;
756 newer_mtime_option
.tv_nsec
= 0;
759 while (0 < (n
= getline (&buf
, &bufsize
, listed_incremental_stream
)))
763 bool nfs
= buf
[0] == '+';
764 char *strp
= buf
+ nfs
;
765 struct timespec mtime
;
769 if (buf
[n
- 1] == '\n')
775 mtime
.tv_sec
= u
= strtoumax (strp
, &ebuf
, 10);
776 if (!isspace (*ebuf
))
777 ERROR ((0, 0, "%s:%ld: %s",
778 quotearg_colon (listed_incremental_option
), lineno
,
779 _("Invalid modification time (seconds)")));
780 else if (mtime
.tv_sec
!= u
)
781 ERROR ((0, 0, "%s:%ld: %s",
782 quotearg_colon (listed_incremental_option
), lineno
,
783 _("Modification time (seconds) out of range")));
787 mtime
.tv_nsec
= u
= strtoumax (strp
, &ebuf
, 10);
788 if (!isspace (*ebuf
))
789 ERROR ((0, 0, "%s:%ld: %s",
790 quotearg_colon (listed_incremental_option
), lineno
,
791 _("Invalid modification time (nanoseconds)")));
792 else if (mtime
.tv_nsec
!= u
)
793 ERROR ((0, 0, "%s:%ld: %s",
794 quotearg_colon (listed_incremental_option
), lineno
,
795 _("Modification time (nanoseconds) out of range")));
799 memset (&mtime
, 0, sizeof mtime
);
802 dev
= u
= strtoumax (strp
, &ebuf
, 10);
803 if (!isspace (*ebuf
))
804 ERROR ((0, 0, "%s:%ld: %s",
805 quotearg_colon (listed_incremental_option
), lineno
,
806 _("Invalid device number")));
808 ERROR ((0, 0, "%s:%ld: %s",
809 quotearg_colon (listed_incremental_option
), lineno
,
810 _("Device number out of range")));
814 ino
= u
= strtoumax (strp
, &ebuf
, 10);
815 if (!isspace (*ebuf
))
816 ERROR ((0, 0, "%s:%ld: %s",
817 quotearg_colon (listed_incremental_option
), lineno
,
818 _("Invalid inode number")));
820 ERROR ((0, 0, "%s:%ld: %s",
821 quotearg_colon (listed_incremental_option
), lineno
,
822 _("Inode number out of range")));
826 unquote_string (strp
);
827 note_directory (strp
, mtime
, dev
, ino
, nfs
, false, NULL
);
832 /* Read a nul-terminated string from FP and store it in STK.
833 Store the number of bytes read (including nul terminator) in PCOUNT.
835 Return the last character read or EOF on end of file. */
837 read_obstack (FILE *fp
, struct obstack
*stk
, size_t *pcount
)
842 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
843 obstack_1grow (stk
, c
);
844 obstack_1grow (stk
, 0);
850 /* Read from file FP a nul-terminated string and convert it to
851 uintmax_t. Return the resulting value in PVAL.
853 Throw fatal error if the string cannot be converted.
855 Return the last character read or EOF on end of file. */
858 read_num (FILE *fp
, uintmax_t *pval
)
862 char buf
[UINTMAX_STRSIZE_BOUND
], *ep
;
864 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
866 if (i
== sizeof buf
- 1)
867 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
871 *pval
= strtoumax (buf
, &ep
, 10);
873 FATAL_ERROR ((0, 0, _("Unexpected field value in snapshot file")));
877 /* Read incremental snapshot format 2 */
886 if (read_num (listed_incremental_stream
, &u
))
887 FATAL_ERROR ((0, 0, "%s: %s",
888 quotearg_colon (listed_incremental_option
),
889 _("Error reading time stamp")));
890 newer_mtime_option
.tv_sec
= u
;
891 if (newer_mtime_option
.tv_sec
!= u
)
892 FATAL_ERROR ((0, 0, "%s: %s",
893 quotearg_colon (listed_incremental_option
),
894 _("Time stamp out of range")));
896 if (read_num (listed_incremental_stream
, &u
))
897 FATAL_ERROR ((0, 0, "%s: %s",
898 quotearg_colon (listed_incremental_option
),
899 _("Error reading time stamp")));
900 newer_mtime_option
.tv_nsec
= u
;
901 if (newer_mtime_option
.tv_nsec
!= u
)
902 FATAL_ERROR ((0, 0, "%s: %s",
903 quotearg_colon (listed_incremental_option
),
904 _("Time stamp out of range")));
908 struct timespec mtime
;
916 if (read_num (listed_incremental_stream
, &u
))
917 return; /* Normal return */
921 if (read_num (listed_incremental_stream
, &u
))
924 if (mtime
.tv_sec
!= u
)
925 FATAL_ERROR ((0, 0, "%s: %s",
926 quotearg_colon (listed_incremental_option
),
927 _("Modification time (seconds) out of range")));
929 if (read_num (listed_incremental_stream
, &u
))
932 if (mtime
.tv_nsec
!= u
)
933 FATAL_ERROR ((0, 0, "%s: %s",
934 quotearg_colon (listed_incremental_option
),
935 _("Modification time (nanoseconds) out of range")));
937 if (read_num (listed_incremental_stream
, &u
))
941 FATAL_ERROR ((0, 0, "%s: %s",
942 quotearg_colon (listed_incremental_option
),
943 _("Device number out of range")));
945 if (read_num (listed_incremental_stream
, &u
))
949 FATAL_ERROR ((0, 0, "%s: %s",
950 quotearg_colon (listed_incremental_option
),
951 _("Inode number out of range")));
953 if (read_obstack (listed_incremental_stream
, &stk
, &s
))
956 name
= obstack_finish (&stk
);
958 while (read_obstack (listed_incremental_stream
, &stk
, &s
) == 0 && s
> 1)
960 if (getc (listed_incremental_stream
) != 0)
961 FATAL_ERROR ((0, 0, "%s: %s",
962 quotearg_colon (listed_incremental_option
),
963 _("Missing record terminator")));
965 content
= obstack_finish (&stk
);
966 note_directory (name
, mtime
, dev
, ino
, nfs
, false, content
);
967 obstack_free (&stk
, content
);
969 FATAL_ERROR ((0, 0, "%s: %s",
970 quotearg_colon (listed_incremental_option
),
971 _("Unexpected EOF")));
974 /* Read incremental snapshot file (directory file).
975 If the file has older incremental version, make sure that it is processed
976 correctly and that tar will use the most conservative backup method among
977 possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
978 etc.) This ensures that the snapshots are updated to the recent version
979 without any loss of data. */
981 read_directory_file (void)
988 /* Open the file for both read and write. That way, we can write
989 it later without having to reopen it, and don't have to worry if
990 we chdir in the meantime. */
991 fd
= open (listed_incremental_option
, O_RDWR
| O_CREAT
, MODE_RW
);
994 open_error (listed_incremental_option
);
998 listed_incremental_stream
= fdopen (fd
, "r+");
999 if (! listed_incremental_stream
)
1001 open_error (listed_incremental_option
);
1006 if (0 < getline (&buf
, &bufsize
, listed_incremental_stream
))
1009 int incremental_version
;
1011 if (strncmp (buf
, PACKAGE_NAME
, sizeof PACKAGE_NAME
- 1) == 0)
1013 ebuf
= buf
+ sizeof PACKAGE_NAME
- 1;
1015 ERROR((1, 0, _("Bad incremental file format")));
1016 for (; *ebuf
!= '-'; ebuf
++)
1018 ERROR((1, 0, _("Bad incremental file format")));
1020 incremental_version
= (errno
= 0, strtoumax (ebuf
+1, &ebuf
, 10));
1023 incremental_version
= 0;
1025 switch (incremental_version
)
1029 read_incr_db_01 (incremental_version
, buf
);
1032 case TAR_INCREMENTAL_VERSION
:
1037 ERROR ((1, 0, _("Unsupported incremental format version: %d"),
1038 incremental_version
));
1043 if (ferror (listed_incremental_stream
))
1044 read_error (listed_incremental_option
);
1049 /* Output incremental data for the directory ENTRY to the file DATA.
1050 Return nonzero if successful, preserving errno on write failure. */
1052 write_directory_file_entry (void *entry
, void *data
)
1054 struct directory
const *directory
= entry
;
1057 if (DIR_IS_FOUND (directory
))
1059 char buf
[UINTMAX_STRSIZE_BOUND
];
1062 s
= DIR_IS_NFS (directory
) ? "1" : "0";
1063 fwrite (s
, 2, 1, fp
);
1064 s
= umaxtostr (directory
->mtime
.tv_sec
, buf
);
1065 fwrite (s
, strlen (s
) + 1, 1, fp
);
1066 s
= umaxtostr (directory
->mtime
.tv_nsec
, buf
);
1067 fwrite (s
, strlen (s
) + 1, 1, fp
);
1068 s
= umaxtostr (directory
->device_number
, buf
);
1069 fwrite (s
, strlen (s
) + 1, 1, fp
);
1070 s
= umaxtostr (directory
->inode_number
, buf
);
1071 fwrite (s
, strlen (s
) + 1, 1, fp
);
1073 fwrite (directory
->name
, strlen (directory
->name
) + 1, 1, fp
);
1074 if (directory
->contents
)
1077 for (p
= directory
->contents
; *p
; p
+= strlen (p
) + 1)
1079 if (strchr ("YND", *p
))
1080 fwrite (p
, strlen (p
) + 1, 1, fp
);
1083 fwrite ("\0\0", 2, 1, fp
);
1086 return ! ferror (fp
);
1090 write_directory_file (void)
1092 FILE *fp
= listed_incremental_stream
;
1093 char buf
[UINTMAX_STRSIZE_BOUND
];
1099 if (fseek (fp
, 0L, SEEK_SET
) != 0)
1100 seek_error (listed_incremental_option
);
1101 if (sys_truncate (fileno (fp
)) != 0)
1102 truncate_error (listed_incremental_option
);
1104 fprintf (fp
, "%s-%s-%d\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1105 TAR_INCREMENTAL_VERSION
);
1107 s
= umaxtostr (start_time
.tv_sec
, buf
);
1108 fwrite (s
, strlen (s
) + 1, 1, fp
);
1109 s
= umaxtostr (start_time
.tv_nsec
, buf
);
1110 fwrite (s
, strlen (s
) + 1, 1, fp
);
1112 if (! ferror (fp
) && directory_table
)
1113 hash_do_for_each (directory_table
, write_directory_file_entry
, fp
);
1116 write_error (listed_incremental_option
);
1117 if (fclose (fp
) != 0)
1118 close_error (listed_incremental_option
);
1122 /* Restoration of incremental dumps. */
1125 get_gnu_dumpdir (struct tar_stat_info
*stat_info
)
1129 union block
*data_block
;
1133 size
= stat_info
->stat
.st_size
;
1135 archive_dir
= xmalloc (size
);
1138 set_next_block_after (current_header
);
1139 mv_begin (stat_info
);
1141 for (; size
> 0; size
-= copied
)
1143 mv_size_left (size
);
1144 data_block
= find_next_block ();
1146 ERROR ((1, 0, _("Unexpected EOF in archive")));
1147 copied
= available_space_after (data_block
);
1150 memcpy (to
, data_block
->buffer
, copied
);
1152 set_next_block_after ((union block
*)
1153 (data_block
->buffer
+ copied
- 1));
1158 stat_info
->dumpdir
= archive_dir
;
1159 stat_info
->skipped
= true; /* For skip_member() and friends
1160 to work correctly */
1163 /* Return T if STAT_INFO represents a dumpdir archive member.
1164 Note: can invalidate current_header. It happens if flush_archive()
1165 gets called within get_gnu_dumpdir() */
1167 is_dumpdir (struct tar_stat_info
*stat_info
)
1169 if (stat_info
->is_dumpdir
&& !stat_info
->dumpdir
)
1170 get_gnu_dumpdir (stat_info
);
1171 return stat_info
->is_dumpdir
;
1174 /* Examine the directories under directory_name and delete any
1175 files that were not there at the time of the back-up. */
1177 purge_directory (char const *directory_name
)
1180 char *cur
, *arc
, *p
;
1182 if (!is_dumpdir (¤t_stat_info
))
1188 current_dir
= savedir (directory_name
);
1192 /* The directory doesn't exist now. It'll be created. In any
1193 case, we don't have to delete any files out of it. */
1199 /* Process renames */
1200 for (arc
= current_stat_info
.dumpdir
; *arc
; arc
+= strlen (arc
) + 1)
1206 arc
+= strlen (arc
) + 1;
1209 if (!rename_directory (src
, dst
))
1212 /* FIXME: Make sure purge_directory(dst) will return
1219 /* Process deletes */
1221 for (cur
= current_dir
; *cur
; cur
+= strlen (cur
) + 1)
1227 p
= new_name (directory_name
, cur
);
1229 if (!(entry
= dumpdir_locate (current_stat_info
.dumpdir
, cur
))
1230 || (*entry
== 'D' && S_ISDIR (st
.st_mode
))
1231 || (*entry
== 'Y' && !S_ISDIR (st
.st_mode
)))
1233 if (deref_stat (false, p
, &st
))
1235 if (errno
!= ENOENT
) /* FIXME: Maybe keep a list of renamed
1236 dirs and check it here? */
1239 WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
1240 quotearg_colon (p
)));
1244 else if (one_file_system_option
&& st
.st_dev
!= root_device
)
1247 _("%s: directory is on a different device: not purging"),
1248 quotearg_colon (p
)));
1252 if (! interactive_option
|| confirm ("delete", p
))
1255 fprintf (stdlis
, _("%s: Deleting %s\n"),
1256 program_name
, quote (p
));
1257 if (! remove_any_file (p
, RECURSIVE_REMOVE_OPTION
))
1260 ERROR ((0, e
, _("%s: Cannot remove"), quotearg_colon (p
)));
1271 list_dumpdir (char *buffer
, size_t size
)
1282 fprintf (stdlis
, "%c ", *buffer
);
1288 fputc ('\n', stdlis
);
1294 fputc (*buffer
, stdlis
);