common.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /* Common declarations for the tar program.
  2. Copyright 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Declare the GNU tar archive format. */
  15. #include "tar.h"
  16. /* The checksum field is filled with this while the checksum is computed. */
  17. #define CHKBLANKS " " /* 8 blanks, no null */
  18. /* Some constants from POSIX are given names. */
  19. #define NAME_FIELD_SIZE 100
  20. #define PREFIX_FIELD_SIZE 155
  21. #define UNAME_FIELD_SIZE 32
  22. #define GNAME_FIELD_SIZE 32
  23. /* Some various global definitions. */
  24. /* Name of file to use for interacting with user. */
  25. /* GLOBAL is defined to empty in tar.c only, and left alone in other *.c
  26. modules. Here, we merely set it to "extern" if it is not already set.
  27. GNU tar does depend on the system loader to preset all GLOBAL variables to
  28. neutral (or zero) values, explicit initialization is usually not done. */
  29. #ifndef GLOBAL
  30. # define GLOBAL extern
  31. #endif
  32. #define TAREXIT_SUCCESS PAXEXIT_SUCCESS
  33. #define TAREXIT_DIFFERS PAXEXIT_DIFFERS
  34. #define TAREXIT_FAILURE PAXEXIT_FAILURE
  35. #include "arith.h"
  36. #include <attribute.h>
  37. #include <backupfile.h>
  38. #include <exclude.h>
  39. #include <full-write.h>
  40. #include <modechange.h>
  41. #include <quote.h>
  42. #include <safe-read.h>
  43. #include <stat-time.h>
  44. #include <timespec.h>
  45. #define obstack_chunk_alloc xmalloc
  46. #define obstack_chunk_free free
  47. #include <obstack.h>
  48. #include <progname.h>
  49. #include <xvasprintf.h>
  50. #include <paxlib.h>
  51. /* Log base 2 of common values. */
  52. #define LG_8 3
  53. #define LG_64 6
  54. #define LG_256 8
  55. _GL_INLINE_HEADER_BEGIN
  56. #ifndef COMMON_INLINE
  57. # define COMMON_INLINE _GL_INLINE
  58. #endif
  59. /* Information gleaned from the command line. */
  60. /* Main command option. */
  61. enum subcommand
  62. {
  63. UNKNOWN_SUBCOMMAND, /* none of the following */
  64. APPEND_SUBCOMMAND, /* -r */
  65. CAT_SUBCOMMAND, /* -A */
  66. CREATE_SUBCOMMAND, /* -c */
  67. DELETE_SUBCOMMAND, /* -D */
  68. DIFF_SUBCOMMAND, /* -d */
  69. EXTRACT_SUBCOMMAND, /* -x */
  70. LIST_SUBCOMMAND, /* -t */
  71. UPDATE_SUBCOMMAND, /* -u */
  72. TEST_LABEL_SUBCOMMAND, /* --test-label */
  73. };
  74. GLOBAL enum subcommand subcommand_option;
  75. /* Selected format for output archive. */
  76. GLOBAL enum archive_format archive_format;
  77. /* Size of each record, once in blocks, once in bytes. Those two variables
  78. are always related, the second being BLOCKSIZE times the first. They do
  79. not have _option in their name, even if their values is derived from
  80. option decoding, as these are especially important in tar. */
  81. GLOBAL int blocking_factor;
  82. GLOBAL size_t record_size;
  83. GLOBAL bool absolute_names_option;
  84. /* Display file times in UTC */
  85. GLOBAL bool utc_option;
  86. /* Output file timestamps to the full resolution */
  87. GLOBAL bool full_time_option;
  88. /* This variable tells how to interpret newer_mtime_option, below. If zero,
  89. files get archived if their mtime is not less than newer_mtime_option.
  90. If nonzero, files get archived if *either* their ctime or mtime is not less
  91. than newer_mtime_option. */
  92. GLOBAL int after_date_option;
  93. enum atime_preserve
  94. {
  95. no_atime_preserve,
  96. replace_atime_preserve,
  97. system_atime_preserve
  98. };
  99. GLOBAL enum atime_preserve atime_preserve_option;
  100. GLOBAL bool backup_option;
  101. /* Type of backups being made. */
  102. GLOBAL enum backup_type backup_type;
  103. GLOBAL bool block_number_option;
  104. GLOBAL unsigned checkpoint_option;
  105. #define DEFAULT_CHECKPOINT 10
  106. /* Specified name of compression program, or "gzip" as implied by -z. */
  107. GLOBAL const char *use_compress_program_option;
  108. GLOBAL bool dereference_option;
  109. GLOBAL bool hard_dereference_option;
  110. /* Patterns that match file names to be excluded. */
  111. GLOBAL struct exclude *excluded;
  112. enum exclusion_tag_type
  113. {
  114. exclusion_tag_none,
  115. /* Exclude the directory contents, but preserve the directory
  116. itself and the exclusion tag file */
  117. exclusion_tag_contents,
  118. /* Exclude everything below the directory, preserving the directory
  119. itself */
  120. exclusion_tag_under,
  121. /* Exclude entire directory */
  122. exclusion_tag_all,
  123. };
  124. /* Specified value to be put into tar file in place of stat () results, or
  125. just null and -1 if such an override should not take place. */
  126. GLOBAL char const *group_name_option;
  127. GLOBAL gid_t group_option;
  128. GLOBAL bool ignore_failed_read_option;
  129. GLOBAL bool ignore_zeros_option;
  130. GLOBAL bool incremental_option;
  131. /* Specified name of script to run at end of each tape change. */
  132. GLOBAL const char *info_script_option;
  133. GLOBAL bool interactive_option;
  134. /* If nonzero, extract only Nth occurrence of each named file */
  135. GLOBAL uintmax_t occurrence_option;
  136. enum old_files
  137. {
  138. DEFAULT_OLD_FILES, /* default */
  139. NO_OVERWRITE_DIR_OLD_FILES, /* --no-overwrite-dir */
  140. OVERWRITE_OLD_FILES, /* --overwrite */
  141. UNLINK_FIRST_OLD_FILES, /* --unlink-first */
  142. KEEP_OLD_FILES, /* --keep-old-files */
  143. SKIP_OLD_FILES, /* --skip-old-files */
  144. KEEP_NEWER_FILES /* --keep-newer-files */
  145. };
  146. GLOBAL enum old_files old_files_option;
  147. GLOBAL bool keep_directory_symlink_option;
  148. /* Specified file name for incremental list. */
  149. GLOBAL const char *listed_incremental_option;
  150. /* Incremental dump level */
  151. GLOBAL int incremental_level;
  152. /* Check device numbers when doing incremental dumps. */
  153. GLOBAL bool check_device_option;
  154. /* Specified mode change string. */
  155. GLOBAL struct mode_change *mode_option;
  156. /* Initial umask, if needed for mode change string. */
  157. GLOBAL mode_t initial_umask;
  158. GLOBAL bool multi_volume_option;
  159. /* Specified threshold date and time. Files having an older time stamp
  160. do not get archived (also see after_date_option above). */
  161. GLOBAL struct timespec newer_mtime_option;
  162. enum set_mtime_option_mode
  163. {
  164. USE_FILE_MTIME,
  165. FORCE_MTIME,
  166. CLAMP_MTIME,
  167. };
  168. /* Override actual mtime if set to FORCE_MTIME or CLAMP_MTIME */
  169. GLOBAL enum set_mtime_option_mode set_mtime_option;
  170. /* Value to use when forcing or clamping the mtime header field. */
  171. GLOBAL struct timespec mtime_option;
  172. /* Return true if mtime_option or newer_mtime_option is initialized. */
  173. #define TIME_OPTION_INITIALIZED(opt) (0 <= (opt).tv_nsec)
  174. /* Return true if the struct stat ST's M time is less than
  175. newer_mtime_option. */
  176. #define OLDER_STAT_TIME(st, m) \
  177. (timespec_cmp (get_stat_##m##time (&(st)), newer_mtime_option) < 0)
  178. /* Likewise, for struct tar_stat_info ST. */
  179. #define OLDER_TAR_STAT_TIME(st, m) \
  180. (timespec_cmp ((st).m##time, newer_mtime_option) < 0)
  181. /* Zero if there is no recursion, otherwise FNM_LEADING_DIR. */
  182. GLOBAL int recursion_option;
  183. GLOBAL bool numeric_owner_option;
  184. GLOBAL bool one_file_system_option;
  185. /* Create a top-level directory for extracting based on the archive name. */
  186. GLOBAL bool one_top_level_option;
  187. GLOBAL char *one_top_level_dir;
  188. /* Specified value to be put into tar file in place of stat () results, or
  189. just null and -1 if such an override should not take place. */
  190. GLOBAL char const *owner_name_option;
  191. GLOBAL uid_t owner_option;
  192. GLOBAL bool recursive_unlink_option;
  193. GLOBAL bool read_full_records_option;
  194. GLOBAL bool remove_files_option;
  195. /* Specified remote shell command. */
  196. GLOBAL const char *rsh_command_option;
  197. GLOBAL bool same_order_option;
  198. /* If positive, preserve ownership when extracting. */
  199. GLOBAL int same_owner_option;
  200. /* If positive, preserve permissions when extracting. */
  201. GLOBAL int same_permissions_option;
  202. /* If positive, save the SELinux context. */
  203. GLOBAL int selinux_context_option;
  204. /* If positive, save the ACLs. */
  205. GLOBAL int acls_option;
  206. /* If positive, save the user and root xattrs. */
  207. GLOBAL int xattrs_option;
  208. /* When set, strip the given number of file name components from the file name
  209. before extracting */
  210. GLOBAL size_t strip_name_components;
  211. GLOBAL bool show_omitted_dirs_option;
  212. GLOBAL bool sparse_option;
  213. GLOBAL unsigned tar_sparse_major;
  214. GLOBAL unsigned tar_sparse_minor;
  215. enum hole_detection_method
  216. {
  217. HOLE_DETECTION_DEFAULT,
  218. HOLE_DETECTION_RAW,
  219. HOLE_DETECTION_SEEK
  220. };
  221. GLOBAL enum hole_detection_method hole_detection;
  222. /* The first entry in names.c:namelist specifies the member name to
  223. start extracting from. Set by add_starting_file() upon seeing the
  224. -K option.
  225. */
  226. GLOBAL bool starting_file_option;
  227. /* Specified maximum byte length of each tape volume (multiple of 1024). */
  228. GLOBAL tarlong tape_length_option;
  229. GLOBAL bool to_stdout_option;
  230. GLOBAL bool totals_option;
  231. GLOBAL bool touch_option;
  232. GLOBAL char *to_command_option;
  233. GLOBAL bool ignore_command_error_option;
  234. /* Restrict some potentially harmful tar options */
  235. GLOBAL bool restrict_option;
  236. /* Return true if the extracted files are not being written to disk */
  237. #define EXTRACT_OVER_PIPE (to_stdout_option || to_command_option)
  238. /* Count how many times the option has been set, multiple setting yields
  239. more verbose behavior. Value 0 means no verbosity, 1 means file name
  240. only, 2 means file name and all attributes. More than 2 is just like 2. */
  241. GLOBAL int verbose_option;
  242. GLOBAL bool verify_option;
  243. /* Specified name of file containing the volume number. */
  244. GLOBAL const char *volno_file_option;
  245. /* Specified value or pattern. */
  246. GLOBAL const char *volume_label_option;
  247. /* Other global variables. */
  248. /* Force POSIX-compliance */
  249. GLOBAL bool posixly_correct;
  250. /* File descriptor for archive file. */
  251. GLOBAL int archive;
  252. /* Nonzero when outputting to /dev/null. */
  253. GLOBAL bool dev_null_output;
  254. /* Timestamps: */
  255. GLOBAL struct timespec start_time; /* when we started execution */
  256. GLOBAL struct timespec volume_start_time; /* when the current volume was
  257. opened*/
  258. GLOBAL struct timespec last_stat_time; /* when the statistics was last
  259. computed */
  260. GLOBAL struct tar_stat_info current_stat_info;
  261. /* List of tape drive names, number of such tape drives,
  262. and current cursor in list. */
  263. GLOBAL const char **archive_name_array;
  264. GLOBAL size_t archive_names;
  265. GLOBAL const char **archive_name_cursor;
  266. /* Output index file name. */
  267. GLOBAL char const *index_file_name;
  268. /* Opaque structure for keeping directory meta-data */
  269. struct directory;
  270. /* Structure for keeping track of filenames and lists thereof. */
  271. struct name
  272. {
  273. struct name *next; /* Link to the next element */
  274. struct name *prev; /* Link to the previous element */
  275. char *name; /* File name or globbing pattern */
  276. size_t length; /* cached strlen (name) */
  277. int matching_flags; /* wildcard flags if name is a pattern */
  278. bool cmdline; /* true if this name was given in the
  279. command line */
  280. int change_dir; /* Number of the directory to change to.
  281. Set with the -C option. */
  282. uintmax_t found_count; /* number of times a matching file has
  283. been found */
  284. /* The following members are used for incremental dumps only,
  285. if this struct name represents a directory;
  286. see incremen.c */
  287. struct directory *directory;/* directory meta-data and contents */
  288. struct name *parent; /* pointer to the parent hierarchy */
  289. struct name *child; /* pointer to the first child */
  290. struct name *sibling; /* pointer to the next sibling */
  291. char *caname; /* canonical name */
  292. };
  293. /* Obnoxious test to see if dimwit is trying to dump the archive. */
  294. GLOBAL dev_t ar_dev;
  295. GLOBAL ino_t ar_ino;
  296. /* Flags for reading, searching, and fstatatting files. */
  297. GLOBAL int open_read_flags;
  298. GLOBAL int open_searchdir_flags;
  299. GLOBAL int fstatat_flags;
  300. GLOBAL int seek_option;
  301. GLOBAL bool seekable_archive;
  302. GLOBAL dev_t root_device;
  303. /* Unquote filenames */
  304. GLOBAL bool unquote_option;
  305. GLOBAL int savedir_sort_order;
  306. /* Show file or archive names after transformation.
  307. In particular, when creating archive in verbose mode, list member names
  308. as stored in the archive */
  309. GLOBAL bool show_transformed_names_option;
  310. /* Delay setting modification times and permissions of extracted directories
  311. until the end of extraction. This variable helps correctly restore directory
  312. timestamps from archives with an unusual member order. It is automatically
  313. set for incremental archives. */
  314. GLOBAL bool delay_directory_restore_option;
  315. /* Declarations for each module. */
  316. /* FIXME: compare.c should not directly handle the following variable,
  317. instead, this should be done in buffer.c only. */
  318. enum access_mode
  319. {
  320. ACCESS_READ,
  321. ACCESS_WRITE,
  322. ACCESS_UPDATE
  323. };
  324. extern enum access_mode access_mode;
  325. /* Module buffer.c. */
  326. extern FILE *stdlis;
  327. extern bool write_archive_to_stdout;
  328. extern char *volume_label;
  329. extern size_t volume_label_count;
  330. extern char *continued_file_name;
  331. extern uintmax_t continued_file_size;
  332. extern uintmax_t continued_file_offset;
  333. extern off_t records_written;
  334. char *drop_volume_label_suffix (const char *label);
  335. size_t available_space_after (union block *pointer);
  336. off_t current_block_ordinal (void);
  337. void close_archive (void);
  338. void closeout_volume_number (void);
  339. double compute_duration (void);
  340. union block *find_next_block (void);
  341. void flush_read (void);
  342. void flush_write (void);
  343. void flush_archive (void);
  344. void init_volume_number (void);
  345. void open_archive (enum access_mode mode);
  346. void print_total_stats (void);
  347. void reset_eof (void);
  348. void set_next_block_after (union block *block);
  349. void clear_read_error_count (void);
  350. void xclose (int fd);
  351. _Noreturn void archive_write_error (ssize_t status);
  352. void archive_read_error (void);
  353. off_t seek_archive (off_t size);
  354. void set_start_time (void);
  355. #define TF_READ 0
  356. #define TF_WRITE 1
  357. #define TF_DELETED 2
  358. int format_total_stats (FILE *fp, char const *const *formats, int eor, int eol);
  359. void print_total_stats (void);
  360. void mv_begin_write (const char *file_name, off_t totsize, off_t sizeleft);
  361. void mv_begin_read (struct tar_stat_info *st);
  362. void mv_end (void);
  363. void mv_size_left (off_t size);
  364. void buffer_write_global_xheader (void);
  365. const char *first_decompress_program (int *pstate);
  366. const char *next_decompress_program (int *pstate);
  367. /* Module create.c. */
  368. enum dump_status
  369. {
  370. dump_status_ok,
  371. dump_status_short,
  372. dump_status_fail,
  373. dump_status_not_implemented
  374. };
  375. void add_exclusion_tag (const char *name, enum exclusion_tag_type type,
  376. bool (*predicate) (int));
  377. bool cachedir_file_p (int fd);
  378. char *get_directory_entries (struct tar_stat_info *st)
  379. _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
  380. void create_archive (void);
  381. void pad_archive (off_t size_left);
  382. void dump_file (struct tar_stat_info *parent, char const *name,
  383. char const *fullname);
  384. union block *start_header (struct tar_stat_info *st);
  385. void finish_header (struct tar_stat_info *st, union block *header,
  386. off_t block_ordinal);
  387. void simple_finish_header (union block *header);
  388. union block * write_extended (bool global, struct tar_stat_info *st,
  389. union block *old_header);
  390. union block *start_private_header (const char *name, size_t size, time_t t);
  391. void write_eot (void);
  392. void check_links (void);
  393. int subfile_open (struct tar_stat_info const *dir, char const *file, int flags);
  394. void restore_parent_fd (struct tar_stat_info const *st);
  395. void exclusion_tag_warning (const char *dirname, const char *tagname,
  396. const char *message);
  397. enum exclusion_tag_type check_exclusion_tags (struct tar_stat_info const *st,
  398. const char **tag_file_name);
  399. #define OFF_TO_CHARS(val, where) off_to_chars (val, where, sizeof (where))
  400. #define TIME_TO_CHARS(val, where) time_to_chars (val, where, sizeof (where))
  401. bool off_to_chars (off_t off, char *buf, size_t size);
  402. bool time_to_chars (time_t t, char *buf, size_t size);
  403. /* Module diffarch.c. */
  404. extern bool now_verifying;
  405. void diff_archive (void);
  406. void diff_init (void);
  407. void verify_volume (void);
  408. /* Module extract.c. */
  409. void extr_init (void);
  410. void extract_archive (void);
  411. void extract_finish (void);
  412. bool rename_directory (char *src, char *dst);
  413. void remove_delayed_set_stat (const char *fname);
  414. /* Module delete.c. */
  415. void delete_archive_members (void);
  416. /* Module incremen.c. */
  417. struct directory *scan_directory (struct tar_stat_info *st);
  418. const char *directory_contents (struct directory *dir);
  419. const char *safe_directory_contents (struct directory *dir);
  420. void rebase_directory (struct directory *dir,
  421. const char *samp, size_t slen,
  422. const char *repl, size_t rlen);
  423. void append_incremental_renames (struct directory *dir);
  424. void show_snapshot_field_ranges (void);
  425. void read_directory_file (void);
  426. void write_directory_file (void);
  427. void purge_directory (char const *directory_name);
  428. void list_dumpdir (char *buffer, size_t size);
  429. void update_parent_directory (struct tar_stat_info *st);
  430. size_t dumpdir_size (const char *p);
  431. bool is_dumpdir (struct tar_stat_info *stat_info);
  432. void clear_directory_table (void);
  433. /* Module list.c. */
  434. enum read_header
  435. {
  436. HEADER_STILL_UNREAD, /* for when read_header has not been called */
  437. HEADER_SUCCESS, /* header successfully read and checksummed */
  438. HEADER_SUCCESS_EXTENDED, /* likewise, but we got an extended header */
  439. HEADER_ZERO_BLOCK, /* zero block where header expected */
  440. HEADER_END_OF_FILE, /* true end of file while header expected */
  441. HEADER_FAILURE /* ill-formed header, or bad checksum */
  442. };
  443. /* Operation mode for read_header: */
  444. enum read_header_mode
  445. {
  446. read_header_auto, /* process extended headers automatically */
  447. read_header_x_raw, /* return raw extended headers (return
  448. HEADER_SUCCESS_EXTENDED) */
  449. read_header_x_global /* when POSIX global extended header is read,
  450. decode it and return
  451. HEADER_SUCCESS_EXTENDED */
  452. };
  453. extern union block *current_header;
  454. extern enum archive_format current_format;
  455. extern size_t recent_long_name_blocks;
  456. extern size_t recent_long_link_blocks;
  457. void decode_header (union block *header, struct tar_stat_info *stat_info,
  458. enum archive_format *format_pointer, int do_user_group);
  459. void transform_stat_info (int typeflag, struct tar_stat_info *stat_info);
  460. char const *tartime (struct timespec t, bool full_time);
  461. #define OFF_FROM_HEADER(where) off_from_header (where, sizeof (where))
  462. #define UINTMAX_FROM_HEADER(where) uintmax_from_header (where, sizeof (where))
  463. off_t off_from_header (const char *buf, size_t size);
  464. uintmax_t uintmax_from_header (const char *buf, size_t size);
  465. void list_archive (void);
  466. void test_archive_label (void);
  467. void print_for_mkdir (char *dirname, int length, mode_t mode);
  468. void print_header (struct tar_stat_info *st, union block *blk,
  469. off_t block_ordinal);
  470. void read_and (void (*do_something) (void));
  471. enum read_header read_header (union block **return_block,
  472. struct tar_stat_info *info,
  473. enum read_header_mode m);
  474. enum read_header tar_checksum (union block *header, bool silent);
  475. void skip_file (off_t size);
  476. void skip_member (void);
  477. /* Module misc.c. */
  478. #define min(a, b) ((a) < (b) ? (a) : (b))
  479. #define max(a, b) ((a) < (b) ? (b) : (a))
  480. char const *quote_n_colon (int n, char const *arg);
  481. void assign_string_or_null (char **dest, const char *src)
  482. ATTRIBUTE_NONNULL ((1));
  483. void assign_string (char **dest, const char *src) ATTRIBUTE_NONNULL ((1, 2));
  484. void assign_null (char **dest) ATTRIBUTE_NONNULL ((1));
  485. void assign_string_n (char **string, const char *value, size_t n);
  486. #define ASSIGN_STRING_N(s,v) assign_string_n (s, v, sizeof (v))
  487. int unquote_string (char *str);
  488. char *zap_slashes (char *name);
  489. char *normalize_filename (int cdidx, const char *name);
  490. void normalize_filename_x (char *name);
  491. void replace_prefix (char **pname, const char *samp, size_t slen,
  492. const char *repl, size_t rlen);
  493. char *tar_savedir (const char *name, int must_exist);
  494. typedef struct namebuf *namebuf_t;
  495. namebuf_t namebuf_create (const char *dir);
  496. void namebuf_free (namebuf_t buf);
  497. char *namebuf_name (namebuf_t buf, const char *name);
  498. const char *tar_dirname (void);
  499. /* Represent N using a signed integer I such that (uintmax_t) I == N.
  500. With a good optimizing compiler, this is equivalent to (intmax_t) i
  501. and requires zero machine instructions. */
  502. #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
  503. # error "represent_uintmax returns intmax_t to represent uintmax_t"
  504. #endif
  505. COMMON_INLINE intmax_t
  506. represent_uintmax (uintmax_t n)
  507. {
  508. if (n <= INTMAX_MAX)
  509. return n;
  510. else
  511. {
  512. /* Avoid signed integer overflow on picky platforms. */
  513. intmax_t nd = n - INTMAX_MIN;
  514. return nd + INTMAX_MIN;
  515. }
  516. }
  517. enum { SYSINT_BUFSIZE =
  518. max (UINTMAX_STRSIZE_BOUND, INT_BUFSIZE_BOUND (intmax_t)) };
  519. char *sysinttostr (uintmax_t, intmax_t, uintmax_t, char buf[SYSINT_BUFSIZE]);
  520. intmax_t strtosysint (char const *, char **, intmax_t, uintmax_t);
  521. void code_ns_fraction (int ns, char *p);
  522. enum { BILLION = 1000000000, LOG10_BILLION = 9 };
  523. enum { TIMESPEC_STRSIZE_BOUND =
  524. UINTMAX_STRSIZE_BOUND + LOG10_BILLION + sizeof "-." - 1 };
  525. char const *code_timespec (struct timespec ts,
  526. char sbuf[TIMESPEC_STRSIZE_BOUND]);
  527. struct timespec decode_timespec (char const *, char **, bool);
  528. /* Return true if T does not represent an out-of-range or invalid value. */
  529. COMMON_INLINE bool
  530. valid_timespec (struct timespec t)
  531. {
  532. return 0 <= t.tv_nsec;
  533. }
  534. bool must_be_dot_or_slash (char const *);
  535. enum remove_option
  536. {
  537. ORDINARY_REMOVE_OPTION,
  538. RECURSIVE_REMOVE_OPTION,
  539. /* FIXME: The following value is never used. It seems to be intended
  540. as a placeholder for a hypothetical option that should instruct tar
  541. to recursively remove subdirectories in purge_directory(),
  542. as opposed to the functionality of --recursive-unlink
  543. (RECURSIVE_REMOVE_OPTION value), which removes them in
  544. prepare_to_extract() phase. However, with the addition of more
  545. meta-info to the incremental dumps, this should become unnecessary */
  546. WANT_DIRECTORY_REMOVE_OPTION
  547. };
  548. int remove_any_file (const char *file_name, enum remove_option option);
  549. bool maybe_backup_file (const char *file_name, bool this_is_the_archive);
  550. void undo_last_backup (void);
  551. int deref_stat (char const *name, struct stat *buf);
  552. size_t blocking_read (int fd, void *buf, size_t count);
  553. size_t blocking_write (int fd, void const *buf, size_t count);
  554. extern int chdir_current;
  555. extern int chdir_fd;
  556. int chdir_arg (char const *dir);
  557. void chdir_do (int dir);
  558. int chdir_count (void);
  559. void close_diag (char const *name);
  560. void open_diag (char const *name);
  561. void read_diag_details (char const *name, off_t offset, size_t size);
  562. void readlink_diag (char const *name);
  563. void savedir_diag (char const *name);
  564. void seek_diag_details (char const *name, off_t offset);
  565. void stat_diag (char const *name);
  566. void file_removed_diag (const char *name, bool top_level,
  567. void (*diagfn) (char const *name));
  568. void write_error_details (char const *name, size_t status, size_t size);
  569. _Noreturn void write_fatal (char const *name);
  570. _Noreturn void write_fatal_details (char const *name, ssize_t status, size_t size);
  571. pid_t xfork (void);
  572. void xpipe (int fd[2]);
  573. void *page_aligned_alloc (void **ptr, size_t size);
  574. int set_file_atime (int fd, int parentfd, char const *file,
  575. struct timespec atime);
  576. /* Module names.c. */
  577. enum files_count
  578. {
  579. FILES_NONE,
  580. FILES_ONE,
  581. FILES_MANY
  582. };
  583. extern enum files_count filename_args;
  584. /* Return true if there are file names in the list */
  585. COMMON_INLINE bool
  586. name_more_files (void)
  587. {
  588. return filename_args != FILES_NONE;
  589. }
  590. extern struct name *gnu_list_name;
  591. void gid_to_gname (gid_t gid, char **gname);
  592. int gname_to_gid (char const *gname, gid_t *pgid);
  593. void uid_to_uname (uid_t uid, char **uname);
  594. int uname_to_uid (char const *uname, uid_t *puid);
  595. void name_init (void);
  596. void name_add_name (const char *name);
  597. void name_term (void);
  598. const char *name_next (int change_dirs);
  599. void name_gather (void);
  600. struct name *addname (char const *string, int change_dir,
  601. bool cmdline, struct name *parent);
  602. void add_starting_file (char const *file_name);
  603. void remname (struct name *name);
  604. bool name_match (const char *name);
  605. void names_notfound (void);
  606. void label_notfound (void);
  607. void collect_and_sort_names (void);
  608. struct name *name_scan (const char *name);
  609. struct name const *name_from_list (void);
  610. void blank_name_list (void);
  611. char *make_file_name (const char *dir_name, const char *name);
  612. size_t stripped_prefix_len (char const *file_name, size_t num);
  613. bool all_names_found (struct tar_stat_info *st);
  614. void add_avoided_name (char const *name);
  615. bool is_avoided_name (char const *name);
  616. bool contains_dot_dot (char const *name);
  617. #define ISFOUND(c) (occurrence_option == 0 \
  618. ? (c)->found_count != 0 \
  619. : (c)->found_count == occurrence_option)
  620. #define WASFOUND(c) (occurrence_option == 0 \
  621. ? (c)->found_count != 0 \
  622. : (c)->found_count >= occurrence_option)
  623. /* Module tar.c. */
  624. _Noreturn void usage (int);
  625. int confirm (const char *message_action, const char *name);
  626. void tar_stat_init (struct tar_stat_info *st);
  627. bool tar_stat_close (struct tar_stat_info *st);
  628. void tar_stat_destroy (struct tar_stat_info *st);
  629. _Noreturn void usage (int);
  630. int tar_timespec_cmp (struct timespec a, struct timespec b);
  631. const char *archive_format_string (enum archive_format fmt);
  632. const char *subcommand_string (enum subcommand c);
  633. void set_exit_status (int val);
  634. void request_stdin (const char *option);
  635. int decode_signal (const char *);
  636. /* Where an option comes from: */
  637. enum option_source
  638. {
  639. OPTS_ENVIRON, /* Environment variable TAR_OPTIONS */
  640. OPTS_COMMAND_LINE, /* Command line */
  641. OPTS_FILE /* File supplied by --files-from */
  642. };
  643. /* Option location */
  644. struct option_locus
  645. {
  646. enum option_source source; /* Option origin */
  647. char const *name; /* File or variable name */
  648. size_t line; /* Number of input line if source is OPTS_FILE */
  649. struct option_locus *prev; /* Previous occurrence of the option of same
  650. class */
  651. };
  652. struct tar_args /* Variables used during option parsing */
  653. {
  654. struct option_locus *loc;
  655. struct textual_date *textual_date; /* Keeps the arguments to --newer-mtime
  656. and/or --date option if they are
  657. textual dates */
  658. bool o_option; /* True if -o option was given */
  659. bool pax_option; /* True if --pax-option was given */
  660. bool compress_autodetect; /* True if compression autodetection should
  661. be attempted when creating archives */
  662. char const *backup_suffix_string; /* --suffix option argument */
  663. char const *version_control_string; /* --backup option argument */
  664. };
  665. #define TAR_ARGS_INITIALIZER(loc) \
  666. { loc, NULL, false, false, false, NULL, NULL }
  667. void more_options (int argc, char **argv, struct option_locus *loc);
  668. /* Module update.c. */
  669. extern char *output_start;
  670. void update_archive (void);
  671. /* Module attrs.c. */
  672. #include "xattrs.h"
  673. /* Module xheader.c. */
  674. void xheader_decode (struct tar_stat_info *stat);
  675. void xheader_decode_global (struct xheader *xhdr);
  676. void xheader_store (char const *keyword, struct tar_stat_info *st,
  677. void const *data);
  678. void xheader_read (struct xheader *xhdr, union block *header, off_t size);
  679. void xheader_write (char type, char *name, time_t t, struct xheader *xhdr);
  680. void xheader_write_global (struct xheader *xhdr);
  681. void xheader_forbid_global (void);
  682. void xheader_finish (struct xheader *hdr);
  683. void xheader_destroy (struct xheader *hdr);
  684. char *xheader_xhdr_name (struct tar_stat_info *st);
  685. char *xheader_ghdr_name (void);
  686. void xheader_set_option (char *string);
  687. void xheader_string_begin (struct xheader *xhdr);
  688. void xheader_string_add (struct xheader *xhdr, char const *s);
  689. bool xheader_string_end (struct xheader *xhdr, char const *keyword);
  690. bool xheader_keyword_deleted_p (const char *kw);
  691. char *xheader_format_name (struct tar_stat_info *st, const char *fmt,
  692. size_t n);
  693. void xheader_xattr_init (struct tar_stat_info *st);
  694. void xheader_xattr_add (struct tar_stat_info *st,
  695. const char *key, const char *val, size_t len);
  696. void xattr_map_init (struct xattr_map *map);
  697. void xattr_map_copy (struct xattr_map *dst,
  698. const struct xattr_map *src);
  699. void xattr_map_add (struct xattr_map *map,
  700. const char *key, const char *val, size_t len);
  701. void xattr_map_free (struct xattr_map *xattr_map);
  702. /* Module system.c */
  703. void sys_detect_dev_null_output (void);
  704. void sys_save_archive_dev_ino (void);
  705. void sys_wait_for_child (pid_t, bool);
  706. void sys_spawn_shell (void);
  707. bool sys_compare_uid (struct stat *a, struct stat *b);
  708. bool sys_compare_gid (struct stat *a, struct stat *b);
  709. bool sys_file_is_archive (struct tar_stat_info *p);
  710. bool sys_compare_links (struct stat *link_data, struct stat *stat_data);
  711. int sys_truncate (int fd);
  712. pid_t sys_child_open_for_compress (void);
  713. pid_t sys_child_open_for_uncompress (void);
  714. size_t sys_write_archive_buffer (void);
  715. bool sys_get_archive_stat (void);
  716. int sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st);
  717. void sys_wait_command (void);
  718. int sys_exec_info_script (const char **archive_name, int volume_number);
  719. void sys_exec_checkpoint_script (const char *script_name,
  720. const char *archive_name,
  721. int checkpoint_number);
  722. /* Module compare.c */
  723. void report_difference (struct tar_stat_info *st, const char *message, ...)
  724. ATTRIBUTE_FORMAT ((printf, 2, 3));
  725. /* Module sparse.c */
  726. bool sparse_member_p (struct tar_stat_info *st);
  727. bool sparse_fixup_header (struct tar_stat_info *st);
  728. enum dump_status sparse_dump_file (int, struct tar_stat_info *st);
  729. enum dump_status sparse_extract_file (int fd, struct tar_stat_info *st,
  730. off_t *size);
  731. enum dump_status sparse_skip_file (struct tar_stat_info *st);
  732. bool sparse_diff_file (int, struct tar_stat_info *st);
  733. /* Module utf8.c */
  734. bool string_ascii_p (const char *str);
  735. bool utf8_convert (bool to_utf, char const *input, char **output);
  736. /* Module transform.c */
  737. #define XFORM_REGFILE 0x01
  738. #define XFORM_LINK 0x02
  739. #define XFORM_SYMLINK 0x04
  740. #define XFORM_ALL (XFORM_REGFILE|XFORM_LINK|XFORM_SYMLINK)
  741. void set_transform_expr (const char *expr);
  742. bool transform_name (char **pinput, int type);
  743. bool transform_name_fp (char **pinput, int type,
  744. char *(*fun)(char *, void *), void *);
  745. bool transform_program_p (void);
  746. /* Module suffix.c */
  747. void set_compression_program_by_suffix (const char *name, const char *defprog);
  748. char *strip_compression_suffix (const char *name);
  749. /* Module checkpoint.c */
  750. void checkpoint_compile_action (const char *str);
  751. void checkpoint_finish_compile (void);
  752. void checkpoint_run (bool do_write);
  753. void checkpoint_finish (void);
  754. void checkpoint_flush_actions (void);
  755. /* Module warning.c */
  756. #define WARN_ALONE_ZERO_BLOCK 0x00000001
  757. #define WARN_BAD_DUMPDIR 0x00000002
  758. #define WARN_CACHEDIR 0x00000004
  759. #define WARN_CONTIGUOUS_CAST 0x00000008
  760. #define WARN_FILE_CHANGED 0x00000010
  761. #define WARN_FILE_IGNORED 0x00000020
  762. #define WARN_FILE_REMOVED 0x00000040
  763. #define WARN_FILE_SHRANK 0x00000080
  764. #define WARN_FILE_UNCHANGED 0x00000100
  765. #define WARN_FILENAME_WITH_NULS 0x00000200
  766. #define WARN_IGNORE_ARCHIVE 0x00000400
  767. #define WARN_IGNORE_NEWER 0x00000800
  768. #define WARN_NEW_DIRECTORY 0x00001000
  769. #define WARN_RENAME_DIRECTORY 0x00002000
  770. #define WARN_SYMLINK_CAST 0x00004000
  771. #define WARN_TIMESTAMP 0x00008000
  772. #define WARN_UNKNOWN_CAST 0x00010000
  773. #define WARN_UNKNOWN_KEYWORD 0x00020000
  774. #define WARN_XDEV 0x00040000
  775. #define WARN_DECOMPRESS_PROGRAM 0x00080000
  776. #define WARN_EXISTING_FILE 0x00100000
  777. #define WARN_XATTR_WRITE 0x00200000
  778. #define WARN_RECORD_SIZE 0x00400000
  779. #define WARN_FAILED_READ 0x00800000
  780. /* These warnings are enabled by default in verbose mode: */
  781. #define WARN_VERBOSE_WARNINGS (WARN_RENAME_DIRECTORY|WARN_NEW_DIRECTORY|\
  782. WARN_DECOMPRESS_PROGRAM|WARN_EXISTING_FILE|\
  783. WARN_RECORD_SIZE)
  784. #define WARN_ALL (~WARN_VERBOSE_WARNINGS)
  785. void set_warning_option (const char *arg);
  786. extern int warning_option;
  787. #define WARNING_ENABLED(opt) (warning_option & (opt))
  788. #define WARNOPT(opt,args) \
  789. do \
  790. { \
  791. if (WARNING_ENABLED(opt)) WARN (args); \
  792. } \
  793. while (0)
  794. /* Module unlink.c */
  795. void queue_deferred_unlink (const char *name, bool is_dir);
  796. void finish_deferred_unlinks (void);
  797. /* Module exit.c */
  798. extern void (*fatal_exit_hook) (void);
  799. /* Module exclist.c */
  800. #define EXCL_DEFAULT 0x00
  801. #define EXCL_RECURSIVE 0x01
  802. #define EXCL_NON_RECURSIVE 0x02
  803. void excfile_add (const char *name, int flags);
  804. void info_attach_exclist (struct tar_stat_info *dir);
  805. void info_free_exclist (struct tar_stat_info *dir);
  806. bool excluded_name (char const *name, struct tar_stat_info *st);
  807. void exclude_vcs_ignores (void);
  808. /* Module map.c */
  809. void owner_map_read (char const *name);
  810. int owner_map_translate (uid_t uid, uid_t *new_uid, char const **new_name);
  811. void group_map_read (char const *file);
  812. int group_map_translate (gid_t gid, gid_t *new_gid, char const **new_name);
  813. _GL_INLINE_HEADER_END