xref: /dragonfly/contrib/libarchive/tar/bsdtar.h (revision 650094e1)
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 #include "matching.h"
32 
33 #define	DEFAULT_BYTES_PER_BLOCK	(20*512)
34 
35 /*
36  * The internal state for the "bsdtar" program.
37  *
38  * Keeping all of the state in a structure like this simplifies memory
39  * leak testing (at exit, anything left on the heap is suspect).  A
40  * pointer to this structure is passed to most bsdtar internal
41  * functions.
42  */
43 struct bsdtar {
44 	/* Options */
45 	const char	 *filename; /* -f filename */
46 	const char	 *create_format; /* -F format */
47 	char		 *pending_chdir; /* -C dir */
48 	const char	 *names_from_file; /* -T file */
49 	int		  newer_ctime_filter; /* --newer/--newer-than */
50 	time_t		  newer_ctime_sec; /* --newer/--newer-than */
51 	long		  newer_ctime_nsec; /* --newer/--newer-than */
52 	int		  newer_mtime_filter; /* --newer-mtime/--newer-mtime-than */
53 	time_t		  newer_mtime_sec; /* --newer-mtime */
54 	long		  newer_mtime_nsec; /* --newer-mtime-than */
55 	int		  bytes_per_block; /* -b block_size */
56 	int		  bytes_in_last_block; /* See -b handling. */
57 	int		  verbose;   /* -v */
58 	int		  extract_flags; /* Flags for extract 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 	char		  mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
65 	char		  symlink_mode; /* H or L, per BSD conventions */
66 	char		  create_compression; /* j, y, or z */
67 	const char	 *compress_program;
68 	char		  option_absolute_paths; /* -P */
69 	char		  option_chroot; /* --chroot */
70 	char		  option_dont_traverse_mounts; /* --one-file-system */
71 	char		  option_fast_read; /* --fast-read */
72 	const char	 *option_options; /* --options */
73 	char		  option_honor_nodump; /* --nodump */
74 	char		  option_interactive; /* -w */
75 	char		  option_no_owner; /* -o */
76 	char		  option_no_subdirs; /* -n */
77 	char		  option_numeric_owner; /* --numeric-owner */
78 	char		  option_null; /* --null */
79 	char		  option_stdout; /* -O */
80 	char		  option_totals; /* --totals */
81 	char		  option_unlink_first; /* -U */
82 	char		  option_warn_links; /* --check-links */
83 	char		  day_first; /* show day before month in -tv output */
84 	char		  enable_copyfile; /* For Mac OS */
85 
86 	/* Option parser state */
87 	int		  getopt_state;
88 	char		 *getopt_word;
89 
90 	/* If >= 0, then close this when done. */
91 	int		  fd;
92 
93 	/* Miscellaneous state information */
94 	int		  argc;
95 	char		**argv;
96 	const char	 *argument;
97 	size_t		  gs_width; /* For 'list_item' in read.c */
98 	size_t		  u_width; /* for 'list_item' in read.c */
99 	uid_t		  user_uid; /* UID running this program */
100 	int		  return_value; /* Value returned by main() */
101 	char		  warned_lead_slash; /* Already displayed warning */
102 	char		  next_line_is_dir; /* Used for -C parsing in -cT */
103 
104 	/*
105 	 * Data for various subsystems.  Full definitions are located in
106 	 * the file where they are used.
107 	 */
108 	struct archive		*diskreader;	/* for write.c */
109 	struct archive_entry_linkresolver *resolver; /* for write.c */
110 	struct archive_dir	*archive_dir;	/* for write.c */
111 	struct name_cache	*gname_cache;	/* for write.c */
112 	char			*buff;		/* for write.c */
113 	size_t			 buff_size;	/* for write.c */
114 	struct lafe_matching	*matching;	/* for matching.c */
115 	struct security		*security;	/* for read.c */
116 	struct name_cache	*uname_cache;	/* for write.c */
117 	struct siginfo_data	*siginfo;	/* for siginfo.c */
118 	struct substitution	*substitution;	/* for subst.c */
119 };
120 
121 /* Fake short equivalents for long options that otherwise lack them. */
122 enum {
123 	OPTION_CHECK_LINKS = 1,
124 	OPTION_CHROOT,
125 	OPTION_DISABLE_COPYFILE,
126 	OPTION_EXCLUDE,
127 	OPTION_FORMAT,
128 	OPTION_GID,
129 	OPTION_GNAME,
130 	OPTION_HELP,
131 	OPTION_INCLUDE,
132 	OPTION_KEEP_NEWER_FILES,
133 	OPTION_LZIP,
134 	OPTION_LZMA,
135 	OPTION_NEWER_CTIME,
136 	OPTION_NEWER_CTIME_THAN,
137 	OPTION_NEWER_MTIME,
138 	OPTION_NEWER_MTIME_THAN,
139 	OPTION_NODUMP,
140 	OPTION_NO_SAME_OWNER,
141 	OPTION_NO_SAME_PERMISSIONS,
142 	OPTION_NULL,
143 	OPTION_NUMERIC_OWNER,
144 	OPTION_ONE_FILE_SYSTEM,
145 	OPTION_OPTIONS,
146 	OPTION_POSIX,
147 	OPTION_SAME_OWNER,
148 	OPTION_STRIP_COMPONENTS,
149 	OPTION_TOTALS,
150 	OPTION_UID,
151 	OPTION_UNAME,
152 	OPTION_USE_COMPRESS_PROGRAM,
153 	OPTION_VERSION
154 };
155 
156 int	bsdtar_getopt(struct bsdtar *);
157 void	do_chdir(struct bsdtar *);
158 int	edit_pathname(struct bsdtar *, struct archive_entry *);
159 int	need_report(void);
160 int	pathcmp(const char *a, const char *b);
161 void	safe_fprintf(FILE *, const char *fmt, ...);
162 void	set_chdir(struct bsdtar *, const char *newdir);
163 const char *tar_i64toa(int64_t);
164 void	tar_mode_c(struct bsdtar *bsdtar);
165 void	tar_mode_r(struct bsdtar *bsdtar);
166 void	tar_mode_t(struct bsdtar *bsdtar);
167 void	tar_mode_u(struct bsdtar *bsdtar);
168 void	tar_mode_x(struct bsdtar *bsdtar);
169 void	usage(void);
170 int	yes(const char *fmt, ...);
171 
172 #if HAVE_REGEX_H
173 void	add_substitution(struct bsdtar *, const char *);
174 int	apply_substitution(struct bsdtar *, const char *, char **, int, int);
175 void	cleanup_substitution(struct bsdtar *);
176 #endif
177