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, 2009 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 *caname
; /* canonical name */
77 char *name
; /* file name of directory */
81 dumpdir_create0 (const char *contents
, const char *cmask
)
84 size_t i
, total
, ctsize
, len
;
88 for (i
= 0, total
= 0, ctsize
= 1, q
= contents
; *q
; total
++, q
+= len
)
92 if (!cmask
|| strchr (cmask
, *q
))
95 dump
= xmalloc (sizeof (*dump
) + ctsize
);
96 dump
->contents
= (char*)(dump
+ 1);
97 memcpy (dump
->contents
, contents
, ctsize
);
100 dump
->elv
= xcalloc (i
+ 1, sizeof (dump
->elv
[0]));
102 for (i
= 0, p
= dump
->contents
; *p
; p
+= strlen (p
) + 1)
104 if (!cmask
|| strchr (cmask
, *p
))
105 dump
->elv
[i
++] = p
+ 1;
112 dumpdir_create (const char *contents
)
114 return dumpdir_create0 (contents
, "YND");
118 dumpdir_free (struct dumpdir
*dump
)
125 compare_dirnames (const void *first
, const void *second
)
127 char const *const *name1
= first
;
128 char const *const *name2
= second
;
129 return strcmp (*name1
, *name2
);
132 /* Locate NAME in the dumpdir array DUMP.
133 Return pointer to the slot in DUMP->contents, or NULL if not found */
135 dumpdir_locate (struct dumpdir
*dump
, const char *name
)
141 ptr
= bsearch (&name
, dump
->elv
, dump
->elc
, sizeof (dump
->elv
[0]),
143 return ptr
? *ptr
- 1: NULL
;
148 struct dumpdir
*dump
; /* Dumpdir being iterated */
149 int all
; /* Iterate over all entries, not only D/N/Y */
150 size_t next
; /* Index of the next element */
154 dumpdir_next (struct dumpdir_iter
*itr
)
156 size_t cur
= itr
->next
;
161 ret
= itr
->dump
->contents
+ cur
;
164 itr
->next
+= strlen (ret
) + 1;
166 else if (cur
< itr
->dump
->elc
)
168 ret
= itr
->dump
->elv
[cur
] - 1;
176 dumpdir_first (struct dumpdir
*dump
, int all
, struct dumpdir_iter
**pitr
)
178 struct dumpdir_iter
*itr
= xmalloc (sizeof (*itr
));
183 return dumpdir_next (itr
);
186 /* Return size in bytes of the dumpdir array P */
188 dumpdir_size (const char *p
)
194 size_t size
= strlen (p
) + 1;
202 static struct directory
*dirhead
, *dirtail
;
203 static Hash_table
*directory_table
;
204 static Hash_table
*directory_meta_table
;
206 #if HAVE_ST_FSTYPE_STRING
207 static char const nfs_string
[] = "nfs";
208 # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
210 # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
211 # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
214 /* Calculate the hash of a directory. */
216 hash_directory_canonical_name (void const *entry
, size_t n_buckets
)
218 struct directory
const *directory
= entry
;
219 return hash_string (directory
->caname
, n_buckets
);
222 /* Compare two directories for equality of their names. */
224 compare_directory_canonical_names (void const *entry1
, void const *entry2
)
226 struct directory
const *directory1
= entry1
;
227 struct directory
const *directory2
= entry2
;
228 return strcmp (directory1
->caname
, directory2
->caname
) == 0;
232 hash_directory_meta (void const *entry
, size_t n_buckets
)
234 struct directory
const *directory
= entry
;
235 /* FIXME: Work out a better algorytm */
236 return (directory
->device_number
+ directory
->inode_number
) % n_buckets
;
239 /* Compare two directories for equality of their device and inode numbers. */
241 compare_directory_meta (void const *entry1
, void const *entry2
)
243 struct directory
const *directory1
= entry1
;
244 struct directory
const *directory2
= entry2
;
245 return directory1
->device_number
== directory2
->device_number
246 && directory1
->inode_number
== directory2
->inode_number
;
249 /* Make a directory entry for given relative NAME and canonical name CANAME.
250 The latter is "stolen", i.e. the returned directory contains pointer to
252 static struct directory
*
253 make_directory (const char *name
, char *caname
)
255 size_t namelen
= strlen (name
);
256 struct directory
*directory
= xmalloc (sizeof (*directory
));
257 directory
->next
= NULL
;
258 directory
->dump
= directory
->idump
= NULL
;
259 directory
->orig
= NULL
;
260 directory
->flags
= false;
261 if (namelen
&& ISSLASH (name
[namelen
- 1]))
263 directory
->name
= xmalloc (namelen
+ 1);
264 memcpy (directory
->name
, name
, namelen
);
265 directory
->name
[namelen
] = 0;
266 directory
->caname
= caname
;
267 directory
->tagfile
= NULL
;
272 free_directory (struct directory
*dir
)
279 static struct directory
*
280 attach_directory (const char *name
)
282 char *cname
= normalize_filename (name
);
283 struct directory
*dir
= make_directory (name
, cname
);
294 dirlist_replace_prefix (const char *pref
, const char *repl
)
296 struct directory
*dp
;
297 size_t pref_len
= strlen (pref
);
298 size_t repl_len
= strlen (repl
);
299 for (dp
= dirhead
; dp
; dp
= dp
->next
)
300 replace_prefix (&dp
->name
, pref
, pref_len
, repl
, repl_len
);
303 /* Create and link a new directory entry for directory NAME, having a
304 device number DEV and an inode number INO, with NFS indicating
305 whether it is an NFS device and FOUND indicating whether we have
306 found that the directory exists. */
307 static struct directory
*
308 note_directory (char const *name
, struct timespec mtime
,
309 dev_t dev
, ino_t ino
, bool nfs
, bool found
,
310 const char *contents
)
312 struct directory
*directory
= attach_directory (name
);
314 directory
->mtime
= mtime
;
315 directory
->device_number
= dev
;
316 directory
->inode_number
= ino
;
317 directory
->children
= CHANGED_CHILDREN
;
319 DIR_SET_FLAG (directory
, DIRF_NFS
);
321 DIR_SET_FLAG (directory
, DIRF_FOUND
);
323 directory
->dump
= dumpdir_create (contents
);
325 directory
->dump
= NULL
;
327 if (! ((directory_table
328 || (directory_table
= hash_initialize (0, 0,
329 hash_directory_canonical_name
,
330 compare_directory_canonical_names
, 0)))
331 && hash_insert (directory_table
, directory
)))
334 if (! ((directory_meta_table
335 || (directory_meta_table
= hash_initialize (0, 0,
337 compare_directory_meta
,
339 && hash_insert (directory_meta_table
, directory
)))
345 /* Return a directory entry for a given file NAME, or zero if none found. */
346 static struct directory
*
347 find_directory (const char *name
)
349 if (! directory_table
)
353 char *caname
= normalize_filename (name
);
354 struct directory
*dir
= make_directory (name
, caname
);
355 struct directory
*ret
= hash_lookup (directory_table
, dir
);
356 free_directory (dir
);
362 /* Remove directory entry for the given CANAME */
364 remove_directory (const char *caname
)
366 struct directory
*dir
= make_directory (caname
, xstrdup (caname
));
367 struct directory
*ret
= hash_delete (directory_table
, dir
);
369 free_directory (ret
);
370 free_directory (dir
);
374 /* If first OLD_PREFIX_LEN bytes of DIR->NAME name match OLD_PREFIX,
375 replace them with NEW_PREFIX. */
377 rebase_directory (struct directory
*dir
,
378 const char *old_prefix
, size_t old_prefix_len
,
379 const char *new_prefix
, size_t new_prefix_len
)
381 replace_prefix (&dir
->name
, old_prefix
, old_prefix_len
,
382 new_prefix
, new_prefix_len
);
385 /* Return a directory entry for a given combination of device and inode
386 numbers, or zero if none found. */
387 static struct directory
*
388 find_directory_meta (dev_t dev
, ino_t ino
)
390 if (! directory_meta_table
)
394 struct directory
*dir
= make_directory ("", NULL
);
395 struct directory
*ret
;
396 dir
->device_number
= dev
;
397 dir
->inode_number
= ino
;
398 ret
= hash_lookup (directory_meta_table
, dir
);
399 free_directory (dir
);
405 update_parent_directory (const char *name
)
407 struct directory
*directory
;
411 directory
= find_directory (p
);
415 if (deref_stat (dereference_option
, p
, &st
) != 0)
418 stat_diag (directory
->name
);
419 /* else: should have been already reported */
422 directory
->mtime
= get_stat_mtime (&st
);
427 #define PD_FORCE_CHILDREN 0x10
428 #define PD_FORCE_INIT 0x20
429 #define PD_CHILDREN(f) ((f) & 3)
431 static struct directory
*
432 procdir (const char *name_buffer
, struct stat
*stat_data
,
437 struct directory
*directory
;
438 bool nfs
= NFS_FILE_STAT (*stat_data
);
440 if ((directory
= find_directory (name_buffer
)) != NULL
)
442 if (DIR_IS_INITED (directory
))
444 if (flag
& PD_FORCE_INIT
)
446 assign_string (&directory
->name
, name_buffer
);
450 *entry
= 'N'; /* Avoid duplicating this directory */
455 if (strcmp (directory
->name
, name_buffer
))
461 /* With NFS, the same file can have two different devices
462 if an NFS directory is mounted in multiple locations,
463 which is relatively common when automounting.
464 To avoid spurious incremental redumping of
465 directories, consider all NFS devices as equal,
466 relying on the i-node to establish differences. */
468 if (! ((!check_device_option
469 || (DIR_IS_NFS (directory
) && nfs
)
470 || directory
->device_number
== stat_data
->st_dev
)
471 && directory
->inode_number
== stat_data
->st_ino
))
473 /* FIXME: find_directory_meta ignores nfs */
474 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
478 if (strcmp (d
->name
, name_buffer
))
480 WARNOPT (WARN_RENAME_DIRECTORY
,
482 _("%s: Directory has been renamed from %s"),
483 quotearg_colon (name_buffer
),
484 quote_n (1, d
->name
)));
486 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
487 dirlist_replace_prefix (d
->name
, name_buffer
);
489 directory
->children
= CHANGED_CHILDREN
;
493 WARNOPT (WARN_RENAME_DIRECTORY
,
494 (0, 0, _("%s: Directory has been renamed"),
495 quotearg_colon (name_buffer
)));
496 directory
->children
= ALL_CHILDREN
;
497 directory
->device_number
= stat_data
->st_dev
;
498 directory
->inode_number
= stat_data
->st_ino
;
501 DIR_SET_FLAG (directory
, DIRF_NFS
);
504 directory
->children
= CHANGED_CHILDREN
;
506 DIR_SET_FLAG (directory
, DIRF_FOUND
);
510 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
513 directory
= note_directory (name_buffer
,
514 get_stat_mtime(stat_data
),
523 if (strcmp (d
->name
, name_buffer
))
525 WARNOPT (WARN_RENAME_DIRECTORY
,
526 (0, 0, _("%s: Directory has been renamed from %s"),
527 quotearg_colon (name_buffer
),
528 quote_n (1, d
->name
)));
530 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
531 dirlist_replace_prefix (d
->name
, name_buffer
);
533 directory
->children
= CHANGED_CHILDREN
;
537 DIR_SET_FLAG (directory
, DIRF_NEW
);
538 WARNOPT (WARN_NEW_DIRECTORY
,
539 (0, 0, _("%s: Directory is new"),
540 quotearg_colon (name_buffer
)));
541 directory
->children
=
542 (listed_incremental_option
543 || (OLDER_STAT_TIME (*stat_data
, m
)
544 || (after_date_option
545 && OLDER_STAT_TIME (*stat_data
, c
))))
551 /* If the directory is on another device and --one-file-system was given,
553 if (one_file_system_option
&& device
!= stat_data
->st_dev
554 /* ... except if it was explicitely given in the command line */
555 && !is_individual_file (name_buffer
))
559 _("%s: directory is on a different filesystem; not dumped"),
560 quotearg_colon (directory->name)));
562 directory
->children
= NO_CHILDREN
;
563 else if (flag
& PD_FORCE_CHILDREN
)
565 directory
->children
= PD_CHILDREN(flag
);
566 if (directory
->children
== NO_CHILDREN
)
570 DIR_SET_FLAG (directory
, DIRF_INIT
);
572 if (directory
->children
!= NO_CHILDREN
)
574 const char *tag_file_name
;
576 switch (check_exclusion_tags (name_buffer
, &tag_file_name
))
578 case exclusion_tag_all
:
579 /* This warning can be duplicated by code in dump_file0, but only
580 in case when the topmost directory being archived contains
582 exclusion_tag_warning (name_buffer
, tag_file_name
,
583 _("directory not dumped"));
585 directory
->children
= NO_CHILDREN
;
588 case exclusion_tag_contents
:
589 exclusion_tag_warning (name_buffer
, tag_file_name
,
590 _("contents not dumped"));
591 directory
->children
= NO_CHILDREN
;
594 case exclusion_tag_under
:
595 exclusion_tag_warning (name_buffer
, tag_file_name
,
596 _("contents not dumped"));
597 directory
->tagfile
= tag_file_name
;
600 case exclusion_tag_none
:
608 /* Compare dumpdir array from DIRECTORY with directory listing DIR and
609 build a new dumpdir template.
611 DIR must be returned by a previous call to savedir().
613 File names in DIRECTORY->dump->contents must be sorted
616 DIRECTORY->dump is replaced with the created template. Each entry is
617 prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
620 makedumpdir (struct directory
*directory
, const char *dir
)
623 dirsize
, /* Number of elements in DIR */
624 len
; /* Length of DIR, including terminating nul */
627 char *new_dump
, *new_dump_ptr
;
628 struct dumpdir
*dump
;
630 if (directory
->children
== ALL_CHILDREN
)
632 else if (DIR_IS_RENAMED (directory
))
633 dump
= directory
->orig
->idump
?
634 directory
->orig
->idump
: directory
->orig
->dump
;
636 dump
= directory
->dump
;
638 /* Count the size of DIR and the number of elements it contains */
641 for (p
= dir
; *p
; p
+= strlen (p
) + 1, dirsize
++)
642 len
+= strlen (p
) + 2;
645 /* Create a sorted directory listing */
646 array
= xcalloc (dirsize
, sizeof array
[0]);
647 for (i
= 0, p
= dir
; *p
; p
+= strlen (p
) + 1, i
++)
650 qsort (array
, dirsize
, sizeof (array
[0]), compare_dirnames
);
652 /* Prepare space for new dumpdir */
653 new_dump
= xmalloc (len
);
654 new_dump_ptr
= new_dump
;
656 /* Fill in the dumpdir template */
657 for (i
= 0; i
< dirsize
; i
++)
659 const char *loc
= dumpdir_locate (dump
, array
[i
]);
662 if (directory
->tagfile
)
663 *new_dump_ptr
= strcmp (directory
->tagfile
, array
[i
]) == 0 ?
669 else if (directory
->tagfile
)
670 *new_dump_ptr
++ = strcmp (directory
->tagfile
, array
[i
]) == 0 ?
673 *new_dump_ptr
++ = 'Y'; /* New entry */
675 /* Copy the file name */
676 for (p
= array
[i
]; (*new_dump_ptr
++ = *p
++); )
680 directory
->idump
= directory
->dump
;
681 directory
->dump
= dumpdir_create0 (new_dump
, NULL
);
685 /* Recursively scan the given directory DIR.
686 DEVICE is the device number where DIR resides (for --one-file-system).
687 If CMDLINE is true, the directory name was explicitly listed in the
689 Unless *PDIR is NULL, store there a pointer to the struct directory
692 scan_directory (char *dir
, dev_t device
, bool cmdline
)
694 char *dirp
= savedir (dir
); /* for scanning directory */
695 char *name_buffer
; /* directory, `/', and directory member */
696 size_t name_buffer_size
; /* allocated size of name_buffer, minus 2 */
697 size_t name_length
; /* used length in name_buffer */
698 struct stat stat_data
;
699 struct directory
*directory
;
705 name_buffer_size
= strlen (dir
) + NAME_FIELD_SIZE
;
706 name_buffer
= xmalloc (name_buffer_size
+ 2);
707 strcpy (name_buffer
, dir
);
708 zap_slashes (name_buffer
);
710 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
712 dir_removed_diag (name_buffer
, cmdline
, stat_diag
);
718 directory
= procdir (name_buffer
, &stat_data
, device
,
719 (cmdline
? PD_FORCE_INIT
: 0),
722 name_length
= strlen (name_buffer
);
723 if (! ISSLASH (name_buffer
[name_length
- 1]))
725 name_buffer
[name_length
] = DIRECTORY_SEPARATOR
;
726 /* name_buffer has been allocated an extra slot */
727 name_buffer
[++name_length
] = 0;
730 if (dirp
&& directory
->children
!= NO_CHILDREN
)
732 char *entry
; /* directory entry being scanned */
733 size_t entrylen
; /* length of directory entry */
736 makedumpdir (directory
, dirp
);
738 for (entry
= dumpdir_first (directory
->dump
, 1, &itr
);
740 entry
= dumpdir_next (itr
))
742 entrylen
= strlen (entry
);
743 if (name_buffer_size
<= entrylen
- 1 + name_length
)
746 name_buffer_size
+= NAME_FIELD_SIZE
;
747 while (name_buffer_size
<= entrylen
- 1 + name_length
);
748 name_buffer
= xrealloc (name_buffer
, name_buffer_size
+ 2);
750 strcpy (name_buffer
+ name_length
, entry
+ 1);
752 if (*entry
== 'I') /* Ignored entry */
754 else if (excluded_name (name_buffer
))
758 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
760 file_removed_diag (name_buffer
, false, stat_diag
);
765 if (S_ISDIR (stat_data
.st_mode
))
768 if (!recursion_option
)
769 pd_flag
|= PD_FORCE_CHILDREN
| NO_CHILDREN
;
770 else if (directory
->children
== ALL_CHILDREN
)
771 pd_flag
|= PD_FORCE_CHILDREN
| ALL_CHILDREN
;
773 procdir (name_buffer
, &stat_data
, device
, pd_flag
, entry
);
776 else if (one_file_system_option
&& device
!= stat_data
.st_dev
)
779 else if (*entry
== 'Y')
780 /* New entry, skip further checks */;
782 /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
784 else if (OLDER_STAT_TIME (stat_data
, m
)
785 && (!after_date_option
786 || OLDER_STAT_TIME (stat_data
, c
)))
802 /* Return pointer to the contents of the directory DIR */
804 directory_contents (struct directory
*dir
)
808 return dir
->dump
? dir
->dump
->contents
: NULL
;
811 /* A "safe" version of directory_contents, which never returns NULL. */
813 safe_directory_contents (struct directory
*dir
)
815 const char *ret
= directory_contents (dir
);
816 return ret
? ret
: "\0\0\0\0";
820 name_fill_directory (struct name
*name
, dev_t device
, bool cmdline
)
822 name
->directory
= scan_directory (name
->name
, device
, cmdline
);
827 obstack_code_rename (struct obstack
*stk
, char *from
, char *to
)
831 s
= from
[0] == 0 ? from
:
832 safer_name_suffix (from
, false, absolute_names_option
);
833 obstack_1grow (stk
, 'R');
834 obstack_grow (stk
, s
, strlen (s
) + 1);
837 safer_name_suffix (to
, false, absolute_names_option
);
838 obstack_1grow (stk
, 'T');
839 obstack_grow (stk
, s
, strlen (s
) + 1);
843 store_rename (struct directory
*dir
, struct obstack
*stk
)
845 if (DIR_IS_RENAMED (dir
))
847 struct directory
*prev
, *p
;
849 /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
850 are ignored when hit by this function next time.
851 If the chain forms a cycle, prev points to the entry DIR is renamed
852 from. In this case it still retains DIRF_RENAMED flag, which will be
853 cleared in the `else' branch below */
854 for (prev
= dir
; prev
&& prev
->orig
!= dir
; prev
= prev
->orig
)
855 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
859 for (p
= dir
; p
&& p
->orig
; p
= p
->orig
)
860 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
866 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
868 /* Break the cycle by using a temporary name for one of its
870 First, create a temp name stub entry. */
871 temp_name
= dir_name (dir
->name
);
872 obstack_1grow (stk
, 'X');
873 obstack_grow (stk
, temp_name
, strlen (temp_name
) + 1);
875 obstack_code_rename (stk
, dir
->name
, "");
877 for (p
= dir
; p
!= prev
; p
= p
->orig
)
878 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
880 obstack_code_rename (stk
, "", prev
->name
);
886 append_incremental_renames (struct directory
*dir
)
890 struct directory
*dp
;
897 dump
= directory_contents (dir
);
900 size
= dumpdir_size (dump
) - 1;
901 obstack_grow (&stk
, dump
, size
);
906 for (dp
= dirhead
; dp
; dp
= dp
->next
)
907 store_rename (dp
, &stk
);
909 if (obstack_object_size (&stk
) != size
)
911 obstack_1grow (&stk
, 0);
912 dumpdir_free (dir
->dump
);
913 dir
->dump
= dumpdir_create (obstack_finish (&stk
));
915 obstack_free (&stk
, NULL
);
920 static FILE *listed_incremental_stream
;
922 /* Version of incremental format snapshots (directory files) used by this
923 tar. Currently it is supposed to be a single decimal number. 0 means
924 incremental snapshots as per tar version before 1.15.2.
926 The current tar version supports incremental versions from
927 0 up to TAR_INCREMENTAL_VERSION, inclusive.
928 It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
930 #define TAR_INCREMENTAL_VERSION 2
932 /* Read incremental snapshot formats 0 and 1 */
934 read_incr_db_01 (int version
, const char *initbuf
)
947 if (getline (&buf
, &bufsize
, listed_incremental_stream
) <= 0)
949 read_error (listed_incremental_option
);
957 buf
= strdup (initbuf
);
958 bufsize
= strlen (buf
) + 1;
961 sec
= TYPE_MINIMUM (time_t);
964 u
= strtoumax (buf
, &ebuf
, 10);
965 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
967 if (errno
|| buf
== ebuf
)
968 ERROR ((0, errno
, "%s:%ld: %s",
969 quotearg_colon (listed_incremental_option
),
971 _("Invalid time stamp")));
976 if (version
== 1 && *ebuf
)
978 char const *buf_ns
= ebuf
+ 1;
980 u
= strtoumax (buf_ns
, &ebuf
, 10);
981 if (!errno
&& BILLION
<= u
)
983 if (errno
|| buf_ns
== ebuf
)
985 ERROR ((0, errno
, "%s:%ld: %s",
986 quotearg_colon (listed_incremental_option
),
988 _("Invalid time stamp")));
989 sec
= TYPE_MINIMUM (time_t);
996 /* pre-1 incremental format does not contain nanoseconds */
1000 newer_mtime_option
.tv_sec
= sec
;
1001 newer_mtime_option
.tv_nsec
= nsec
;
1004 while (0 < (n
= getline (&buf
, &bufsize
, listed_incremental_stream
)))
1008 bool nfs
= buf
[0] == '+';
1009 char *strp
= buf
+ nfs
;
1010 struct timespec mtime
;
1014 if (buf
[n
- 1] == '\n')
1020 u
= strtoumax (strp
, &ebuf
, 10);
1021 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
1023 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
1025 ERROR ((0, errno
, "%s:%ld: %s",
1026 quotearg_colon (listed_incremental_option
), lineno
,
1027 _("Invalid modification time (seconds)")));
1035 u
= strtoumax (strp
, &ebuf
, 10);
1036 if (!errno
&& BILLION
<= u
)
1038 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
1040 ERROR ((0, errno
, "%s:%ld: %s",
1041 quotearg_colon (listed_incremental_option
), lineno
,
1042 _("Invalid modification time (nanoseconds)")));
1048 mtime
.tv_nsec
= nsec
;
1052 memset (&mtime
, 0, sizeof mtime
);
1055 u
= strtoumax (strp
, &ebuf
, 10);
1056 if (!errno
&& TYPE_MAXIMUM (dev_t
) < u
)
1058 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
1060 ERROR ((0, errno
, "%s:%ld: %s",
1061 quotearg_colon (listed_incremental_option
), lineno
,
1062 _("Invalid device number")));
1070 u
= strtoumax (strp
, &ebuf
, 10);
1071 if (!errno
&& TYPE_MAXIMUM (ino_t
) < u
)
1073 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
1075 ERROR ((0, errno
, "%s:%ld: %s",
1076 quotearg_colon (listed_incremental_option
), lineno
,
1077 _("Invalid inode number")));
1085 unquote_string (strp
);
1086 note_directory (strp
, mtime
, dev
, ino
, nfs
, false, NULL
);
1091 /* Read a nul-terminated string from FP and store it in STK.
1092 Store the number of bytes read (including nul terminator) in PCOUNT.
1094 Return the last character read or EOF on end of file. */
1096 read_obstack (FILE *fp
, struct obstack
*stk
, size_t *pcount
)
1101 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
1102 obstack_1grow (stk
, c
);
1103 obstack_1grow (stk
, 0);
1109 /* Read from file FP a nul-terminated string and convert it to
1110 intmax_t. Return the resulting value in PVAL. Assume '-' has
1113 Throw a fatal error if the string cannot be converted or if the
1114 converted value is less than MIN_VAL. */
1117 read_negative_num (FILE *fp
, intmax_t min_val
, intmax_t *pval
)
1121 char buf
[INT_BUFSIZE_BOUND (intmax_t)];
1125 for (i
= 1; ISDIGIT (c
= getc (fp
)); i
++)
1127 if (i
== sizeof buf
- 1)
1128 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
1135 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
1137 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1142 *pval
= strtoimax (buf
, &ep
, 10);
1143 if (c
|| errno
|| *pval
< min_val
)
1144 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1147 /* Read from file FP a nul-terminated string and convert it to
1148 uintmax_t. Return the resulting value in PVAL. Assume C has
1151 Throw a fatal error if the string cannot be converted or if the
1152 converted value exceeds MAX_VAL.
1154 Return the last character read or EOF on end of file. */
1157 read_unsigned_num (int c
, FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1160 char buf
[UINTMAX_STRSIZE_BOUND
], *ep
;
1162 for (i
= 0; ISDIGIT (c
); i
++)
1164 if (i
== sizeof buf
- 1)
1165 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
1173 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
1177 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1182 *pval
= strtoumax (buf
, &ep
, 10);
1183 if (c
|| errno
|| max_val
< *pval
)
1184 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1188 /* Read from file FP a nul-terminated string and convert it to
1189 uintmax_t. Return the resulting value in PVAL.
1191 Throw a fatal error if the string cannot be converted or if the
1192 converted value exceeds MAX_VAL.
1194 Return the last character read or EOF on end of file. */
1197 read_num (FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1199 return read_unsigned_num (getc (fp
), fp
, max_val
, pval
);
1202 /* Read from FP two NUL-terminated strings representing a struct
1203 timespec. Return the resulting value in PVAL.
1205 Throw a fatal error if the string cannot be converted. */
1208 read_timespec (FILE *fp
, struct timespec
*pval
)
1216 read_negative_num (fp
, TYPE_MINIMUM (time_t), &i
);
1222 c
= read_unsigned_num (c
, fp
, TYPE_MAXIMUM (time_t), &u
);
1226 if (c
|| read_num (fp
, BILLION
- 1, &u
))
1227 FATAL_ERROR ((0, 0, "%s: %s",
1228 quotearg_colon (listed_incremental_option
),
1229 _("Unexpected EOF in snapshot file")));
1233 /* Read incremental snapshot format 2 */
1240 obstack_init (&stk
);
1242 read_timespec (listed_incremental_stream
, &newer_mtime_option
);
1246 struct timespec mtime
;
1254 if (read_num (listed_incremental_stream
, 1, &u
))
1255 return; /* Normal return */
1259 read_timespec (listed_incremental_stream
, &mtime
);
1261 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (dev_t
), &u
))
1265 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (ino_t
), &u
))
1269 if (read_obstack (listed_incremental_stream
, &stk
, &s
))
1272 name
= obstack_finish (&stk
);
1274 while (read_obstack (listed_incremental_stream
, &stk
, &s
) == 0 && s
> 1)
1276 if (getc (listed_incremental_stream
) != 0)
1277 FATAL_ERROR ((0, 0, "%s: %s",
1278 quotearg_colon (listed_incremental_option
),
1279 _("Missing record terminator")));
1281 content
= obstack_finish (&stk
);
1282 note_directory (name
, mtime
, dev
, ino
, nfs
, false, content
);
1283 obstack_free (&stk
, content
);
1285 FATAL_ERROR ((0, 0, "%s: %s",
1286 quotearg_colon (listed_incremental_option
),
1287 _("Unexpected EOF in snapshot file")));
1290 /* Read incremental snapshot file (directory file).
1291 If the file has older incremental version, make sure that it is processed
1292 correctly and that tar will use the most conservative backup method among
1293 possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
1294 etc.) This ensures that the snapshots are updated to the recent version
1295 without any loss of data. */
1297 read_directory_file (void)
1302 int flags
= O_RDWR
| O_CREAT
;
1304 if (incremental_level
== 0)
1306 /* Open the file for both read and write. That way, we can write
1307 it later without having to reopen it, and don't have to worry if
1308 we chdir in the meantime. */
1309 fd
= open (listed_incremental_option
, flags
, MODE_RW
);
1312 open_error (listed_incremental_option
);
1316 listed_incremental_stream
= fdopen (fd
, "r+");
1317 if (! listed_incremental_stream
)
1319 open_error (listed_incremental_option
);
1324 /* Consume the first name from the name list and reset the
1325 list afterwards. This is done to change to the new
1326 directory, if the first name is a chdir request (-C dir),
1327 which is necessary to recreate absolute file names. */
1331 if (0 < getline (&buf
, &bufsize
, listed_incremental_stream
))
1334 uintmax_t incremental_version
;
1336 if (strncmp (buf
, PACKAGE_NAME
, sizeof PACKAGE_NAME
- 1) == 0)
1338 ebuf
= buf
+ sizeof PACKAGE_NAME
- 1;
1340 ERROR((1, 0, _("Bad incremental file format")));
1341 for (; *ebuf
!= '-'; ebuf
++)
1343 ERROR((1, 0, _("Bad incremental file format")));
1345 incremental_version
= strtoumax (ebuf
+ 1, NULL
, 10);
1348 incremental_version
= 0;
1350 switch (incremental_version
)
1354 read_incr_db_01 (incremental_version
, buf
);
1357 case TAR_INCREMENTAL_VERSION
:
1362 ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX
),
1363 incremental_version
));
1368 if (ferror (listed_incremental_stream
))
1369 read_error (listed_incremental_option
);
1374 /* Output incremental data for the directory ENTRY to the file DATA.
1375 Return nonzero if successful, preserving errno on write failure. */
1377 write_directory_file_entry (void *entry
, void *data
)
1379 struct directory
const *directory
= entry
;
1382 if (DIR_IS_FOUND (directory
))
1384 char buf
[UINTMAX_STRSIZE_BOUND
];
1387 s
= DIR_IS_NFS (directory
) ? "1" : "0";
1388 fwrite (s
, 2, 1, fp
);
1389 s
= (TYPE_SIGNED (time_t)
1390 ? imaxtostr (directory
->mtime
.tv_sec
, buf
)
1391 : umaxtostr (directory
->mtime
.tv_sec
, buf
));
1392 fwrite (s
, strlen (s
) + 1, 1, fp
);
1393 s
= umaxtostr (directory
->mtime
.tv_nsec
, buf
);
1394 fwrite (s
, strlen (s
) + 1, 1, fp
);
1395 s
= umaxtostr (directory
->device_number
, buf
);
1396 fwrite (s
, strlen (s
) + 1, 1, fp
);
1397 s
= umaxtostr (directory
->inode_number
, buf
);
1398 fwrite (s
, strlen (s
) + 1, 1, fp
);
1400 fwrite (directory
->name
, strlen (directory
->name
) + 1, 1, fp
);
1401 if (directory
->dump
)
1406 for (p
= dumpdir_first (directory
->dump
, 0, &itr
);
1408 p
= dumpdir_next (itr
))
1409 fwrite (p
, strlen (p
) + 1, 1, fp
);
1412 fwrite ("\0\0", 2, 1, fp
);
1415 return ! ferror (fp
);
1419 write_directory_file (void)
1421 FILE *fp
= listed_incremental_stream
;
1422 char buf
[UINTMAX_STRSIZE_BOUND
];
1428 if (fseek (fp
, 0L, SEEK_SET
) != 0)
1429 seek_error (listed_incremental_option
);
1430 if (sys_truncate (fileno (fp
)) != 0)
1431 truncate_error (listed_incremental_option
);
1433 fprintf (fp
, "%s-%s-%d\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1434 TAR_INCREMENTAL_VERSION
);
1436 s
= (TYPE_SIGNED (time_t)
1437 ? imaxtostr (start_time
.tv_sec
, buf
)
1438 : umaxtostr (start_time
.tv_sec
, buf
));
1439 fwrite (s
, strlen (s
) + 1, 1, fp
);
1440 s
= umaxtostr (start_time
.tv_nsec
, buf
);
1441 fwrite (s
, strlen (s
) + 1, 1, fp
);
1443 if (! ferror (fp
) && directory_table
)
1444 hash_do_for_each (directory_table
, write_directory_file_entry
, fp
);
1447 write_error (listed_incremental_option
);
1448 if (fclose (fp
) != 0)
1449 close_error (listed_incremental_option
);
1453 /* Restoration of incremental dumps. */
1456 get_gnu_dumpdir (struct tar_stat_info
*stat_info
)
1460 union block
*data_block
;
1464 size
= stat_info
->stat
.st_size
;
1466 archive_dir
= xmalloc (size
);
1469 set_next_block_after (current_header
);
1470 mv_begin (stat_info
);
1472 for (; size
> 0; size
-= copied
)
1474 mv_size_left (size
);
1475 data_block
= find_next_block ();
1477 ERROR ((1, 0, _("Unexpected EOF in archive")));
1478 copied
= available_space_after (data_block
);
1481 memcpy (to
, data_block
->buffer
, copied
);
1483 set_next_block_after ((union block
*)
1484 (data_block
->buffer
+ copied
- 1));
1489 stat_info
->dumpdir
= archive_dir
;
1490 stat_info
->skipped
= true; /* For skip_member() and friends
1491 to work correctly */
1494 /* Return T if STAT_INFO represents a dumpdir archive member.
1495 Note: can invalidate current_header. It happens if flush_archive()
1496 gets called within get_gnu_dumpdir() */
1498 is_dumpdir (struct tar_stat_info
*stat_info
)
1500 if (stat_info
->is_dumpdir
&& !stat_info
->dumpdir
)
1501 get_gnu_dumpdir (stat_info
);
1502 return stat_info
->is_dumpdir
;
1506 dumpdir_ok (char *dumpdir
)
1509 int has_tempdir
= 0;
1512 for (p
= dumpdir
; *p
; p
+= strlen (p
) + 1)
1514 if (expect
&& *p
!= expect
)
1517 _("Malformed dumpdir: expected '%c' but found %#3o"),
1527 _("Malformed dumpdir: 'X' duplicated")));
1540 _("Malformed dumpdir: empty name in 'R'")));
1553 _("Malformed dumpdir: 'T' not preceeded by 'R'")));
1556 if (p
[1] == 0 && !has_tempdir
)
1559 _("Malformed dumpdir: empty name in 'T'")));
1571 /* FIXME: bail out? */
1579 _("Malformed dumpdir: expected '%c' but found end of data"),
1585 WARNOPT (WARN_BAD_DUMPDIR
,
1586 (0, 0, _("Malformed dumpdir: 'X' never used")));
1591 /* Examine the directories under directory_name and delete any
1592 files that were not there at the time of the back-up. */
1594 try_purge_directory (char const *directory_name
)
1597 char *cur
, *arc
, *p
;
1598 char *temp_stub
= NULL
;
1599 struct dumpdir
*dump
;
1601 if (!is_dumpdir (¤t_stat_info
))
1604 current_dir
= savedir (directory_name
);
1607 /* The directory doesn't exist now. It'll be created. In any
1608 case, we don't have to delete any files out of it. */
1611 /* Verify if dump directory is sane */
1612 if (!dumpdir_ok (current_stat_info
.dumpdir
))
1615 /* Process renames */
1616 for (arc
= current_stat_info
.dumpdir
; *arc
; arc
+= strlen (arc
) + 1)
1620 #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
1621 size_t len
= strlen (arc
+ 1);
1622 temp_stub
= xrealloc (temp_stub
, len
+ 1 + sizeof TEMP_DIR_TEMPLATE
);
1623 memcpy (temp_stub
, arc
+ 1, len
);
1624 temp_stub
[len
] = '/';
1625 memcpy (temp_stub
+ len
+ 1, TEMP_DIR_TEMPLATE
,
1626 sizeof TEMP_DIR_TEMPLATE
);
1627 if (!mkdtemp (temp_stub
))
1630 _("Cannot create temporary directory using template %s"),
1631 quote (temp_stub
)));
1637 else if (*arc
== 'R')
1641 arc
+= strlen (arc
) + 1;
1644 /* Ensure that neither source nor destination are absolute file
1645 names (unless permitted by -P option), and that they do not
1646 contain dubious parts (e.g. ../).
1648 This is an extra safety precaution. Besides, it might be
1649 necessary to extract from archives created with tar versions
1653 src
= safer_name_suffix (src
, false, absolute_names_option
);
1655 dst
= safer_name_suffix (dst
, false, absolute_names_option
);
1662 if (!rename_directory (src
, dst
))
1666 /* FIXME: Make sure purge_directory(dst) will return
1675 /* Process deletes */
1676 dump
= dumpdir_create (current_stat_info
.dumpdir
);
1678 for (cur
= current_dir
; *cur
; cur
+= strlen (cur
) + 1)
1684 p
= new_name (directory_name
, cur
);
1686 if (deref_stat (false, p
, &st
))
1688 if (errno
!= ENOENT
) /* FIXME: Maybe keep a list of renamed
1689 dirs and check it here? */
1692 WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
1693 quotearg_colon (p
)));
1698 if (!(entry
= dumpdir_locate (dump
, cur
))
1699 || (*entry
== 'D' && !S_ISDIR (st
.st_mode
))
1700 || (*entry
== 'Y' && S_ISDIR (st
.st_mode
)))
1702 if (one_file_system_option
&& st
.st_dev
!= root_device
)
1705 _("%s: directory is on a different device: not purging"),
1706 quotearg_colon (p
)));
1710 if (! interactive_option
|| confirm ("delete", p
))
1713 fprintf (stdlis
, _("%s: Deleting %s\n"),
1714 program_name
, quote (p
));
1715 if (! remove_any_file (p
, RECURSIVE_REMOVE_OPTION
))
1718 ERROR ((0, e
, _("%s: Cannot remove"), quotearg_colon (p
)));
1724 dumpdir_free (dump
);
1731 purge_directory (char const *directory_name
)
1733 if (!try_purge_directory (directory_name
))
1738 list_dumpdir (char *buffer
, size_t size
)
1751 fprintf (stdlis
, "%c", *buffer
);
1754 fprintf (stdlis
, " ");
1762 fputc ('\n', stdlis
);
1769 fputc (*buffer
, stdlis
);