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