1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD: stable/11/contrib/libarchive/tar/bsdtar.h 358088 2020-02-19 01:50:47Z mm $
26 */
27
28#ifndef BSDTAR_H_INCLUDED
29#define BSDTAR_H_INCLUDED
30
31#include "bsdtar_platform.h"
32#include <stdio.h>
33
34#define	DEFAULT_BYTES_PER_BLOCK	(20*512)
35#define ENV_READER_OPTIONS	"TAR_READER_OPTIONS"
36#define ENV_WRITER_OPTIONS	"TAR_WRITER_OPTIONS"
37#define IGNORE_WRONG_MODULE_NAME "__ignore_wrong_module_name__,"
38
39struct creation_set;
40/*
41 * The internal state for the "bsdtar" program.
42 *
43 * Keeping all of the state in a structure like this simplifies memory
44 * leak testing (at exit, anything left on the heap is suspect).  A
45 * pointer to this structure is passed to most bsdtar internal
46 * functions.
47 */
48struct bsdtar {
49	/* Options */
50	const char	 *filename; /* -f filename */
51	char		 *pending_chdir; /* -C dir */
52	const char	 *names_from_file; /* -T file */
53	int		  bytes_per_block; /* -b block_size */
54	int		  bytes_in_last_block; /* See -b handling. */
55	int		  verbose;   /* -v */
56	unsigned int	  flags; /* Bitfield of boolean options */
57	int		  extract_flags; /* Flags for extract operation */
58	int		  readdisk_flags; /* Flags for read disk operation */
59	int		  strip_components; /* Remove this many leading dirs */
60	int		  gid;  /* --gid */
61	const char	 *gname; /* --gname */
62	int		  uid;  /* --uid */
63	const char	 *uname; /* --uname */
64	const char	 *passphrase; /* --passphrase */
65	char		  mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
66	char		  symlink_mode; /* H or L, per BSD conventions */
67	const char	 *option_options; /* --options */
68	char		  day_first; /* show day before month in -tv output */
69	struct creation_set *cset;
70
71	/* Option parser state */
72	int		  getopt_state;
73	char		 *getopt_word;
74
75	/* If >= 0, then close this when done. */
76	int		  fd;
77
78	/* Miscellaneous state information */
79	int		  argc;
80	char		**argv;
81	const char	 *argument;
82	size_t		  gs_width; /* For 'list_item' in read.c */
83	size_t		  u_width; /* for 'list_item' in read.c */
84	uid_t		  user_uid; /* UID running this program */
85	int		  return_value; /* Value returned by main() */
86	char		  warned_lead_slash; /* Already displayed warning */
87	char		  next_line_is_dir; /* Used for -C parsing in -cT */
88
89	/*
90	 * Data for various subsystems.  Full definitions are located in
91	 * the file where they are used.
92	 */
93	struct archive		*diskreader;	/* for write.c */
94	struct archive_entry_linkresolver *resolver; /* for write.c */
95	struct archive_dir	*archive_dir;	/* for write.c */
96	struct name_cache	*gname_cache;	/* for write.c */
97	char			*buff;		/* for write.c */
98	size_t			 buff_size;	/* for write.c */
99	int			 first_fs;	/* for write.c */
100	struct archive		*matching;	/* for matching.c */
101	struct security		*security;	/* for read.c */
102	struct name_cache	*uname_cache;	/* for write.c */
103	struct siginfo_data	*siginfo;	/* for siginfo.c */
104	struct substitution	*substitution;	/* for subst.c */
105	char			*ppbuff;	/* for util.c */
106};
107
108/* Options for flags bitfield */
109#define	OPTFLAG_AUTO_COMPRESS	(0x00000001)	/* -a */
110#define	OPTFLAG_ABSOLUTE_PATHS	(0x00000002)	/* -P */
111#define	OPTFLAG_CHROOT		(0x00000004)	/* --chroot */
112#define	OPTFLAG_FAST_READ	(0x00000008)	/* --fast-read */
113#define	OPTFLAG_IGNORE_ZEROS	(0x00000010)	/* --ignore-zeros */
114#define	OPTFLAG_INTERACTIVE	(0x00000020)	/* -w */
115#define	OPTFLAG_NO_OWNER	(0x00000040)	/* -o */
116#define	OPTFLAG_NO_SUBDIRS	(0x00000080)	/* -n */
117#define	OPTFLAG_NULL		(0x00000100)	/* --null */
118#define	OPTFLAG_NUMERIC_OWNER	(0x00000200)	/* --numeric-owner */
119#define	OPTFLAG_O		(0x00000400)	/* -o */
120#define	OPTFLAG_STDOUT		(0x00000800)	/* -O */
121#define	OPTFLAG_TOTALS		(0x00001000)	/* --totals */
122#define	OPTFLAG_UNLINK_FIRST	(0x00002000)	/* -U */
123#define	OPTFLAG_WARN_LINKS	(0x00004000)	/* --check-links */
124#define	OPTFLAG_NO_XATTRS	(0x00008000)	/* --no-xattrs */
125#define	OPTFLAG_XATTRS		(0x00010000)	/* --xattrs */
126#define	OPTFLAG_NO_ACLS		(0x00020000)	/* --no-acls */
127#define	OPTFLAG_ACLS		(0x00040000)	/* --acls */
128#define	OPTFLAG_NO_FFLAGS	(0x00080000)	/* --no-fflags */
129#define	OPTFLAG_FFLAGS		(0x00100000)	/* --fflags */
130#define	OPTFLAG_NO_MAC_METADATA	(0x00200000)	/* --no-mac-metadata */
131#define	OPTFLAG_MAC_METADATA	(0x00400000)	/* --mac-metadata */
132
133/* Fake short equivalents for long options that otherwise lack them. */
134enum {
135	OPTION_ACLS = 1,
136	OPTION_B64ENCODE,
137	OPTION_CHECK_LINKS,
138	OPTION_CHROOT,
139	OPTION_CLEAR_NOCHANGE_FFLAGS,
140	OPTION_EXCLUDE,
141	OPTION_EXCLUDE_VCS,
142	OPTION_FFLAGS,
143	OPTION_FORMAT,
144	OPTION_GID,
145	OPTION_GNAME,
146	OPTION_GRZIP,
147	OPTION_HELP,
148	OPTION_HFS_COMPRESSION,
149	OPTION_IGNORE_ZEROS,
150	OPTION_INCLUDE,
151	OPTION_KEEP_NEWER_FILES,
152	OPTION_LRZIP,
153	OPTION_LZ4,
154	OPTION_LZIP,
155	OPTION_LZMA,
156	OPTION_LZOP,
157	OPTION_MAC_METADATA,
158	OPTION_NEWER_CTIME,
159	OPTION_NEWER_CTIME_THAN,
160	OPTION_NEWER_MTIME,
161	OPTION_NEWER_MTIME_THAN,
162	OPTION_NODUMP,
163	OPTION_NOPRESERVE_HFS_COMPRESSION,
164	OPTION_NO_ACLS,
165	OPTION_NO_FFLAGS,
166	OPTION_NO_MAC_METADATA,
167	OPTION_NO_SAFE_WRITES,
168	OPTION_NO_SAME_OWNER,
169	OPTION_NO_SAME_PERMISSIONS,
170	OPTION_NO_XATTRS,
171	OPTION_NULL,
172	OPTION_NUMERIC_OWNER,
173	OPTION_OLDER_CTIME,
174	OPTION_OLDER_CTIME_THAN,
175	OPTION_OLDER_MTIME,
176	OPTION_OLDER_MTIME_THAN,
177	OPTION_ONE_FILE_SYSTEM,
178	OPTION_OPTIONS,
179	OPTION_PASSPHRASE,
180	OPTION_POSIX,
181	OPTION_SAFE_WRITES,
182	OPTION_SAME_OWNER,
183	OPTION_STRIP_COMPONENTS,
184	OPTION_TOTALS,
185	OPTION_UID,
186	OPTION_UNAME,
187	OPTION_USE_COMPRESS_PROGRAM,
188	OPTION_UUENCODE,
189	OPTION_VERSION,
190	OPTION_XATTRS,
191	OPTION_ZSTD,
192};
193
194int	bsdtar_getopt(struct bsdtar *);
195void	do_chdir(struct bsdtar *);
196int	edit_pathname(struct bsdtar *, struct archive_entry *);
197int	need_report(void);
198int	pathcmp(const char *a, const char *b);
199void	safe_fprintf(FILE *, const char *fmt, ...) __LA_PRINTF(2, 3);
200void	set_chdir(struct bsdtar *, const char *newdir);
201const char *tar_i64toa(int64_t);
202void	tar_mode_c(struct bsdtar *bsdtar);
203void	tar_mode_r(struct bsdtar *bsdtar);
204void	tar_mode_t(struct bsdtar *bsdtar);
205void	tar_mode_u(struct bsdtar *bsdtar);
206void	tar_mode_x(struct bsdtar *bsdtar);
207void	usage(void) __LA_DEAD;
208int	yes(const char *fmt, ...) __LA_PRINTF(1, 2);
209
210#if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
211void	add_substitution(struct bsdtar *, const char *);
212int	apply_substitution(struct bsdtar *, const char *, char **, int, int);
213void	cleanup_substitution(struct bsdtar *);
214#endif
215
216void		cset_add_filter(struct creation_set *, const char *);
217void		cset_add_filter_program(struct creation_set *, const char *);
218int		cset_auto_compress(struct creation_set *, const char *);
219void		cset_free(struct creation_set *);
220const char *	cset_get_format(struct creation_set *);
221struct creation_set *cset_new(void);
222int		cset_read_support_filter_program(struct creation_set *,
223		    struct archive *);
224void		cset_set_format(struct creation_set *, const char *);
225int		cset_write_add_filters(struct creation_set *,
226		    struct archive *, const void **);
227
228const char * passphrase_callback(struct archive *, void *);
229void	     passphrase_free(char *);
230void	list_item_verbose(struct bsdtar *, FILE *,
231		    struct archive_entry *);
232
233#endif
234