1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm *
25228763Smm * $FreeBSD: stable/11/contrib/libarchive/tar/bsdtar.h 358088 2020-02-19 01:50:47Z mm $
26228753Smm */
27228753Smm
28358088Smm#ifndef BSDTAR_H_INCLUDED
29358088Smm#define BSDTAR_H_INCLUDED
30358088Smm
31228753Smm#include "bsdtar_platform.h"
32228753Smm#include <stdio.h>
33228753Smm
34228753Smm#define	DEFAULT_BYTES_PER_BLOCK	(20*512)
35248616Smm#define ENV_READER_OPTIONS	"TAR_READER_OPTIONS"
36248616Smm#define ENV_WRITER_OPTIONS	"TAR_WRITER_OPTIONS"
37248616Smm#define IGNORE_WRONG_MODULE_NAME "__ignore_wrong_module_name__,"
38228753Smm
39248616Smmstruct creation_set;
40228753Smm/*
41228753Smm * The internal state for the "bsdtar" program.
42228753Smm *
43228753Smm * Keeping all of the state in a structure like this simplifies memory
44228753Smm * leak testing (at exit, anything left on the heap is suspect).  A
45228753Smm * pointer to this structure is passed to most bsdtar internal
46228753Smm * functions.
47228753Smm */
48228753Smmstruct bsdtar {
49228753Smm	/* Options */
50228753Smm	const char	 *filename; /* -f filename */
51228753Smm	char		 *pending_chdir; /* -C dir */
52228753Smm	const char	 *names_from_file; /* -T file */
53228753Smm	int		  bytes_per_block; /* -b block_size */
54232153Smm	int		  bytes_in_last_block; /* See -b handling. */
55228753Smm	int		  verbose;   /* -v */
56315432Smm	unsigned int	  flags; /* Bitfield of boolean options */
57228753Smm	int		  extract_flags; /* Flags for extract operation */
58238856Smm	int		  readdisk_flags; /* Flags for read disk operation */
59228753Smm	int		  strip_components; /* Remove this many leading dirs */
60228753Smm	int		  gid;  /* --gid */
61228753Smm	const char	 *gname; /* --gname */
62228753Smm	int		  uid;  /* --uid */
63228753Smm	const char	 *uname; /* --uname */
64299529Smm	const char	 *passphrase; /* --passphrase */
65228753Smm	char		  mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
66228753Smm	char		  symlink_mode; /* H or L, per BSD conventions */
67228753Smm	const char	 *option_options; /* --options */
68228753Smm	char		  day_first; /* show day before month in -tv output */
69248616Smm	struct creation_set *cset;
70228753Smm
71232153Smm	/* Option parser state */
72232153Smm	int		  getopt_state;
73232153Smm	char		 *getopt_word;
74232153Smm
75228753Smm	/* If >= 0, then close this when done. */
76228753Smm	int		  fd;
77228753Smm
78228753Smm	/* Miscellaneous state information */
79228753Smm	int		  argc;
80228753Smm	char		**argv;
81232153Smm	const char	 *argument;
82228753Smm	size_t		  gs_width; /* For 'list_item' in read.c */
83228753Smm	size_t		  u_width; /* for 'list_item' in read.c */
84228753Smm	uid_t		  user_uid; /* UID running this program */
85228753Smm	int		  return_value; /* Value returned by main() */
86228753Smm	char		  warned_lead_slash; /* Already displayed warning */
87228753Smm	char		  next_line_is_dir; /* Used for -C parsing in -cT */
88228753Smm
89228753Smm	/*
90228753Smm	 * Data for various subsystems.  Full definitions are located in
91228753Smm	 * the file where they are used.
92228753Smm	 */
93228753Smm	struct archive		*diskreader;	/* for write.c */
94228753Smm	struct archive_entry_linkresolver *resolver; /* for write.c */
95228753Smm	struct archive_dir	*archive_dir;	/* for write.c */
96228753Smm	struct name_cache	*gname_cache;	/* for write.c */
97228753Smm	char			*buff;		/* for write.c */
98232153Smm	size_t			 buff_size;	/* for write.c */
99238856Smm	int			 first_fs;	/* for write.c */
100238856Smm	struct archive		*matching;	/* for matching.c */
101228753Smm	struct security		*security;	/* for read.c */
102228753Smm	struct name_cache	*uname_cache;	/* for write.c */
103228753Smm	struct siginfo_data	*siginfo;	/* for siginfo.c */
104228753Smm	struct substitution	*substitution;	/* for subst.c */
105299529Smm	char			*ppbuff;	/* for util.c */
106228753Smm};
107228753Smm
108315432Smm/* Options for flags bitfield */
109315432Smm#define	OPTFLAG_AUTO_COMPRESS	(0x00000001)	/* -a */
110315432Smm#define	OPTFLAG_ABSOLUTE_PATHS	(0x00000002)	/* -P */
111315432Smm#define	OPTFLAG_CHROOT		(0x00000004)	/* --chroot */
112315432Smm#define	OPTFLAG_FAST_READ	(0x00000008)	/* --fast-read */
113315432Smm#define	OPTFLAG_IGNORE_ZEROS	(0x00000010)	/* --ignore-zeros */
114315432Smm#define	OPTFLAG_INTERACTIVE	(0x00000020)	/* -w */
115315432Smm#define	OPTFLAG_NO_OWNER	(0x00000040)	/* -o */
116315432Smm#define	OPTFLAG_NO_SUBDIRS	(0x00000080)	/* -n */
117315432Smm#define	OPTFLAG_NULL		(0x00000100)	/* --null */
118315432Smm#define	OPTFLAG_NUMERIC_OWNER	(0x00000200)	/* --numeric-owner */
119315432Smm#define	OPTFLAG_O		(0x00000400)	/* -o */
120315432Smm#define	OPTFLAG_STDOUT		(0x00000800)	/* -O */
121315432Smm#define	OPTFLAG_TOTALS		(0x00001000)	/* --totals */
122315432Smm#define	OPTFLAG_UNLINK_FIRST	(0x00002000)	/* -U */
123315432Smm#define	OPTFLAG_WARN_LINKS	(0x00004000)	/* --check-links */
124315432Smm#define	OPTFLAG_NO_XATTRS	(0x00008000)	/* --no-xattrs */
125315432Smm#define	OPTFLAG_XATTRS		(0x00010000)	/* --xattrs */
126315432Smm#define	OPTFLAG_NO_ACLS		(0x00020000)	/* --no-acls */
127315432Smm#define	OPTFLAG_ACLS		(0x00040000)	/* --acls */
128315432Smm#define	OPTFLAG_NO_FFLAGS	(0x00080000)	/* --no-fflags */
129315432Smm#define	OPTFLAG_FFLAGS		(0x00100000)	/* --fflags */
130315432Smm#define	OPTFLAG_NO_MAC_METADATA	(0x00200000)	/* --no-mac-metadata */
131315432Smm#define	OPTFLAG_MAC_METADATA	(0x00400000)	/* --mac-metadata */
132315432Smm
133228753Smm/* Fake short equivalents for long options that otherwise lack them. */
134228753Smmenum {
135315432Smm	OPTION_ACLS = 1,
136315432Smm	OPTION_B64ENCODE,
137248616Smm	OPTION_CHECK_LINKS,
138228753Smm	OPTION_CHROOT,
139299529Smm	OPTION_CLEAR_NOCHANGE_FFLAGS,
140228753Smm	OPTION_EXCLUDE,
141348607Smm	OPTION_EXCLUDE_VCS,
142315432Smm	OPTION_FFLAGS,
143228753Smm	OPTION_FORMAT,
144228753Smm	OPTION_GID,
145228753Smm	OPTION_GNAME,
146248616Smm	OPTION_GRZIP,
147228753Smm	OPTION_HELP,
148248616Smm	OPTION_HFS_COMPRESSION,
149299529Smm	OPTION_IGNORE_ZEROS,
150228753Smm	OPTION_INCLUDE,
151228753Smm	OPTION_KEEP_NEWER_FILES,
152248616Smm	OPTION_LRZIP,
153299529Smm	OPTION_LZ4,
154232153Smm	OPTION_LZIP,
155228753Smm	OPTION_LZMA,
156248616Smm	OPTION_LZOP,
157315432Smm	OPTION_MAC_METADATA,
158228753Smm	OPTION_NEWER_CTIME,
159228753Smm	OPTION_NEWER_CTIME_THAN,
160228753Smm	OPTION_NEWER_MTIME,
161228753Smm	OPTION_NEWER_MTIME_THAN,
162228753Smm	OPTION_NODUMP,
163248616Smm	OPTION_NOPRESERVE_HFS_COMPRESSION,
164315432Smm	OPTION_NO_ACLS,
165315432Smm	OPTION_NO_FFLAGS,
166315432Smm	OPTION_NO_MAC_METADATA,
167358088Smm	OPTION_NO_SAFE_WRITES,
168228753Smm	OPTION_NO_SAME_OWNER,
169228753Smm	OPTION_NO_SAME_PERMISSIONS,
170315432Smm	OPTION_NO_XATTRS,
171228753Smm	OPTION_NULL,
172228753Smm	OPTION_NUMERIC_OWNER,
173248616Smm	OPTION_OLDER_CTIME,
174248616Smm	OPTION_OLDER_CTIME_THAN,
175248616Smm	OPTION_OLDER_MTIME,
176248616Smm	OPTION_OLDER_MTIME_THAN,
177228753Smm	OPTION_ONE_FILE_SYSTEM,
178228753Smm	OPTION_OPTIONS,
179299529Smm	OPTION_PASSPHRASE,
180228753Smm	OPTION_POSIX,
181358088Smm	OPTION_SAFE_WRITES,
182228753Smm	OPTION_SAME_OWNER,
183228753Smm	OPTION_STRIP_COMPONENTS,
184228753Smm	OPTION_TOTALS,
185228753Smm	OPTION_UID,
186228753Smm	OPTION_UNAME,
187228753Smm	OPTION_USE_COMPRESS_PROGRAM,
188248616Smm	OPTION_UUENCODE,
189315432Smm	OPTION_VERSION,
190324417Smm	OPTION_XATTRS,
191324417Smm	OPTION_ZSTD,
192228753Smm};
193228753Smm
194228753Smmint	bsdtar_getopt(struct bsdtar *);
195228753Smmvoid	do_chdir(struct bsdtar *);
196228753Smmint	edit_pathname(struct bsdtar *, struct archive_entry *);
197228753Smmint	need_report(void);
198228753Smmint	pathcmp(const char *a, const char *b);
199315432Smmvoid	safe_fprintf(FILE *, const char *fmt, ...) __LA_PRINTF(2, 3);
200228753Smmvoid	set_chdir(struct bsdtar *, const char *newdir);
201228753Smmconst char *tar_i64toa(int64_t);
202228753Smmvoid	tar_mode_c(struct bsdtar *bsdtar);
203228753Smmvoid	tar_mode_r(struct bsdtar *bsdtar);
204228753Smmvoid	tar_mode_t(struct bsdtar *bsdtar);
205228753Smmvoid	tar_mode_u(struct bsdtar *bsdtar);
206228753Smmvoid	tar_mode_x(struct bsdtar *bsdtar);
207315432Smmvoid	usage(void) __LA_DEAD;
208315432Smmint	yes(const char *fmt, ...) __LA_PRINTF(1, 2);
209228753Smm
210248616Smm#if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
211228753Smmvoid	add_substitution(struct bsdtar *, const char *);
212232153Smmint	apply_substitution(struct bsdtar *, const char *, char **, int, int);
213228753Smmvoid	cleanup_substitution(struct bsdtar *);
214228753Smm#endif
215248616Smm
216248616Smmvoid		cset_add_filter(struct creation_set *, const char *);
217248616Smmvoid		cset_add_filter_program(struct creation_set *, const char *);
218248616Smmint		cset_auto_compress(struct creation_set *, const char *);
219248616Smmvoid		cset_free(struct creation_set *);
220248616Smmconst char *	cset_get_format(struct creation_set *);
221248616Smmstruct creation_set *cset_new(void);
222248616Smmint		cset_read_support_filter_program(struct creation_set *,
223248616Smm		    struct archive *);
224248616Smmvoid		cset_set_format(struct creation_set *, const char *);
225248616Smmint		cset_write_add_filters(struct creation_set *,
226248616Smm		    struct archive *, const void **);
227248616Smm
228299529Smmconst char * passphrase_callback(struct archive *, void *);
229299529Smmvoid	     passphrase_free(char *);
230299529Smmvoid	list_item_verbose(struct bsdtar *, FILE *,
231299529Smm		    struct archive_entry *);
232358088Smm
233358088Smm#endif
234