blob: f42b20109eb654bb5b9c16092fdae4a8e9d94c1e [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
bigbiff7ba75002020-04-11 20:47:09 -040023#ifdef USE_FSCRYPT
24#include "fscrypt_policy.h"
25#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
Mohd Faraz566ef6b2023-01-16 16:16:21 +010048#define LOG(...) printf("libtar: " __VA_ARGS__)
49
bigbiff bigbiff9c754052013-01-09 09:09:08 -050050/* our version of the tar header structure */
51struct tar_header
52{
53 char name[100];
54 char mode[8];
55 char uid[8];
56 char gid[8];
57 char size[12];
58 char mtime[12];
59 char chksum[8];
60 char typeflag;
61 char linkname[100];
62 char magic[6];
63 char version[2];
64 char uname[32];
65 char gname[32];
66 char devmajor[8];
67 char devminor[8];
68 char prefix[155];
69 char padding[12];
70 char *gnu_longname;
71 char *gnu_longlink;
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020072 char *selinux_context;
bigbiff7ba75002020-04-11 20:47:09 -040073#ifdef USE_FSCRYPT
Mohd Faraz97b84992023-01-07 21:26:56 +010074 fscrypt_policy *fep;
bigbiff7ba75002020-04-11 20:47:09 -040075#endif
Ethan Yonker71187742017-01-13 13:30:10 -060076 int has_cap_data;
77 struct vfs_cap_data cap_data;
Ethan Yonker8d039f72017-02-03 14:26:15 -060078 int has_user_default;
79 int has_user_cache;
80 int has_user_code_cache;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050081};
82
83
84/***** handle.c ************************************************************/
85
86typedef int (*openfunc_t)(const char *, int, ...);
87typedef int (*closefunc_t)(int);
88typedef ssize_t (*readfunc_t)(int, void *, size_t);
89typedef ssize_t (*writefunc_t)(int, const void *, size_t);
90
91typedef struct
92{
93 openfunc_t openfunc;
94 closefunc_t closefunc;
95 readfunc_t readfunc;
96 writefunc_t writefunc;
97}
98tartype_t;
99
100typedef struct
101{
102 tartype_t *type;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500103 const char *pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500104 long fd;
105 int oflags;
106 int options;
107 struct tar_header th_buf;
108 libtar_hash_t *h;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500109
110 /* introduced in libtar 1.2.21 */
111 char *th_pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500112}
113TAR;
114
115/* constant values for the TAR options field */
116#define TAR_GNU 1 /* use GNU extensions */
117#define TAR_VERBOSE 2 /* output file info to stdout */
118#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
119#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
120#define TAR_CHECK_MAGIC 16 /* check magic in file header */
121#define TAR_CHECK_VERSION 32 /* check version in file header */
122#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200123#define TAR_STORE_SELINUX 128 /* store selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500124#define TAR_USE_NUMERIC_ID 256 /* favor numeric owner over names */
bigbiffa957f072021-03-07 18:20:29 -0500125
bigbiff7ba75002020-04-11 20:47:09 -0400126#ifdef USE_FSCRYPT
127#define TAR_STORE_FSCRYPT_POL 512 /* store fscrypt crypto policy */
128#endif
bigbiffa957f072021-03-07 18:20:29 -0500129
Ethan Yonker71187742017-01-13 13:30:10 -0600130#define TAR_STORE_POSIX_CAP 1024 /* store posix file capabilities */
Ethan Yonker8d039f72017-02-03 14:26:15 -0600131#define TAR_STORE_ANDROID_USER_XATTR 2048 /* store android user.* xattr */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500132
133/* this is obsolete - it's here for backwards-compatibility only */
134#define TAR_IGNORE_MAGIC 0
135
136extern const char libtar_version[];
137
138
139/* open a new tarfile handle */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500140int tar_open(TAR **t, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500141 int oflags, int mode, int options);
142
143/* make a tarfile handle out of a previously-opened descriptor */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500144int tar_fdopen(TAR **t, int fd, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500145 int oflags, int mode, int options);
146
147/* returns the descriptor associated with t */
148int tar_fd(TAR *t);
149
150/* close tarfile handle */
151int tar_close(TAR *t);
152
153
154/***** append.c ************************************************************/
155
156/* forward declaration to appease the compiler */
157struct tar_dev;
158
159/* cleanup function */
160void tar_dev_free(struct tar_dev *tdp);
161
162/* Appends a file to the tar archive.
163 * Arguments:
164 * t = TAR handle to append to
165 * realname = path of file to append
166 * savename = name to save the file under in the archive
167 */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500168int tar_append_file(TAR *t, const char *realname, const char *savename);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500169
170/* write EOF indicator */
171int tar_append_eof(TAR *t);
172
173/* add file contents to a tarchive */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500174int tar_append_regfile(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500175
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500176/* Appends in-memory file contents to a tarchive.
177 * Arguments:
178 * t = TAR handle to append to
179 * savename = name to save the file under in the archive
180 * mode = mode
181 * uid, gid = owner
182 * buf, len = in-memory buffer
183 */
184int tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
185 uid_t uid, gid_t gid, void *buf, size_t len);
186
187/* add buffer to a tarchive */
188int tar_append_buffer(TAR *t, void *buf, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500189
190/***** block.c *************************************************************/
191
192/* macros for reading/writing tarchive blocks */
193#define tar_block_read(t, buf) \
194 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
195#define tar_block_write(t, buf) \
196 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
197
198/* read/write a header block */
199int th_read(TAR *t);
200int th_write(TAR *t);
201
202
203/***** decode.c ************************************************************/
204
205/* determine file type */
206#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
207 || (t)->th_buf.typeflag == AREGTYPE \
208 || (t)->th_buf.typeflag == CONTTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500209 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500210 && (t)->th_buf.typeflag != LNKTYPE))
211#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
212#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500213 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500214#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500215 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500216#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500217 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500218#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500219 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500220 || ((t)->th_buf.typeflag == AREGTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500221 && strnlen((t)->th_buf.name, T_NAMELEN) \
222 && ((t)->th_buf.name[strnlen((t)->th_buf.name, T_NAMELEN) - 1] == '/')))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500223#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500224 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500225#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
226#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200227#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
Ethan Yonker71187742017-01-13 13:30:10 -0600228#define TH_ISPOLHEADER(t) ((t)->th_buf.typeflag == TH_POL_TYPE_DO_NOT_USE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500229
230/* decode tar header info */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500231#define th_get_crc(t) oct_to_int((t)->th_buf.chksum, sizeof((t)->th_buf.chksum))
232#define th_get_size(t) oct_to_int_ex((t)->th_buf.size, sizeof((t)->th_buf.size))
233#define th_get_mtime(t) oct_to_int_ex((t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
234#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor, sizeof((t)->th_buf.devmajor))
235#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor, sizeof((t)->th_buf.devminor))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500236#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
237 ? (t)->th_buf.gnu_longlink \
238 : (t)->th_buf.linkname)
239char *th_get_pathname(TAR *t);
240mode_t th_get_mode(TAR *t);
241uid_t th_get_uid(TAR *t);
242gid_t th_get_gid(TAR *t);
243
244
245/***** encode.c ************************************************************/
246
247/* encode file info in th_header */
248void th_set_type(TAR *t, mode_t mode);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500249void th_set_path(TAR *t, const char *pathname);
250void th_set_link(TAR *t, const char *linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500251void th_set_device(TAR *t, dev_t device);
252void th_set_user(TAR *t, uid_t uid);
253void th_set_group(TAR *t, gid_t gid);
254void th_set_mode(TAR *t, mode_t fmode);
255#define th_set_mtime(t, fmtime) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500256 int_to_oct_ex((fmtime), (t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500257#define th_set_size(t, fsize) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500258 int_to_oct_ex((fsize), (t)->th_buf.size, sizeof((t)->th_buf.size))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500259
260/* encode everything at once (except the pathname and linkname) */
261void th_set_from_stat(TAR *t, struct stat *s);
262
263/* encode magic, version, and crc - must be done after everything else is set */
264void th_finish(TAR *t);
265
266
267/***** extract.c ***********************************************************/
268
269/* sequentially extract next file from t */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500270int tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500271
272/* extract different file types */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500273int tar_extract_dir(TAR *t, const char *realname);
274int tar_extract_hardlink(TAR *t, const char *realname, const char *prefix);
275int tar_extract_symlink(TAR *t, const char *realname);
276int tar_extract_chardev(TAR *t, const char *realname);
277int tar_extract_blockdev(TAR *t, const char *realname);
278int tar_extract_fifo(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500279
280/* for regfiles, we need to extract the content blocks as well */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500281int tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500282int tar_skip_regfile(TAR *t);
283
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500284/* extract regfile to buffer */
285int tar_extract_file_contents(TAR *t, void *buf, size_t *lenp);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500286
287/***** output.c ************************************************************/
288
289/* print the tar header */
290void th_print(TAR *t);
291
292/* print "ls -l"-like output for the file described by th */
293void th_print_long_ls(TAR *t);
294
295
296/***** util.c *************************************************************/
297
298/* hashing function for pathnames */
299int path_hashfunc(char *key, int numbuckets);
300
301/* matching function for dev_t's */
302int dev_match(dev_t *dev1, dev_t *dev2);
303
304/* matching function for ino_t's */
305int ino_match(ino_t *ino1, ino_t *ino2);
306
307/* hashing function for dev_t's */
308int dev_hash(dev_t *dev);
309
310/* hashing function for ino_t's */
311int ino_hash(ino_t *inode);
312
313/* create any necessary dirs */
314int mkdirhier(char *path);
315
316/* calculate header checksum */
317int th_crc_calc(TAR *t);
318
319/* calculate a signed header checksum */
320int th_signed_crc_calc(TAR *t);
321
322/* compare checksums in a forgiving way */
323#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
324
325/* string-octal to integer conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500326int64_t oct_to_int(char *oct, size_t len);
327
328/* string-octal or binary to integer conversion */
329int64_t oct_to_int_ex(char *oct, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500330
331/* integer to NULL-terminated string-octal conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500332void int_to_oct(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500333
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500334/* integer to string-octal conversion, or binary as necessary */
335void int_to_oct_ex(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500336
Ethan Yonker71187742017-01-13 13:30:10 -0600337/* prints posix file capabilities */
338void print_caps(struct vfs_cap_data *cap_data);
339
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500340
341/***** wrapper.c **********************************************************/
342
343/* extract groups of files */
344int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500345int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500346
347/* add a whole tree of files */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500348int tar_append_tree(TAR *t, char *realdir, char *savedir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500349
n0d33b511632013-03-06 21:14:15 +0200350/* find an entry */
351int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500352
353#ifdef __cplusplus
354}
355#endif
356
357#endif /* ! LIBTAR_H */
358