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 timespec mtime
; /* Modification time */
64 dev_t device_number
; /* device number for directory */
65 ino_t inode_number
; /* inode number for directory */
66 struct dumpdir
*dump
; /* Directory contents */
67 struct dumpdir
*idump
; /* Initial contents if the directory was
69 enum children children
; /* What to save under this directory */
70 unsigned flags
; /* See DIRF_ macros above */
71 struct directory
*orig
; /* If the directory was renamed, points to
72 the original directory structure */
73 const char *tagfile
; /* Tag file, if the directory falls under
74 exclusion_tag_under */
75 char name
[1]; /* file name of directory */
79 dumpdir_create0 (const char *contents
, const char *cmask
)
82 size_t i
, total
, ctsize
, len
;
85 for (i
= 0, total
= 0, ctsize
= 1, p
= contents
; *p
; total
++, p
+= len
)
89 if (!cmask
|| strchr (cmask
, *p
))
92 dump
= xmalloc (sizeof (*dump
) + ctsize
);
93 dump
->contents
= (char*)(dump
+ 1);
94 memcpy (dump
->contents
, contents
, ctsize
);
97 dump
->elv
= xcalloc (i
+ 1, sizeof (dump
->elv
[0]));
99 for (i
= 0, p
= dump
->contents
; *p
; p
+= strlen (p
) + 1)
101 if (!cmask
|| strchr (cmask
, *p
))
102 dump
->elv
[i
++] = p
+ 1;
109 dumpdir_create (const char *contents
)
111 return dumpdir_create0 (contents
, "YND");
115 dumpdir_free (struct dumpdir
*dump
)
122 compare_dirnames (const void *first
, const void *second
)
124 char const *const *name1
= first
;
125 char const *const *name2
= second
;
126 return strcmp (*name1
, *name2
);
129 /* Locate NAME in the dumpdir array DUMP.
130 Return pointer to the slot in DUMP->contents, or NULL if not found */
132 dumpdir_locate (struct dumpdir
*dump
, const char *name
)
138 ptr
= bsearch (&name
, dump
->elv
, dump
->elc
, sizeof (dump
->elv
[0]),
140 return ptr
? *ptr
- 1: NULL
;
145 struct dumpdir
*dump
; /* Dumpdir being iterated */
146 int all
; /* Iterate over all entries, not only D/N/Y */
147 size_t next
; /* Index of the next element */
151 dumpdir_next (struct dumpdir_iter
*itr
)
153 size_t cur
= itr
->next
;
158 ret
= itr
->dump
->contents
+ cur
;
161 itr
->next
+= strlen (ret
) + 1;
163 else if (cur
< itr
->dump
->elc
)
165 ret
= itr
->dump
->elv
[cur
] - 1;
173 dumpdir_first (struct dumpdir
*dump
, int all
, struct dumpdir_iter
**pitr
)
175 struct dumpdir_iter
*itr
= xmalloc (sizeof (*itr
));
180 return dumpdir_next (itr
);
183 /* Return size in bytes of the dumpdir array P */
185 dumpdir_size (const char *p
)
191 size_t size
= strlen (p
) + 1;
199 static Hash_table
*directory_table
;
200 static Hash_table
*directory_meta_table
;
202 #if HAVE_ST_FSTYPE_STRING
203 static char const nfs_string
[] = "nfs";
204 # define NFS_FILE_STAT(st) (strcmp ((st).st_fstype, nfs_string) == 0)
206 # define ST_DEV_MSB(st) (~ (dev_t) 0 << (sizeof (st).st_dev * CHAR_BIT - 1))
207 # define NFS_FILE_STAT(st) (((st).st_dev & ST_DEV_MSB (st)) != 0)
210 /* Calculate the hash of a directory. */
212 hash_directory_name (void const *entry
, size_t n_buckets
)
214 struct directory
const *directory
= entry
;
215 return hash_string (directory
->name
, n_buckets
);
218 /* Compare two directories for equality of their names. */
220 compare_directory_names (void const *entry1
, void const *entry2
)
222 struct directory
const *directory1
= entry1
;
223 struct directory
const *directory2
= entry2
;
224 return strcmp (directory1
->name
, directory2
->name
) == 0;
228 hash_directory_meta (void const *entry
, size_t n_buckets
)
230 struct directory
const *directory
= entry
;
231 /* FIXME: Work out a better algorytm */
232 return (directory
->device_number
+ directory
->inode_number
) % n_buckets
;
235 /* Compare two directories for equality of their device and inode numbers. */
237 compare_directory_meta (void const *entry1
, void const *entry2
)
239 struct directory
const *directory1
= entry1
;
240 struct directory
const *directory2
= entry2
;
241 return directory1
->device_number
== directory2
->device_number
242 && directory1
->inode_number
== directory2
->inode_number
;
245 /* Make a directory entry for given NAME */
246 static struct directory
*
247 make_directory (const char *name
)
249 size_t namelen
= strlen (name
);
250 size_t size
= offsetof (struct directory
, name
) + namelen
+ 1;
251 struct directory
*directory
= xmalloc (size
);
252 directory
->dump
= directory
->idump
= NULL
;
253 directory
->orig
= NULL
;
254 directory
->flags
= false;
255 strcpy (directory
->name
, name
);
256 if (namelen
&& ISSLASH (directory
->name
[namelen
- 1]))
257 directory
->name
[namelen
- 1] = 0;
258 directory
->tagfile
= NULL
;
262 /* Create and link a new directory entry for directory NAME, having a
263 device number DEV and an inode number INO, with NFS indicating
264 whether it is an NFS device and FOUND indicating whether we have
265 found that the directory exists. */
266 static struct directory
*
267 note_directory (char const *name
, struct timespec mtime
,
268 dev_t dev
, ino_t ino
, bool nfs
, bool found
,
269 const char *contents
)
271 struct directory
*directory
= make_directory (name
);
273 directory
->mtime
= mtime
;
274 directory
->device_number
= dev
;
275 directory
->inode_number
= ino
;
276 directory
->children
= CHANGED_CHILDREN
;
278 DIR_SET_FLAG (directory
, DIRF_NFS
);
280 DIR_SET_FLAG (directory
, DIRF_FOUND
);
282 directory
->dump
= dumpdir_create (contents
);
284 directory
->dump
= NULL
;
286 if (! ((directory_table
287 || (directory_table
= hash_initialize (0, 0,
289 compare_directory_names
, 0)))
290 && hash_insert (directory_table
, directory
)))
293 if (! ((directory_meta_table
294 || (directory_meta_table
= hash_initialize (0, 0,
296 compare_directory_meta
,
298 && hash_insert (directory_meta_table
, directory
)))
304 /* Return a directory entry for a given file NAME, or zero if none found. */
305 static struct directory
*
306 find_directory (const char *name
)
308 if (! directory_table
)
312 struct directory
*dir
= make_directory (name
);
313 struct directory
*ret
= hash_lookup (directory_table
, dir
);
319 /* Return a directory entry for a given combination of device and inode
320 numbers, or zero if none found. */
321 static struct directory
*
322 find_directory_meta (dev_t dev
, ino_t ino
)
324 if (! directory_meta_table
)
328 struct directory
*dir
= make_directory ("");
329 struct directory
*ret
;
330 dir
->device_number
= dev
;
331 dir
->inode_number
= ino
;
332 ret
= hash_lookup (directory_meta_table
, dir
);
339 update_parent_directory (const char *name
)
341 struct directory
*directory
;
345 directory
= find_directory (p
);
349 if (deref_stat (dereference_option
, p
, &st
) != 0)
352 directory
->mtime
= get_stat_mtime (&st
);
357 static struct directory
*
358 procdir (char *name_buffer
, struct stat
*stat_data
,
360 enum children children
,
364 struct directory
*directory
;
365 bool nfs
= NFS_FILE_STAT (*stat_data
);
367 if ((directory
= find_directory (name_buffer
)) != NULL
)
369 if (DIR_IS_INITED (directory
))
372 /* With NFS, the same file can have two different devices
373 if an NFS directory is mounted in multiple locations,
374 which is relatively common when automounting.
375 To avoid spurious incremental redumping of
376 directories, consider all NFS devices as equal,
377 relying on the i-node to establish differences. */
379 if (! ((!check_device_option
380 || (DIR_IS_NFS (directory
) && nfs
)
381 || directory
->device_number
== stat_data
->st_dev
)
382 && directory
->inode_number
== stat_data
->st_ino
))
384 /* FIXME: find_directory_meta ignores nfs */
385 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
390 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
391 quotearg_colon (name_buffer
),
392 quote_n (1, d
->name
)));
394 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
395 directory
->children
= CHANGED_CHILDREN
;
400 WARN ((0, 0, _("%s: Directory has been renamed"),
401 quotearg_colon (name_buffer
)));
402 directory
->children
= ALL_CHILDREN
;
403 directory
->device_number
= stat_data
->st_dev
;
404 directory
->inode_number
= stat_data
->st_ino
;
407 DIR_SET_FLAG (directory
, DIRF_NFS
);
410 directory
->children
= CHANGED_CHILDREN
;
412 DIR_SET_FLAG (directory
, DIRF_FOUND
);
416 struct directory
*d
= find_directory_meta (stat_data
->st_dev
,
419 directory
= note_directory (name_buffer
,
420 get_stat_mtime(stat_data
),
430 WARN ((0, 0, _("%s: Directory has been renamed from %s"),
431 quotearg_colon (name_buffer
),
432 quote_n (1, d
->name
)));
434 DIR_SET_FLAG (directory
, DIRF_RENAMED
);
435 directory
->children
= CHANGED_CHILDREN
;
439 DIR_SET_FLAG (directory
, DIRF_NEW
);
441 WARN ((0, 0, _("%s: Directory is new"),
442 quotearg_colon (name_buffer
)));
443 directory
->children
=
444 (listed_incremental_option
445 || (OLDER_STAT_TIME (*stat_data
, m
)
446 || (after_date_option
447 && OLDER_STAT_TIME (*stat_data
, c
))))
453 /* If the directory is on another device and --one-file-system was given,
455 if (one_file_system_option
&& device
!= stat_data
->st_dev
456 /* ... except if it was explicitely given in the command line */
457 && !is_individual_file (name_buffer
))
458 directory
->children
= NO_CHILDREN
;
459 else if (children
== ALL_CHILDREN
)
460 directory
->children
= ALL_CHILDREN
;
462 DIR_SET_FLAG (directory
, DIRF_INIT
);
465 const char *tag_file_name
;
467 switch (check_exclusion_tags (name_buffer
, &tag_file_name
))
469 case exclusion_tag_all
:
470 /* This warning can be duplicated by code in dump_file0, but only
471 in case when the topmost directory being archived contains
473 exclusion_tag_warning (name_buffer
, tag_file_name
,
474 _("directory not dumped"));
477 directory
->children
= NO_CHILDREN
;
480 case exclusion_tag_contents
:
481 exclusion_tag_warning (name_buffer
, tag_file_name
,
482 _("contents not dumped"));
483 directory
->children
= NO_CHILDREN
;
486 case exclusion_tag_under
:
487 exclusion_tag_warning (name_buffer
, tag_file_name
,
488 _("contents not dumped"));
489 directory
->tagfile
= tag_file_name
;
492 case exclusion_tag_none
:
500 /* Compare dumpdir array from DIRECTORY with directory listing DIR and
501 build a new dumpdir template.
503 DIR must be returned by a previous call to savedir().
505 File names in DIRECTORY->dump->contents must be sorted
508 DIRECTORY->dump is replaced with the created template. Each entry is
509 prefixed with ' ' if it was present in DUMP and with 'Y' otherwise. */
512 makedumpdir (struct directory
*directory
, const char *dir
)
515 dirsize
, /* Number of elements in DIR */
516 len
; /* Length of DIR, including terminating nul */
519 char *new_dump
, *new_dump_ptr
;
520 struct dumpdir
*dump
;
522 if (directory
->children
== ALL_CHILDREN
)
524 else if (DIR_IS_RENAMED (directory
))
525 dump
= directory
->orig
->idump
?
526 directory
->orig
->idump
: directory
->orig
->dump
;
528 dump
= directory
->dump
;
530 /* Count the size of DIR and the number of elements it contains */
533 for (p
= dir
; *p
; p
+= strlen (p
) + 1, dirsize
++)
534 len
+= strlen (p
) + 2;
537 /* Create a sorted directory listing */
538 array
= xcalloc (dirsize
, sizeof array
[0]);
539 for (i
= 0, p
= dir
; *p
; p
+= strlen (p
) + 1, i
++)
542 qsort (array
, dirsize
, sizeof (array
[0]), compare_dirnames
);
544 /* Prepare space for new dumpdir */
545 new_dump
= xmalloc (len
);
546 new_dump_ptr
= new_dump
;
548 /* Fill in the dumpdir template */
549 for (i
= 0; i
< dirsize
; i
++)
551 const char *loc
= dumpdir_locate (dump
, array
[i
]);
554 if (directory
->tagfile
)
555 *new_dump_ptr
= strcmp (directory
->tagfile
, array
[i
]) == 0 ?
561 else if (directory
->tagfile
)
562 *new_dump_ptr
++ = strcmp (directory
->tagfile
, array
[i
]) == 0 ?
565 *new_dump_ptr
++ = 'Y'; /* New entry */
567 /* Copy the file name */
568 for (p
= array
[i
]; (*new_dump_ptr
++ = *p
++); )
572 directory
->idump
= directory
->dump
;
573 directory
->dump
= dumpdir_create0 (new_dump
, NULL
);
577 /* Recursively scan the given directory. */
579 scan_directory (char *dir
, dev_t device
)
581 char *dirp
= savedir (dir
); /* for scanning directory */
582 char *name_buffer
; /* directory, `/', and directory member */
583 size_t name_buffer_size
; /* allocated size of name_buffer, minus 2 */
584 size_t name_length
; /* used length in name_buffer */
585 struct stat stat_data
;
586 struct directory
*directory
;
591 name_buffer_size
= strlen (dir
) + NAME_FIELD_SIZE
;
592 name_buffer
= xmalloc (name_buffer_size
+ 2);
593 strcpy (name_buffer
, dir
);
594 if (! ISSLASH (dir
[strlen (dir
) - 1]))
595 strcat (name_buffer
, "/");
596 name_length
= strlen (name_buffer
);
598 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
600 stat_diag (name_buffer
);
602 children = CHANGED_CHILDREN;
609 directory
= procdir (name_buffer
, &stat_data
, device
, NO_CHILDREN
, false,
612 if (dirp
&& directory
->children
!= NO_CHILDREN
)
614 char *entry
; /* directory entry being scanned */
615 size_t entrylen
; /* length of directory entry */
618 makedumpdir (directory
, dirp
);
620 for (entry
= dumpdir_first (directory
->dump
, 1, &itr
);
622 entry
= dumpdir_next (itr
))
624 entrylen
= strlen (entry
);
625 if (name_buffer_size
<= entrylen
- 1 + name_length
)
628 name_buffer_size
+= NAME_FIELD_SIZE
;
629 while (name_buffer_size
<= entrylen
- 1 + name_length
);
630 name_buffer
= xrealloc (name_buffer
, name_buffer_size
+ 2);
632 strcpy (name_buffer
+ name_length
, entry
+ 1);
634 if (*entry
== 'I') /* Ignored entry */
636 else if (excluded_name (name_buffer
))
640 if (deref_stat (dereference_option
, name_buffer
, &stat_data
))
642 stat_diag (name_buffer
);
647 if (S_ISDIR (stat_data
.st_mode
))
650 procdir (name_buffer
, &stat_data
, device
,
652 verbose_option
, entry
);
655 else if (one_file_system_option
&& device
!= stat_data
.st_dev
)
658 else if (*entry
== 'Y')
659 /* New entry, skip further checks */;
661 /* FIXME: if (S_ISHIDDEN (stat_data.st_mode))?? */
663 else if (OLDER_STAT_TIME (stat_data
, m
)
664 && (!after_date_option
665 || OLDER_STAT_TIME (stat_data
, c
)))
678 return directory
->dump
? directory
->dump
->contents
: NULL
;
682 get_directory_contents (char *dir
, dev_t device
)
684 return scan_directory (dir
, device
);
689 obstack_code_rename (struct obstack
*stk
, char *from
, char *to
)
693 s
= from
[0] == 0 ? from
:
694 safer_name_suffix (from
, false, absolute_names_option
);
695 obstack_1grow (stk
, 'R');
696 obstack_grow (stk
, s
, strlen (s
) + 1);
699 safer_name_suffix (to
, false, absolute_names_option
);
700 obstack_1grow (stk
, 'T');
701 obstack_grow (stk
, s
, strlen (s
) + 1);
705 rename_handler (void *data
, void *proc_data
)
707 struct directory
*dir
= data
;
708 struct obstack
*stk
= proc_data
;
710 if (DIR_IS_RENAMED (dir
))
712 struct directory
*prev
, *p
;
714 /* Detect eventual cycles and clear DIRF_RENAMED flag, so these entries
715 are ignored when hit by this function next time.
716 If the chain forms a cycle, prev points to the entry DIR is renamed
717 from. In this case it still retains DIRF_RENAMED flag, which will be
718 cleared in the `else' branch below */
719 for (prev
= dir
; prev
&& prev
->orig
!= dir
; prev
= prev
->orig
)
720 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
724 for (p
= dir
; p
&& p
->orig
; p
= p
->orig
)
725 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
731 DIR_CLEAR_FLAG (prev
, DIRF_RENAMED
);
733 /* Break the cycle by using a temporary name for one of its
735 First, create a temp name stub entry. */
736 temp_name
= dir_name (dir
->name
);
737 obstack_1grow (stk
, 'X');
738 obstack_grow (stk
, temp_name
, strlen (temp_name
) + 1);
740 obstack_code_rename (stk
, dir
->name
, "");
742 for (p
= dir
; p
!= prev
; p
= p
->orig
)
743 obstack_code_rename (stk
, p
->orig
->name
, p
->name
);
745 obstack_code_rename (stk
, "", prev
->name
);
752 append_incremental_renames (const char *dump
)
757 if (directory_table
== NULL
)
763 size
= dumpdir_size (dump
) - 1;
764 obstack_grow (&stk
, dump
, size
);
769 hash_do_for_each (directory_table
, rename_handler
, &stk
);
770 if (obstack_object_size (&stk
) != size
)
772 obstack_1grow (&stk
, 0);
773 dump
= obstack_finish (&stk
);
776 obstack_free (&stk
, NULL
);
782 static FILE *listed_incremental_stream
;
784 /* Version of incremental format snapshots (directory files) used by this
785 tar. Currently it is supposed to be a single decimal number. 0 means
786 incremental snapshots as per tar version before 1.15.2.
788 The current tar version supports incremental versions from
789 0 up to TAR_INCREMENTAL_VERSION, inclusive.
790 It is able to create only snapshots of TAR_INCREMENTAL_VERSION */
792 #define TAR_INCREMENTAL_VERSION 2
794 /* Read incremental snapshot formats 0 and 1 */
796 read_incr_db_01 (int version
, const char *initbuf
)
809 if (getline (&buf
, &bufsize
, listed_incremental_stream
) <= 0)
811 read_error (listed_incremental_option
);
819 buf
= strdup (initbuf
);
820 bufsize
= strlen (buf
) + 1;
823 sec
= TYPE_MINIMUM (time_t);
826 u
= strtoumax (buf
, &ebuf
, 10);
827 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
829 if (errno
|| buf
== ebuf
)
830 ERROR ((0, errno
, "%s:%ld: %s",
831 quotearg_colon (listed_incremental_option
),
833 _("Invalid time stamp")));
838 if (version
== 1 && *ebuf
)
840 char const *buf_ns
= ebuf
+ 1;
842 u
= strtoumax (buf_ns
, &ebuf
, 10);
843 if (!errno
&& BILLION
<= u
)
845 if (errno
|| buf_ns
== ebuf
)
847 ERROR ((0, errno
, "%s:%ld: %s",
848 quotearg_colon (listed_incremental_option
),
850 _("Invalid time stamp")));
851 sec
= TYPE_MINIMUM (time_t);
858 /* pre-1 incremental format does not contain nanoseconds */
862 newer_mtime_option
.tv_sec
= sec
;
863 newer_mtime_option
.tv_nsec
= nsec
;
866 while (0 < (n
= getline (&buf
, &bufsize
, listed_incremental_stream
)))
870 bool nfs
= buf
[0] == '+';
871 char *strp
= buf
+ nfs
;
872 struct timespec mtime
;
876 if (buf
[n
- 1] == '\n')
882 u
= strtoumax (strp
, &ebuf
, 10);
883 if (!errno
&& TYPE_MAXIMUM (time_t) < u
)
885 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
887 ERROR ((0, errno
, "%s:%ld: %s",
888 quotearg_colon (listed_incremental_option
), lineno
,
889 _("Invalid modification time (seconds)")));
897 u
= strtoumax (strp
, &ebuf
, 10);
898 if (!errno
&& BILLION
<= u
)
900 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
902 ERROR ((0, errno
, "%s:%ld: %s",
903 quotearg_colon (listed_incremental_option
), lineno
,
904 _("Invalid modification time (nanoseconds)")));
910 mtime
.tv_nsec
= nsec
;
914 memset (&mtime
, 0, sizeof mtime
);
917 u
= strtoumax (strp
, &ebuf
, 10);
918 if (!errno
&& TYPE_MAXIMUM (dev_t
) < u
)
920 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
922 ERROR ((0, errno
, "%s:%ld: %s",
923 quotearg_colon (listed_incremental_option
), lineno
,
924 _("Invalid device number")));
932 u
= strtoumax (strp
, &ebuf
, 10);
933 if (!errno
&& TYPE_MAXIMUM (ino_t
) < u
)
935 if (errno
|| strp
== ebuf
|| *ebuf
!= ' ')
937 ERROR ((0, errno
, "%s:%ld: %s",
938 quotearg_colon (listed_incremental_option
), lineno
,
939 _("Invalid inode number")));
947 unquote_string (strp
);
948 note_directory (strp
, mtime
, dev
, ino
, nfs
, false, NULL
);
953 /* Read a nul-terminated string from FP and store it in STK.
954 Store the number of bytes read (including nul terminator) in PCOUNT.
956 Return the last character read or EOF on end of file. */
958 read_obstack (FILE *fp
, struct obstack
*stk
, size_t *pcount
)
963 for (i
= 0, c
= getc (fp
); c
!= EOF
&& c
!= 0; c
= getc (fp
), i
++)
964 obstack_1grow (stk
, c
);
965 obstack_1grow (stk
, 0);
971 /* Read from file FP a nul-terminated string and convert it to
972 intmax_t. Return the resulting value in PVAL. Assume '-' has
975 Throw a fatal error if the string cannot be converted or if the
976 converted value is less than MIN_VAL. */
979 read_negative_num (FILE *fp
, intmax_t min_val
, intmax_t *pval
)
983 char buf
[INT_BUFSIZE_BOUND (intmax_t)];
987 for (i
= 1; ISDIGIT (c
= getc (fp
)); i
++)
989 if (i
== sizeof buf
- 1)
990 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
997 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
999 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1004 *pval
= strtoimax (buf
, &ep
, 10);
1005 if (c
|| errno
|| *pval
< min_val
)
1006 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1009 /* Read from file FP a nul-terminated string and convert it to
1010 uintmax_t. Return the resulting value in PVAL. Assume C has
1013 Throw a fatal error if the string cannot be converted or if the
1014 converted value exceeds MAX_VAL.
1016 Return the last character read or EOF on end of file. */
1019 read_unsigned_num (int c
, FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1022 char buf
[UINTMAX_STRSIZE_BOUND
], *ep
;
1024 for (i
= 0; ISDIGIT (c
); i
++)
1026 if (i
== sizeof buf
- 1)
1027 FATAL_ERROR ((0, 0, _("Field too long while reading snapshot file")));
1035 FATAL_ERROR ((0, errno
, _("Read error in snapshot file")));
1039 FATAL_ERROR ((0, 0, _("Unexpected EOF in snapshot file")));
1044 *pval
= strtoumax (buf
, &ep
, 10);
1045 if (c
|| errno
|| max_val
< *pval
)
1046 FATAL_ERROR ((0, errno
, _("Unexpected field value in snapshot file")));
1050 /* Read from file FP a nul-terminated string and convert it to
1051 uintmax_t. Return the resulting value in PVAL.
1053 Throw a fatal error if the string cannot be converted or if the
1054 converted value exceeds MAX_VAL.
1056 Return the last character read or EOF on end of file. */
1059 read_num (FILE *fp
, uintmax_t max_val
, uintmax_t *pval
)
1061 return read_unsigned_num (getc (fp
), fp
, max_val
, pval
);
1064 /* Read from FP two NUL-terminated strings representing a struct
1065 timespec. Return the resulting value in PVAL.
1067 Throw a fatal error if the string cannot be converted. */
1070 read_timespec (FILE *fp
, struct timespec
*pval
)
1078 read_negative_num (fp
, TYPE_MINIMUM (time_t), &i
);
1084 c
= read_unsigned_num (c
, fp
, TYPE_MAXIMUM (time_t), &u
);
1088 if (c
|| read_num (fp
, BILLION
- 1, &u
))
1089 FATAL_ERROR ((0, 0, "%s: %s",
1090 quotearg_colon (listed_incremental_option
),
1091 _("Unexpected EOF in snapshot file")));
1095 /* Read incremental snapshot format 2 */
1102 obstack_init (&stk
);
1104 read_timespec (listed_incremental_stream
, &newer_mtime_option
);
1108 struct timespec mtime
;
1116 if (read_num (listed_incremental_stream
, 1, &u
))
1117 return; /* Normal return */
1121 read_timespec (listed_incremental_stream
, &mtime
);
1123 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (dev_t
), &u
))
1127 if (read_num (listed_incremental_stream
, TYPE_MAXIMUM (ino_t
), &u
))
1131 if (read_obstack (listed_incremental_stream
, &stk
, &s
))
1134 name
= obstack_finish (&stk
);
1136 while (read_obstack (listed_incremental_stream
, &stk
, &s
) == 0 && s
> 1)
1138 if (getc (listed_incremental_stream
) != 0)
1139 FATAL_ERROR ((0, 0, "%s: %s",
1140 quotearg_colon (listed_incremental_option
),
1141 _("Missing record terminator")));
1143 content
= obstack_finish (&stk
);
1144 note_directory (name
, mtime
, dev
, ino
, nfs
, false, content
);
1145 obstack_free (&stk
, content
);
1147 FATAL_ERROR ((0, 0, "%s: %s",
1148 quotearg_colon (listed_incremental_option
),
1149 _("Unexpected EOF in snapshot file")));
1152 /* Read incremental snapshot file (directory file).
1153 If the file has older incremental version, make sure that it is processed
1154 correctly and that tar will use the most conservative backup method among
1155 possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN,
1156 etc.) This ensures that the snapshots are updated to the recent version
1157 without any loss of data. */
1159 read_directory_file (void)
1165 /* Open the file for both read and write. That way, we can write
1166 it later without having to reopen it, and don't have to worry if
1167 we chdir in the meantime. */
1168 fd
= open (listed_incremental_option
, O_RDWR
| O_CREAT
, MODE_RW
);
1171 open_error (listed_incremental_option
);
1175 listed_incremental_stream
= fdopen (fd
, "r+");
1176 if (! listed_incremental_stream
)
1178 open_error (listed_incremental_option
);
1183 if (0 < getline (&buf
, &bufsize
, listed_incremental_stream
))
1186 uintmax_t incremental_version
;
1188 if (strncmp (buf
, PACKAGE_NAME
, sizeof PACKAGE_NAME
- 1) == 0)
1190 ebuf
= buf
+ sizeof PACKAGE_NAME
- 1;
1192 ERROR((1, 0, _("Bad incremental file format")));
1193 for (; *ebuf
!= '-'; ebuf
++)
1195 ERROR((1, 0, _("Bad incremental file format")));
1197 incremental_version
= strtoumax (ebuf
+ 1, NULL
, 10);
1200 incremental_version
= 0;
1202 switch (incremental_version
)
1206 read_incr_db_01 (incremental_version
, buf
);
1209 case TAR_INCREMENTAL_VERSION
:
1214 ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX
),
1215 incremental_version
));
1220 if (ferror (listed_incremental_stream
))
1221 read_error (listed_incremental_option
);
1226 /* Output incremental data for the directory ENTRY to the file DATA.
1227 Return nonzero if successful, preserving errno on write failure. */
1229 write_directory_file_entry (void *entry
, void *data
)
1231 struct directory
const *directory
= entry
;
1234 if (DIR_IS_FOUND (directory
))
1236 char buf
[UINTMAX_STRSIZE_BOUND
];
1239 s
= DIR_IS_NFS (directory
) ? "1" : "0";
1240 fwrite (s
, 2, 1, fp
);
1241 s
= (TYPE_SIGNED (time_t)
1242 ? imaxtostr (directory
->mtime
.tv_sec
, buf
)
1243 : umaxtostr (directory
->mtime
.tv_sec
, buf
));
1244 fwrite (s
, strlen (s
) + 1, 1, fp
);
1245 s
= umaxtostr (directory
->mtime
.tv_nsec
, buf
);
1246 fwrite (s
, strlen (s
) + 1, 1, fp
);
1247 s
= umaxtostr (directory
->device_number
, buf
);
1248 fwrite (s
, strlen (s
) + 1, 1, fp
);
1249 s
= umaxtostr (directory
->inode_number
, buf
);
1250 fwrite (s
, strlen (s
) + 1, 1, fp
);
1252 fwrite (directory
->name
, strlen (directory
->name
) + 1, 1, fp
);
1253 if (directory
->dump
)
1258 for (p
= dumpdir_first (directory
->dump
, 0, &itr
);
1260 p
= dumpdir_next (itr
))
1261 fwrite (p
, strlen (p
) + 1, 1, fp
);
1264 fwrite ("\0\0", 2, 1, fp
);
1267 return ! ferror (fp
);
1271 write_directory_file (void)
1273 FILE *fp
= listed_incremental_stream
;
1274 char buf
[UINTMAX_STRSIZE_BOUND
];
1280 if (fseek (fp
, 0L, SEEK_SET
) != 0)
1281 seek_error (listed_incremental_option
);
1282 if (sys_truncate (fileno (fp
)) != 0)
1283 truncate_error (listed_incremental_option
);
1285 fprintf (fp
, "%s-%s-%d\n", PACKAGE_NAME
, PACKAGE_VERSION
,
1286 TAR_INCREMENTAL_VERSION
);
1288 s
= (TYPE_SIGNED (time_t)
1289 ? imaxtostr (start_time
.tv_sec
, buf
)
1290 : umaxtostr (start_time
.tv_sec
, buf
));
1291 fwrite (s
, strlen (s
) + 1, 1, fp
);
1292 s
= umaxtostr (start_time
.tv_nsec
, buf
);
1293 fwrite (s
, strlen (s
) + 1, 1, fp
);
1295 if (! ferror (fp
) && directory_table
)
1296 hash_do_for_each (directory_table
, write_directory_file_entry
, fp
);
1299 write_error (listed_incremental_option
);
1300 if (fclose (fp
) != 0)
1301 close_error (listed_incremental_option
);
1305 /* Restoration of incremental dumps. */
1308 get_gnu_dumpdir (struct tar_stat_info
*stat_info
)
1312 union block
*data_block
;
1316 size
= stat_info
->stat
.st_size
;
1318 archive_dir
= xmalloc (size
);
1321 set_next_block_after (current_header
);
1322 mv_begin (stat_info
);
1324 for (; size
> 0; size
-= copied
)
1326 mv_size_left (size
);
1327 data_block
= find_next_block ();
1329 ERROR ((1, 0, _("Unexpected EOF in archive")));
1330 copied
= available_space_after (data_block
);
1333 memcpy (to
, data_block
->buffer
, copied
);
1335 set_next_block_after ((union block
*)
1336 (data_block
->buffer
+ copied
- 1));
1341 stat_info
->dumpdir
= archive_dir
;
1342 stat_info
->skipped
= true; /* For skip_member() and friends
1343 to work correctly */
1346 /* Return T if STAT_INFO represents a dumpdir archive member.
1347 Note: can invalidate current_header. It happens if flush_archive()
1348 gets called within get_gnu_dumpdir() */
1350 is_dumpdir (struct tar_stat_info
*stat_info
)
1352 if (stat_info
->is_dumpdir
&& !stat_info
->dumpdir
)
1353 get_gnu_dumpdir (stat_info
);
1354 return stat_info
->is_dumpdir
;
1358 dumpdir_ok (char *dumpdir
)
1361 int has_tempdir
= 0;
1364 for (p
= dumpdir
; *p
; p
+= strlen (p
) + 1)
1366 if (expect
&& *p
!= expect
)
1369 _("Malformed dumpdir: expected '%c' but found %#3o"),
1379 _("Malformed dumpdir: 'X' duplicated")));
1392 _("Malformed dumpdir: empty name in 'R'")));
1405 _("Malformed dumpdir: 'T' not preceeded by 'R'")));
1408 if (p
[1] == 0 && !has_tempdir
)
1411 _("Malformed dumpdir: empty name in 'T'")));
1423 /* FIXME: bail out? */
1431 _("Malformed dumpdir: expected '%c' but found end of data"),
1437 WARN ((0, 0, _("Malformed dumpdir: 'X' never used")));
1442 /* Examine the directories under directory_name and delete any
1443 files that were not there at the time of the back-up. */
1445 try_purge_directory (char const *directory_name
)
1448 char *cur
, *arc
, *p
;
1449 char *temp_stub
= NULL
;
1450 struct dumpdir
*dump
;
1452 if (!is_dumpdir (¤t_stat_info
))
1455 current_dir
= savedir (directory_name
);
1458 /* The directory doesn't exist now. It'll be created. In any
1459 case, we don't have to delete any files out of it. */
1462 /* Verify if dump directory is sane */
1463 if (!dumpdir_ok (current_stat_info
.dumpdir
))
1466 /* Process renames */
1467 for (arc
= current_stat_info
.dumpdir
; *arc
; arc
+= strlen (arc
) + 1)
1471 #define TEMP_DIR_TEMPLATE "tar.XXXXXX"
1472 size_t len
= strlen (arc
+ 1);
1473 temp_stub
= xrealloc (temp_stub
, len
+ 1 + sizeof TEMP_DIR_TEMPLATE
);
1474 memcpy (temp_stub
, arc
+ 1, len
);
1475 temp_stub
[len
] = '/';
1476 memcpy (temp_stub
+ len
+ 1, TEMP_DIR_TEMPLATE
,
1477 sizeof TEMP_DIR_TEMPLATE
);
1478 if (!mkdtemp (temp_stub
))
1481 _("Cannot create temporary directory using template %s"),
1482 quote (temp_stub
)));
1488 else if (*arc
== 'R')
1492 arc
+= strlen (arc
) + 1;
1495 /* Ensure that neither source nor destination are absolute file
1496 names (unless permitted by -P option), and that they do not
1497 contain dubious parts (e.g. ../).
1499 This is an extra safety precaution. Besides, it might be
1500 necessary to extract from archives created with tar versions
1504 src
= safer_name_suffix (src
, false, absolute_names_option
);
1506 dst
= safer_name_suffix (dst
, false, absolute_names_option
);
1513 if (!rename_directory (src
, dst
))
1517 /* FIXME: Make sure purge_directory(dst) will return
1526 /* Process deletes */
1527 dump
= dumpdir_create (current_stat_info
.dumpdir
);
1529 for (cur
= current_dir
; *cur
; cur
+= strlen (cur
) + 1)
1535 p
= new_name (directory_name
, cur
);
1537 if (deref_stat (false, p
, &st
))
1539 if (errno
!= ENOENT
) /* FIXME: Maybe keep a list of renamed
1540 dirs and check it here? */
1543 WARN ((0, 0, _("%s: Not purging directory: unable to stat"),
1544 quotearg_colon (p
)));
1549 if (!(entry
= dumpdir_locate (dump
, cur
))
1550 || (*entry
== 'D' && !S_ISDIR (st
.st_mode
))
1551 || (*entry
== 'Y' && S_ISDIR (st
.st_mode
)))
1553 if (one_file_system_option
&& st
.st_dev
!= root_device
)
1556 _("%s: directory is on a different device: not purging"),
1557 quotearg_colon (p
)));
1561 if (! interactive_option
|| confirm ("delete", p
))
1564 fprintf (stdlis
, _("%s: Deleting %s\n"),
1565 program_name
, quote (p
));
1566 if (! remove_any_file (p
, RECURSIVE_REMOVE_OPTION
))
1569 ERROR ((0, e
, _("%s: Cannot remove"), quotearg_colon (p
)));
1575 dumpdir_free (dump
);
1582 purge_directory (char const *directory_name
)
1584 if (!try_purge_directory (directory_name
))
1589 list_dumpdir (char *buffer
, size_t size
)
1602 fprintf (stdlis
, "%c", *buffer
);
1605 fprintf (stdlis
, " ");
1613 fputc ('\n', stdlis
);
1620 fputc (*buffer
, stdlis
);