blob: 2d0a3d3fc9ec2777827d934415ceb608ec71ca01 [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
24#define EXT4_KEY_DESCRIPTOR_SIZE 8
25#define EXT4_KEY_DESCRIPTOR_HEX 17
26#endif
27
bigbiff bigbiff9c754052013-01-09 09:09:08 -050028#ifdef __cplusplus
29extern "C"
30{
31#endif
32
33
34/* useful constants */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050035/* see FIXME note in block.c regarding T_BLOCKSIZE */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050036#define T_BLOCKSIZE 512
37#define T_NAMELEN 100
38#define T_PREFIXLEN 155
39#define T_MAXPATHLEN (T_NAMELEN + T_PREFIXLEN)
40
41/* GNU extensions for typeflag */
42#define GNU_LONGNAME_TYPE 'L'
43#define GNU_LONGLINK_TYPE 'K'
44
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020045/* extended metadata for next file - used to store selinux_context */
46#define TH_EXT_TYPE 'x'
Ethan Yonker71187742017-01-13 13:30:10 -060047#define TH_POL_TYPE_DO_NOT_USE 'p'
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020048
bigbiff bigbiff9c754052013-01-09 09:09:08 -050049/* our version of the tar header structure */
50struct tar_header
51{
52 char name[100];
53 char mode[8];
54 char uid[8];
55 char gid[8];
56 char size[12];
57 char mtime[12];
58 char chksum[8];
59 char typeflag;
60 char linkname[100];
61 char magic[6];
62 char version[2];
63 char uname[32];
64 char gname[32];
65 char devmajor[8];
66 char devminor[8];
67 char prefix[155];
68 char padding[12];
69 char *gnu_longname;
70 char *gnu_longlink;
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020071 char *selinux_context;
Ethan Yonker79f88bd2016-12-09 14:52:12 -060072#ifdef HAVE_EXT4_CRYPT
73 char *e4crypt_policy;
74#endif
Ethan Yonker71187742017-01-13 13:30:10 -060075 int has_cap_data;
76 struct vfs_cap_data cap_data;
Ethan Yonker8d039f72017-02-03 14:26:15 -060077 int has_user_default;
78 int has_user_cache;
79 int has_user_code_cache;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050080};
81
82
83/***** handle.c ************************************************************/
84
85typedef int (*openfunc_t)(const char *, int, ...);
86typedef int (*closefunc_t)(int);
87typedef ssize_t (*readfunc_t)(int, void *, size_t);
88typedef ssize_t (*writefunc_t)(int, const void *, size_t);
89
90typedef struct
91{
92 openfunc_t openfunc;
93 closefunc_t closefunc;
94 readfunc_t readfunc;
95 writefunc_t writefunc;
96}
97tartype_t;
98
99typedef struct
100{
101 tartype_t *type;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500102 const char *pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500103 long fd;
104 int oflags;
105 int options;
106 struct tar_header th_buf;
107 libtar_hash_t *h;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500108
109 /* introduced in libtar 1.2.21 */
110 char *th_pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500111}
112TAR;
113
114/* constant values for the TAR options field */
115#define TAR_GNU 1 /* use GNU extensions */
116#define TAR_VERBOSE 2 /* output file info to stdout */
117#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
118#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
119#define TAR_CHECK_MAGIC 16 /* check magic in file header */
120#define TAR_CHECK_VERSION 32 /* check version in file header */
121#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200122#define TAR_STORE_SELINUX 128 /* store selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500123#define TAR_USE_NUMERIC_ID 256 /* favor numeric owner over names */
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600124#define TAR_STORE_EXT4_POL 512 /* store ext4 crypto policy */
Ethan Yonker71187742017-01-13 13:30:10 -0600125#define TAR_STORE_POSIX_CAP 1024 /* store posix file capabilities */
Ethan Yonker8d039f72017-02-03 14:26:15 -0600126#define TAR_STORE_ANDROID_USER_XATTR 2048 /* store android user.* xattr */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500127
128/* this is obsolete - it's here for backwards-compatibility only */
129#define TAR_IGNORE_MAGIC 0
130
131extern const char libtar_version[];
132
133
134/* open a new tarfile handle */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500135int tar_open(TAR **t, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500136 int oflags, int mode, int options);
137
138/* make a tarfile handle out of a previously-opened descriptor */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500139int tar_fdopen(TAR **t, int fd, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500140 int oflags, int mode, int options);
141
142/* returns the descriptor associated with t */
143int tar_fd(TAR *t);
144
145/* close tarfile handle */
146int tar_close(TAR *t);
147
148
149/***** append.c ************************************************************/
150
151/* forward declaration to appease the compiler */
152struct tar_dev;
153
154/* cleanup function */
155void tar_dev_free(struct tar_dev *tdp);
156
157/* Appends a file to the tar archive.
158 * Arguments:
159 * t = TAR handle to append to
160 * realname = path of file to append
161 * savename = name to save the file under in the archive
162 */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500163int tar_append_file(TAR *t, const char *realname, const char *savename);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500164
165/* write EOF indicator */
166int tar_append_eof(TAR *t);
167
168/* add file contents to a tarchive */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500169int tar_append_regfile(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500170
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500171/* Appends in-memory file contents to a tarchive.
172 * Arguments:
173 * t = TAR handle to append to
174 * savename = name to save the file under in the archive
175 * mode = mode
176 * uid, gid = owner
177 * buf, len = in-memory buffer
178 */
179int tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
180 uid_t uid, gid_t gid, void *buf, size_t len);
181
182/* add buffer to a tarchive */
183int tar_append_buffer(TAR *t, void *buf, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500184
185/***** block.c *************************************************************/
186
187/* macros for reading/writing tarchive blocks */
188#define tar_block_read(t, buf) \
189 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
190#define tar_block_write(t, buf) \
191 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
192
193/* read/write a header block */
194int th_read(TAR *t);
195int th_write(TAR *t);
196
197
198/***** decode.c ************************************************************/
199
200/* determine file type */
201#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
202 || (t)->th_buf.typeflag == AREGTYPE \
203 || (t)->th_buf.typeflag == CONTTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500204 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500205 && (t)->th_buf.typeflag != LNKTYPE))
206#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
207#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500208 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500209#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500210 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500212 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500213#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500214 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500215 || ((t)->th_buf.typeflag == AREGTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500216 && strnlen((t)->th_buf.name, T_NAMELEN) \
217 && ((t)->th_buf.name[strnlen((t)->th_buf.name, T_NAMELEN) - 1] == '/')))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500218#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500219 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500220#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
221#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200222#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
Ethan Yonker71187742017-01-13 13:30:10 -0600223#define TH_ISPOLHEADER(t) ((t)->th_buf.typeflag == TH_POL_TYPE_DO_NOT_USE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500224
225/* decode tar header info */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500226#define th_get_crc(t) oct_to_int((t)->th_buf.chksum, sizeof((t)->th_buf.chksum))
227#define th_get_size(t) oct_to_int_ex((t)->th_buf.size, sizeof((t)->th_buf.size))
228#define th_get_mtime(t) oct_to_int_ex((t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
229#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor, sizeof((t)->th_buf.devmajor))
230#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor, sizeof((t)->th_buf.devminor))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500231#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
232 ? (t)->th_buf.gnu_longlink \
233 : (t)->th_buf.linkname)
234char *th_get_pathname(TAR *t);
235mode_t th_get_mode(TAR *t);
236uid_t th_get_uid(TAR *t);
237gid_t th_get_gid(TAR *t);
238
239
240/***** encode.c ************************************************************/
241
242/* encode file info in th_header */
243void th_set_type(TAR *t, mode_t mode);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500244void th_set_path(TAR *t, const char *pathname);
245void th_set_link(TAR *t, const char *linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500246void th_set_device(TAR *t, dev_t device);
247void th_set_user(TAR *t, uid_t uid);
248void th_set_group(TAR *t, gid_t gid);
249void th_set_mode(TAR *t, mode_t fmode);
250#define th_set_mtime(t, fmtime) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500251 int_to_oct_ex((fmtime), (t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500252#define th_set_size(t, fsize) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500253 int_to_oct_ex((fsize), (t)->th_buf.size, sizeof((t)->th_buf.size))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500254
255/* encode everything at once (except the pathname and linkname) */
256void th_set_from_stat(TAR *t, struct stat *s);
257
258/* encode magic, version, and crc - must be done after everything else is set */
259void th_finish(TAR *t);
260
261
262/***** extract.c ***********************************************************/
263
264/* sequentially extract next file from t */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500265int tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500266
267/* extract different file types */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500268int tar_extract_dir(TAR *t, const char *realname);
269int tar_extract_hardlink(TAR *t, const char *realname, const char *prefix);
270int tar_extract_symlink(TAR *t, const char *realname);
271int tar_extract_chardev(TAR *t, const char *realname);
272int tar_extract_blockdev(TAR *t, const char *realname);
273int tar_extract_fifo(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500274
275/* for regfiles, we need to extract the content blocks as well */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500276int tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500277int tar_skip_regfile(TAR *t);
278
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500279/* extract regfile to buffer */
280int tar_extract_file_contents(TAR *t, void *buf, size_t *lenp);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500281
282/***** output.c ************************************************************/
283
284/* print the tar header */
285void th_print(TAR *t);
286
287/* print "ls -l"-like output for the file described by th */
288void th_print_long_ls(TAR *t);
289
290
291/***** util.c *************************************************************/
292
293/* hashing function for pathnames */
294int path_hashfunc(char *key, int numbuckets);
295
296/* matching function for dev_t's */
297int dev_match(dev_t *dev1, dev_t *dev2);
298
299/* matching function for ino_t's */
300int ino_match(ino_t *ino1, ino_t *ino2);
301
302/* hashing function for dev_t's */
303int dev_hash(dev_t *dev);
304
305/* hashing function for ino_t's */
306int ino_hash(ino_t *inode);
307
308/* create any necessary dirs */
309int mkdirhier(char *path);
310
311/* calculate header checksum */
312int th_crc_calc(TAR *t);
313
314/* calculate a signed header checksum */
315int th_signed_crc_calc(TAR *t);
316
317/* compare checksums in a forgiving way */
318#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
319
320/* string-octal to integer conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500321int64_t oct_to_int(char *oct, size_t len);
322
323/* string-octal or binary to integer conversion */
324int64_t oct_to_int_ex(char *oct, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500325
326/* integer to NULL-terminated string-octal conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500327void int_to_oct(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500328
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500329/* integer to string-octal conversion, or binary as necessary */
330void int_to_oct_ex(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500331
Ethan Yonker71187742017-01-13 13:30:10 -0600332/* prints posix file capabilities */
333void print_caps(struct vfs_cap_data *cap_data);
334
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500335
336/***** wrapper.c **********************************************************/
337
338/* extract groups of files */
339int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500340int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500341
342/* add a whole tree of files */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500343int tar_append_tree(TAR *t, char *realdir, char *savedir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500344
n0d33b511632013-03-06 21:14:15 +0200345/* find an entry */
346int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500347
348#ifdef __cplusplus
349}
350#endif
351
352#endif /* ! LIBTAR_H */
353