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$ 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 unsigned int flags; /* Bitfield of boolean options */ 54 int extract_flags; /* Flags for extract operation */ 55 int readdisk_flags; /* Flags for read disk operation */ 56 int strip_components; /* Remove this many leading dirs */ 57 int gid; /* --gid */ 58 const char *gname; /* --gname */ 59 int uid; /* --uid */ 60 const char *uname; /* --uname */ 61 const char *passphrase; /* --passphrase */ 62 char mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */ 63 char symlink_mode; /* H or L, per BSD conventions */ 64 const char *option_options; /* --options */ 65 char day_first; /* show day before month in -tv output */ 66 struct creation_set *cset; 67 68 /* Option parser state */ 69 int getopt_state; 70 char *getopt_word; 71 72 /* If >= 0, then close this when done. */ 73 int fd; 74 75 /* Miscellaneous state information */ 76 int argc; 77 char **argv; 78 const char *argument; 79 size_t gs_width; /* For 'list_item' in read.c */ 80 size_t u_width; /* for 'list_item' in read.c */ 81 uid_t user_uid; /* UID running this program */ 82 int return_value; /* Value returned by main() */ 83 char warned_lead_slash; /* Already displayed warning */ 84 char next_line_is_dir; /* Used for -C parsing in -cT */ 85 86 /* 87 * Data for various subsystems. Full definitions are located in 88 * the file where they are used. 89 */ 90 struct archive *diskreader; /* for write.c */ 91 struct archive_entry_linkresolver *resolver; /* for write.c */ 92 struct archive_dir *archive_dir; /* for write.c */ 93 struct name_cache *gname_cache; /* for write.c */ 94 char *buff; /* for write.c */ 95 size_t buff_size; /* for write.c */ 96 int first_fs; /* for write.c */ 97 struct archive *matching; /* for matching.c */ 98 struct security *security; /* for read.c */ 99 struct name_cache *uname_cache; /* for write.c */ 100 struct siginfo_data *siginfo; /* for siginfo.c */ 101 struct substitution *substitution; /* for subst.c */ 102 char *ppbuff; /* for util.c */ 103 }; 104 105 /* Options for flags bitfield */ 106 #define OPTFLAG_AUTO_COMPRESS (0x00000001) /* -a */ 107 #define OPTFLAG_ABSOLUTE_PATHS (0x00000002) /* -P */ 108 #define OPTFLAG_CHROOT (0x00000004) /* --chroot */ 109 #define OPTFLAG_FAST_READ (0x00000008) /* --fast-read */ 110 #define OPTFLAG_IGNORE_ZEROS (0x00000010) /* --ignore-zeros */ 111 #define OPTFLAG_INTERACTIVE (0x00000020) /* -w */ 112 #define OPTFLAG_NO_OWNER (0x00000040) /* -o */ 113 #define OPTFLAG_NO_SUBDIRS (0x00000080) /* -n */ 114 #define OPTFLAG_NULL (0x00000100) /* --null */ 115 #define OPTFLAG_NUMERIC_OWNER (0x00000200) /* --numeric-owner */ 116 #define OPTFLAG_O (0x00000400) /* -o */ 117 #define OPTFLAG_STDOUT (0x00000800) /* -O */ 118 #define OPTFLAG_TOTALS (0x00001000) /* --totals */ 119 #define OPTFLAG_UNLINK_FIRST (0x00002000) /* -U */ 120 #define OPTFLAG_WARN_LINKS (0x00004000) /* --check-links */ 121 #define OPTFLAG_NO_XATTRS (0x00008000) /* --no-xattrs */ 122 #define OPTFLAG_XATTRS (0x00010000) /* --xattrs */ 123 #define OPTFLAG_NO_ACLS (0x00020000) /* --no-acls */ 124 #define OPTFLAG_ACLS (0x00040000) /* --acls */ 125 #define OPTFLAG_NO_FFLAGS (0x00080000) /* --no-fflags */ 126 #define OPTFLAG_FFLAGS (0x00100000) /* --fflags */ 127 #define OPTFLAG_NO_MAC_METADATA (0x00200000) /* --no-mac-metadata */ 128 #define OPTFLAG_MAC_METADATA (0x00400000) /* --mac-metadata */ 129 130 /* Fake short equivalents for long options that otherwise lack them. */ 131 enum { 132 OPTION_ACLS = 1, 133 OPTION_B64ENCODE, 134 OPTION_CHECK_LINKS, 135 OPTION_CHROOT, 136 OPTION_CLEAR_NOCHANGE_FFLAGS, 137 OPTION_EXCLUDE, 138 OPTION_EXCLUDE_VCS, 139 OPTION_FFLAGS, 140 OPTION_FORMAT, 141 OPTION_GID, 142 OPTION_GNAME, 143 OPTION_GRZIP, 144 OPTION_HELP, 145 OPTION_HFS_COMPRESSION, 146 OPTION_IGNORE_ZEROS, 147 OPTION_INCLUDE, 148 OPTION_KEEP_NEWER_FILES, 149 OPTION_LRZIP, 150 OPTION_LZ4, 151 OPTION_LZIP, 152 OPTION_LZMA, 153 OPTION_LZOP, 154 OPTION_MAC_METADATA, 155 OPTION_NEWER_CTIME, 156 OPTION_NEWER_CTIME_THAN, 157 OPTION_NEWER_MTIME, 158 OPTION_NEWER_MTIME_THAN, 159 OPTION_NODUMP, 160 OPTION_NOPRESERVE_HFS_COMPRESSION, 161 OPTION_NO_ACLS, 162 OPTION_NO_FFLAGS, 163 OPTION_NO_MAC_METADATA, 164 OPTION_NO_SAME_OWNER, 165 OPTION_NO_SAME_PERMISSIONS, 166 OPTION_NO_XATTRS, 167 OPTION_NULL, 168 OPTION_NUMERIC_OWNER, 169 OPTION_OLDER_CTIME, 170 OPTION_OLDER_CTIME_THAN, 171 OPTION_OLDER_MTIME, 172 OPTION_OLDER_MTIME_THAN, 173 OPTION_ONE_FILE_SYSTEM, 174 OPTION_OPTIONS, 175 OPTION_PASSPHRASE, 176 OPTION_POSIX, 177 OPTION_SAME_OWNER, 178 OPTION_STRIP_COMPONENTS, 179 OPTION_TOTALS, 180 OPTION_UID, 181 OPTION_UNAME, 182 OPTION_USE_COMPRESS_PROGRAM, 183 OPTION_UUENCODE, 184 OPTION_VERSION, 185 OPTION_XATTRS, 186 OPTION_ZSTD, 187 }; 188 189 int bsdtar_getopt(struct bsdtar *); 190 void do_chdir(struct bsdtar *); 191 int edit_pathname(struct bsdtar *, struct archive_entry *); 192 int need_report(void); 193 int pathcmp(const char *a, const char *b); 194 void safe_fprintf(FILE *, const char *fmt, ...) __LA_PRINTF(2, 3); 195 void set_chdir(struct bsdtar *, const char *newdir); 196 const char *tar_i64toa(int64_t); 197 void tar_mode_c(struct bsdtar *bsdtar); 198 void tar_mode_r(struct bsdtar *bsdtar); 199 void tar_mode_t(struct bsdtar *bsdtar); 200 void tar_mode_u(struct bsdtar *bsdtar); 201 void tar_mode_x(struct bsdtar *bsdtar); 202 void usage(void) __LA_DEAD; 203 int yes(const char *fmt, ...) __LA_PRINTF(1, 2); 204 205 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) 206 void add_substitution(struct bsdtar *, const char *); 207 int apply_substitution(struct bsdtar *, const char *, char **, int, int); 208 void cleanup_substitution(struct bsdtar *); 209 #endif 210 211 void cset_add_filter(struct creation_set *, const char *); 212 void cset_add_filter_program(struct creation_set *, const char *); 213 int cset_auto_compress(struct creation_set *, const char *); 214 void cset_free(struct creation_set *); 215 const char * cset_get_format(struct creation_set *); 216 struct creation_set *cset_new(void); 217 int cset_read_support_filter_program(struct creation_set *, 218 struct archive *); 219 void cset_set_format(struct creation_set *, const char *); 220 int cset_write_add_filters(struct creation_set *, 221 struct archive *, const void **); 222 223 const char * passphrase_callback(struct archive *, void *); 224 void passphrase_free(char *); 225 void list_item_verbose(struct bsdtar *, FILE *, 226 struct archive_entry *); 227