Fix a bug when '-f -H' is used and the target already exists. cpdup was
[dragonfly.git] / contrib / tar / src / common.h
1 /* Common declarations for the tar program.
2
3    Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001 Free
4    Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any later
9    version.
10
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.
15
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    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* $FreeBSD: src/contrib/tar/src/common.h,v 1.2.2.1 2002/07/14 13:19:44 sobomax Exp $ */
21 /* $DragonFly: src/contrib/tar/src/Attic/common.h,v 1.2 2003/06/17 04:24:06 dillon Exp $ */
22
23 /* Declare the GNU tar archive format.  */
24 #include "tar.h"
25
26 /* The checksum field is filled with this while the checksum is computed.  */
27 #define CHKBLANKS       "        "      /* 8 blanks, no null */
28
29 /* Some constants from POSIX are given names.  */
30 #define NAME_FIELD_SIZE   100
31 #define PREFIX_FIELD_SIZE 155
32 #define UNAME_FIELD_SIZE   32
33 #define GNAME_FIELD_SIZE   32
34 \f
35 /* Some various global definitions.  */
36
37 /* Name of file to use for interacting with user.  */
38 #if MSDOS
39 # define TTY_NAME "con"
40 #else
41 # define TTY_NAME "/dev/tty"
42 #endif
43
44 /* GLOBAL is defined to empty in tar.c only, and left alone in other *.c
45    modules.  Here, we merely set it to "extern" if it is not already set.
46    GNU tar does depend on the system loader to preset all GLOBAL variables to
47    neutral (or zero) values, explicit initialization is usually not done.  */
48 #ifndef GLOBAL
49 # define GLOBAL extern
50 #endif
51
52 /* Exit status for GNU tar.  Let's try to keep this list as simple as
53    possible.  -d option strongly invites a status different for unequal
54    comparison and other errors.  */
55 GLOBAL int exit_status;
56
57 #define TAREXIT_SUCCESS 0
58 #define TAREXIT_DIFFERS 1
59 #define TAREXIT_FAILURE 2
60
61 /* Both WARN and ERROR write a message on stderr and continue processing,
62    however ERROR manages so tar will exit unsuccessfully.  FATAL_ERROR
63    writes a message on stderr and aborts immediately, with another message
64    line telling so.  USAGE_ERROR works like FATAL_ERROR except that the
65    other message line suggests trying --help.  All four macros accept a
66    single argument of the form ((0, errno, _("FORMAT"), Args...)).  errno
67    is zero when the error is not being detected by the system.  */
68
69 #define WARN(Args) \
70   error Args
71 #define ERROR(Args) \
72   (error Args, exit_status = TAREXIT_FAILURE)
73 #define FATAL_ERROR(Args) \
74   (error Args, fatal_exit ())
75 #define USAGE_ERROR(Args) \
76   (error Args, usage (TAREXIT_FAILURE))
77 \f
78 /* Information gleaned from the command line.  */
79
80 #include "arith.h"
81 #include <backupfile.h>
82 #include <exclude.h>
83 #include <modechange.h>
84 #include <safe-read.h>
85 #include <full-write.h>
86
87 /* Log base 2 of common values.  */
88 #define LG_8 3
89 #define LG_64 6
90 #define LG_256 8
91
92 /* Name of this program.  */
93 GLOBAL const char *program_name;
94
95 /* Main command option.  */
96
97 enum subcommand
98 {
99   UNKNOWN_SUBCOMMAND,           /* none of the following */
100   APPEND_SUBCOMMAND,            /* -r */
101   CAT_SUBCOMMAND,               /* -A */
102   CREATE_SUBCOMMAND,            /* -c */
103   DELETE_SUBCOMMAND,            /* -D */
104   DIFF_SUBCOMMAND,              /* -d */
105   EXTRACT_SUBCOMMAND,           /* -x */
106   LIST_SUBCOMMAND,              /* -t */
107   UPDATE_SUBCOMMAND             /* -u */
108 };
109
110 GLOBAL enum subcommand subcommand_option;
111
112 /* Selected format for output archive.  */
113 GLOBAL enum archive_format archive_format;
114
115 /* Either NL or NUL, as decided by the --null option.  */
116 GLOBAL char filename_terminator;
117
118 /* Size of each record, once in blocks, once in bytes.  Those two variables
119    are always related, the second being BLOCKSIZE times the first.  They do
120    not have _option in their name, even if their values is derived from
121    option decoding, as these are especially important in tar.  */
122 GLOBAL int blocking_factor;
123 GLOBAL size_t record_size;
124
125 /* Boolean value.  */
126 GLOBAL int absolute_names_option;
127
128 /* This variable tells how to interpret newer_mtime_option, below.  If zero,
129    files get archived if their mtime is not less than newer_mtime_option.
130    If nonzero, files get archived if *either* their ctime or mtime is not less
131    than newer_mtime_option.  */
132 GLOBAL int after_date_option;
133
134 /* Boolean value.  */
135 GLOBAL int atime_preserve_option;
136
137 /* Boolean value.  */
138 GLOBAL int backup_option;
139
140 /* Type of backups being made.  */
141 GLOBAL enum backup_type backup_type;
142
143 /* Boolean value.  */
144 GLOBAL int block_number_option;
145
146 /* Boolean value.  */
147 GLOBAL int checkpoint_option;
148
149 /* Specified name of compression program, or "gzip" as implied by -z.  */
150 GLOBAL const char *use_compress_program_option;
151
152 /* Boolean value.  */
153 GLOBAL int dereference_option;
154
155 /* Patterns that match file names to be excluded.  */
156 GLOBAL struct exclude *excluded;
157
158 /* Boolean value.  */
159 GLOBAL int fast_read_option;
160
161 /* Specified file containing names to work on.  */
162 GLOBAL const char *files_from_option;
163
164 /* Boolean value.  */
165 GLOBAL int force_local_option;
166
167 /* Specified value to be put into tar file in place of stat () results, or
168    just -1 if such an override should not take place.  */
169 GLOBAL gid_t group_option;
170
171 /* Boolean value.  */
172 GLOBAL int ignore_failed_read_option;
173
174 /* Boolean value.  */
175 GLOBAL int ignore_zeros_option;
176
177 /* Boolean value.  */
178 GLOBAL int incremental_option;
179
180 /* Specified name of script to run at end of each tape change.  */
181 GLOBAL const char *info_script_option;
182
183 /* Boolean value.  */
184 GLOBAL int interactive_option;
185
186 enum old_files
187 {
188   DEFAULT_OLD_FILES, /* default */
189   UNLINK_FIRST_OLD_FILES, /* --unlink-first */
190   KEEP_OLD_FILES, /* --keep-old-files */
191   OVERWRITE_OLD_DIRS, /* --overwrite-dir */
192   OVERWRITE_OLD_FILES /* --overwrite */
193 };
194 GLOBAL enum old_files old_files_option;
195
196 /* Specified file name for incremental list.  */
197 GLOBAL const char *listed_incremental_option;
198
199 /* Specified mode change string.  */
200 GLOBAL struct mode_change *mode_option;
201
202 /* Boolean value.  */
203 GLOBAL int multi_volume_option;
204
205 /* Boolean value.  */
206 GLOBAL int namelist_freed;
207
208 /* The same variable hold the time, whether mtime or ctime.  Just fake a
209    non-existing option, for making the code clearer, elsewhere.  */
210 #define newer_ctime_option newer_mtime_option
211
212 /* Specified threshold date and time.  Files having an older time stamp
213    do not get archived (also see after_date_option above).  */
214 GLOBAL time_t newer_mtime_option;
215
216 /* Zero if there is no recursion, otherwise FNM_LEADING_DIR.  */
217 GLOBAL int recursion_option;
218
219 /* Boolean value.  */
220 GLOBAL int numeric_owner_option;
221
222 /* Boolean value.  */
223 GLOBAL int one_file_system_option;
224
225 /* Specified value to be put into tar file in place of stat () results, or
226    just -1 if such an override should not take place.  */
227 GLOBAL uid_t owner_option;
228
229 /* Boolean value.  */
230 GLOBAL int recursive_unlink_option;
231
232 /* Boolean value.  */
233 GLOBAL int read_full_records_option;
234
235 /* Boolean value.  */
236 GLOBAL int remove_files_option;
237
238 /* Specified remote shell command.  */
239 GLOBAL const char *rsh_command_option;
240
241 /* Boolean value.  */
242 GLOBAL int same_order_option;
243
244 /* If positive, preserve ownership when extracting.  */
245 GLOBAL int same_owner_option;
246
247 /* If positive, preserve permissions when extracting.  */
248 GLOBAL int same_permissions_option;
249
250 /* Boolean value.  */
251 GLOBAL int show_omitted_dirs_option;
252
253 /* Boolean value.  */
254 GLOBAL int sparse_option;
255
256 /* Boolean value.  */
257 GLOBAL int starting_file_option;
258
259 /* Specified maximum byte length of each tape volume (multiple of 1024).  */
260 GLOBAL tarlong tape_length_option;
261
262 /* Boolean value.  */
263 GLOBAL int to_stdout_option;
264
265 /* Boolean value.  */
266 GLOBAL int totals_option;
267
268 /* Boolean value.  */
269 GLOBAL int touch_option;
270  
271 /* Count how many times the option has been set, multiple setting yields
272    more verbose behavior.  Value 0 means no verbosity, 1 means file name
273    only, 2 means file name and all attributes.  More than 2 is just like 2.  */
274 GLOBAL int verbose_option;
275
276 /* Boolean value.  */
277 GLOBAL int verify_option;
278
279 /* Specified name of file containing the volume number.  */
280 GLOBAL const char *volno_file_option;
281
282 /* Specified value or pattern.  */
283 GLOBAL const char *volume_label_option;
284 \f
285 /* Other global variables.  */
286
287 /* File descriptor for archive file.  */
288 GLOBAL int archive;
289
290 /* Nonzero when outputting to /dev/null.  */
291 GLOBAL int dev_null_output;
292
293 /* Timestamp for when we started execution.  */
294 #if HAVE_CLOCK_GETTIME
295   GLOBAL struct timespec start_timespec;
296 # define start_time (start_timespec.tv_sec)
297 #else
298   GLOBAL time_t start_time;
299 #endif
300
301 /* Name of file for the current archive entry.  */
302 GLOBAL char *current_file_name;
303
304 /* Name of link for the current archive entry.  */
305 GLOBAL char *current_link_name;
306
307 /* List of tape drive names, number of such tape drives, allocated number,
308    and current cursor in list.  */
309 GLOBAL const char **archive_name_array;
310 GLOBAL int archive_names;
311 GLOBAL int allocated_archive_names;
312 GLOBAL const char **archive_name_cursor;
313
314 /* Structure for keeping track of filenames and lists thereof.  */
315 struct name
316   {
317     struct name *next;
318     size_t length;              /* cached strlen(name) */
319     char found;                 /* a matching file has been found */
320     char firstch;               /* first char is literally matched */
321     char regexp;                /* this name is a regexp, not literal */
322     int change_dir;             /* set with the -C option */
323     char const *dir_contents;   /* for incremental_option */
324     char fake;                  /* dummy entry */
325     char name[1];
326   };
327
328 /* Information about a sparse file.  */
329 struct sp_array
330   {
331     off_t offset;
332     size_t numbytes;
333   };
334 GLOBAL struct sp_array *sparsearray;
335
336 /* Number of elements in sparsearray.  */
337 GLOBAL int sp_array_size;
338 \f
339 /* Declarations for each module.  */
340
341 /* FIXME: compare.c should not directly handle the following variable,
342    instead, this should be done in buffer.c only.  */
343
344 enum access_mode
345 {
346   ACCESS_READ,
347   ACCESS_WRITE,
348   ACCESS_UPDATE
349 };
350 extern enum access_mode access_mode;
351
352 /* Module buffer.c.  */
353
354 extern FILE *stdlis;
355 extern char *save_name;
356 extern off_t save_sizeleft;
357 extern off_t save_totsize;
358 extern bool write_archive_to_stdout;
359
360 size_t available_space_after PARAMS ((union block *));
361 off_t current_block_ordinal PARAMS ((void));
362 void close_archive PARAMS ((void));
363 void closeout_volume_number PARAMS ((void));
364 union block *find_next_block PARAMS ((void));
365 void flush_read PARAMS ((void));
366 void flush_write PARAMS ((void));
367 void flush_archive PARAMS ((void));
368 void init_volume_number PARAMS ((void));
369 void open_archive PARAMS ((enum access_mode));
370 void print_total_written PARAMS ((void));
371 void reset_eof PARAMS ((void));
372 void set_next_block_after PARAMS ((union block *));
373
374 /* Module create.c.  */
375
376 void create_archive PARAMS ((void));
377 void dump_file PARAMS ((char *, int, dev_t));
378 void finish_header PARAMS ((union block *));
379 void write_eot PARAMS ((void));
380
381 #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
382 #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
383 #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
384 #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
385 #define OFF_TO_CHARS(val, where) off_to_chars (val, where, sizeof (where))
386 #define SIZE_TO_CHARS(val, where) size_to_chars (val, where, sizeof (where))
387 #define TIME_TO_CHARS(val, where) time_to_chars (val, where, sizeof (where))
388 #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
389 #define UINTMAX_TO_CHARS(val, where) uintmax_to_chars (val, where, sizeof (where))
390
391 void gid_to_chars PARAMS ((gid_t, char *, size_t));
392 void major_to_chars PARAMS ((major_t, char *, size_t));
393 void minor_to_chars PARAMS ((minor_t, char *, size_t));
394 void mode_to_chars PARAMS ((mode_t, char *, size_t));
395 void off_to_chars PARAMS ((off_t, char *, size_t));
396 void size_to_chars PARAMS ((size_t, char *, size_t));
397 void time_to_chars PARAMS ((time_t, char *, size_t));
398 void uid_to_chars PARAMS ((uid_t, char *, size_t));
399 void uintmax_to_chars PARAMS ((uintmax_t, char *, size_t));
400
401 /* Module diffarch.c.  */
402
403 extern int now_verifying;
404
405 void diff_archive PARAMS ((void));
406 void diff_init PARAMS ((void));
407 void verify_volume PARAMS ((void));
408
409 /* Module extract.c.  */
410
411 extern int we_are_root;
412 void extr_init PARAMS ((void));
413 void extract_archive PARAMS ((void));
414 void extract_finish PARAMS ((void));
415 void fatal_exit PARAMS ((void)) __attribute__ ((noreturn));
416
417 /* Module delete.c.  */
418
419 void delete_archive_members PARAMS ((void));
420
421 /* Module incremen.c.  */
422
423 char *get_directory_contents PARAMS ((char *, dev_t));
424 void read_directory_file PARAMS ((void));
425 void write_directory_file PARAMS ((void));
426 void gnu_restore PARAMS ((size_t));
427
428 /* Module list.c.  */
429
430 enum read_header
431 {
432   HEADER_STILL_UNREAD,          /* for when read_header has not been called */
433   HEADER_SUCCESS,               /* header successfully read and checksummed */
434   HEADER_SUCCESS_EXTENDED,      /* likewise, but we got an extended header */
435   HEADER_ZERO_BLOCK,            /* zero block where header expected */
436   HEADER_END_OF_FILE,           /* true end of file while header expected */
437   HEADER_FAILURE                /* ill-formed header, or bad checksum */
438 };
439
440 extern union block *current_header;
441 extern struct stat current_stat;
442 extern enum archive_format current_format;
443
444 void decode_header PARAMS ((union block *, struct stat *,
445                             enum archive_format *, int));
446 #define STRINGIFY_BIGINT(i, b) \
447   stringify_uintmax_t_backwards ((uintmax_t) (i), (b) + UINTMAX_STRSIZE_BOUND)
448 char *stringify_uintmax_t_backwards PARAMS ((uintmax_t, char *));
449 char const *tartime PARAMS ((time_t));
450
451 #define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where))
452 #define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where))
453 #define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where))
454 #define MODE_FROM_HEADER(where) mode_from_header (where, sizeof (where))
455 #define OFF_FROM_HEADER(where) off_from_header (where, sizeof (where))
456 #define SIZE_FROM_HEADER(where) size_from_header (where, sizeof (where))
457 #define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where))
458 #define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where))
459 #define UINTMAX_FROM_HEADER(where) uintmax_from_header (where, sizeof (where))
460
461 gid_t gid_from_header PARAMS ((const char *, size_t));
462 major_t major_from_header PARAMS ((const char *, size_t));
463 minor_t minor_from_header PARAMS ((const char *, size_t));
464 mode_t mode_from_header PARAMS ((const char *, size_t));
465 off_t off_from_header PARAMS ((const char *, size_t));
466 size_t size_from_header PARAMS ((const char *, size_t));
467 time_t time_from_header PARAMS ((const char *, size_t));
468 uid_t uid_from_header PARAMS ((const char *, size_t));
469 uintmax_t uintmax_from_header PARAMS ((const char *, size_t));
470
471 void list_archive PARAMS ((void));
472 void print_for_mkdir PARAMS ((char *, int, mode_t));
473 void print_header PARAMS ((void));
474 void read_and PARAMS ((void (*do_) ()));
475 enum read_header read_header PARAMS ((bool));
476 void skip_file PARAMS ((off_t));
477 void skip_member PARAMS ((void));
478
479 /* Module mangle.c.  */
480
481 void extract_mangle PARAMS ((void));
482
483 /* Module misc.c.  */
484
485 void assign_string PARAMS ((char **, const char *));
486 char *quote_copy_string PARAMS ((const char *));
487 int unquote_string PARAMS ((char *));
488
489 int contains_dot_dot PARAMS ((char const *));
490
491 int remove_any_file PARAMS ((const char *, int));
492 int maybe_backup_file PARAMS ((const char *, int));
493 void undo_last_backup PARAMS ((void));
494
495 int deref_stat PARAMS ((int, char const *, struct stat *));
496
497 int chdir_arg PARAMS ((char const *));
498 void chdir_do PARAMS ((int));
499
500 void decode_mode PARAMS ((mode_t, char *));
501
502 void chdir_fatal PARAMS ((char const *)) __attribute__ ((noreturn));
503 void chmod_error_details PARAMS ((char const *, mode_t));
504 void chown_error_details PARAMS ((char const *, uid_t, gid_t));
505 void close_error PARAMS ((char const *));
506 void close_warn PARAMS ((char const *));
507 void exec_fatal PARAMS ((char const *)) __attribute__ ((noreturn));
508 void link_error PARAMS ((char const *, char const *));
509 void mkdir_error PARAMS ((char const *));
510 void mkfifo_error PARAMS ((char const *));
511 void mknod_error PARAMS ((char const *));
512 void open_error PARAMS ((char const *));
513 void open_fatal PARAMS ((char const *)) __attribute__ ((noreturn));
514 void open_warn PARAMS ((char const *));
515 void read_error PARAMS ((char const *));
516 void read_error_details PARAMS ((char const *, off_t, size_t));
517 void read_fatal PARAMS ((char const *)) __attribute__ ((noreturn));
518 void read_fatal_details PARAMS ((char const *, off_t, size_t));
519 void read_warn_details PARAMS ((char const *, off_t, size_t));
520 void readlink_error PARAMS ((char const *));
521 void readlink_warn PARAMS ((char const *));
522 void savedir_error PARAMS ((char const *));
523 void savedir_warn PARAMS ((char const *));
524 void seek_error PARAMS ((char const *));
525 void seek_error_details PARAMS ((char const *, off_t));
526 void seek_warn PARAMS ((char const *));
527 void seek_warn_details PARAMS ((char const *, off_t));
528 void stat_error PARAMS ((char const *));
529 void stat_warn PARAMS ((char const *));
530 void symlink_error PARAMS ((char const *, char const *));
531 void truncate_error PARAMS ((char const *));
532 void truncate_warn PARAMS ((char const *));
533 void unlink_error PARAMS ((char const *));
534 void utime_error PARAMS ((char const *));
535 void waitpid_error PARAMS ((char const *));
536 void write_error PARAMS ((char const *));
537 void write_error_details PARAMS ((char const *, ssize_t, size_t));
538 void write_fatal PARAMS ((char const *)) __attribute__ ((noreturn));
539 void write_fatal_details PARAMS ((char const *, ssize_t, size_t))
540      __attribute__ ((noreturn));
541
542 pid_t xfork PARAMS ((void));
543 void xpipe PARAMS ((int[2]));
544
545 char const *quote PARAMS ((char const *));
546 char const *quote_n PARAMS ((int, char const *));
547
548 /* Module names.c.  */
549
550 extern struct name *gnu_list_name;
551
552 void gid_to_gname PARAMS ((gid_t, char gname[GNAME_FIELD_SIZE]));
553 int gname_to_gid PARAMS ((char gname[GNAME_FIELD_SIZE], gid_t *));
554 void uid_to_uname PARAMS ((uid_t, char uname[UNAME_FIELD_SIZE]));
555 int uname_to_uid PARAMS ((char uname[UNAME_FIELD_SIZE], uid_t *));
556
557 void init_names PARAMS ((void));
558 void name_add PARAMS ((const char *));
559 void name_init PARAMS ((int, char *const *));
560 void name_term PARAMS ((void));
561 char *name_next PARAMS ((int));
562 void name_close PARAMS ((void));
563 void name_gather PARAMS ((void));
564 struct name *addname PARAMS ((char const *, int));
565 int name_match PARAMS ((const char *));
566 void names_notfound PARAMS ((void));
567 void collect_and_sort_names PARAMS ((void));
568 struct name *name_scan PARAMS ((const char *));
569 char *name_from_list PARAMS ((void));
570 void blank_name_list PARAMS ((void));
571 char *new_name PARAMS ((const char *, const char *));
572
573 bool excluded_name PARAMS ((char const *));
574
575 void add_avoided_name PARAMS ((char const *));
576 int is_avoided_name PARAMS ((char const *));
577
578 /* Module tar.c.  */
579
580 int confirm PARAMS ((const char *, const char *));
581 void request_stdin PARAMS ((const char *));
582
583 /* Module update.c.  */
584
585 extern char *output_start;
586
587 void update_archive PARAMS ((void));