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