xref: /freebsd/contrib/libarchive/tar/bsdtar.h (revision 401026e4825a05abba6f945cf1b74b3328876fa2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2007 Tim Kientzle
5  * All rights reserved.
6  */
7 
8 #ifndef BSDTAR_H_INCLUDED
9 #define BSDTAR_H_INCLUDED
10 
11 #include "bsdtar_platform.h"
12 #include <stdio.h>
13 
14 #define	DEFAULT_BYTES_PER_BLOCK	(20*512)
15 #define ENV_READER_OPTIONS	"TAR_READER_OPTIONS"
16 #define ENV_WRITER_OPTIONS	"TAR_WRITER_OPTIONS"
17 #define IGNORE_WRONG_MODULE_NAME "__ignore_wrong_module_name__,"
18 
19 #if defined(_MSC_VER ) && (_MSC_VER < 1927 )	/* Check if compiler pre-dated Visual Studio 2019 Release 16.8 */
20 #define ARCHIVE_RESTRICT
21 #else
22 #define ARCHIVE_RESTRICT restrict
23 #endif
24 
25 struct creation_set;
26 /*
27  * The internal state for the "bsdtar" program.
28  *
29  * Keeping all of the state in a structure like this simplifies memory
30  * leak testing (at exit, anything left on the heap is suspect).  A
31  * pointer to this structure is passed to most bsdtar internal
32  * functions.
33  */
34 struct bsdtar {
35 	/* Options */
36 	const char	 *filename; /* -f filename */
37 	char		 *pending_chdir; /* -C dir */
38 	const char	 *names_from_file; /* -T file */
39 	int		  bytes_per_block; /* -b block_size */
40 	int		  bytes_in_last_block; /* See -b handling. */
41 	int		  verbose;   /* -v */
42 	unsigned int	  flags; /* Bitfield of boolean options */
43 	int		  extract_flags; /* Flags for extract operation */
44 	int		  readdisk_flags; /* Flags for read disk operation */
45 	int		  strip_components; /* Remove this many leading dirs */
46 	int		  gid;  /* --gid */
47 	const char	 *gname; /* --gname */
48 	int		  uid;  /* --uid */
49 	const char	 *uname; /* --uname */
50 	const char	 *passphrase; /* --passphrase */
51 	int		  mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
52 	char		  symlink_mode; /* H or L, per BSD conventions */
53 	const char	 *option_options; /* --options */
54 	char		  day_first; /* show day before month in -tv output */
55 	char		has_mtime; /* --mtime exists (0 or 1) */
56 	char		clamp_mtime; /* --clamp-mtime (0 or 1)*/
57 	time_t		mtime; /* --mtime */
58 	struct creation_set *cset;
59 
60 	/* Option parser state */
61 	int		  getopt_state;
62 	char		 *getopt_word;
63 
64 	/* If >= 0, then close this when done. */
65 	int		  fd;
66 
67 	/* Miscellaneous state information */
68 	int		  argc;
69 	char		**argv;
70 	const char	 *argument;
71 	size_t		  gs_width; /* For 'list_item' in read.c */
72 	size_t		  u_width; /* for 'list_item' in read.c */
73 	uid_t		  user_uid; /* UID running this program */
74 	int		  return_value; /* Value returned by main() */
75 	char		  warned_lead_slash; /* Already displayed warning */
76 	char		  next_line_is_dir; /* Used for -C parsing in -cT */
77 
78 	/*
79 	 * Data for various subsystems.  Full definitions are located in
80 	 * the file where they are used.
81 	 */
82 	struct archive		*diskreader;	/* for write.c */
83 	struct archive_entry_linkresolver *resolver; /* for write.c */
84 	struct archive_dir	*archive_dir;	/* for write.c */
85 	struct name_cache	*gname_cache;	/* for write.c */
86 	char			*buff;		/* for write.c */
87 	size_t			 buff_size;	/* for write.c */
88 	int			 first_fs;	/* for write.c */
89 	struct archive		*matching;	/* for matching.c */
90 	struct security		*security;	/* for read.c */
91 	struct name_cache	*uname_cache;	/* for write.c */
92 	struct siginfo_data	*siginfo;	/* for siginfo.c */
93 	struct substitution	*substitution;	/* for subst.c */
94 	char			*ppbuff;	/* for util.c */
95 };
96 
97 /* Options for flags bitfield */
98 #define	OPTFLAG_AUTO_COMPRESS	(0x00000001)	/* -a */
99 #define	OPTFLAG_ABSOLUTE_PATHS	(0x00000002)	/* -P */
100 #define	OPTFLAG_CHROOT		(0x00000004)	/* --chroot */
101 #define	OPTFLAG_FAST_READ	(0x00000008)	/* --fast-read */
102 #define	OPTFLAG_IGNORE_ZEROS	(0x00000010)	/* --ignore-zeros */
103 #define	OPTFLAG_INTERACTIVE	(0x00000020)	/* -w */
104 #define	OPTFLAG_NO_OWNER	(0x00000040)	/* -o */
105 #define	OPTFLAG_NO_SUBDIRS	(0x00000080)	/* -n */
106 #define	OPTFLAG_NULL		(0x00000100)	/* --null */
107 #define	OPTFLAG_NUMERIC_OWNER	(0x00000200)	/* --numeric-owner */
108 #define	OPTFLAG_O		(0x00000400)	/* -o */
109 #define	OPTFLAG_STDOUT		(0x00000800)	/* -O */
110 #define	OPTFLAG_TOTALS		(0x00001000)	/* --totals */
111 #define	OPTFLAG_UNLINK_FIRST	(0x00002000)	/* -U */
112 #define	OPTFLAG_WARN_LINKS	(0x00004000)	/* --check-links */
113 #define	OPTFLAG_NO_XATTRS	(0x00008000)	/* --no-xattrs */
114 #define	OPTFLAG_XATTRS		(0x00010000)	/* --xattrs */
115 #define	OPTFLAG_NO_ACLS		(0x00020000)	/* --no-acls */
116 #define	OPTFLAG_ACLS		(0x00040000)	/* --acls */
117 #define	OPTFLAG_NO_FFLAGS	(0x00080000)	/* --no-fflags */
118 #define	OPTFLAG_FFLAGS		(0x00100000)	/* --fflags */
119 #define	OPTFLAG_NO_MAC_METADATA	(0x00200000)	/* --no-mac-metadata */
120 #define	OPTFLAG_MAC_METADATA	(0x00400000)	/* --mac-metadata */
121 #define	OPTFLAG_NO_READ_SPARSE	(0x00800000)    /* --no-read-sparse */
122 #define	OPTFLAG_READ_SPARSE		(0x01000000)    /* --read-sparse */
123 
124 /* Fake short equivalents for long options that otherwise lack them. */
125 enum {
126 	OPTION_ACLS = 256,
127 	OPTION_B64ENCODE,
128 	OPTION_CHECK_LINKS,
129 	OPTION_CHROOT,
130 	OPTION_CLEAR_NOCHANGE_FFLAGS,
131 	OPTION_EXCLUDE,
132 	OPTION_EXCLUDE_VCS,
133 	OPTION_FFLAGS,
134 	OPTION_FORMAT,
135 	OPTION_GID,
136 	OPTION_GNAME,
137 	OPTION_GROUP,
138 	OPTION_GRZIP,
139 	OPTION_HELP,
140 	OPTION_HFS_COMPRESSION,
141 	OPTION_IGNORE_ZEROS,
142 	OPTION_INCLUDE,
143 	OPTION_KEEP_NEWER_FILES,
144 	OPTION_LRZIP,
145 	OPTION_LZ4,
146 	OPTION_LZIP,
147 	OPTION_LZMA,
148 	OPTION_LZOP,
149 	OPTION_MAC_METADATA,
150 	OPTION_NEWER_CTIME,
151 	OPTION_NEWER_CTIME_THAN,
152 	OPTION_NEWER_MTIME,
153 	OPTION_NEWER_MTIME_THAN,
154 	OPTION_NODUMP,
155 	OPTION_NOPRESERVE_HFS_COMPRESSION,
156 	OPTION_NO_ACLS,
157 	OPTION_NO_FFLAGS,
158 	OPTION_NO_MAC_METADATA,
159 	OPTION_NO_READ_SPARSE,
160 	OPTION_NO_SAFE_WRITES,
161 	OPTION_NO_SAME_OWNER,
162 	OPTION_NO_SAME_PERMISSIONS,
163 	OPTION_NO_XATTRS,
164 	OPTION_NULL,
165 	OPTION_NUMERIC_OWNER,
166 	OPTION_OLDER_CTIME,
167 	OPTION_OLDER_CTIME_THAN,
168 	OPTION_OLDER_MTIME,
169 	OPTION_OLDER_MTIME_THAN,
170 	OPTION_ONE_FILE_SYSTEM,
171 	OPTION_OPTIONS,
172 	OPTION_OWNER,
173 	OPTION_PASSPHRASE,
174 	OPTION_POSIX,
175 	OPTION_READ_SPARSE,
176 	OPTION_SAFE_WRITES,
177 	OPTION_SAME_OWNER,
178 	OPTION_STRIP_COMPONENTS,
179 	OPTION_TOTALS,
180 	OPTION_UID,
181 	OPTION_UNAME,
182 	OPTION_USE_COMPRESS_PROGRAM,
183 	OPTION_UUENCODE,
184 	OPTION_VERSION,
185 	OPTION_XATTRS,
186 	OPTION_ZSTD,
187 	OPTION_MTIME,
188 	OPTION_CLAMP_MTIME,
189 };
190 
191 int	bsdtar_getopt(struct bsdtar *);
192 void	do_chdir(struct bsdtar *);
193 int	edit_pathname(struct bsdtar *, struct archive_entry *);
194 void	edit_mtime(struct bsdtar *, struct archive_entry *);
195 int	need_report(void);
196 int	pathcmp(const char *a, const char *b);
197 void	safe_fprintf(FILE * ARCHIVE_RESTRICT, const char * ARCHIVE_RESTRICT fmt, ...) __LA_PRINTF(2, 3);
198 void	set_chdir(struct bsdtar *, const char *newdir);
199 const char *tar_i64toa(int64_t);
200 void	tar_mode_c(struct bsdtar *bsdtar);
201 void	tar_mode_r(struct bsdtar *bsdtar);
202 void	tar_mode_t(struct bsdtar *bsdtar);
203 void	tar_mode_u(struct bsdtar *bsdtar);
204 void	tar_mode_x(struct bsdtar *bsdtar);
205 __LA_NORETURN void	usage(void);
206 int	yes(const char *fmt, ...) __LA_PRINTF(1, 2);
207 
208 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) || defined(HAVE_PCRE2POSIX_H)
209 void	add_substitution(struct bsdtar *, const char *);
210 int	apply_substitution(struct bsdtar *, const char *, char **, int, int);
211 void	cleanup_substitution(struct bsdtar *);
212 #endif
213 
214 void		cset_add_filter(struct creation_set *, const char *);
215 void		cset_add_filter_program(struct creation_set *, const char *);
216 int		cset_auto_compress(struct creation_set *, const char *);
217 void		cset_free(struct creation_set *);
218 const char *	cset_get_format(struct creation_set *);
219 struct creation_set *cset_new(void);
220 int		cset_read_support_filter_program(struct creation_set *,
221 		    struct archive *);
222 void		cset_set_format(struct creation_set *, const char *);
223 int		cset_write_add_filters(struct creation_set *,
224 		    struct archive *, const void **);
225 
226 const char * passphrase_callback(struct archive *, void *);
227 void	     passphrase_free(char *);
228 void	list_item_verbose(struct bsdtar *, FILE *,
229 		    struct archive_entry *);
230 
231 #endif
232