1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003-2007 Tim Kientzle 5 * All rights reserved. 6 */ 7 8 #ifndef BSDTAR_H_INCLUDED 9 #define BSDTAR_H_INCLUDED 10 11 #include "bsdtar_platform.h" 12 #include <stdio.h> 13 14 #define DEFAULT_BYTES_PER_BLOCK (20*512) 15 #define ENV_READER_OPTIONS "TAR_READER_OPTIONS" 16 #define ENV_WRITER_OPTIONS "TAR_WRITER_OPTIONS" 17 #define IGNORE_WRONG_MODULE_NAME "__ignore_wrong_module_name__," 18 19 struct creation_set; 20 /* 21 * The internal state for the "bsdtar" program. 22 * 23 * Keeping all of the state in a structure like this simplifies memory 24 * leak testing (at exit, anything left on the heap is suspect). A 25 * pointer to this structure is passed to most bsdtar internal 26 * functions. 27 */ 28 struct bsdtar { 29 /* Options */ 30 const char *filename; /* -f filename */ 31 char *pending_chdir; /* -C dir */ 32 const char *names_from_file; /* -T file */ 33 int bytes_per_block; /* -b block_size */ 34 int bytes_in_last_block; /* See -b handling. */ 35 int verbose; /* -v */ 36 unsigned int flags; /* Bitfield of boolean options */ 37 int extract_flags; /* Flags for extract operation */ 38 int readdisk_flags; /* Flags for read disk operation */ 39 int strip_components; /* Remove this many leading dirs */ 40 int gid; /* --gid */ 41 const char *gname; /* --gname */ 42 int uid; /* --uid */ 43 const char *uname; /* --uname */ 44 const char *passphrase; /* --passphrase */ 45 char mode; /* Program mode: 'c', 't', 'r', 'u', 'x' */ 46 char symlink_mode; /* H or L, per BSD conventions */ 47 const char *option_options; /* --options */ 48 char day_first; /* show day before month in -tv output */ 49 struct creation_set *cset; 50 51 /* Option parser state */ 52 int getopt_state; 53 char *getopt_word; 54 55 /* If >= 0, then close this when done. */ 56 int fd; 57 58 /* Miscellaneous state information */ 59 int argc; 60 char **argv; 61 const char *argument; 62 size_t gs_width; /* For 'list_item' in read.c */ 63 size_t u_width; /* for 'list_item' in read.c */ 64 uid_t user_uid; /* UID running this program */ 65 int return_value; /* Value returned by main() */ 66 char warned_lead_slash; /* Already displayed warning */ 67 char next_line_is_dir; /* Used for -C parsing in -cT */ 68 69 /* 70 * Data for various subsystems. Full definitions are located in 71 * the file where they are used. 72 */ 73 struct archive *diskreader; /* for write.c */ 74 struct archive_entry_linkresolver *resolver; /* for write.c */ 75 struct archive_dir *archive_dir; /* for write.c */ 76 struct name_cache *gname_cache; /* for write.c */ 77 char *buff; /* for write.c */ 78 size_t buff_size; /* for write.c */ 79 int first_fs; /* for write.c */ 80 struct archive *matching; /* for matching.c */ 81 struct security *security; /* for read.c */ 82 struct name_cache *uname_cache; /* for write.c */ 83 struct siginfo_data *siginfo; /* for siginfo.c */ 84 struct substitution *substitution; /* for subst.c */ 85 char *ppbuff; /* for util.c */ 86 }; 87 88 /* Options for flags bitfield */ 89 #define OPTFLAG_AUTO_COMPRESS (0x00000001) /* -a */ 90 #define OPTFLAG_ABSOLUTE_PATHS (0x00000002) /* -P */ 91 #define OPTFLAG_CHROOT (0x00000004) /* --chroot */ 92 #define OPTFLAG_FAST_READ (0x00000008) /* --fast-read */ 93 #define OPTFLAG_IGNORE_ZEROS (0x00000010) /* --ignore-zeros */ 94 #define OPTFLAG_INTERACTIVE (0x00000020) /* -w */ 95 #define OPTFLAG_NO_OWNER (0x00000040) /* -o */ 96 #define OPTFLAG_NO_SUBDIRS (0x00000080) /* -n */ 97 #define OPTFLAG_NULL (0x00000100) /* --null */ 98 #define OPTFLAG_NUMERIC_OWNER (0x00000200) /* --numeric-owner */ 99 #define OPTFLAG_O (0x00000400) /* -o */ 100 #define OPTFLAG_STDOUT (0x00000800) /* -O */ 101 #define OPTFLAG_TOTALS (0x00001000) /* --totals */ 102 #define OPTFLAG_UNLINK_FIRST (0x00002000) /* -U */ 103 #define OPTFLAG_WARN_LINKS (0x00004000) /* --check-links */ 104 #define OPTFLAG_NO_XATTRS (0x00008000) /* --no-xattrs */ 105 #define OPTFLAG_XATTRS (0x00010000) /* --xattrs */ 106 #define OPTFLAG_NO_ACLS (0x00020000) /* --no-acls */ 107 #define OPTFLAG_ACLS (0x00040000) /* --acls */ 108 #define OPTFLAG_NO_FFLAGS (0x00080000) /* --no-fflags */ 109 #define OPTFLAG_FFLAGS (0x00100000) /* --fflags */ 110 #define OPTFLAG_NO_MAC_METADATA (0x00200000) /* --no-mac-metadata */ 111 #define OPTFLAG_MAC_METADATA (0x00400000) /* --mac-metadata */ 112 #define OPTFLAG_NO_READ_SPARSE (0x00800000) /* --no-read-sparse */ 113 #define OPTFLAG_READ_SPARSE (0x01000000) /* --read-sparse */ 114 115 /* Fake short equivalents for long options that otherwise lack them. */ 116 enum { 117 OPTION_ACLS = 1, 118 OPTION_B64ENCODE, 119 OPTION_CHECK_LINKS, 120 OPTION_CHROOT, 121 OPTION_CLEAR_NOCHANGE_FFLAGS, 122 OPTION_EXCLUDE, 123 OPTION_EXCLUDE_VCS, 124 OPTION_FFLAGS, 125 OPTION_FORMAT, 126 OPTION_GID, 127 OPTION_GNAME, 128 OPTION_GROUP, 129 OPTION_GRZIP, 130 OPTION_HELP, 131 OPTION_HFS_COMPRESSION, 132 OPTION_IGNORE_ZEROS, 133 OPTION_INCLUDE, 134 OPTION_KEEP_NEWER_FILES, 135 OPTION_LRZIP, 136 OPTION_LZ4, 137 OPTION_LZIP, 138 OPTION_LZMA, 139 OPTION_LZOP, 140 OPTION_MAC_METADATA, 141 OPTION_NEWER_CTIME, 142 OPTION_NEWER_CTIME_THAN, 143 OPTION_NEWER_MTIME, 144 OPTION_NEWER_MTIME_THAN, 145 OPTION_NODUMP, 146 OPTION_NOPRESERVE_HFS_COMPRESSION, 147 OPTION_NO_ACLS, 148 OPTION_NO_FFLAGS, 149 OPTION_NO_MAC_METADATA, 150 OPTION_NO_READ_SPARSE, 151 OPTION_NO_SAFE_WRITES, 152 OPTION_NO_SAME_OWNER, 153 OPTION_NO_SAME_PERMISSIONS, 154 OPTION_NO_XATTRS, 155 OPTION_NULL, 156 OPTION_NUMERIC_OWNER, 157 OPTION_OLDER_CTIME, 158 OPTION_OLDER_CTIME_THAN, 159 OPTION_OLDER_MTIME, 160 OPTION_OLDER_MTIME_THAN, 161 OPTION_ONE_FILE_SYSTEM, 162 OPTION_OPTIONS, 163 OPTION_OWNER, 164 OPTION_PASSPHRASE, 165 OPTION_POSIX, 166 OPTION_READ_SPARSE, 167 OPTION_SAFE_WRITES, 168 OPTION_SAME_OWNER, 169 OPTION_STRIP_COMPONENTS, 170 OPTION_TOTALS, 171 OPTION_UID, 172 OPTION_UNAME, 173 OPTION_USE_COMPRESS_PROGRAM, 174 OPTION_UUENCODE, 175 OPTION_VERSION, 176 OPTION_XATTRS, 177 OPTION_ZSTD, 178 }; 179 180 int bsdtar_getopt(struct bsdtar *); 181 void do_chdir(struct bsdtar *); 182 int edit_pathname(struct bsdtar *, struct archive_entry *); 183 int need_report(void); 184 int pathcmp(const char *a, const char *b); 185 void safe_fprintf(FILE *, const char *fmt, ...) __LA_PRINTF(2, 3); 186 void set_chdir(struct bsdtar *, const char *newdir); 187 const char *tar_i64toa(int64_t); 188 void tar_mode_c(struct bsdtar *bsdtar); 189 void tar_mode_r(struct bsdtar *bsdtar); 190 void tar_mode_t(struct bsdtar *bsdtar); 191 void tar_mode_u(struct bsdtar *bsdtar); 192 void tar_mode_x(struct bsdtar *bsdtar); 193 __LA_NORETURN void usage(void); 194 int yes(const char *fmt, ...) __LA_PRINTF(1, 2); 195 196 #if defined(HAVE_REGEX_H) || defined(HAVE_PCREPOSIX_H) || defined(HAVE_PCRE2POSIX_H) 197 void add_substitution(struct bsdtar *, const char *); 198 int apply_substitution(struct bsdtar *, const char *, char **, int, int); 199 void cleanup_substitution(struct bsdtar *); 200 #endif 201 202 void cset_add_filter(struct creation_set *, const char *); 203 void cset_add_filter_program(struct creation_set *, const char *); 204 int cset_auto_compress(struct creation_set *, const char *); 205 void cset_free(struct creation_set *); 206 const char * cset_get_format(struct creation_set *); 207 struct creation_set *cset_new(void); 208 int cset_read_support_filter_program(struct creation_set *, 209 struct archive *); 210 void cset_set_format(struct creation_set *, const char *); 211 int cset_write_add_filters(struct creation_set *, 212 struct archive *, const void **); 213 214 const char * passphrase_callback(struct archive *, void *); 215 void passphrase_free(char *); 216 void list_item_verbose(struct bsdtar *, FILE *, 217 struct archive_entry *); 218 219 #endif 220