1 /* GNU dump extensions to tar.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2008 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 struct dumpdir
/* Dump directory listing */
54 char *contents
; /* Actual contents */
55 size_t total
; /* Total number of elements */
56 size_t elc
; /* Number of D/N/Y elements. */
57 char **elv
; /* Array of D/N/Y elements */
60 /* Directory attributes. */
63 struct directory
*next
;
64 struct timespec mtime
; /* Modification time */
65 dev_t device_number
; /* device number for directory */
66 ino_t inode_number
; /* inode number for directory */
67 struct dumpdir
*dump
; /* Directory contents */
68 struct dumpdir
*idump
; /* Initial contents if the directory was
70 enum children children
; /* What to save under this directory */
71 unsigned flags
; /* See DIRF_ macros above */
72 struct directory
*orig
; /* If the directory was renamed, points to
73 the original directory structure */
74 const char *tagfile
; /* Tag file, if the directory falls under
75 exclusion_tag_under */
76 char *name
; /* file name of directory */
80 dumpdir_create0 (const char *contents
, const char *cmask
)
83 size_t i
, total
, ctsize
, len
;
86 for (i
= 0, total
= 0, ctsize
= 1, p
= contents
; *p
; total
++, p
+= len
)
90 if (!cmask
|| strchr (cmask
, *p
))
93 dump
= xmalloc (sizeof (*dump
) + ctsize
);
94 dump
->contents
= (char*)(dump
+ 1);
95 memcpy (dump
->contents
, contents
, ctsize
);
98 dump
->elv
= xcalloc (i
+ 1, sizeof (dump
->elv
[0]));
100 for (i
= 0, p
= dump
->contents
; *p
; p
+= strlen (p
) + 1)
102 if (!cmask
|| strchr (cmask
, *p
))
103 dump
->elv
[i
++] = p
+ 1;
110 dumpdir_create (const char *contents
)
112 return dumpdir_create0 (contents
, "YND");
116 dumpdir_free (struct dumpdir
*dump
)
123 compare_dirnames (const void *first
, const void *second
)
125 char const *const *name1
= first
;
126 char const *const *name2
= second
;
127 return strcmp (*name1
, *name2
);
130 /* Locate NAME in the dumpdir array DUMP.
131 Return pointer to the slot in DUMP->contents, or NULL if not found */
133 dumpdir_locate (struct dumpdir
*dump
, const char *name
)
139 ptr
= bsearch (&name
, dump
->elv
, dump
->elc
, sizeof (dump
->elv
[0]),
141 return ptr
? *ptr
- 1: NULL
;
146 struct dumpdir
*dump
; /* Dumpdir being iterated */
147 int all
; /* Iterate over all entries, not only D/N/Y */
148 size_t next
; /* Index of the next element */
152 dumpdir_next (struct dumpdir_iter
*itr
)
154 size_t cur
= itr
->next
;
159 ret
= itr
->dump
->contents
+ cur
;
162 itr
->next
+= strlen (ret
) + 1;
164 else if (cur
< itr
->dump
->elc
)
166 ret
= itr
->dump
->elv
[cur
] - 1;
174 dumpdir_first (struct dumpdir
*dump
, int all
, struct dumpdir_iter
**pitr
)
176 struct dumpdir_iter
*itr
= xmalloc (sizeof (*itr
));
181 return dumpdir_next (itr
);
184 /* Return size in bytes of the dumpdir array P */
186 dumpdir_size (const char *p
)
192 size_t size
= strlen (p
) + 1;
200 static struct directory
*dirhead
, *dirtail
;
201 static Hash_table
*directory_table
;
202 static Hash_table
*directory_meta_table
;
204 #if HAVE_ST_FSTYPE_STRING
205 static char const nfs_string
[] = "nfs";
206 # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
208 # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
209 # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
212 /* Calculate the hash of a directory. */
214 hash_directory_name (void const *entry
, size_t n_buckets
)
216 struct directory
const *directory
= entry
;
217 return hash_string (directory
->name
, n_buckets
);
220 /* Compare two directories for equality of their names. */
222 compare_directory_names (void const *entry1
, void const *entry2
)
224 struct directory
const *directory1
= entry1
;
225 struct directory
const *directory2
= entry2
;
226 return strcmp (directory1
->name
, directory2
->name
) == 0;
230 hash_directory_meta (void const *entry
, size_t n_buckets
)
232 struct directory
const *directory
= entry
;
233 /* FIXME: Work out a better algorytm */
234 return (directory
->device_number
+ directory
->inode_number
) % n_buckets
;
237 /* Compare two directories for equality of their device and inode numbers. */
239 compare_directory_meta (void const *entry1
, void const *entry2
)
241 struct directory
const *directory1
= entry1
;
242 struct directory
const *directory2
= entry2
;
243 return directory1
->device_number
== directory2
->device_number
244 && directory1
->inode_number
== directory2
->inode_number
;
247 /* Make a directory entry for given NAME */
248 static struct directory
*
249 make_directory (const char *name
)
251 size_t namelen
= strlen (name
);
252 struct directory
*directory
= xmalloc (sizeof (*directory
));
253 directory
->next
= NULL
;
254 directory
->dump
= directory
->idump
= NULL
;
255 directory
->orig
= NULL
;
256 directory
->flags
= false;
257 if (namelen
&& ISSLASH (name
[namelen
- 1]))
259 directory
->name
= xmalloc (namelen
+ 1);
260 memcpy (directory
->name
, name
, namelen
);
261 directory
->name
[namelen
] = 0;
262 directory
->tagfile
= NULL
;
267 free_directory (struct directory
*dir
)
273 static struct directory
*
274 attach_directory (const char *name
)
276 struct directory
*dir
= make_directory (name
);
286 replace_prefix (char **pname
, const char *samp
, size_t slen
,
287 const char *repl
, size_t rlen
)
290 size_t nlen
= strlen (name
);
291 if (nlen
> slen
&& memcmp (name
, samp
, slen
) == 0 && ISSLASH (name
[slen
]))
295 name
= xrealloc (name
, nlen
- slen
+ rlen
+ 1);
298 memmove (name
+ rlen
, name
+ slen
, nlen
- slen
+ 1);
299 memcpy (name
, repl
, rlen
);
304 dirlist_replace_prefix (const char *pref
, const char *repl
)
306 struct directory
*dp
;
307 size_t pref_len
= strlen (pref
);
308 size_t repl_len
= strlen (repl
);
309 for (dp
= dirhead
; dp
; dp
= dp
->next
)
310 replace_prefix (&dp
->name
, pref
, pref_len
, repl
, repl_len
);
313 /* Create and link a new directory entry for directory NAME, having a
314 device number DEV and an inode number INO, with NFS indicating
315 whether it is an NFS device and FOUND indicating whether we have
316 found that the directory exists. */
317 static struct directory
*
318 note_directory (char const *name
, struct timespec mtime
,
319 dev_t dev
, ino_t ino
, bool nfs
, bool found
,
320 const char *contents
)
322 struct directory
*directory
= attach_directory (name
);
324 directory
->mtime
= mtime
;
325 directory
->device_number
= dev
;
326 directory
->inode_number
= ino
;
327 directory
->children
= CHANGED_CHILDREN
;
329 DIR_SET_FLAG (directory
, DIRF_NFS
);
331 DIR_SET_FLAG (directory
, DIRF_FOUND
);
333 directory
->dump
= dumpdir_create (contents
);
335 directory
->dump
= NULL
;
337 if (! ((directory_table
338 || (directory_table
= hash_initialize (0, 0,
340 compare_directory_names
, 0)))
341 && hash_insert (directory_table
, directory
)))
344 if (! ((directory_meta_table
345 || (directory_meta_table
= hash_initialize (0, 0,
347 compare_directory_meta
,
349 && hash_insert (directory_meta_table
, directory
)))
355 /* Return a directory entry for a given file NAME, or zero if none found. */
356 static struct directory
*
357 find_directory (const char *name
)
359 if (! directory_table
)
363 struct directory
*dir
= make_directory (name
);
364 struct directory
*ret
= hash_lookup (directory_table
, dir
);
365 free_directory (dir
);
370 /* Return a directory entry for a given combination of device and inode
371 numbers, or zero if none found. */
372 static struct directory
*
373 find_directory_meta (dev_t dev
, ino_t ino
)
375 if (! directory_meta_table
)
379 struct directory
*dir
= make_directory ("");
380 struct directory
*ret
;
381 dir
->device_number
= dev
;
382 dir
->inode_number
= ino
;
383 ret
= hash_lookup (directory_meta_table
, dir
);
384 free_directory (dir
);
390 update_parent_directory (const char *name
)
392 struct directory
*directory
;
396 directory
= find_directory (p
);
400 if (deref_stat (dereference_option
, p
, &st
) != 0)
403 directory
->mtime
= get_stat_mtime (&st
);
408 static struct directory
*
409 procdir (char *name_buffer
, struct stat
*stat_data
,
411 enum children children
,
415 struct directory
*directory
;
416 bool nfs
= NFS_FILE_STAT (*stat_data
);
418 if ((directory
= find_directory (name_buffer
)) != NULL
)
420 if (DIR_IS_INITED (directory
))
423 /* With NFS, the same file can have two different devices
424 if an NFS directory is mounted in multiple locations,
425 which is relatively common when automounting.
426 To avoid spurious incremental redumping of
427 directories, consider all NFS devices as equal,
428 relying on the i-node to establish differences. */
430 if (! ((!check_device_option
431 || (DIR_IS_NFS (directory
) && nfs
)
432 || directory
->device_number
== stat_data
->st_dev
)
433 && directory
->inode_number
== stat_data
->st_ino
))
435 /* FIXME: find_directory_meta ignores nfs */
436 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
440 if (strcmp (d
->name
, name_buffer
))
443 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
444 quotearg_colon (name_buffer
),
445 quote_n (1, d
->name
)));
447 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
448 dirlist_replace_prefix (d
->name
, name_buffer
);
450 directory
->children
= CHANGED_CHILDREN
;
455 WARN ((0, 0, _("%s: Directory has been renamed"),
456 quotearg_colon (name_buffer
)));
457 directory
->children
= ALL_CHILDREN
;
458 directory
->device_number
= stat_data
->st_dev
;
459 directory
->inode_number
= stat_data
->st_ino
;
462 DIR_SET_FLAG (directory
, DIRF_NFS
);
465 directory
->children
= CHANGED_CHILDREN
;
467 DIR_SET_FLAG (directory
, DIRF_FOUND
);
471 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
474 directory
= note_directory (name_buffer
,
475 get_stat_mtime(stat_data
),
484 if (strcmp (d
->name
, name_buffer
))
487 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
488 quotearg_colon (name_buffer
),
489 quote_n (1, d
->name
)));
491 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
492 dirlist_replace_prefix (d
->name
, name_buffer
);
494 directory
->children
= CHANGED_CHILDREN
;
498 DIR_SET_FLAG (directory
, DIRF_NEW
);
500 WARN ((0, 0, _("%s: Directory is new"),
501 quotearg_colon (name_buffer
)));
502 directory
->children
=
503 (listed_incremental_option
504 || (OLDER_STAT_TIME (*stat_data
, m
)
505 || (after_date_option
506 && OLDER_STAT_TIME (*stat_data
, c
))))
512 /* If the directory is on another device and --one-file-system was given,
514 if (one_file_system_option
&& device
!= stat_data
->st_dev
515 /* ... except if it was explicitely given in the command line */
516 && !is_individual_file (name_buffer
))
517 directory
->children
= NO_CHILDREN
;
518 else if (children
== ALL_CHILDREN
)
519 directory
->children
= ALL_CHILDREN
;
521 DIR_SET_FLAG (directory
, DIRF_INIT
);
524 const char *tag_file_name
;
526 switch (check_exclusion_tags (name_buffer
, &tag_file_name
))
528 case exclusion_tag_all
:
529 /* This warning can be duplicated by code in dump_file0, but only
530 in case when the topmost directory being archived contains
532 exclusion_tag_warning (name_buffer
, tag_file_name
,
533 _("directory not dumped"));
536 directory
->children
= NO_CHILDREN
;
539 case exclusion_tag_contents
:
540 exclusion_tag_warning (name_buffer
, tag_file_name
,
541 _("contents not dumped"));
542 directory
->children
= NO_CHILDREN
;
545 case exclusion_tag_under
:
546 exclusion_tag_warning (name_buffer
, tag_file_name
,
547 _("contents not dumped"));
548 directory
->tagfile
= tag_file_name
;
551 case exclusion_tag_none
:
559 /* Compare dumpdir array from DIRECTORY with directory listing DIR and
560 build a new dumpdir template.
562 DIR must be returned by a previous call to savedir().
564 File names in DIRECTORY->dump->contents must be sorted
567 DIRECTORY->dump is replaced with the created template. Each entry is
568 prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
571 makedumpdir (struct directory
*directory
, const char *dir
)
574 dirsize
, /* Number of elements in DIR */
575 len
; /* Length of DIR, including terminating nul */
578 char *new_dump
, *new_dump_ptr
;
579 struct dumpdir
*dump
;
581 if (directory
->children
== ALL_CHILDREN
)
583 else if (DIR_IS_RENAMED (directory
))
584 dump
= directory
->orig
->idump
?
585 directory
->orig
->idump
: directory
->orig
->dump
;
587 dump
= directory
->dump
;
589 /* Count the size of DIR and the number of elements it contains */
592 for (p
= dir
; *p
; p
+= strlen (p
) + 1, dirsize
++)
593 len
+= strlen (p
) + 2;
596 /* Create a sorted directory listing */
597 array
= xcalloc (dirsize
, sizeof array
[0]);
598 for (i
= 0, p
= dir
; *p
; p
+= strlen (p
) + 1, i
++)
601 qsort (array
, dirsize
, sizeof (array
[0]), compare_dirnames
);
603 /* Prepare space for new dumpdir */
604 new_dump
= xmalloc (len
);
605 new_dump_ptr
= new_dump
;
607 /* Fill in the dumpdir template */
608 for (i
= 0; i
< dirsize
; i
++)
610 const char *loc
= dumpdir_locate (dump
, array
[i
]);
613 if (directory
->tagfile
)
614 *new_dump_ptr
= strcmp (directory
->tagfile
, array
[i
]) == 0 ?
620 else if (directory
->tagfile
)
621 *new_dump_ptr
++ = strcmp (directory
->tagfile
, array
[i
]) == 0 ?
624 *new_dump_ptr
++ = 'Y'; /* New entry */
626 /* Copy the file name */
627 for (p
= array
[i
]; (*new_dump_ptr
++ = *p
++); )
631 directory
->idump
= directory
->dump
;
632 directory
->dump
= dumpdir_create0 (new_dump
, NULL
);
636 /* Recursively scan the given directory. */
638 scan_directory (char *dir
, dev_t device
)
640 char *dirp
= savedir (dir
); /* for scanning directory */
641 char *name_buffer
; /* directory, `/', and directory member */
642 size_t name_buffer_size
; /* allocated size of name_buffer, minus 2 */
643 size_t name_length
; /* used length in name_buffer */
644 struct stat stat_data
;
645 struct directory
*directory
;
650 name_buffer_size
= strlen (dir
) + NAME_FIELD_SIZE
;
651 name_buffer
= xmalloc (name_buffer_size
+ 2);
652 strcpy (name_buffer
, dir
);
653 if (! ISSLASH (dir
[strlen (dir
) - 1]))
654 strcat (name_buffer
, "/");
655 name_length
= strlen (name_buffer
);
657 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
659 stat_diag (name_buffer
);
661 children = CHANGED_CHILDREN;
668 directory
= procdir (name_buffer
, &stat_data
, device
, NO_CHILDREN
, false,
671 if (dirp
&& directory
->children
!= NO_CHILDREN
)
673 char *entry
; /* directory entry being scanned */
674 size_t entrylen
; /* length of directory entry */
677 makedumpdir (directory
, dirp
);
679 for (entry
= dumpdir_first (directory
->dump
, 1, &itr
);
681 entry
= dumpdir_next (itr
))
683 entrylen
= strlen (entry
);
684 if (name_buffer_size
<= entrylen
- 1 + name_length
)
687 name_buffer_size
+= NAME_FIELD_SIZE
;
688 while (name_buffer_size
<= entrylen
- 1 + name_length
);
689 name_buffer
= xrealloc (name_buffer
, name_buffer_size
+ 2);
691 strcpy (name_buffer
+ name_length
, entry
+ 1);
693 if (*entry
== 'I') /* Ignored entry */
695 else if (excluded_name (name_buffer
))
699 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
701 stat_diag (name_buffer
);
706 if (S_ISDIR (stat_data
.st_mode
))
709 procdir (name_buffer
, &stat_data
, device
,
711 verbose_option
, entry
);
714 else if (one_file_system_option
&& device
!= stat_data
.st_dev
)
717 else if (*entry
== 'Y')
718 /* New entry, skip further checks */;
720 /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
722 else if (OLDER_STAT_TIME (stat_data
, m
)
723 && (!after_date_option
724 || OLDER_STAT_TIME (stat_data
, c
)))
737 return directory
->dump
? directory
->dump
->contents
: NULL
;
741 get_directory_contents (char *dir
, dev_t device
)
743 return scan_directory (dir
, device
);
748 obstack_code_rename (struct obstack
*stk
, char *from
, char *to
)
752 s
= from
[0] == 0 ? from
:
753 safer_name_suffix (from
, false, absolute_names_option
);
754 obstack_1grow (stk
, 'R');
755 obstack_grow (stk
, s
, strlen (s
) + 1);
758 safer_name_suffix (to
, false, absolute_names_option
);
759 obstack_1grow (stk
, 'T');
760 obstack_grow (stk
, s
, strlen (s
) + 1);
764 store_rename (struct directory
*dir
, struct obstack
*stk
)
766 if (DIR_IS_RENAMED (dir
))
768 struct directory
*prev
, *p
;
770 /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
771 are ignored when hit by this function next time.
772 If the chain forms a cycle, prev points to the entry DIR is renamed
773 from. In this case it still retains DIRF_RENAMED flag, which will be
774 cleared in the `else' branch below */
775 for (prev
= dir
; prev
&& prev
->orig
!= dir
; prev
= prev
->orig
)
776 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
780 for (p
= dir
; p
&& p
->orig
; p
= p
->orig
)
781 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
787 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
789 /* Break the cycle by using a temporary name for one of its
791 First, create a temp name stub entry. */
792 temp_name
= dir_name (dir
->name
);
793 obstack_1grow (stk
, 'X');
794 obstack_grow (stk
, temp_name
, strlen (temp_name
) + 1);
796 obstack_code_rename (stk
, dir
->name
, "");
798 for (p
= dir
; p
!= prev
; p
= p
->orig
)
799 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
801 obstack_code_rename (stk
, "", prev
->name
);
807 append_incremental_renames (const char *dump
)
811 struct directory
*dp
;
819 size
= dumpdir_size (dump
) - 1;
820 obstack_grow (&stk
, dump
, size
);
825 for (dp
= dirhead
; dp
; dp
= dp
->next
)
826 store_rename (dp
, &stk
);
828 if (obstack_object_size (&stk
) != size
)
830 obstack_1grow (&stk
, 0);
831 dump
= obstack_finish (&stk
);
834 obstack_free (&stk
, NULL
);
840 static FILE *listed_incremental_stream
;
842 /* Version of incremental format snapshots (directory files) used by this
843 tar. Currently it is supposed to be a single decimal number. 0 means
844 incremental snapshots as per tar version before 1.15.2.
846 The current tar version supports incremental versions from
847 0 up to TAR_INCREMENTAL_VERSION, inclusive.
848 It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
850 #define TAR_INCREMENTAL_VERSION 2
852 /* Read incremental snapshot formats 0 and 1 */
854 read_incr_db_01 (int version
, const char *initbuf
)
867 if (getline (&buf
, &bufsize
, listed_incremental_stream
) <= 0)
869 read_error (listed_incremental_option
);
877 buf
= strdup (initbuf
);
878 bufsize
= strlen (buf
) + 1;
881 sec
= TYPE_MINIMUM (time_t);
884 u
= strtoumax (buf
, &ebuf
, 10);
885 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
887 if (errno
|| buf
== ebuf
)
888 ERROR ((0, errno
, "%s:%ld: %s",
889 quotearg_colon (listed_incremental_option
),
891 _("Invalid time stamp")));
896 if (version
== 1 && *ebuf
)
898 char const *buf_ns
= ebuf
+ 1;
900 u
= strtoumax (buf_ns
, &ebuf
, 10);
901 if (!errno
&& BILLION
<= u
)
903 if (errno
|| buf_ns
== ebuf
)
905 ERROR ((0, errno
, "%s:%ld: %s",
906 quotearg_colon (listed_incremental_option
),
908 _("Invalid time stamp")));
909 sec
= TYPE_MINIMUM (time_t);
916 /* pre-1 incremental format does not contain nanoseconds */
920 newer_mtime_option
.tv_sec
= sec
;
921 newer_mtime_option
.tv_nsec
= nsec
;
924 while (0 < (n
= getline (&buf
, &bufsize
, listed_incremental_stream
)))
928 bool nfs
= buf
[0] == '+';
929 char *strp
= buf
+ nfs
;
930 struct timespec mtime
;
934 if (buf
[n
- 1] == '\n')
940 u
= strtoumax (strp
, &ebuf
, 10);
941 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
943 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
945 ERROR ((0, errno
, "%s:%ld: %s",
946 quotearg_colon (listed_incremental_option
), lineno
,
947 _("Invalid modification time (seconds)")));
955 u
= strtoumax (strp
, &ebuf
, 10);
956 if (!errno
&& BILLION
<= u
)
958 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
960 ERROR ((0, errno
, "%s:%ld: %s",
961 quotearg_colon (listed_incremental_option
), lineno
,
962 _("Invalid modification time (nanoseconds)")));
968 mtime
.tv_nsec
= nsec
;
972 memset (&mtime
, 0, sizeof mtime
);
975 u
= strtoumax (strp
, &ebuf
, 10);
976 if (!errno
&& TYPE_MAXIMUM (dev_t
) < u
)
978 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
980 ERROR ((0, errno
, "%s:%ld: %s",
981 quotearg_colon (listed_incremental_option
), lineno
,
982 _("Invalid device number")));
990 u
= strtoumax (strp
, &ebuf
, 10);
991 if (!errno
&& TYPE_MAXIMUM (ino_t
) < u
)
993 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
995 ERROR ((0, errno
, "%s:%ld: %s",
996 quotearg_colon (listed_incremental_option
), lineno
,
997 _("Invalid inode number")));
1005 unquote_string (strp
);
1006 note_directory (strp
, mtime
, dev
, ino
, nfs
, false, NULL
);
1011 /* Read a nul-terminated string from FP and store it in STK.
1012 Store the number of bytes read (including nul terminator) in PCOUNT.
1014 Return the last character read or EOF on end of file. */
1016 read_obstack (FILE *fp
, struct obstack
*stk
, size_t *pcount
)
1021 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
1022 obstack_1grow (stk
, c
);
1023 obstack_1grow (stk
, 0);
1029 /* Read from file FP a nul-terminated string and convert it to
1030 intmax_t. Return the resulting value in PVAL. Assume '-' has
1033 Throw a fatal error if the string cannot be converted or if the
1034 converted value is less than MIN_VAL. */
1037 read_negative_num (FILE *fp
, intmax_t min_val
, intmax_t *pval
)
1041 char buf
[INT_BUFSIZE_BOUND (intmax_t)];
1045 for (i
= 1; ISDIGIT (c
= getc (fp
)); i
++)
1047 if (i
== sizeof buf
- 1)
1048 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
1055 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
1057 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1062 *pval
= strtoimax (buf
, &ep
, 10);
1063 if (c
|| errno
|| *pval
< min_val
)
1064 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1067 /* Read from file FP a nul-terminated string and convert it to
1068 uintmax_t. Return the resulting value in PVAL. Assume C has
1071 Throw a fatal error if the string cannot be converted or if the
1072 converted value exceeds MAX_VAL.
1074 Return the last character read or EOF on end of file. */
1077 read_unsigned_num (int c
, FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1080 char buf
[UINTMAX_STRSIZE_BOUND
], *ep
;
1082 for (i
= 0; ISDIGIT (c
); i
++)
1084 if (i
== sizeof buf
- 1)
1085 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
1093 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
1097 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1102 *pval
= strtoumax (buf
, &ep
, 10);
1103 if (c
|| errno
|| max_val
< *pval
)
1104 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1108 /* Read from file FP a nul-terminated string and convert it to
1109 uintmax_t. Return the resulting value in PVAL.
1111 Throw a fatal error if the string cannot be converted or if the
1112 converted value exceeds MAX_VAL.
1114 Return the last character read or EOF on end of file. */
1117 read_num (FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1119 return read_unsigned_num (getc (fp
), fp
, max_val
, pval
);
1122 /* Read from FP two NUL-terminated strings representing a struct
1123 timespec. Return the resulting value in PVAL.
1125 Throw a fatal error if the string cannot be converted. */
1128 read_timespec (FILE *fp
, struct timespec
*pval
)
1136 read_negative_num (fp
, TYPE_MINIMUM (time_t), &i
);
1142 c
= read_unsigned_num (c
, fp
, TYPE_MAXIMUM (time_t), &u
);
1146 if (c
|| read_num (fp
, BILLION
- 1, &u
))
1147 FATAL_ERROR ((0, 0, "%s: %s",
1148 quotearg_colon (listed_incremental_option
),
1149 _("Unexpected EOF in snapshot file")));
1153 /* Read incremental snapshot format 2 */
1160 obstack_init (&stk
);
1162 read_timespec (listed_incremental_stream
, &newer_mtime_option
);
1166 struct timespec mtime
;
1174 if (read_num (listed_incremental_stream
, 1, &u
))
1175 return; /* Normal return */
1179 read_timespec (listed_incremental_stream
, &mtime
);
1181 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (dev_t
), &u
))
1185 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (ino_t
), &u
))
1189 if (read_obstack (listed_incremental_stream
, &stk
, &s
))
1192 name
= obstack_finish (&stk
);
1194 while (read_obstack (listed_incremental_stream
, &stk
, &s
) == 0 && s
> 1)
1196 if (getc (listed_incremental_stream
) != 0)
1197 FATAL_ERROR ((0, 0, "%s: %s",
1198 quotearg_colon (listed_incremental_option
),
1199 _("Missing record terminator")));
1201 content
= obstack_finish (&stk
);
1202 note_directory (name
, mtime
, dev
, ino
, nfs
, false, content
);
1203 obstack_free (&stk
, content
);
1205 FATAL_ERROR ((0, 0, "%s: %s",
1206 quotearg_colon (listed_incremental_option
),
1207 _("Unexpected EOF in snapshot file")));
1210 /* Read incremental snapshot file (directory file).
1211 If the file has older incremental version, make sure that it is processed
1212 correctly and that tar will use the most conservative backup method among
1213 possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
1214 etc.) This ensures that the snapshots are updated to the recent version
1215 without any loss of data. */
1217 read_directory_file (void)
1223 /* Open the file for both read and write. That way, we can write
1224 it later without having to reopen it, and don't have to worry if
1225 we chdir in the meantime. */
1226 fd
= open (listed_incremental_option
, O_RDWR
| O_CREAT
, MODE_RW
);
1229 open_error (listed_incremental_option
);
1233 listed_incremental_stream
= fdopen (fd
, "r+");
1234 if (! listed_incremental_stream
)
1236 open_error (listed_incremental_option
);
1241 if (0 < getline (&buf
, &bufsize
, listed_incremental_stream
))
1244 uintmax_t incremental_version
;
1246 if (strncmp (buf
, PACKAGE_NAME
, sizeof PACKAGE_NAME
- 1) == 0)
1248 ebuf
= buf
+ sizeof PACKAGE_NAME
- 1;
1250 ERROR((1, 0, _("Bad incremental file format")));
1251 for (; *ebuf
!= '-'; ebuf
++)
1253 ERROR((1, 0, _("Bad incremental file format")));
1255 incremental_version
= strtoumax (ebuf
+ 1, NULL
, 10);
1258 incremental_version
= 0;
1260 switch (incremental_version
)
1264 read_incr_db_01 (incremental_version
, buf
);
1267 case TAR_INCREMENTAL_VERSION
:
1272 ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX
),
1273 incremental_version
));
1278 if (ferror (listed_incremental_stream
))
1279 read_error (listed_incremental_option
);
1284 /* Output incremental data for the directory ENTRY to the file DATA.
1285 Return nonzero if successful, preserving errno on write failure. */
1287 write_directory_file_entry (void *entry
, void *data
)
1289 struct directory
const *directory
= entry
;
1292 if (DIR_IS_FOUND (directory
))
1294 char buf
[UINTMAX_STRSIZE_BOUND
];
1297 s
= DIR_IS_NFS (directory
) ? "1" : "0";
1298 fwrite (s
, 2, 1, fp
);
1299 s
= (TYPE_SIGNED (time_t)
1300 ? imaxtostr (directory
->mtime
.tv_sec
, buf
)
1301 : umaxtostr (directory
->mtime
.tv_sec
, buf
));
1302 fwrite (s
, strlen (s
) + 1, 1, fp
);
1303 s
= umaxtostr (directory
->mtime
.tv_nsec
, buf
);
1304 fwrite (s
, strlen (s
) + 1, 1, fp
);
1305 s
= umaxtostr (directory
->device_number
, buf
);
1306 fwrite (s
, strlen (s
) + 1, 1, fp
);
1307 s
= umaxtostr (directory
->inode_number
, buf
);
1308 fwrite (s
, strlen (s
) + 1, 1, fp
);
1310 fwrite (directory
->name
, strlen (directory
->name
) + 1, 1, fp
);
1311 if (directory
->dump
)
1316 for (p
= dumpdir_first (directory
->dump
, 0, &itr
);
1318 p
= dumpdir_next (itr
))
1319 fwrite (p
, strlen (p
) + 1, 1, fp
);
1322 fwrite ("\0\0", 2, 1, fp
);
1325 return ! ferror (fp
);
1329 write_directory_file (void)
1331 FILE *fp
= listed_incremental_stream
;
1332 char buf
[UINTMAX_STRSIZE_BOUND
];
1338 if (fseek (fp
, 0L, SEEK_SET
) != 0)
1339 seek_error (listed_incremental_option
);
1340 if (sys_truncate (fileno (fp
)) != 0)
1341 truncate_error (listed_incremental_option
);
1343 fprintf (fp
, "%s-%s-%d\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1344 TAR_INCREMENTAL_VERSION
);
1346 s
= (TYPE_SIGNED (time_t)
1347 ? imaxtostr (start_time
.tv_sec
, buf
)
1348 : umaxtostr (start_time
.tv_sec
, buf
));
1349 fwrite (s
, strlen (s
) + 1, 1, fp
);
1350 s
= umaxtostr (start_time
.tv_nsec
, buf
);
1351 fwrite (s
, strlen (s
) + 1, 1, fp
);
1353 if (! ferror (fp
) && directory_table
)
1354 hash_do_for_each (directory_table
, write_directory_file_entry
, fp
);
1357 write_error (listed_incremental_option
);
1358 if (fclose (fp
) != 0)
1359 close_error (listed_incremental_option
);
1363 /* Restoration of incremental dumps. */
1366 get_gnu_dumpdir (struct tar_stat_info
*stat_info
)
1370 union block
*data_block
;
1374 size
= stat_info
->stat
.st_size
;
1376 archive_dir
= xmalloc (size
);
1379 set_next_block_after (current_header
);
1380 mv_begin (stat_info
);
1382 for (; size
> 0; size
-= copied
)
1384 mv_size_left (size
);
1385 data_block
= find_next_block ();
1387 ERROR ((1, 0, _("Unexpected EOF in archive")));
1388 copied
= available_space_after (data_block
);
1391 memcpy (to
, data_block
->buffer
, copied
);
1393 set_next_block_after ((union block
*)
1394 (data_block
->buffer
+ copied
- 1));
1399 stat_info
->dumpdir
= archive_dir
;
1400 stat_info
->skipped
= true; /* For skip_member() and friends
1401 to work correctly */
1404 /* Return T if STAT_INFO represents a dumpdir archive member.
1405 Note: can invalidate current_header. It happens if flush_archive()
1406 gets called within get_gnu_dumpdir() */
1408 is_dumpdir (struct tar_stat_info
*stat_info
)
1410 if (stat_info
->is_dumpdir
&& !stat_info
->dumpdir
)
1411 get_gnu_dumpdir (stat_info
);
1412 return stat_info
->is_dumpdir
;
1416 dumpdir_ok (char *dumpdir
)
1419 int has_tempdir
= 0;
1422 for (p
= dumpdir
; *p
; p
+= strlen (p
) + 1)
1424 if (expect
&& *p
!= expect
)
1427 _("Malformed dumpdir: expected '%c' but found %#3o"),
1437 _("Malformed dumpdir: 'X' duplicated")));
1450 _("Malformed dumpdir: empty name in 'R'")));
1463 _("Malformed dumpdir: 'T' not preceeded by 'R'")));
1466 if (p
[1] == 0 && !has_tempdir
)
1469 _("Malformed dumpdir: empty name in 'T'")));
1481 /* FIXME: bail out? */
1489 _("Malformed dumpdir: expected '%c' but found end of data"),
1495 WARN ((0, 0, _("Malformed dumpdir: 'X' never used")));
1500 /* Examine the directories under directory_name and delete any
1501 files that were not there at the time of the back-up. */
1503 try_purge_directory (char const *directory_name
)
1506 char *cur
, *arc
, *p
;
1507 char *temp_stub
= NULL
;
1508 struct dumpdir
*dump
;
1510 if (!is_dumpdir (¤t_stat_info
))
1513 current_dir
= savedir (directory_name
);
1516 /* The directory doesn't exist now. It'll be created. In any
1517 case, we don't have to delete any files out of it. */
1520 /* Verify if dump directory is sane */
1521 if (!dumpdir_ok (current_stat_info
.dumpdir
))
1524 /* Process renames */
1525 for (arc
= current_stat_info
.dumpdir
; *arc
; arc
+= strlen (arc
) + 1)
1529 #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
1530 size_t len
= strlen (arc
+ 1);
1531 temp_stub
= xrealloc (temp_stub
, len
+ 1 + sizeof TEMP_DIR_TEMPLATE
);
1532 memcpy (temp_stub
, arc
+ 1, len
);
1533 temp_stub
[len
] = '/';
1534 memcpy (temp_stub
+ len
+ 1, TEMP_DIR_TEMPLATE
,
1535 sizeof TEMP_DIR_TEMPLATE
);
1536 if (!mkdtemp (temp_stub
))
1539 _("Cannot create temporary directory using template %s"),
1540 quote (temp_stub
)));
1546 else if (*arc
== 'R')
1550 arc
+= strlen (arc
) + 1;
1553 /* Ensure that neither source nor destination are absolute file
1554 names (unless permitted by -P option), and that they do not
1555 contain dubious parts (e.g. ../).
1557 This is an extra safety precaution. Besides, it might be
1558 necessary to extract from archives created with tar versions
1562 src
= safer_name_suffix (src
, false, absolute_names_option
);
1564 dst
= safer_name_suffix (dst
, false, absolute_names_option
);
1571 if (!rename_directory (src
, dst
))
1575 /* FIXME: Make sure purge_directory(dst) will return
1584 /* Process deletes */
1585 dump
= dumpdir_create (current_stat_info
.dumpdir
);
1587 for (cur
= current_dir
; *cur
; cur
+= strlen (cur
) + 1)
1593 p
= new_name (directory_name
, cur
);
1595 if (deref_stat (false, p
, &st
))
1597 if (errno
!= ENOENT
) /* FIXME: Maybe keep a list of renamed
1598 dirs and check it here? */
1601 WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
1602 quotearg_colon (p
)));
1607 if (!(entry
= dumpdir_locate (dump
, cur
))
1608 || (*entry
== 'D' && !S_ISDIR (st
.st_mode
))
1609 || (*entry
== 'Y' && S_ISDIR (st
.st_mode
)))
1611 if (one_file_system_option
&& st
.st_dev
!= root_device
)
1614 _("%s: directory is on a different device: not purging"),
1615 quotearg_colon (p
)));
1619 if (! interactive_option
|| confirm ("delete", p
))
1622 fprintf (stdlis
, _("%s: Deleting %s\n"),
1623 program_name
, quote (p
));
1624 if (! remove_any_file (p
, RECURSIVE_REMOVE_OPTION
))
1627 ERROR ((0, e
, _("%s: Cannot remove"), quotearg_colon (p
)));
1633 dumpdir_free (dump
);
1640 purge_directory (char const *directory_name
)
1642 if (!try_purge_directory (directory_name
))
1647 list_dumpdir (char *buffer
, size_t size
)
1660 fprintf (stdlis
, "%c", *buffer
);
1663 fprintf (stdlis
, " ");
1671 fputc ('\n', stdlis
);
1678 fputc (*buffer
, stdlis
);