common.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /* Common declarations for the tar program.
  2. Copyright 1988, 1992-1994, 1996-1997, 1999-2010, 2012-2013 Free
  3. Software Foundation, Inc.
  4. This file is part of GNU tar.
  5. GNU tar is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. GNU tar is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* Declare the GNU tar archive format. */
  16. #include "tar.h"
  17. /* The checksum field is filled with this while the checksum is computed. */
  18. #define CHKBLANKS " " /* 8 blanks, no null */
  19. /* Some constants from POSIX are given names. */
  20. #define NAME_FIELD_SIZE 100
  21. #define PREFIX_FIELD_SIZE 155
  22. #define UNAME_FIELD_SIZE 32
  23. #define GNAME_FIELD_SIZE 32
  24. /* Some various global definitions. */
  25. /* Name of file to use for interacting with user. */
  26. /* GLOBAL is defined to empty in tar.c only, and left alone in other *.c
  27. modules. Here, we merely set it to "extern" if it is not already set.
  28. GNU tar does depend on the system loader to preset all GLOBAL variables to
  29. neutral (or zero) values, explicit initialization is usually not done. */
  30. #ifndef GLOBAL
  31. # define GLOBAL extern
  32. #endif
  33. #define TAREXIT_SUCCESS PAXEXIT_SUCCESS
  34. #define TAREXIT_DIFFERS PAXEXIT_DIFFERS
  35. #define TAREXIT_FAILURE PAXEXIT_FAILURE
  36. #include "arith.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. /* If true, override actual mtime (see below) */
  163. GLOBAL bool set_mtime_option;
  164. /* Value to be put in mtime header field instead of the actual mtime */
  165. GLOBAL struct timespec mtime_option;
  166. /* Return true if newer_mtime_option is initialized. */
  167. #define NEWER_OPTION_INITIALIZED(opt) (0 <= (opt).tv_nsec)
  168. /* Return true if the struct stat ST's M time is less than
  169. newer_mtime_option. */
  170. #define OLDER_STAT_TIME(st, m) \
  171. (timespec_cmp (get_stat_##m##time (&(st)), newer_mtime_option) < 0)
  172. /* Likewise, for struct tar_stat_info ST. */
  173. #define OLDER_TAR_STAT_TIME(st, m) \
  174. (timespec_cmp ((st).m##time, newer_mtime_option) < 0)
  175. /* Zero if there is no recursion, otherwise FNM_LEADING_DIR. */
  176. GLOBAL int recursion_option;
  177. GLOBAL bool numeric_owner_option;
  178. GLOBAL bool one_file_system_option;
  179. /* Create a top-level directory for extracting based on the archive name. */
  180. GLOBAL bool one_top_level_option;
  181. GLOBAL char *one_top_level_dir;
  182. /* Specified value to be put into tar file in place of stat () results, or
  183. just null and -1 if such an override should not take place. */
  184. GLOBAL char const *owner_name_option;
  185. GLOBAL uid_t owner_option;
  186. GLOBAL bool recursive_unlink_option;
  187. GLOBAL bool read_full_records_option;
  188. GLOBAL bool remove_files_option;
  189. /* Specified remote shell command. */
  190. GLOBAL const char *rsh_command_option;
  191. GLOBAL bool same_order_option;
  192. /* If positive, preserve ownership when extracting. */
  193. GLOBAL int same_owner_option;
  194. /* If positive, preserve permissions when extracting. */
  195. GLOBAL int same_permissions_option;
  196. /* If positive, save the SELinux context. */
  197. GLOBAL int selinux_context_option;
  198. /* If positive, save the ACLs. */
  199. GLOBAL int acls_option;
  200. /* If positive, save the user and root xattrs. */
  201. GLOBAL int xattrs_option;
  202. /* When set, strip the given number of file name components from the file name
  203. before extracting */
  204. GLOBAL size_t strip_name_components;
  205. GLOBAL bool show_omitted_dirs_option;
  206. GLOBAL bool sparse_option;
  207. GLOBAL unsigned tar_sparse_major;
  208. GLOBAL unsigned tar_sparse_minor;
  209. GLOBAL bool starting_file_option;
  210. /* Specified maximum byte length of each tape volume (multiple of 1024). */
  211. GLOBAL tarlong tape_length_option;
  212. GLOBAL bool to_stdout_option;
  213. GLOBAL bool totals_option;
  214. GLOBAL bool touch_option;
  215. GLOBAL char *to_command_option;
  216. GLOBAL bool ignore_command_error_option;
  217. /* Restrict some potentially harmful tar options */
  218. GLOBAL bool restrict_option;
  219. /* Return true if the extracted files are not being written to disk */
  220. #define EXTRACT_OVER_PIPE (to_stdout_option || to_command_option)
  221. /* Count how many times the option has been set, multiple setting yields
  222. more verbose behavior. Value 0 means no verbosity, 1 means file name
  223. only, 2 means file name and all attributes. More than 2 is just like 2. */
  224. GLOBAL int verbose_option;
  225. GLOBAL bool verify_option;
  226. /* Specified name of file containing the volume number. */
  227. GLOBAL const char *volno_file_option;
  228. /* Specified value or pattern. */
  229. GLOBAL const char *volume_label_option;
  230. /* Other global variables. */
  231. /* File descriptor for archive file. */
  232. GLOBAL int archive;
  233. /* Nonzero when outputting to /dev/null. */
  234. GLOBAL bool dev_null_output;
  235. /* Timestamps: */
  236. GLOBAL struct timespec start_time; /* when we started execution */
  237. GLOBAL struct timespec volume_start_time; /* when the current volume was
  238. opened*/
  239. GLOBAL struct timespec last_stat_time; /* when the statistics was last
  240. computed */
  241. GLOBAL struct tar_stat_info current_stat_info;
  242. /* List of tape drive names, number of such tape drives,
  243. and current cursor in list. */
  244. GLOBAL const char **archive_name_array;
  245. GLOBAL size_t archive_names;
  246. GLOBAL const char **archive_name_cursor;
  247. /* Output index file name. */
  248. GLOBAL char const *index_file_name;
  249. /* Opaque structure for keeping directory meta-data */
  250. struct directory;
  251. /* Structure for keeping track of filenames and lists thereof. */
  252. struct name
  253. {
  254. struct name *next; /* Link to the next element */
  255. struct name *prev; /* Link to the previous element */
  256. char *name; /* File name or globbing pattern */
  257. size_t length; /* cached strlen (name) */
  258. int matching_flags; /* wildcard flags if name is a pattern */
  259. bool cmdline; /* true if this name was given in the
  260. command line */
  261. int change_dir; /* Number of the directory to change to.
  262. Set with the -C option. */
  263. uintmax_t found_count; /* number of times a matching file has
  264. been found */
  265. /* The following members are used for incremental dumps only,
  266. if this struct name represents a directory;
  267. see incremen.c */
  268. struct directory *directory;/* directory meta-data and contents */
  269. struct name *parent; /* pointer to the parent hierarchy */
  270. struct name *child; /* pointer to the first child */
  271. struct name *sibling; /* pointer to the next sibling */
  272. char *caname; /* canonical name */
  273. };
  274. /* Obnoxious test to see if dimwit is trying to dump the archive. */
  275. GLOBAL dev_t ar_dev;
  276. GLOBAL ino_t ar_ino;
  277. /* Flags for reading, searching, and fstatatting files. */
  278. GLOBAL int open_read_flags;
  279. GLOBAL int open_searchdir_flags;
  280. GLOBAL int fstatat_flags;
  281. GLOBAL int seek_option;
  282. GLOBAL bool seekable_archive;
  283. GLOBAL dev_t root_device;
  284. /* Unquote filenames */
  285. GLOBAL bool unquote_option;
  286. /* Show file or archive names after transformation.
  287. In particular, when creating archive in verbose mode, list member names
  288. as stored in the archive */
  289. GLOBAL bool show_transformed_names_option;
  290. /* Delay setting modification times and permissions of extracted directories
  291. until the end of extraction. This variable helps correctly restore directory
  292. timestamps from archives with an unusual member order. It is automatically
  293. set for incremental archives. */
  294. GLOBAL bool delay_directory_restore_option;
  295. /* Warn about implicit use of the wildcards in command line arguments.
  296. (Default for tar prior to 1.15.91, but changed afterwards */
  297. GLOBAL bool warn_regex_usage;
  298. /* Declarations for each module. */
  299. /* FIXME: compare.c should not directly handle the following variable,
  300. instead, this should be done in buffer.c only. */
  301. enum access_mode
  302. {
  303. ACCESS_READ,
  304. ACCESS_WRITE,
  305. ACCESS_UPDATE
  306. };
  307. extern enum access_mode access_mode;
  308. /* Module buffer.c. */
  309. extern FILE *stdlis;
  310. extern bool write_archive_to_stdout;
  311. extern char *volume_label;
  312. extern size_t volume_label_count;
  313. extern char *continued_file_name;
  314. extern uintmax_t continued_file_size;
  315. extern uintmax_t continued_file_offset;
  316. extern off_t records_written;
  317. char *drop_volume_label_suffix (const char *label);
  318. size_t available_space_after (union block *pointer);
  319. off_t current_block_ordinal (void);
  320. void close_archive (void);
  321. void closeout_volume_number (void);
  322. double compute_duration (void);
  323. union block *find_next_block (void);
  324. void flush_read (void);
  325. void flush_write (void);
  326. void flush_archive (void);
  327. void init_volume_number (void);
  328. void open_archive (enum access_mode mode);
  329. void print_total_stats (void);
  330. void reset_eof (void);
  331. void set_next_block_after (union block *block);
  332. void clear_read_error_count (void);
  333. void xclose (int fd);
  334. void archive_write_error (ssize_t status) __attribute__ ((noreturn));
  335. void archive_read_error (void);
  336. off_t seek_archive (off_t size);
  337. void set_start_time (void);
  338. #define TF_READ 0
  339. #define TF_WRITE 1
  340. #define TF_DELETED 2
  341. int format_total_stats (FILE *fp, const char **formats, int eor, int eol);
  342. void print_total_stats (void);
  343. void mv_begin_write (const char *file_name, off_t totsize, off_t sizeleft);
  344. void mv_begin_read (struct tar_stat_info *st);
  345. void mv_end (void);
  346. void mv_size_left (off_t size);
  347. void buffer_write_global_xheader (void);
  348. const char *first_decompress_program (int *pstate);
  349. const char *next_decompress_program (int *pstate);
  350. /* Module create.c. */
  351. enum dump_status
  352. {
  353. dump_status_ok,
  354. dump_status_short,
  355. dump_status_fail,
  356. dump_status_not_implemented
  357. };
  358. void add_exclusion_tag (const char *name, enum exclusion_tag_type type,
  359. bool (*predicate) (int));
  360. bool cachedir_file_p (int fd);
  361. char *get_directory_entries (struct tar_stat_info *st);
  362. void create_archive (void);
  363. void pad_archive (off_t size_left);
  364. void dump_file (struct tar_stat_info *parent, char const *name,
  365. char const *fullname);
  366. union block *start_header (struct tar_stat_info *st);
  367. void finish_header (struct tar_stat_info *st, union block *header,
  368. off_t block_ordinal);
  369. void simple_finish_header (union block *header);
  370. union block * write_extended (bool global, struct tar_stat_info *st,
  371. union block *old_header);
  372. union block *start_private_header (const char *name, size_t size, time_t t);
  373. void write_eot (void);
  374. void check_links (void);
  375. int subfile_open (struct tar_stat_info const *dir, char const *file, int flags);
  376. void restore_parent_fd (struct tar_stat_info const *st);
  377. void exclusion_tag_warning (const char *dirname, const char *tagname,
  378. const char *message);
  379. enum exclusion_tag_type check_exclusion_tags (struct tar_stat_info const *st,
  380. const char **tag_file_name);
  381. #define OFF_TO_CHARS(val, where) off_to_chars (val, where, sizeof (where))
  382. #define TIME_TO_CHARS(val, where) time_to_chars (val, where, sizeof (where))
  383. bool off_to_chars (off_t off, char *buf, size_t size);
  384. bool time_to_chars (time_t t, char *buf, size_t size);
  385. /* Module diffarch.c. */
  386. extern bool now_verifying;
  387. void diff_archive (void);
  388. void diff_init (void);
  389. void verify_volume (void);
  390. /* Module extract.c. */
  391. void extr_init (void);
  392. void extract_archive (void);
  393. void extract_finish (void);
  394. bool rename_directory (char *src, char *dst);
  395. /* Module delete.c. */
  396. void delete_archive_members (void);
  397. /* Module incremen.c. */
  398. struct directory *scan_directory (struct tar_stat_info *st);
  399. const char *directory_contents (struct directory *dir);
  400. const char *safe_directory_contents (struct directory *dir);
  401. void rebase_directory (struct directory *dir,
  402. const char *samp, size_t slen,
  403. const char *repl, size_t rlen);
  404. void append_incremental_renames (struct directory *dir);
  405. void show_snapshot_field_ranges (void);
  406. void read_directory_file (void);
  407. void write_directory_file (void);
  408. void purge_directory (char const *directory_name);
  409. void list_dumpdir (char *buffer, size_t size);
  410. void update_parent_directory (struct tar_stat_info *st);
  411. size_t dumpdir_size (const char *p);
  412. bool is_dumpdir (struct tar_stat_info *stat_info);
  413. void clear_directory_table (void);
  414. /* Module list.c. */
  415. enum read_header
  416. {
  417. HEADER_STILL_UNREAD, /* for when read_header has not been called */
  418. HEADER_SUCCESS, /* header successfully read and checksummed */
  419. HEADER_SUCCESS_EXTENDED, /* likewise, but we got an extended header */
  420. HEADER_ZERO_BLOCK, /* zero block where header expected */
  421. HEADER_END_OF_FILE, /* true end of file while header expected */
  422. HEADER_FAILURE /* ill-formed header, or bad checksum */
  423. };
  424. /* Operation mode for read_header: */
  425. enum read_header_mode
  426. {
  427. read_header_auto, /* process extended headers automatically */
  428. read_header_x_raw, /* return raw extended headers (return
  429. HEADER_SUCCESS_EXTENDED) */
  430. read_header_x_global /* when POSIX global extended header is read,
  431. decode it and return
  432. HEADER_SUCCESS_EXTENDED */
  433. };
  434. extern union block *current_header;
  435. extern enum archive_format current_format;
  436. extern size_t recent_long_name_blocks;
  437. extern size_t recent_long_link_blocks;
  438. void decode_header (union block *header, struct tar_stat_info *stat_info,
  439. enum archive_format *format_pointer, int do_user_group);
  440. void transform_stat_info (int typeflag, struct tar_stat_info *stat_info);
  441. char const *tartime (struct timespec t, bool full_time);
  442. #define OFF_FROM_HEADER(where) off_from_header (where, sizeof (where))
  443. #define UINTMAX_FROM_HEADER(where) uintmax_from_header (where, sizeof (where))
  444. off_t off_from_header (const char *buf, size_t size);
  445. uintmax_t uintmax_from_header (const char *buf, size_t size);
  446. void list_archive (void);
  447. void test_archive_label (void);
  448. void print_for_mkdir (char *dirname, int length, mode_t mode);
  449. void print_header (struct tar_stat_info *st, union block *blk,
  450. off_t block_ordinal);
  451. void read_and (void (*do_something) (void));
  452. enum read_header read_header (union block **return_block,
  453. struct tar_stat_info *info,
  454. enum read_header_mode m);
  455. enum read_header tar_checksum (union block *header, bool silent);
  456. void skip_file (off_t size);
  457. void skip_member (void);
  458. /* Module misc.c. */
  459. #define min(a, b) ((a) < (b) ? (a) : (b))
  460. #define max(a, b) ((a) < (b) ? (b) : (a))
  461. void assign_string (char **dest, const char *src);
  462. int unquote_string (char *str);
  463. char *zap_slashes (char *name);
  464. char *normalize_filename (int cdidx, const char *name);
  465. void normalize_filename_x (char *name);
  466. void replace_prefix (char **pname, const char *samp, size_t slen,
  467. const char *repl, size_t rlen);
  468. char *tar_savedir (const char *name, int must_exist);
  469. typedef struct namebuf *namebuf_t;
  470. namebuf_t namebuf_create (const char *dir);
  471. void namebuf_free (namebuf_t buf);
  472. char *namebuf_name (namebuf_t buf, const char *name);
  473. void namebuf_add_dir (namebuf_t buf, const char *name);
  474. char *namebuf_finish (namebuf_t buf);
  475. const char *tar_dirname (void);
  476. /* Represent N using a signed integer I such that (uintmax_t) I == N.
  477. With a good optimizing compiler, this is equivalent to (intmax_t) i
  478. and requires zero machine instructions. */
  479. #if ! (UINTMAX_MAX / 2 <= INTMAX_MAX)
  480. # error "represent_uintmax returns intmax_t to represent uintmax_t"
  481. #endif
  482. COMMON_INLINE intmax_t
  483. represent_uintmax (uintmax_t n)
  484. {
  485. if (n <= INTMAX_MAX)
  486. return n;
  487. else
  488. {
  489. /* Avoid signed integer overflow on picky platforms. */
  490. intmax_t nd = n - INTMAX_MIN;
  491. return nd + INTMAX_MIN;
  492. }
  493. }
  494. enum { SYSINT_BUFSIZE =
  495. max (UINTMAX_STRSIZE_BOUND, INT_BUFSIZE_BOUND (intmax_t)) };
  496. char *sysinttostr (uintmax_t, intmax_t, uintmax_t, char buf[SYSINT_BUFSIZE]);
  497. intmax_t strtosysint (char const *, char **, intmax_t, uintmax_t);
  498. void code_ns_fraction (int ns, char *p);
  499. char const *code_timespec (struct timespec ts, char *sbuf);
  500. enum { BILLION = 1000000000, LOG10_BILLION = 9 };
  501. enum { TIMESPEC_STRSIZE_BOUND =
  502. UINTMAX_STRSIZE_BOUND + LOG10_BILLION + sizeof "-." - 1 };
  503. struct timespec decode_timespec (char const *, char **, bool);
  504. /* Return true if T does not represent an out-of-range or invalid value. */
  505. COMMON_INLINE bool
  506. valid_timespec (struct timespec t)
  507. {
  508. return 0 <= t.tv_nsec;
  509. }
  510. bool must_be_dot_or_slash (char const *);
  511. enum remove_option
  512. {
  513. ORDINARY_REMOVE_OPTION,
  514. RECURSIVE_REMOVE_OPTION,
  515. /* FIXME: The following value is never used. It seems to be intended
  516. as a placeholder for a hypothetical option that should instruct tar
  517. to recursively remove subdirectories in purge_directory(),
  518. as opposed to the functionality of --recursive-unlink
  519. (RECURSIVE_REMOVE_OPTION value), which removes them in
  520. prepare_to_extract() phase. However, with the addition of more
  521. meta-info to the incremental dumps, this should become unnecessary */
  522. WANT_DIRECTORY_REMOVE_OPTION
  523. };
  524. int remove_any_file (const char *file_name, enum remove_option option);
  525. bool maybe_backup_file (const char *file_name, bool this_is_the_archive);
  526. void undo_last_backup (void);
  527. int deref_stat (char const *name, struct stat *buf);
  528. size_t blocking_read (int fd, void *buf, size_t count);
  529. size_t blocking_write (int fd, void const *buf, size_t count);
  530. extern int chdir_current;
  531. extern int chdir_fd;
  532. int chdir_arg (char const *dir);
  533. void chdir_do (int dir);
  534. int chdir_count (void);
  535. void close_diag (char const *name);
  536. void open_diag (char const *name);
  537. void read_diag_details (char const *name, off_t offset, size_t size);
  538. void readlink_diag (char const *name);
  539. void savedir_diag (char const *name);
  540. void seek_diag_details (char const *name, off_t offset);
  541. void stat_diag (char const *name);
  542. void file_removed_diag (const char *name, bool top_level,
  543. void (*diagfn) (char const *name));
  544. void write_error_details (char const *name, size_t status, size_t size);
  545. void write_fatal (char const *name) __attribute__ ((noreturn));
  546. void write_fatal_details (char const *name, ssize_t status, size_t size)
  547. __attribute__ ((noreturn));
  548. pid_t xfork (void);
  549. void xpipe (int fd[2]);
  550. void *page_aligned_alloc (void **ptr, size_t size);
  551. int set_file_atime (int fd, int parentfd, char const *file,
  552. struct timespec atime);
  553. /* Module names.c. */
  554. extern size_t name_count;
  555. extern struct name *gnu_list_name;
  556. void gid_to_gname (gid_t gid, char **gname);
  557. int gname_to_gid (char const *gname, gid_t *pgid);
  558. void uid_to_uname (uid_t uid, char **uname);
  559. int uname_to_uid (char const *uname, uid_t *puid);
  560. void name_init (void);
  561. void name_add_name (const char *name, int matching_flags);
  562. void name_add_dir (const char *name);
  563. void name_add_file (const char *name, int term);
  564. void name_term (void);
  565. const char *name_next (int change_dirs);
  566. void name_gather (void);
  567. struct name *addname (char const *string, int change_dir,
  568. bool cmdline, struct name *parent);
  569. void remname (struct name *name);
  570. bool name_match (const char *name);
  571. void names_notfound (void);
  572. void label_notfound (void);
  573. void collect_and_sort_names (void);
  574. struct name *name_scan (const char *name);
  575. struct name const *name_from_list (void);
  576. void blank_name_list (void);
  577. char *new_name (const char *dir_name, const char *name);
  578. size_t stripped_prefix_len (char const *file_name, size_t num);
  579. bool all_names_found (struct tar_stat_info *st);
  580. bool excluded_name (char const *name);
  581. void add_avoided_name (char const *name);
  582. bool is_avoided_name (char const *name);
  583. bool contains_dot_dot (char const *name);
  584. #define ISFOUND(c) ((occurrence_option == 0) ? (c)->found_count : \
  585. (c)->found_count == occurrence_option)
  586. #define WASFOUND(c) ((occurrence_option == 0) ? (c)->found_count : \
  587. (c)->found_count >= occurrence_option)
  588. /* Module tar.c. */
  589. void usage (int);
  590. int confirm (const char *message_action, const char *name);
  591. void tar_stat_init (struct tar_stat_info *st);
  592. bool tar_stat_close (struct tar_stat_info *st);
  593. void tar_stat_destroy (struct tar_stat_info *st);
  594. void usage (int) __attribute__ ((noreturn));
  595. int tar_timespec_cmp (struct timespec a, struct timespec b);
  596. const char *archive_format_string (enum archive_format fmt);
  597. const char *subcommand_string (enum subcommand c);
  598. void set_exit_status (int val);
  599. void request_stdin (const char *option);
  600. void more_options (int argc, char **argv);
  601. /* Module update.c. */
  602. extern char *output_start;
  603. void update_archive (void);
  604. /* Module attrs.c. */
  605. #include "xattrs.h"
  606. /* Module xheader.c. */
  607. void xheader_decode (struct tar_stat_info *stat);
  608. void xheader_decode_global (struct xheader *xhdr);
  609. void xheader_store (char const *keyword, struct tar_stat_info *st,
  610. void const *data);
  611. void xheader_read (struct xheader *xhdr, union block *header, off_t size);
  612. void xheader_write (char type, char *name, time_t t, struct xheader *xhdr);
  613. void xheader_write_global (struct xheader *xhdr);
  614. void xheader_finish (struct xheader *hdr);
  615. void xheader_destroy (struct xheader *hdr);
  616. char *xheader_xhdr_name (struct tar_stat_info *st);
  617. char *xheader_ghdr_name (void);
  618. void xheader_set_option (char *string);
  619. void xheader_string_begin (struct xheader *xhdr);
  620. void xheader_string_add (struct xheader *xhdr, char const *s);
  621. bool xheader_string_end (struct xheader *xhdr, char const *keyword);
  622. bool xheader_keyword_deleted_p (const char *kw);
  623. char *xheader_format_name (struct tar_stat_info *st, const char *fmt,
  624. size_t n);
  625. void xheader_xattr_init (struct tar_stat_info *st);
  626. void xheader_xattr_free (struct xattr_array *vals, size_t sz);
  627. void xheader_xattr_copy (const struct tar_stat_info *st,
  628. struct xattr_array **vals, size_t *sz);
  629. void xheader_xattr_add (struct tar_stat_info *st,
  630. const char *key, const char *val, size_t len);
  631. /* Module system.c */
  632. void sys_detect_dev_null_output (void);
  633. void sys_save_archive_dev_ino (void);
  634. void sys_wait_for_child (pid_t, bool);
  635. void sys_spawn_shell (void);
  636. bool sys_compare_uid (struct stat *a, struct stat *b);
  637. bool sys_compare_gid (struct stat *a, struct stat *b);
  638. bool sys_file_is_archive (struct tar_stat_info *p);
  639. bool sys_compare_links (struct stat *link_data, struct stat *stat_data);
  640. int sys_truncate (int fd);
  641. pid_t sys_child_open_for_compress (void);
  642. pid_t sys_child_open_for_uncompress (void);
  643. size_t sys_write_archive_buffer (void);
  644. bool sys_get_archive_stat (void);
  645. int sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st);
  646. void sys_wait_command (void);
  647. int sys_exec_info_script (const char **archive_name, int volume_number);
  648. void sys_exec_checkpoint_script (const char *script_name,
  649. const char *archive_name,
  650. int checkpoint_number);
  651. /* Module compare.c */
  652. void report_difference (struct tar_stat_info *st, const char *message, ...)
  653. __attribute__ ((format (printf, 2, 3)));
  654. /* Module sparse.c */
  655. bool sparse_member_p (struct tar_stat_info *st);
  656. bool sparse_fixup_header (struct tar_stat_info *st);
  657. enum dump_status sparse_dump_file (int, struct tar_stat_info *st);
  658. enum dump_status sparse_extract_file (int fd, struct tar_stat_info *st,
  659. off_t *size);
  660. enum dump_status sparse_skip_file (struct tar_stat_info *st);
  661. bool sparse_diff_file (int, struct tar_stat_info *st);
  662. /* Module utf8.c */
  663. bool string_ascii_p (const char *str);
  664. bool utf8_convert (bool to_utf, char const *input, char **output);
  665. /* Module transform.c */
  666. #define XFORM_REGFILE 0x01
  667. #define XFORM_LINK 0x02
  668. #define XFORM_SYMLINK 0x04
  669. #define XFORM_ALL (XFORM_REGFILE|XFORM_LINK|XFORM_SYMLINK)
  670. void set_transform_expr (const char *expr);
  671. bool transform_name (char **pinput, int type);
  672. bool transform_name_fp (char **pinput, int type,
  673. char *(*fun)(char *, void *), void *);
  674. bool transform_program_p (void);
  675. /* Module suffix.c */
  676. void set_compression_program_by_suffix (const char *name, const char *defprog);
  677. char *strip_compression_suffix (const char *name);
  678. /* Module checkpoint.c */
  679. void checkpoint_compile_action (const char *str);
  680. void checkpoint_finish_compile (void);
  681. void checkpoint_run (bool do_write);
  682. void checkpoint_finish (void);
  683. void checkpoint_flush_actions (void);
  684. /* Module warning.c */
  685. #define WARN_ALONE_ZERO_BLOCK 0x00000001
  686. #define WARN_BAD_DUMPDIR 0x00000002
  687. #define WARN_CACHEDIR 0x00000004
  688. #define WARN_CONTIGUOUS_CAST 0x00000008
  689. #define WARN_FILE_CHANGED 0x00000010
  690. #define WARN_FILE_IGNORED 0x00000020
  691. #define WARN_FILE_REMOVED 0x00000040
  692. #define WARN_FILE_SHRANK 0x00000080
  693. #define WARN_FILE_UNCHANGED 0x00000100
  694. #define WARN_FILENAME_WITH_NULS 0x00000200
  695. #define WARN_IGNORE_ARCHIVE 0x00000400
  696. #define WARN_IGNORE_NEWER 0x00000800
  697. #define WARN_NEW_DIRECTORY 0x00001000
  698. #define WARN_RENAME_DIRECTORY 0x00002000
  699. #define WARN_SYMLINK_CAST 0x00004000
  700. #define WARN_TIMESTAMP 0x00008000
  701. #define WARN_UNKNOWN_CAST 0x00010000
  702. #define WARN_UNKNOWN_KEYWORD 0x00020000
  703. #define WARN_XDEV 0x00040000
  704. #define WARN_DECOMPRESS_PROGRAM 0x00080000
  705. #define WARN_EXISTING_FILE 0x00100000
  706. #define WARN_XATTR_WRITE 0x00200000
  707. #define WARN_RECORD_SIZE 0x00400000
  708. /* These warnings are enabled by default in verbose mode: */
  709. #define WARN_VERBOSE_WARNINGS (WARN_RENAME_DIRECTORY|WARN_NEW_DIRECTORY|\
  710. WARN_DECOMPRESS_PROGRAM|WARN_EXISTING_FILE|\
  711. WARN_RECORD_SIZE)
  712. #define WARN_ALL (~WARN_VERBOSE_WARNINGS)
  713. void set_warning_option (const char *arg);
  714. extern int warning_option;
  715. #define WARNOPT(opt,args) \
  716. do \
  717. { \
  718. if (warning_option & opt) WARN (args); \
  719. } \
  720. while (0)
  721. /* Module unlink.c */
  722. void queue_deferred_unlink (const char *name, bool is_dir);
  723. void finish_deferred_unlinks (void);
  724. /* Module exit.c */
  725. extern void (*fatal_exit_hook) (void);
  726. _GL_INLINE_HEADER_END