blob: aa637b13a1a627bc4e23462c6b98a5ca15ba7b44 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2** Copyright 1998-2003 University of Illinois Board of Trustees
3** Copyright 1998-2003 Mark D. Roth
4** All rights reserved.
5**
6** libtar.h - header file for libtar library
7**
8** Mark D. Roth <roth@uiuc.edu>
9** Campus Information Technologies and Educational Services
10** University of Illinois at Urbana-Champaign
11*/
12
13#ifndef LIBTAR_H
14#define LIBTAR_H
15
16#include <sys/types.h>
17#include <sys/stat.h>
Ethan Yonker71187742017-01-13 13:30:10 -060018#include <linux/capability.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050019#include "tar.h"
20
21#include "libtar_listhash.h"
22
Ethan Yonker79f88bd2016-12-09 14:52:12 -060023#ifdef HAVE_EXT4_CRYPT
Ethan Yonkerfefe5912017-09-30 22:22:13 -050024# include "ext4crypt_tar.h"
Ethan Yonker79f88bd2016-12-09 14:52:12 -060025#endif
26
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027#ifdef __cplusplus
28extern "C"
29{
30#endif
31
32
33/* useful constants */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050034/* see FIXME note in block.c regarding T_BLOCKSIZE */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050035#define T_BLOCKSIZE 512
36#define T_NAMELEN 100
37#define T_PREFIXLEN 155
38#define T_MAXPATHLEN (T_NAMELEN + T_PREFIXLEN)
39
40/* GNU extensions for typeflag */
41#define GNU_LONGNAME_TYPE 'L'
42#define GNU_LONGLINK_TYPE 'K'
43
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020044/* extended metadata for next file - used to store selinux_context */
45#define TH_EXT_TYPE 'x'
Ethan Yonker71187742017-01-13 13:30:10 -060046#define TH_POL_TYPE_DO_NOT_USE 'p'
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020047
bigbiff bigbiff9c754052013-01-09 09:09:08 -050048/* our version of the tar header structure */
49struct tar_header
50{
51 char name[100];
52 char mode[8];
53 char uid[8];
54 char gid[8];
55 char size[12];
56 char mtime[12];
57 char chksum[8];
58 char typeflag;
59 char linkname[100];
60 char magic[6];
61 char version[2];
62 char uname[32];
63 char gname[32];
64 char devmajor[8];
65 char devminor[8];
66 char prefix[155];
67 char padding[12];
68 char *gnu_longname;
69 char *gnu_longlink;
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020070 char *selinux_context;
Ethan Yonker79f88bd2016-12-09 14:52:12 -060071#ifdef HAVE_EXT4_CRYPT
Ethan Yonkerfefe5912017-09-30 22:22:13 -050072 struct ext4_encryption_policy *eep;
Ethan Yonker79f88bd2016-12-09 14:52:12 -060073#endif
Ethan Yonker71187742017-01-13 13:30:10 -060074 int has_cap_data;
75 struct vfs_cap_data cap_data;
Ethan Yonker8d039f72017-02-03 14:26:15 -060076 int has_user_default;
77 int has_user_cache;
78 int has_user_code_cache;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050079};
80
81
82/***** handle.c ************************************************************/
83
84typedef int (*openfunc_t)(const char *, int, ...);
85typedef int (*closefunc_t)(int);
86typedef ssize_t (*readfunc_t)(int, void *, size_t);
87typedef ssize_t (*writefunc_t)(int, const void *, size_t);
88
89typedef struct
90{
91 openfunc_t openfunc;
92 closefunc_t closefunc;
93 readfunc_t readfunc;
94 writefunc_t writefunc;
95}
96tartype_t;
97
98typedef struct
99{
100 tartype_t *type;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500101 const char *pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500102 long fd;
103 int oflags;
104 int options;
105 struct tar_header th_buf;
106 libtar_hash_t *h;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500107
108 /* introduced in libtar 1.2.21 */
109 char *th_pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500110}
111TAR;
112
113/* constant values for the TAR options field */
114#define TAR_GNU 1 /* use GNU extensions */
115#define TAR_VERBOSE 2 /* output file info to stdout */
116#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
117#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
118#define TAR_CHECK_MAGIC 16 /* check magic in file header */
119#define TAR_CHECK_VERSION 32 /* check version in file header */
120#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200121#define TAR_STORE_SELINUX 128 /* store selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500122#define TAR_USE_NUMERIC_ID 256 /* favor numeric owner over names */
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600123#define TAR_STORE_EXT4_POL 512 /* store ext4 crypto policy */
Ethan Yonker71187742017-01-13 13:30:10 -0600124#define TAR_STORE_POSIX_CAP 1024 /* store posix file capabilities */
Ethan Yonker8d039f72017-02-03 14:26:15 -0600125#define TAR_STORE_ANDROID_USER_XATTR 2048 /* store android user.* xattr */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500126
127/* this is obsolete - it's here for backwards-compatibility only */
128#define TAR_IGNORE_MAGIC 0
129
130extern const char libtar_version[];
131
132
133/* open a new tarfile handle */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500134int tar_open(TAR **t, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500135 int oflags, int mode, int options);
136
137/* make a tarfile handle out of a previously-opened descriptor */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500138int tar_fdopen(TAR **t, int fd, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500139 int oflags, int mode, int options);
140
141/* returns the descriptor associated with t */
142int tar_fd(TAR *t);
143
144/* close tarfile handle */
145int tar_close(TAR *t);
146
147
148/***** append.c ************************************************************/
149
150/* forward declaration to appease the compiler */
151struct tar_dev;
152
153/* cleanup function */
154void tar_dev_free(struct tar_dev *tdp);
155
156/* Appends a file to the tar archive.
157 * Arguments:
158 * t = TAR handle to append to
159 * realname = path of file to append
160 * savename = name to save the file under in the archive
161 */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500162int tar_append_file(TAR *t, const char *realname, const char *savename);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500163
164/* write EOF indicator */
165int tar_append_eof(TAR *t);
166
167/* add file contents to a tarchive */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500168int tar_append_regfile(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500169
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500170/* Appends in-memory file contents to a tarchive.
171 * Arguments:
172 * t = TAR handle to append to
173 * savename = name to save the file under in the archive
174 * mode = mode
175 * uid, gid = owner
176 * buf, len = in-memory buffer
177 */
178int tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
179 uid_t uid, gid_t gid, void *buf, size_t len);
180
181/* add buffer to a tarchive */
182int tar_append_buffer(TAR *t, void *buf, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500183
184/***** block.c *************************************************************/
185
186/* macros for reading/writing tarchive blocks */
187#define tar_block_read(t, buf) \
188 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
189#define tar_block_write(t, buf) \
190 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
191
192/* read/write a header block */
193int th_read(TAR *t);
194int th_write(TAR *t);
195
196
197/***** decode.c ************************************************************/
198
199/* determine file type */
200#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
201 || (t)->th_buf.typeflag == AREGTYPE \
202 || (t)->th_buf.typeflag == CONTTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500203 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500204 && (t)->th_buf.typeflag != LNKTYPE))
205#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
206#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500207 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500208#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500209 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500210#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500211 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500212#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500213 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500214 || ((t)->th_buf.typeflag == AREGTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500215 && strnlen((t)->th_buf.name, T_NAMELEN) \
216 && ((t)->th_buf.name[strnlen((t)->th_buf.name, T_NAMELEN) - 1] == '/')))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500217#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500218 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500219#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
220#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200221#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
Ethan Yonker71187742017-01-13 13:30:10 -0600222#define TH_ISPOLHEADER(t) ((t)->th_buf.typeflag == TH_POL_TYPE_DO_NOT_USE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500223
224/* decode tar header info */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500225#define th_get_crc(t) oct_to_int((t)->th_buf.chksum, sizeof((t)->th_buf.chksum))
226#define th_get_size(t) oct_to_int_ex((t)->th_buf.size, sizeof((t)->th_buf.size))
227#define th_get_mtime(t) oct_to_int_ex((t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
228#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor, sizeof((t)->th_buf.devmajor))
229#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor, sizeof((t)->th_buf.devminor))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500230#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
231 ? (t)->th_buf.gnu_longlink \
232 : (t)->th_buf.linkname)
233char *th_get_pathname(TAR *t);
234mode_t th_get_mode(TAR *t);
235uid_t th_get_uid(TAR *t);
236gid_t th_get_gid(TAR *t);
237
238
239/***** encode.c ************************************************************/
240
241/* encode file info in th_header */
242void th_set_type(TAR *t, mode_t mode);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500243void th_set_path(TAR *t, const char *pathname);
244void th_set_link(TAR *t, const char *linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500245void th_set_device(TAR *t, dev_t device);
246void th_set_user(TAR *t, uid_t uid);
247void th_set_group(TAR *t, gid_t gid);
248void th_set_mode(TAR *t, mode_t fmode);
249#define th_set_mtime(t, fmtime) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500250 int_to_oct_ex((fmtime), (t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500251#define th_set_size(t, fsize) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500252 int_to_oct_ex((fsize), (t)->th_buf.size, sizeof((t)->th_buf.size))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500253
254/* encode everything at once (except the pathname and linkname) */
255void th_set_from_stat(TAR *t, struct stat *s);
256
257/* encode magic, version, and crc - must be done after everything else is set */
258void th_finish(TAR *t);
259
260
261/***** extract.c ***********************************************************/
262
263/* sequentially extract next file from t */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500264int tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500265
266/* extract different file types */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500267int tar_extract_dir(TAR *t, const char *realname);
268int tar_extract_hardlink(TAR *t, const char *realname, const char *prefix);
269int tar_extract_symlink(TAR *t, const char *realname);
270int tar_extract_chardev(TAR *t, const char *realname);
271int tar_extract_blockdev(TAR *t, const char *realname);
272int tar_extract_fifo(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500273
274/* for regfiles, we need to extract the content blocks as well */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500275int tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500276int tar_skip_regfile(TAR *t);
277
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500278/* extract regfile to buffer */
279int tar_extract_file_contents(TAR *t, void *buf, size_t *lenp);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500280
281/***** output.c ************************************************************/
282
283/* print the tar header */
284void th_print(TAR *t);
285
286/* print "ls -l"-like output for the file described by th */
287void th_print_long_ls(TAR *t);
288
289
290/***** util.c *************************************************************/
291
292/* hashing function for pathnames */
293int path_hashfunc(char *key, int numbuckets);
294
295/* matching function for dev_t's */
296int dev_match(dev_t *dev1, dev_t *dev2);
297
298/* matching function for ino_t's */
299int ino_match(ino_t *ino1, ino_t *ino2);
300
301/* hashing function for dev_t's */
302int dev_hash(dev_t *dev);
303
304/* hashing function for ino_t's */
305int ino_hash(ino_t *inode);
306
307/* create any necessary dirs */
308int mkdirhier(char *path);
309
310/* calculate header checksum */
311int th_crc_calc(TAR *t);
312
313/* calculate a signed header checksum */
314int th_signed_crc_calc(TAR *t);
315
316/* compare checksums in a forgiving way */
317#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
318
319/* string-octal to integer conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500320int64_t oct_to_int(char *oct, size_t len);
321
322/* string-octal or binary to integer conversion */
323int64_t oct_to_int_ex(char *oct, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500324
325/* integer to NULL-terminated string-octal conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500326void int_to_oct(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500327
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500328/* integer to string-octal conversion, or binary as necessary */
329void int_to_oct_ex(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500330
Ethan Yonker71187742017-01-13 13:30:10 -0600331/* prints posix file capabilities */
332void print_caps(struct vfs_cap_data *cap_data);
333
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500334
335/***** wrapper.c **********************************************************/
336
337/* extract groups of files */
338int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500339int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500340
341/* add a whole tree of files */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500342int tar_append_tree(TAR *t, char *realdir, char *savedir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500343
n0d33b511632013-03-06 21:14:15 +0200344/* find an entry */
345int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500346
347#ifdef __cplusplus
348}
349#endif
350
351#endif /* ! LIBTAR_H */
352