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$
26228753Smm */
27228753Smm
28228753Smm#include "bsdtar_platform.h"
29228753Smm#include <stdio.h>
30228753Smm
31228753Smm#define	DEFAULT_BYTES_PER_BLOCK	(20*512)
32248616Smm#define ENV_READER_OPTIONS	"TAR_READER_OPTIONS"
33248616Smm#define ENV_WRITER_OPTIONS	"TAR_WRITER_OPTIONS"
34248616Smm#define IGNORE_WRONG_MODULE_NAME "__ignore_wrong_module_name__,"
35228753Smm
36248616Smmstruct creation_set;
37228753Smm/*
38228753Smm * The internal state for the "bsdtar" program.
39228753Smm *
40228753Smm * Keeping all of the state in a structure like this simplifies memory
41228753Smm * leak testing (at exit, anything left on the heap is suspect).  A
42228753Smm * pointer to this structure is passed to most bsdtar internal
43228753Smm * functions.
44228753Smm */
45228753Smmstruct bsdtar {
46228753Smm	/* Options */
47228753Smm	const char	 *filename; /* -f filename */
48228753Smm	char		 *pending_chdir; /* -C dir */
49228753Smm	const char	 *names_from_file; /* -T file */
50228753Smm	int		  bytes_per_block; /* -b block_size */
51232153Smm	int		  bytes_in_last_block; /* See -b handling. */
52228753Smm	int		  verbose;   /* -v */
53228753Smm	int		  extract_flags; /* Flags for extract operation */
54238856Smm	int		  readdisk_flags; /* Flags for read disk operation */
55228753Smm	int		  strip_components; /* Remove this many leading dirs */
56228753Smm	int		  gid;  /* --gid */
57228753Smm	const char	 *gname; /* --gname */
58228753Smm	int		  uid;  /* --uid */
59228753Smm	const char	 *uname; /* --uname */
60228753Smm	char		  mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
61228753Smm	char		  symlink_mode; /* H or L, per BSD conventions */
62228753Smm	char		  option_absolute_paths; /* -P */
63228753Smm	char		  option_chroot; /* --chroot */
64228753Smm	char		  option_fast_read; /* --fast-read */
65228753Smm	const char	 *option_options; /* --options */
66228753Smm	char		  option_interactive; /* -w */
67228753Smm	char		  option_no_owner; /* -o */
68228753Smm	char		  option_no_subdirs; /* -n */
69232153Smm	char		  option_numeric_owner; /* --numeric-owner */
70228753Smm	char		  option_null; /* --null */
71228753Smm	char		  option_stdout; /* -O */
72228753Smm	char		  option_totals; /* --totals */
73228753Smm	char		  option_unlink_first; /* -U */
74228753Smm	char		  option_warn_links; /* --check-links */
75228753Smm	char		  day_first; /* show day before month in -tv output */
76248616Smm	struct creation_set *cset;
77228753Smm
78232153Smm	/* Option parser state */
79232153Smm	int		  getopt_state;
80232153Smm	char		 *getopt_word;
81232153Smm
82228753Smm	/* If >= 0, then close this when done. */
83228753Smm	int		  fd;
84228753Smm
85228753Smm	/* Miscellaneous state information */
86228753Smm	int		  argc;
87228753Smm	char		**argv;
88232153Smm	const char	 *argument;
89228753Smm	size_t		  gs_width; /* For 'list_item' in read.c */
90228753Smm	size_t		  u_width; /* for 'list_item' in read.c */
91228753Smm	uid_t		  user_uid; /* UID running this program */
92228753Smm	int		  return_value; /* Value returned by main() */
93228753Smm	char		  warned_lead_slash; /* Already displayed warning */
94228753Smm	char		  next_line_is_dir; /* Used for -C parsing in -cT */
95228753Smm
96228753Smm	/*
97228753Smm	 * Data for various subsystems.  Full definitions are located in
98228753Smm	 * the file where they are used.
99228753Smm	 */
100228753Smm	struct archive		*diskreader;	/* for write.c */
101228753Smm	struct archive_entry_linkresolver *resolver; /* for write.c */
102228753Smm	struct archive_dir	*archive_dir;	/* for write.c */
103228753Smm	struct name_cache	*gname_cache;	/* for write.c */
104228753Smm	char			*buff;		/* for write.c */
105232153Smm	size_t			 buff_size;	/* for write.c */
106238856Smm	int			 first_fs;	/* for write.c */
107238856Smm	struct archive		*matching;	/* for matching.c */
108228753Smm	struct security		*security;	/* for read.c */
109228753Smm	struct name_cache	*uname_cache;	/* for write.c */
110228753Smm	struct siginfo_data	*siginfo;	/* for siginfo.c */
111228753Smm	struct substitution	*substitution;	/* for subst.c */
112228753Smm};
113228753Smm
114228753Smm/* Fake short equivalents for long options that otherwise lack them. */
115228753Smmenum {
116248616Smm	OPTION_B64ENCODE = 1,
117248616Smm	OPTION_CHECK_LINKS,
118228753Smm	OPTION_CHROOT,
119232153Smm	OPTION_DISABLE_COPYFILE,
120228753Smm	OPTION_EXCLUDE,
121228753Smm	OPTION_FORMAT,
122228753Smm	OPTION_GID,
123228753Smm	OPTION_GNAME,
124248616Smm	OPTION_GRZIP,
125228753Smm	OPTION_HELP,
126248616Smm	OPTION_HFS_COMPRESSION,
127228753Smm	OPTION_INCLUDE,
128228753Smm	OPTION_KEEP_NEWER_FILES,
129248616Smm	OPTION_LRZIP,
130232153Smm	OPTION_LZIP,
131228753Smm	OPTION_LZMA,
132248616Smm	OPTION_LZOP,
133228753Smm	OPTION_NEWER_CTIME,
134228753Smm	OPTION_NEWER_CTIME_THAN,
135228753Smm	OPTION_NEWER_MTIME,
136228753Smm	OPTION_NEWER_MTIME_THAN,
137228753Smm	OPTION_NODUMP,
138248616Smm	OPTION_NOPRESERVE_HFS_COMPRESSION,
139228753Smm	OPTION_NO_SAME_OWNER,
140228753Smm	OPTION_NO_SAME_PERMISSIONS,
141228753Smm	OPTION_NULL,
142228753Smm	OPTION_NUMERIC_OWNER,
143248616Smm	OPTION_OLDER_CTIME,
144248616Smm	OPTION_OLDER_CTIME_THAN,
145248616Smm	OPTION_OLDER_MTIME,
146248616Smm	OPTION_OLDER_MTIME_THAN,
147228753Smm	OPTION_ONE_FILE_SYSTEM,
148228753Smm	OPTION_OPTIONS,
149228753Smm	OPTION_POSIX,
150228753Smm	OPTION_SAME_OWNER,
151228753Smm	OPTION_STRIP_COMPONENTS,
152228753Smm	OPTION_TOTALS,
153228753Smm	OPTION_UID,
154228753Smm	OPTION_UNAME,
155228753Smm	OPTION_USE_COMPRESS_PROGRAM,
156248616Smm	OPTION_UUENCODE,
157228753Smm	OPTION_VERSION
158228753Smm};
159228753Smm
160228753Smmint	bsdtar_getopt(struct bsdtar *);
161228753Smmvoid	do_chdir(struct bsdtar *);
162228753Smmint	edit_pathname(struct bsdtar *, struct archive_entry *);
163228753Smmint	need_report(void);
164228753Smmint	pathcmp(const char *a, const char *b);
165228753Smmvoid	safe_fprintf(FILE *, const char *fmt, ...);
166228753Smmvoid	set_chdir(struct bsdtar *, const char *newdir);
167228753Smmconst char *tar_i64toa(int64_t);
168228753Smmvoid	tar_mode_c(struct bsdtar *bsdtar);
169228753Smmvoid	tar_mode_r(struct bsdtar *bsdtar);
170228753Smmvoid	tar_mode_t(struct bsdtar *bsdtar);
171228753Smmvoid	tar_mode_u(struct bsdtar *bsdtar);
172228753Smmvoid	tar_mode_x(struct bsdtar *bsdtar);
173228753Smmvoid	usage(void);
174228753Smmint	yes(const char *fmt, ...);
175228753Smm
176248616Smm#if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H)
177228753Smmvoid	add_substitution(struct bsdtar *, const char *);
178232153Smmint	apply_substitution(struct bsdtar *, const char *, char **, int, int);
179228753Smmvoid	cleanup_substitution(struct bsdtar *);
180228753Smm#endif
181248616Smm
182248616Smmvoid		cset_add_filter(struct creation_set *, const char *);
183248616Smmvoid		cset_add_filter_program(struct creation_set *, const char *);
184248616Smmint		cset_auto_compress(struct creation_set *, const char *);
185248616Smmvoid		cset_free(struct creation_set *);
186248616Smmconst char *	cset_get_format(struct creation_set *);
187248616Smmstruct creation_set *cset_new(void);
188248616Smmint		cset_read_support_filter_program(struct creation_set *,
189248616Smm		    struct archive *);
190248616Smmvoid		cset_set_format(struct creation_set *, const char *);
191248616Smmint		cset_write_add_filters(struct creation_set *,
192248616Smm		    struct archive *, const void **);
193248616Smm
194