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
;
87 for (i
= 0, total
= 0, ctsize
= 1, q
= contents
; *q
; total
++, q
+= len
)
91 if (!cmask
|| strchr (cmask
, *q
))
94 dump
= xmalloc (sizeof (*dump
) + ctsize
);
95 dump
->contents
= (char*)(dump
+ 1);
96 memcpy (dump
->contents
, contents
, ctsize
);
99 dump
->elv
= xcalloc (i
+ 1, sizeof (dump
->elv
[0]));
101 for (i
= 0, p
= dump
->contents
; *p
; p
+= strlen (p
) + 1)
103 if (!cmask
|| strchr (cmask
, *p
))
104 dump
->elv
[i
++] = p
+ 1;
111 dumpdir_create (const char *contents
)
113 return dumpdir_create0 (contents
, "YND");
117 dumpdir_free (struct dumpdir
*dump
)
124 compare_dirnames (const void *first
, const void *second
)
126 char const *const *name1
= first
;
127 char const *const *name2
= second
;
128 return strcmp (*name1
, *name2
);
131 /* Locate NAME in the dumpdir array DUMP.
132 Return pointer to the slot in DUMP->contents, or NULL if not found */
134 dumpdir_locate (struct dumpdir
*dump
, const char *name
)
140 ptr
= bsearch (&name
, dump
->elv
, dump
->elc
, sizeof (dump
->elv
[0]),
142 return ptr
? *ptr
- 1: NULL
;
147 struct dumpdir
*dump
; /* Dumpdir being iterated */
148 int all
; /* Iterate over all entries, not only D/N/Y */
149 size_t next
; /* Index of the next element */
153 dumpdir_next (struct dumpdir_iter
*itr
)
155 size_t cur
= itr
->next
;
160 ret
= itr
->dump
->contents
+ cur
;
163 itr
->next
+= strlen (ret
) + 1;
165 else if (cur
< itr
->dump
->elc
)
167 ret
= itr
->dump
->elv
[cur
] - 1;
175 dumpdir_first (struct dumpdir
*dump
, int all
, struct dumpdir_iter
**pitr
)
177 struct dumpdir_iter
*itr
= xmalloc (sizeof (*itr
));
182 return dumpdir_next (itr
);
185 /* Return size in bytes of the dumpdir array P */
187 dumpdir_size (const char *p
)
193 size_t size
= strlen (p
) + 1;
201 static struct directory
*dirhead
, *dirtail
;
202 static Hash_table
*directory_table
;
203 static Hash_table
*directory_meta_table
;
205 #if HAVE_ST_FSTYPE_STRING
206 static char const nfs_string
[] = "nfs";
207 # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
209 # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
210 # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
213 /* Calculate the hash of a directory. */
215 hash_directory_name (void const *entry
, size_t n_buckets
)
217 struct directory
const *directory
= entry
;
218 return hash_string (directory
->name
, n_buckets
);
221 /* Compare two directories for equality of their names. */
223 compare_directory_names (void const *entry1
, void const *entry2
)
225 struct directory
const *directory1
= entry1
;
226 struct directory
const *directory2
= entry2
;
227 return strcmp (directory1
->name
, directory2
->name
) == 0;
231 hash_directory_meta (void const *entry
, size_t n_buckets
)
233 struct directory
const *directory
= entry
;
234 /* FIXME: Work out a better algorytm */
235 return (directory
->device_number
+ directory
->inode_number
) % n_buckets
;
238 /* Compare two directories for equality of their device and inode numbers. */
240 compare_directory_meta (void const *entry1
, void const *entry2
)
242 struct directory
const *directory1
= entry1
;
243 struct directory
const *directory2
= entry2
;
244 return directory1
->device_number
== directory2
->device_number
245 && directory1
->inode_number
== directory2
->inode_number
;
248 /* Make a directory entry for given NAME */
249 static struct directory
*
250 make_directory (const char *name
)
252 size_t namelen
= strlen (name
);
253 struct directory
*directory
= xmalloc (sizeof (*directory
));
254 directory
->next
= NULL
;
255 directory
->dump
= directory
->idump
= NULL
;
256 directory
->orig
= NULL
;
257 directory
->flags
= false;
258 if (namelen
&& ISSLASH (name
[namelen
- 1]))
260 directory
->name
= xmalloc (namelen
+ 1);
261 memcpy (directory
->name
, name
, namelen
);
262 directory
->name
[namelen
] = 0;
263 directory
->tagfile
= NULL
;
268 free_directory (struct directory
*dir
)
274 static struct directory
*
275 attach_directory (const char *name
)
277 struct directory
*dir
= make_directory (name
);
288 replace_prefix (char **pname
, const char *samp
, size_t slen
,
289 const char *repl
, size_t rlen
)
292 size_t nlen
= strlen (name
);
293 if (nlen
> slen
&& memcmp (name
, samp
, slen
) == 0 && ISSLASH (name
[slen
]))
297 name
= xrealloc (name
, nlen
- slen
+ rlen
+ 1);
300 memmove (name
+ rlen
, name
+ slen
, nlen
- slen
+ 1);
301 memcpy (name
, repl
, rlen
);
306 dirlist_replace_prefix (const char *pref
, const char *repl
)
308 struct directory
*dp
;
309 size_t pref_len
= strlen (pref
);
310 size_t repl_len
= strlen (repl
);
311 for (dp
= dirhead
; dp
; dp
= dp
->next
)
312 replace_prefix (&dp
->name
, pref
, pref_len
, repl
, repl_len
);
315 /* Create and link a new directory entry for directory NAME, having a
316 device number DEV and an inode number INO, with NFS indicating
317 whether it is an NFS device and FOUND indicating whether we have
318 found that the directory exists. */
319 static struct directory
*
320 note_directory (char const *name
, struct timespec mtime
,
321 dev_t dev
, ino_t ino
, bool nfs
, bool found
,
322 const char *contents
)
324 struct directory
*directory
= attach_directory (name
);
326 directory
->mtime
= mtime
;
327 directory
->device_number
= dev
;
328 directory
->inode_number
= ino
;
329 directory
->children
= CHANGED_CHILDREN
;
331 DIR_SET_FLAG (directory
, DIRF_NFS
);
333 DIR_SET_FLAG (directory
, DIRF_FOUND
);
335 directory
->dump
= dumpdir_create (contents
);
337 directory
->dump
= NULL
;
339 if (! ((directory_table
340 || (directory_table
= hash_initialize (0, 0,
342 compare_directory_names
, 0)))
343 && hash_insert (directory_table
, directory
)))
346 if (! ((directory_meta_table
347 || (directory_meta_table
= hash_initialize (0, 0,
349 compare_directory_meta
,
351 && hash_insert (directory_meta_table
, directory
)))
357 /* Return a directory entry for a given file NAME, or zero if none found. */
358 static struct directory
*
359 find_directory (const char *name
)
361 if (! directory_table
)
365 struct directory
*dir
= make_directory (name
);
366 struct directory
*ret
= hash_lookup (directory_table
, dir
);
367 free_directory (dir
);
372 /* Return a directory entry for a given combination of device and inode
373 numbers, or zero if none found. */
374 static struct directory
*
375 find_directory_meta (dev_t dev
, ino_t ino
)
377 if (! directory_meta_table
)
381 struct directory
*dir
= make_directory ("");
382 struct directory
*ret
;
383 dir
->device_number
= dev
;
384 dir
->inode_number
= ino
;
385 ret
= hash_lookup (directory_meta_table
, dir
);
386 free_directory (dir
);
392 update_parent_directory (const char *name
)
394 struct directory
*directory
;
398 directory
= find_directory (p
);
402 if (deref_stat (dereference_option
, p
, &st
) != 0)
405 directory
->mtime
= get_stat_mtime (&st
);
410 #define PD_VERBOSE 0x10
411 #define PD_FORCE_CHILDREN 0x20
412 #define PD_CHILDREN(f) ((f) & 3)
414 static struct directory
*
415 procdir (char *name_buffer
, struct stat
*stat_data
,
420 struct directory
*directory
;
421 bool nfs
= NFS_FILE_STAT (*stat_data
);
423 if ((directory
= find_directory (name_buffer
)) != NULL
)
425 if (DIR_IS_INITED (directory
))
428 /* With NFS, the same file can have two different devices
429 if an NFS directory is mounted in multiple locations,
430 which is relatively common when automounting.
431 To avoid spurious incremental redumping of
432 directories, consider all NFS devices as equal,
433 relying on the i-node to establish differences. */
435 if (! ((!check_device_option
436 || (DIR_IS_NFS (directory
) && nfs
)
437 || directory
->device_number
== stat_data
->st_dev
)
438 && directory
->inode_number
== stat_data
->st_ino
))
440 /* FIXME: find_directory_meta ignores nfs */
441 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
445 if (strcmp (d
->name
, name_buffer
))
448 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
449 quotearg_colon (name_buffer
),
450 quote_n (1, d
->name
)));
452 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
453 dirlist_replace_prefix (d
->name
, name_buffer
);
455 directory
->children
= CHANGED_CHILDREN
;
460 WARN ((0, 0, _("%s: Directory has been renamed"),
461 quotearg_colon (name_buffer
)));
462 directory
->children
= ALL_CHILDREN
;
463 directory
->device_number
= stat_data
->st_dev
;
464 directory
->inode_number
= stat_data
->st_ino
;
467 DIR_SET_FLAG (directory
, DIRF_NFS
);
470 directory
->children
= CHANGED_CHILDREN
;
472 DIR_SET_FLAG (directory
, DIRF_FOUND
);
476 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
479 directory
= note_directory (name_buffer
,
480 get_stat_mtime(stat_data
),
489 if (strcmp (d
->name
, name_buffer
))
491 if (flag
& PD_VERBOSE
)
492 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
493 quotearg_colon (name_buffer
),
494 quote_n (1, d
->name
)));
496 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
497 dirlist_replace_prefix (d
->name
, name_buffer
);
499 directory
->children
= CHANGED_CHILDREN
;
503 DIR_SET_FLAG (directory
, DIRF_NEW
);
504 if (flag
& PD_VERBOSE
)
505 WARN ((0, 0, _("%s: Directory is new"),
506 quotearg_colon (name_buffer
)));
507 directory
->children
=
508 (listed_incremental_option
509 || (OLDER_STAT_TIME (*stat_data
, m
)
510 || (after_date_option
511 && OLDER_STAT_TIME (*stat_data
, c
))))
517 /* If the directory is on another device and --one-file-system was given,
519 if (one_file_system_option
&& device
!= stat_data
->st_dev
520 /* ... except if it was explicitely given in the command line */
521 && !is_individual_file (name_buffer
))
522 directory
->children
= NO_CHILDREN
;
523 else if (flag
& PD_FORCE_CHILDREN
)
525 directory
->children
= PD_CHILDREN(flag
);
526 if (directory
->children
== NO_CHILDREN
)
530 DIR_SET_FLAG (directory
, DIRF_INIT
);
532 if (directory
->children
!= NO_CHILDREN
)
534 const char *tag_file_name
;
536 switch (check_exclusion_tags (name_buffer
, &tag_file_name
))
538 case exclusion_tag_all
:
539 /* This warning can be duplicated by code in dump_file0, but only
540 in case when the topmost directory being archived contains
542 exclusion_tag_warning (name_buffer
, tag_file_name
,
543 _("directory not dumped"));
546 directory
->children
= NO_CHILDREN
;
549 case exclusion_tag_contents
:
550 exclusion_tag_warning (name_buffer
, tag_file_name
,
551 _("contents not dumped"));
552 directory
->children
= NO_CHILDREN
;
555 case exclusion_tag_under
:
556 exclusion_tag_warning (name_buffer
, tag_file_name
,
557 _("contents not dumped"));
558 directory
->tagfile
= tag_file_name
;
561 case exclusion_tag_none
:
569 /* Compare dumpdir array from DIRECTORY with directory listing DIR and
570 build a new dumpdir template.
572 DIR must be returned by a previous call to savedir().
574 File names in DIRECTORY->dump->contents must be sorted
577 DIRECTORY->dump is replaced with the created template. Each entry is
578 prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
581 makedumpdir (struct directory
*directory
, const char *dir
)
584 dirsize
, /* Number of elements in DIR */
585 len
; /* Length of DIR, including terminating nul */
588 char *new_dump
, *new_dump_ptr
;
589 struct dumpdir
*dump
;
591 if (directory
->children
== ALL_CHILDREN
)
593 else if (DIR_IS_RENAMED (directory
))
594 dump
= directory
->orig
->idump
?
595 directory
->orig
->idump
: directory
->orig
->dump
;
597 dump
= directory
->dump
;
599 /* Count the size of DIR and the number of elements it contains */
602 for (p
= dir
; *p
; p
+= strlen (p
) + 1, dirsize
++)
603 len
+= strlen (p
) + 2;
606 /* Create a sorted directory listing */
607 array
= xcalloc (dirsize
, sizeof array
[0]);
608 for (i
= 0, p
= dir
; *p
; p
+= strlen (p
) + 1, i
++)
611 qsort (array
, dirsize
, sizeof (array
[0]), compare_dirnames
);
613 /* Prepare space for new dumpdir */
614 new_dump
= xmalloc (len
);
615 new_dump_ptr
= new_dump
;
617 /* Fill in the dumpdir template */
618 for (i
= 0; i
< dirsize
; i
++)
620 const char *loc
= dumpdir_locate (dump
, array
[i
]);
623 if (directory
->tagfile
)
624 *new_dump_ptr
= strcmp (directory
->tagfile
, array
[i
]) == 0 ?
630 else if (directory
->tagfile
)
631 *new_dump_ptr
++ = strcmp (directory
->tagfile
, array
[i
]) == 0 ?
634 *new_dump_ptr
++ = 'Y'; /* New entry */
636 /* Copy the file name */
637 for (p
= array
[i
]; (*new_dump_ptr
++ = *p
++); )
641 directory
->idump
= directory
->dump
;
642 directory
->dump
= dumpdir_create0 (new_dump
, NULL
);
646 /* Recursively scan the given directory. */
648 scan_directory (char *dir
, dev_t device
)
650 char *dirp
= savedir (dir
); /* for scanning directory */
651 char *name_buffer
; /* directory, `/', and directory member */
652 size_t name_buffer_size
; /* allocated size of name_buffer, minus 2 */
653 size_t name_length
; /* used length in name_buffer */
654 struct stat stat_data
;
655 struct directory
*directory
;
660 name_buffer_size
= strlen (dir
) + NAME_FIELD_SIZE
;
661 name_buffer
= xmalloc (name_buffer_size
+ 2);
662 strcpy (name_buffer
, dir
);
663 if (! ISSLASH (dir
[strlen (dir
) - 1]))
664 strcat (name_buffer
, "/");
665 name_length
= strlen (name_buffer
);
667 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
669 stat_diag (name_buffer
);
671 children = CHANGED_CHILDREN;
678 directory
= procdir (name_buffer
, &stat_data
, device
, 0, NULL
);
680 if (dirp
&& directory
->children
!= NO_CHILDREN
)
682 char *entry
; /* directory entry being scanned */
683 size_t entrylen
; /* length of directory entry */
686 makedumpdir (directory
, dirp
);
688 for (entry
= dumpdir_first (directory
->dump
, 1, &itr
);
690 entry
= dumpdir_next (itr
))
692 entrylen
= strlen (entry
);
693 if (name_buffer_size
<= entrylen
- 1 + name_length
)
696 name_buffer_size
+= NAME_FIELD_SIZE
;
697 while (name_buffer_size
<= entrylen
- 1 + name_length
);
698 name_buffer
= xrealloc (name_buffer
, name_buffer_size
+ 2);
700 strcpy (name_buffer
+ name_length
, entry
+ 1);
702 if (*entry
== 'I') /* Ignored entry */
704 else if (excluded_name (name_buffer
))
708 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
710 stat_diag (name_buffer
);
715 if (S_ISDIR (stat_data
.st_mode
))
717 int pd_flag
= (verbose_option
? PD_VERBOSE
: 0);
718 if (!recursion_option
)
719 pd_flag
|= PD_FORCE_CHILDREN
| NO_CHILDREN
;
720 else if (directory
->children
== ALL_CHILDREN
)
721 pd_flag
|= PD_FORCE_CHILDREN
| ALL_CHILDREN
;
723 procdir (name_buffer
, &stat_data
, device
, pd_flag
, entry
);
726 else if (one_file_system_option
&& device
!= stat_data
.st_dev
)
729 else if (*entry
== 'Y')
730 /* New entry, skip further checks */;
732 /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
734 else if (OLDER_STAT_TIME (stat_data
, m
)
735 && (!after_date_option
736 || OLDER_STAT_TIME (stat_data
, c
)))
749 return directory
->dump
? directory
->dump
->contents
: NULL
;
753 get_directory_contents (char *dir
, dev_t device
)
755 return scan_directory (dir
, device
);
760 obstack_code_rename (struct obstack
*stk
, char *from
, char *to
)
764 s
= from
[0] == 0 ? from
:
765 safer_name_suffix (from
, false, absolute_names_option
);
766 obstack_1grow (stk
, 'R');
767 obstack_grow (stk
, s
, strlen (s
) + 1);
770 safer_name_suffix (to
, false, absolute_names_option
);
771 obstack_1grow (stk
, 'T');
772 obstack_grow (stk
, s
, strlen (s
) + 1);
776 store_rename (struct directory
*dir
, struct obstack
*stk
)
778 if (DIR_IS_RENAMED (dir
))
780 struct directory
*prev
, *p
;
782 /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
783 are ignored when hit by this function next time.
784 If the chain forms a cycle, prev points to the entry DIR is renamed
785 from. In this case it still retains DIRF_RENAMED flag, which will be
786 cleared in the `else' branch below */
787 for (prev
= dir
; prev
&& prev
->orig
!= dir
; prev
= prev
->orig
)
788 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
792 for (p
= dir
; p
&& p
->orig
; p
= p
->orig
)
793 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
799 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
801 /* Break the cycle by using a temporary name for one of its
803 First, create a temp name stub entry. */
804 temp_name
= dir_name (dir
->name
);
805 obstack_1grow (stk
, 'X');
806 obstack_grow (stk
, temp_name
, strlen (temp_name
) + 1);
808 obstack_code_rename (stk
, dir
->name
, "");
810 for (p
= dir
; p
!= prev
; p
= p
->orig
)
811 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
813 obstack_code_rename (stk
, "", prev
->name
);
819 append_incremental_renames (const char *dump
)
823 struct directory
*dp
;
831 size
= dumpdir_size (dump
) - 1;
832 obstack_grow (&stk
, dump
, size
);
837 for (dp
= dirhead
; dp
; dp
= dp
->next
)
838 store_rename (dp
, &stk
);
840 if (obstack_object_size (&stk
) != size
)
842 obstack_1grow (&stk
, 0);
843 dump
= obstack_finish (&stk
);
846 obstack_free (&stk
, NULL
);
852 static FILE *listed_incremental_stream
;
854 /* Version of incremental format snapshots (directory files) used by this
855 tar. Currently it is supposed to be a single decimal number. 0 means
856 incremental snapshots as per tar version before 1.15.2.
858 The current tar version supports incremental versions from
859 0 up to TAR_INCREMENTAL_VERSION, inclusive.
860 It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
862 #define TAR_INCREMENTAL_VERSION 2
864 /* Read incremental snapshot formats 0 and 1 */
866 read_incr_db_01 (int version
, const char *initbuf
)
879 if (getline (&buf
, &bufsize
, listed_incremental_stream
) <= 0)
881 read_error (listed_incremental_option
);
889 buf
= strdup (initbuf
);
890 bufsize
= strlen (buf
) + 1;
893 sec
= TYPE_MINIMUM (time_t);
896 u
= strtoumax (buf
, &ebuf
, 10);
897 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
899 if (errno
|| buf
== ebuf
)
900 ERROR ((0, errno
, "%s:%ld: %s",
901 quotearg_colon (listed_incremental_option
),
903 _("Invalid time stamp")));
908 if (version
== 1 && *ebuf
)
910 char const *buf_ns
= ebuf
+ 1;
912 u
= strtoumax (buf_ns
, &ebuf
, 10);
913 if (!errno
&& BILLION
<= u
)
915 if (errno
|| buf_ns
== ebuf
)
917 ERROR ((0, errno
, "%s:%ld: %s",
918 quotearg_colon (listed_incremental_option
),
920 _("Invalid time stamp")));
921 sec
= TYPE_MINIMUM (time_t);
928 /* pre-1 incremental format does not contain nanoseconds */
932 newer_mtime_option
.tv_sec
= sec
;
933 newer_mtime_option
.tv_nsec
= nsec
;
936 while (0 < (n
= getline (&buf
, &bufsize
, listed_incremental_stream
)))
940 bool nfs
= buf
[0] == '+';
941 char *strp
= buf
+ nfs
;
942 struct timespec mtime
;
946 if (buf
[n
- 1] == '\n')
952 u
= strtoumax (strp
, &ebuf
, 10);
953 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
955 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
957 ERROR ((0, errno
, "%s:%ld: %s",
958 quotearg_colon (listed_incremental_option
), lineno
,
959 _("Invalid modification time (seconds)")));
967 u
= strtoumax (strp
, &ebuf
, 10);
968 if (!errno
&& BILLION
<= u
)
970 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
972 ERROR ((0, errno
, "%s:%ld: %s",
973 quotearg_colon (listed_incremental_option
), lineno
,
974 _("Invalid modification time (nanoseconds)")));
980 mtime
.tv_nsec
= nsec
;
984 memset (&mtime
, 0, sizeof mtime
);
987 u
= strtoumax (strp
, &ebuf
, 10);
988 if (!errno
&& TYPE_MAXIMUM (dev_t
) < u
)
990 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
992 ERROR ((0, errno
, "%s:%ld: %s",
993 quotearg_colon (listed_incremental_option
), lineno
,
994 _("Invalid device number")));
1002 u
= strtoumax (strp
, &ebuf
, 10);
1003 if (!errno
&& TYPE_MAXIMUM (ino_t
) < u
)
1005 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
1007 ERROR ((0, errno
, "%s:%ld: %s",
1008 quotearg_colon (listed_incremental_option
), lineno
,
1009 _("Invalid inode number")));
1017 unquote_string (strp
);
1018 note_directory (strp
, mtime
, dev
, ino
, nfs
, false, NULL
);
1023 /* Read a nul-terminated string from FP and store it in STK.
1024 Store the number of bytes read (including nul terminator) in PCOUNT.
1026 Return the last character read or EOF on end of file. */
1028 read_obstack (FILE *fp
, struct obstack
*stk
, size_t *pcount
)
1033 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
1034 obstack_1grow (stk
, c
);
1035 obstack_1grow (stk
, 0);
1041 /* Read from file FP a nul-terminated string and convert it to
1042 intmax_t. Return the resulting value in PVAL. Assume '-' has
1045 Throw a fatal error if the string cannot be converted or if the
1046 converted value is less than MIN_VAL. */
1049 read_negative_num (FILE *fp
, intmax_t min_val
, intmax_t *pval
)
1053 char buf
[INT_BUFSIZE_BOUND (intmax_t)];
1057 for (i
= 1; ISDIGIT (c
= getc (fp
)); i
++)
1059 if (i
== sizeof buf
- 1)
1060 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
1067 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
1069 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1074 *pval
= strtoimax (buf
, &ep
, 10);
1075 if (c
|| errno
|| *pval
< min_val
)
1076 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1079 /* Read from file FP a nul-terminated string and convert it to
1080 uintmax_t. Return the resulting value in PVAL. Assume C has
1083 Throw a fatal error if the string cannot be converted or if the
1084 converted value exceeds MAX_VAL.
1086 Return the last character read or EOF on end of file. */
1089 read_unsigned_num (int c
, FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1092 char buf
[UINTMAX_STRSIZE_BOUND
], *ep
;
1094 for (i
= 0; ISDIGIT (c
); i
++)
1096 if (i
== sizeof buf
- 1)
1097 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
1105 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
1109 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1114 *pval
= strtoumax (buf
, &ep
, 10);
1115 if (c
|| errno
|| max_val
< *pval
)
1116 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1120 /* Read from file FP a nul-terminated string and convert it to
1121 uintmax_t. Return the resulting value in PVAL.
1123 Throw a fatal error if the string cannot be converted or if the
1124 converted value exceeds MAX_VAL.
1126 Return the last character read or EOF on end of file. */
1129 read_num (FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1131 return read_unsigned_num (getc (fp
), fp
, max_val
, pval
);
1134 /* Read from FP two NUL-terminated strings representing a struct
1135 timespec. Return the resulting value in PVAL.
1137 Throw a fatal error if the string cannot be converted. */
1140 read_timespec (FILE *fp
, struct timespec
*pval
)
1148 read_negative_num (fp
, TYPE_MINIMUM (time_t), &i
);
1154 c
= read_unsigned_num (c
, fp
, TYPE_MAXIMUM (time_t), &u
);
1158 if (c
|| read_num (fp
, BILLION
- 1, &u
))
1159 FATAL_ERROR ((0, 0, "%s: %s",
1160 quotearg_colon (listed_incremental_option
),
1161 _("Unexpected EOF in snapshot file")));
1165 /* Read incremental snapshot format 2 */
1172 obstack_init (&stk
);
1174 read_timespec (listed_incremental_stream
, &newer_mtime_option
);
1178 struct timespec mtime
;
1186 if (read_num (listed_incremental_stream
, 1, &u
))
1187 return; /* Normal return */
1191 read_timespec (listed_incremental_stream
, &mtime
);
1193 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (dev_t
), &u
))
1197 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (ino_t
), &u
))
1201 if (read_obstack (listed_incremental_stream
, &stk
, &s
))
1204 name
= obstack_finish (&stk
);
1206 while (read_obstack (listed_incremental_stream
, &stk
, &s
) == 0 && s
> 1)
1208 if (getc (listed_incremental_stream
) != 0)
1209 FATAL_ERROR ((0, 0, "%s: %s",
1210 quotearg_colon (listed_incremental_option
),
1211 _("Missing record terminator")));
1213 content
= obstack_finish (&stk
);
1214 note_directory (name
, mtime
, dev
, ino
, nfs
, false, content
);
1215 obstack_free (&stk
, content
);
1217 FATAL_ERROR ((0, 0, "%s: %s",
1218 quotearg_colon (listed_incremental_option
),
1219 _("Unexpected EOF in snapshot file")));
1222 /* Read incremental snapshot file (directory file).
1223 If the file has older incremental version, make sure that it is processed
1224 correctly and that tar will use the most conservative backup method among
1225 possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
1226 etc.) This ensures that the snapshots are updated to the recent version
1227 without any loss of data. */
1229 read_directory_file (void)
1235 /* Open the file for both read and write. That way, we can write
1236 it later without having to reopen it, and don't have to worry if
1237 we chdir in the meantime. */
1238 fd
= open (listed_incremental_option
, O_RDWR
| O_CREAT
, MODE_RW
);
1241 open_error (listed_incremental_option
);
1245 listed_incremental_stream
= fdopen (fd
, "r+");
1246 if (! listed_incremental_stream
)
1248 open_error (listed_incremental_option
);
1253 if (0 < getline (&buf
, &bufsize
, listed_incremental_stream
))
1256 uintmax_t incremental_version
;
1258 if (strncmp (buf
, PACKAGE_NAME
, sizeof PACKAGE_NAME
- 1) == 0)
1260 ebuf
= buf
+ sizeof PACKAGE_NAME
- 1;
1262 ERROR((1, 0, _("Bad incremental file format")));
1263 for (; *ebuf
!= '-'; ebuf
++)
1265 ERROR((1, 0, _("Bad incremental file format")));
1267 incremental_version
= strtoumax (ebuf
+ 1, NULL
, 10);
1270 incremental_version
= 0;
1272 switch (incremental_version
)
1276 read_incr_db_01 (incremental_version
, buf
);
1279 case TAR_INCREMENTAL_VERSION
:
1284 ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX
),
1285 incremental_version
));
1290 if (ferror (listed_incremental_stream
))
1291 read_error (listed_incremental_option
);
1296 /* Output incremental data for the directory ENTRY to the file DATA.
1297 Return nonzero if successful, preserving errno on write failure. */
1299 write_directory_file_entry (void *entry
, void *data
)
1301 struct directory
const *directory
= entry
;
1304 if (DIR_IS_FOUND (directory
))
1306 char buf
[UINTMAX_STRSIZE_BOUND
];
1309 s
= DIR_IS_NFS (directory
) ? "1" : "0";
1310 fwrite (s
, 2, 1, fp
);
1311 s
= (TYPE_SIGNED (time_t)
1312 ? imaxtostr (directory
->mtime
.tv_sec
, buf
)
1313 : umaxtostr (directory
->mtime
.tv_sec
, buf
));
1314 fwrite (s
, strlen (s
) + 1, 1, fp
);
1315 s
= umaxtostr (directory
->mtime
.tv_nsec
, buf
);
1316 fwrite (s
, strlen (s
) + 1, 1, fp
);
1317 s
= umaxtostr (directory
->device_number
, buf
);
1318 fwrite (s
, strlen (s
) + 1, 1, fp
);
1319 s
= umaxtostr (directory
->inode_number
, buf
);
1320 fwrite (s
, strlen (s
) + 1, 1, fp
);
1322 fwrite (directory
->name
, strlen (directory
->name
) + 1, 1, fp
);
1323 if (directory
->dump
)
1328 for (p
= dumpdir_first (directory
->dump
, 0, &itr
);
1330 p
= dumpdir_next (itr
))
1331 fwrite (p
, strlen (p
) + 1, 1, fp
);
1334 fwrite ("\0\0", 2, 1, fp
);
1337 return ! ferror (fp
);
1341 write_directory_file (void)
1343 FILE *fp
= listed_incremental_stream
;
1344 char buf
[UINTMAX_STRSIZE_BOUND
];
1350 if (fseek (fp
, 0L, SEEK_SET
) != 0)
1351 seek_error (listed_incremental_option
);
1352 if (sys_truncate (fileno (fp
)) != 0)
1353 truncate_error (listed_incremental_option
);
1355 fprintf (fp
, "%s-%s-%d\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1356 TAR_INCREMENTAL_VERSION
);
1358 s
= (TYPE_SIGNED (time_t)
1359 ? imaxtostr (start_time
.tv_sec
, buf
)
1360 : umaxtostr (start_time
.tv_sec
, buf
));
1361 fwrite (s
, strlen (s
) + 1, 1, fp
);
1362 s
= umaxtostr (start_time
.tv_nsec
, buf
);
1363 fwrite (s
, strlen (s
) + 1, 1, fp
);
1365 if (! ferror (fp
) && directory_table
)
1366 hash_do_for_each (directory_table
, write_directory_file_entry
, fp
);
1369 write_error (listed_incremental_option
);
1370 if (fclose (fp
) != 0)
1371 close_error (listed_incremental_option
);
1375 /* Restoration of incremental dumps. */
1378 get_gnu_dumpdir (struct tar_stat_info
*stat_info
)
1382 union block
*data_block
;
1386 size
= stat_info
->stat
.st_size
;
1388 archive_dir
= xmalloc (size
);
1391 set_next_block_after (current_header
);
1392 mv_begin (stat_info
);
1394 for (; size
> 0; size
-= copied
)
1396 mv_size_left (size
);
1397 data_block
= find_next_block ();
1399 ERROR ((1, 0, _("Unexpected EOF in archive")));
1400 copied
= available_space_after (data_block
);
1403 memcpy (to
, data_block
->buffer
, copied
);
1405 set_next_block_after ((union block
*)
1406 (data_block
->buffer
+ copied
- 1));
1411 stat_info
->dumpdir
= archive_dir
;
1412 stat_info
->skipped
= true; /* For skip_member() and friends
1413 to work correctly */
1416 /* Return T if STAT_INFO represents a dumpdir archive member.
1417 Note: can invalidate current_header. It happens if flush_archive()
1418 gets called within get_gnu_dumpdir() */
1420 is_dumpdir (struct tar_stat_info
*stat_info
)
1422 if (stat_info
->is_dumpdir
&& !stat_info
->dumpdir
)
1423 get_gnu_dumpdir (stat_info
);
1424 return stat_info
->is_dumpdir
;
1428 dumpdir_ok (char *dumpdir
)
1431 int has_tempdir
= 0;
1434 for (p
= dumpdir
; *p
; p
+= strlen (p
) + 1)
1436 if (expect
&& *p
!= expect
)
1439 _("Malformed dumpdir: expected '%c' but found %#3o"),
1449 _("Malformed dumpdir: 'X' duplicated")));
1462 _("Malformed dumpdir: empty name in 'R'")));
1475 _("Malformed dumpdir: 'T' not preceeded by 'R'")));
1478 if (p
[1] == 0 && !has_tempdir
)
1481 _("Malformed dumpdir: empty name in 'T'")));
1493 /* FIXME: bail out? */
1501 _("Malformed dumpdir: expected '%c' but found end of data"),
1507 WARN ((0, 0, _("Malformed dumpdir: 'X' never used")));
1512 /* Examine the directories under directory_name and delete any
1513 files that were not there at the time of the back-up. */
1515 try_purge_directory (char const *directory_name
)
1518 char *cur
, *arc
, *p
;
1519 char *temp_stub
= NULL
;
1520 struct dumpdir
*dump
;
1522 if (!is_dumpdir (¤t_stat_info
))
1525 current_dir
= savedir (directory_name
);
1528 /* The directory doesn't exist now. It'll be created. In any
1529 case, we don't have to delete any files out of it. */
1532 /* Verify if dump directory is sane */
1533 if (!dumpdir_ok (current_stat_info
.dumpdir
))
1536 /* Process renames */
1537 for (arc
= current_stat_info
.dumpdir
; *arc
; arc
+= strlen (arc
) + 1)
1541 #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
1542 size_t len
= strlen (arc
+ 1);
1543 temp_stub
= xrealloc (temp_stub
, len
+ 1 + sizeof TEMP_DIR_TEMPLATE
);
1544 memcpy (temp_stub
, arc
+ 1, len
);
1545 temp_stub
[len
] = '/';
1546 memcpy (temp_stub
+ len
+ 1, TEMP_DIR_TEMPLATE
,
1547 sizeof TEMP_DIR_TEMPLATE
);
1548 if (!mkdtemp (temp_stub
))
1551 _("Cannot create temporary directory using template %s"),
1552 quote (temp_stub
)));
1558 else if (*arc
== 'R')
1562 arc
+= strlen (arc
) + 1;
1565 /* Ensure that neither source nor destination are absolute file
1566 names (unless permitted by -P option), and that they do not
1567 contain dubious parts (e.g. ../).
1569 This is an extra safety precaution. Besides, it might be
1570 necessary to extract from archives created with tar versions
1574 src
= safer_name_suffix (src
, false, absolute_names_option
);
1576 dst
= safer_name_suffix (dst
, false, absolute_names_option
);
1583 if (!rename_directory (src
, dst
))
1587 /* FIXME: Make sure purge_directory(dst) will return
1596 /* Process deletes */
1597 dump
= dumpdir_create (current_stat_info
.dumpdir
);
1599 for (cur
= current_dir
; *cur
; cur
+= strlen (cur
) + 1)
1605 p
= new_name (directory_name
, cur
);
1607 if (deref_stat (false, p
, &st
))
1609 if (errno
!= ENOENT
) /* FIXME: Maybe keep a list of renamed
1610 dirs and check it here? */
1613 WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
1614 quotearg_colon (p
)));
1619 if (!(entry
= dumpdir_locate (dump
, cur
))
1620 || (*entry
== 'D' && !S_ISDIR (st
.st_mode
))
1621 || (*entry
== 'Y' && S_ISDIR (st
.st_mode
)))
1623 if (one_file_system_option
&& st
.st_dev
!= root_device
)
1626 _("%s: directory is on a different device: not purging"),
1627 quotearg_colon (p
)));
1631 if (! interactive_option
|| confirm ("delete", p
))
1634 fprintf (stdlis
, _("%s: Deleting %s\n"),
1635 program_name
, quote (p
));
1636 if (! remove_any_file (p
, RECURSIVE_REMOVE_OPTION
))
1639 ERROR ((0, e
, _("%s: Cannot remove"), quotearg_colon (p
)));
1645 dumpdir_free (dump
);
1652 purge_directory (char const *directory_name
)
1654 if (!try_purge_directory (directory_name
))
1659 list_dumpdir (char *buffer
, size_t size
)
1672 fprintf (stdlis
, "%c", *buffer
);
1675 fprintf (stdlis
, " ");
1683 fputc ('\n', stdlis
);
1690 fputc (*buffer
, stdlis
);