xref: /dragonfly/contrib/libarchive/tar/bsdtar.h (revision 07ed7d32)
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: src/usr.bin/tar/bsdtar.h,v 1.37 2008/12/06 07:37:14 kientzle Exp $
26  */
27 
28 #include "bsdtar_platform.h"
29 #include <stdio.h>
30 
31 #define	DEFAULT_BYTES_PER_BLOCK	(20*512)
32 #define ENV_READER_OPTIONS	"TAR_READER_OPTIONS"
33 #define ENV_WRITER_OPTIONS	"TAR_WRITER_OPTIONS"
34 #define IGNORE_WRONG_MODULE_NAME "__ignore_wrong_module_name__,"
35 
36 struct creation_set;
37 /*
38  * The internal state for the "bsdtar" program.
39  *
40  * Keeping all of the state in a structure like this simplifies memory
41  * leak testing (at exit, anything left on the heap is suspect).  A
42  * pointer to this structure is passed to most bsdtar internal
43  * functions.
44  */
45 struct bsdtar {
46 	/* Options */
47 	const char	 *filename; /* -f filename */
48 	char		 *pending_chdir; /* -C dir */
49 	const char	 *names_from_file; /* -T file */
50 	int		  bytes_per_block; /* -b block_size */
51 	int		  bytes_in_last_block; /* See -b handling. */
52 	int		  verbose;   /* -v */
53 	int		  extract_flags; /* Flags for extract operation */
54 	int		  readdisk_flags; /* Flags for read disk operation */
55 	int		  strip_components; /* Remove this many leading dirs */
56 	int		  gid;  /* --gid */
57 	const char	 *gname; /* --gname */
58 	int		  uid;  /* --uid */
59 	const char	 *uname; /* --uname */
60 	const char	 *passphrase; /* --passphrase */
61 	char		  mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
62 	char		  symlink_mode; /* H or L, per BSD conventions */
63 	char		  option_absolute_paths; /* -P */
64 	char		  option_chroot; /* --chroot */
65 	char		  option_fast_read; /* --fast-read */
66 	const char	 *option_options; /* --options */
67 	char		  option_ignore_zeros; /* --ignore-zeros */
68 	char		  option_interactive; /* -w */
69 	char		  option_no_owner; /* -o */
70 	char		  option_no_subdirs; /* -n */
71 	char		  option_numeric_owner; /* --numeric-owner */
72 	char		  option_null; /* --null */
73 	char		  option_stdout; /* -O */
74 	char		  option_totals; /* --totals */
75 	char		  option_unlink_first; /* -U */
76 	char		  option_warn_links; /* --check-links */
77 	char		  day_first; /* show day before month in -tv output */
78 	struct creation_set *cset;
79 
80 	/* Option parser state */
81 	int		  getopt_state;
82 	char		 *getopt_word;
83 
84 	/* If >= 0, then close this when done. */
85 	int		  fd;
86 
87 	/* Miscellaneous state information */
88 	int		  argc;
89 	char		**argv;
90 	const char	 *argument;
91 	size_t		  gs_width; /* For 'list_item' in read.c */
92 	size_t		  u_width; /* for 'list_item' in read.c */
93 	uid_t		  user_uid; /* UID running this program */
94 	int		  return_value; /* Value returned by main() */
95 	char		  warned_lead_slash; /* Already displayed warning */
96 	char		  next_line_is_dir; /* Used for -C parsing in -cT */
97 
98 	/*
99 	 * Data for various subsystems.  Full definitions are located in
100 	 * the file where they are used.
101 	 */
102 	struct archive		*diskreader;	/* for write.c */
103 	struct archive_entry_linkresolver *resolver; /* for write.c */
104 	struct archive_dir	*archive_dir;	/* for write.c */
105 	struct name_cache	*gname_cache;	/* for write.c */
106 	char			*buff;		/* for write.c */
107 	size_t			 buff_size;	/* for write.c */
108 	int			 first_fs;	/* for write.c */
109 	struct archive		*matching;	/* for matching.c */
110 	struct security		*security;	/* for read.c */
111 	struct name_cache	*uname_cache;	/* for write.c */
112 	struct siginfo_data	*siginfo;	/* for siginfo.c */
113 	struct substitution	*substitution;	/* for subst.c */
114 	char			*ppbuff;	/* for util.c */
115 };
116 
117 /* Fake short equivalents for long options that otherwise lack them. */
118 enum {
119 	OPTION_B64ENCODE = 1,
120 	OPTION_CHECK_LINKS,
121 	OPTION_CHROOT,
122 	OPTION_CLEAR_NOCHANGE_FFLAGS,
123 	OPTION_DISABLE_COPYFILE,
124 	OPTION_EXCLUDE,
125 	OPTION_FORMAT,
126 	OPTION_GID,
127 	OPTION_GNAME,
128 	OPTION_GRZIP,
129 	OPTION_HELP,
130 	OPTION_HFS_COMPRESSION,
131 	OPTION_IGNORE_ZEROS,
132 	OPTION_INCLUDE,
133 	OPTION_KEEP_NEWER_FILES,
134 	OPTION_LRZIP,
135 	OPTION_LZ4,
136 	OPTION_LZIP,
137 	OPTION_LZMA,
138 	OPTION_LZOP,
139 	OPTION_NEWER_CTIME,
140 	OPTION_NEWER_CTIME_THAN,
141 	OPTION_NEWER_MTIME,
142 	OPTION_NEWER_MTIME_THAN,
143 	OPTION_NODUMP,
144 	OPTION_NOPRESERVE_HFS_COMPRESSION,
145 	OPTION_NO_SAME_OWNER,
146 	OPTION_NO_SAME_PERMISSIONS,
147 	OPTION_NO_XATTR,
148 	OPTION_NULL,
149 	OPTION_NUMERIC_OWNER,
150 	OPTION_OLDER_CTIME,
151 	OPTION_OLDER_CTIME_THAN,
152 	OPTION_OLDER_MTIME,
153 	OPTION_OLDER_MTIME_THAN,
154 	OPTION_ONE_FILE_SYSTEM,
155 	OPTION_OPTIONS,
156 	OPTION_PASSPHRASE,
157 	OPTION_POSIX,
158 	OPTION_SAME_OWNER,
159 	OPTION_STRIP_COMPONENTS,
160 	OPTION_TOTALS,
161 	OPTION_UID,
162 	OPTION_UNAME,
163 	OPTION_USE_COMPRESS_PROGRAM,
164 	OPTION_UUENCODE,
165 	OPTION_VERSION
166 };
167 
168 int	bsdtar_getopt(struct bsdtar *);
169 void	do_chdir(struct bsdtar *);
170 int	edit_pathname(struct bsdtar *, struct archive_entry *);
171 int	need_report(void);
172 int	pathcmp(const char *a, const char *b);
173 void	safe_fprintf(FILE *, const char *fmt, ...);
174 void	set_chdir(struct bsdtar *, const char *newdir);
175 const char *tar_i64toa(int64_t);
176 void	tar_mode_c(struct bsdtar *bsdtar);
177 void	tar_mode_r(struct bsdtar *bsdtar);
178 void	tar_mode_t(struct bsdtar *bsdtar);
179 void	tar_mode_u(struct bsdtar *bsdtar);
180 void	tar_mode_x(struct bsdtar *bsdtar);
181 void	usage(void);
182 int	yes(const char *fmt, ...);
183 
184 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
185 void	add_substitution(struct bsdtar *, const char *);
186 int	apply_substitution(struct bsdtar *, const char *, char **, int, int);
187 void	cleanup_substitution(struct bsdtar *);
188 #endif
189 
190 void		cset_add_filter(struct creation_set *, const char *);
191 void		cset_add_filter_program(struct creation_set *, const char *);
192 int		cset_auto_compress(struct creation_set *, const char *);
193 void		cset_free(struct creation_set *);
194 const char *	cset_get_format(struct creation_set *);
195 struct creation_set *cset_new(void);
196 int		cset_read_support_filter_program(struct creation_set *,
197 		    struct archive *);
198 void		cset_set_format(struct creation_set *, const char *);
199 int		cset_write_add_filters(struct creation_set *,
200 		    struct archive *, const void **);
201 
202 const char * passphrase_callback(struct archive *, void *);
203 void	     passphrase_free(char *);
204 void	list_item_verbose(struct bsdtar *, FILE *,
205 		    struct archive_entry *);
206