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 *
25229592Smm * $FreeBSD$
26228753Smm */
27228753Smm
28228753Smm#include "bsdtar_platform.h"
29228753Smm#include <stdio.h>
30228753Smm
31228753Smm#include "matching.h"
32228753Smm
33228753Smm#define	DEFAULT_BYTES_PER_BLOCK	(20*512)
34228753Smm
35228753Smm/*
36228753Smm * The internal state for the "bsdtar" program.
37228753Smm *
38228753Smm * Keeping all of the state in a structure like this simplifies memory
39228753Smm * leak testing (at exit, anything left on the heap is suspect).  A
40228753Smm * pointer to this structure is passed to most bsdtar internal
41228753Smm * functions.
42228753Smm */
43228753Smmstruct bsdtar {
44228753Smm	/* Options */
45228753Smm	const char	 *filename; /* -f filename */
46228753Smm	const char	 *create_format; /* -F format */
47228753Smm	char		 *pending_chdir; /* -C dir */
48228753Smm	const char	 *names_from_file; /* -T file */
49228753Smm	time_t		  newer_ctime_sec; /* --newer/--newer-than */
50228753Smm	long		  newer_ctime_nsec; /* --newer/--newer-than */
51228753Smm	time_t		  newer_mtime_sec; /* --newer-mtime */
52228753Smm	long		  newer_mtime_nsec; /* --newer-mtime-than */
53228753Smm	int		  bytes_per_block; /* -b block_size */
54228753Smm	int		  verbose;   /* -v */
55228753Smm	int		  extract_flags; /* Flags for extract operation */
56228753Smm	int		  strip_components; /* Remove this many leading dirs */
57228753Smm	int		  gid;  /* --gid */
58228753Smm	const char	 *gname; /* --gname */
59228753Smm	int		  uid;  /* --uid */
60228753Smm	const char	 *uname; /* --uname */
61228753Smm	char		  mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */
62228753Smm	char		  symlink_mode; /* H or L, per BSD conventions */
63228753Smm	char		  create_compression; /* j, y, or z */
64228753Smm	const char	 *compress_program;
65228753Smm	char		  option_absolute_paths; /* -P */
66228753Smm	char		  option_chroot; /* --chroot */
67228753Smm	char		  option_dont_traverse_mounts; /* --one-file-system */
68228753Smm	char		  option_fast_read; /* --fast-read */
69228753Smm	const char	 *option_options; /* --options */
70228753Smm	char		  option_honor_nodump; /* --nodump */
71228753Smm	char		  option_interactive; /* -w */
72228753Smm	char		  option_no_owner; /* -o */
73228753Smm	char		  option_no_subdirs; /* -n */
74228753Smm	char		  option_null; /* --null */
75228753Smm	char		  option_stdout; /* -O */
76228753Smm	char		  option_totals; /* --totals */
77228753Smm	char		  option_unlink_first; /* -U */
78228753Smm	char		  option_warn_links; /* --check-links */
79228753Smm	char		  day_first; /* show day before month in -tv output */
80228753Smm
81228753Smm	/* If >= 0, then close this when done. */
82228753Smm	int		  fd;
83228753Smm
84228753Smm	/* Miscellaneous state information */
85228753Smm	int		  argc;
86228753Smm	char		**argv;
87228753Smm	const char	 *optarg;
88228753Smm	size_t		  gs_width; /* For 'list_item' in read.c */
89228753Smm	size_t		  u_width; /* for 'list_item' in read.c */
90228753Smm	uid_t		  user_uid; /* UID running this program */
91228753Smm	int		  return_value; /* Value returned by main() */
92228753Smm	char		  warned_lead_slash; /* Already displayed warning */
93228753Smm	char		  next_line_is_dir; /* Used for -C parsing in -cT */
94228753Smm
95228753Smm	/*
96228753Smm	 * Data for various subsystems.  Full definitions are located in
97228753Smm	 * the file where they are used.
98228753Smm	 */
99228753Smm	struct archive		*diskreader;	/* for write.c */
100228753Smm	struct archive_entry_linkresolver *resolver; /* for write.c */
101228753Smm	struct archive_dir	*archive_dir;	/* for write.c */
102228753Smm	struct name_cache	*gname_cache;	/* for write.c */
103228753Smm	char			*buff;		/* for write.c */
104228753Smm	struct lafe_matching	*matching;	/* for matching.c */
105228753Smm	struct security		*security;	/* for read.c */
106228753Smm	struct name_cache	*uname_cache;	/* for write.c */
107228753Smm	struct siginfo_data	*siginfo;	/* for siginfo.c */
108228753Smm	struct substitution	*substitution;	/* for subst.c */
109228753Smm};
110228753Smm
111228753Smm/* Fake short equivalents for long options that otherwise lack them. */
112228753Smmenum {
113228753Smm	OPTION_CHECK_LINKS = 1,
114228753Smm	OPTION_CHROOT,
115228753Smm	OPTION_EXCLUDE,
116228753Smm	OPTION_FORMAT,
117228753Smm	OPTION_GID,
118228753Smm	OPTION_GNAME,
119228753Smm	OPTION_HELP,
120228753Smm	OPTION_INCLUDE,
121228753Smm	OPTION_KEEP_NEWER_FILES,
122228753Smm	OPTION_LZMA,
123228753Smm	OPTION_NEWER_CTIME,
124228753Smm	OPTION_NEWER_CTIME_THAN,
125228753Smm	OPTION_NEWER_MTIME,
126228753Smm	OPTION_NEWER_MTIME_THAN,
127228753Smm	OPTION_NODUMP,
128228753Smm	OPTION_NO_SAME_OWNER,
129228753Smm	OPTION_NO_SAME_PERMISSIONS,
130228753Smm	OPTION_NULL,
131228753Smm	OPTION_NUMERIC_OWNER,
132228753Smm	OPTION_ONE_FILE_SYSTEM,
133228753Smm	OPTION_OPTIONS,
134228753Smm	OPTION_POSIX,
135228753Smm	OPTION_SAME_OWNER,
136228753Smm	OPTION_STRIP_COMPONENTS,
137228753Smm	OPTION_TOTALS,
138228753Smm	OPTION_UID,
139228753Smm	OPTION_UNAME,
140228753Smm	OPTION_USE_COMPRESS_PROGRAM,
141228753Smm	OPTION_VERSION
142228753Smm};
143228753Smm
144228753Smm
145228753Smmint	bsdtar_getopt(struct bsdtar *);
146228753Smmvoid	do_chdir(struct bsdtar *);
147228753Smmint	edit_pathname(struct bsdtar *, struct archive_entry *);
148228753Smmint	need_report(void);
149228753Smmint	pathcmp(const char *a, const char *b);
150228753Smmvoid	safe_fprintf(FILE *, const char *fmt, ...);
151228753Smmvoid	set_chdir(struct bsdtar *, const char *newdir);
152228753Smmconst char *tar_i64toa(int64_t);
153228753Smmvoid	tar_mode_c(struct bsdtar *bsdtar);
154228753Smmvoid	tar_mode_r(struct bsdtar *bsdtar);
155228753Smmvoid	tar_mode_t(struct bsdtar *bsdtar);
156228753Smmvoid	tar_mode_u(struct bsdtar *bsdtar);
157228753Smmvoid	tar_mode_x(struct bsdtar *bsdtar);
158228753Smmvoid	usage(void);
159228753Smmint	yes(const char *fmt, ...);
160228753Smm
161228753Smm#if HAVE_REGEX_H
162228753Smmvoid	add_substitution(struct bsdtar *, const char *);
163228753Smmint	apply_substitution(struct bsdtar *, const char *, char **, int);
164228753Smmvoid	cleanup_substitution(struct bsdtar *);
165228753Smm#endif
166