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