blob: 4d921247bfd9206c02ff011b2a0d7e3a742f7de3 [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;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050077};
78
79
80/***** handle.c ************************************************************/
81
82typedef int (*openfunc_t)(const char *, int, ...);
83typedef int (*closefunc_t)(int);
84typedef ssize_t (*readfunc_t)(int, void *, size_t);
85typedef ssize_t (*writefunc_t)(int, const void *, size_t);
86
87typedef struct
88{
89 openfunc_t openfunc;
90 closefunc_t closefunc;
91 readfunc_t readfunc;
92 writefunc_t writefunc;
93}
94tartype_t;
95
96typedef struct
97{
98 tartype_t *type;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050099 const char *pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500100 long fd;
101 int oflags;
102 int options;
103 struct tar_header th_buf;
104 libtar_hash_t *h;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500105
106 /* introduced in libtar 1.2.21 */
107 char *th_pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500108}
109TAR;
110
111/* constant values for the TAR options field */
112#define TAR_GNU 1 /* use GNU extensions */
113#define TAR_VERBOSE 2 /* output file info to stdout */
114#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
115#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
116#define TAR_CHECK_MAGIC 16 /* check magic in file header */
117#define TAR_CHECK_VERSION 32 /* check version in file header */
118#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200119#define TAR_STORE_SELINUX 128 /* store selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500120#define TAR_USE_NUMERIC_ID 256 /* favor numeric owner over names */
Ethan Yonker79f88bd2016-12-09 14:52:12 -0600121#define TAR_STORE_EXT4_POL 512 /* store ext4 crypto policy */
Ethan Yonker71187742017-01-13 13:30:10 -0600122#define TAR_STORE_POSIX_CAP 1024 /* store posix file capabilities */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500123
124/* this is obsolete - it's here for backwards-compatibility only */
125#define TAR_IGNORE_MAGIC 0
126
127extern const char libtar_version[];
128
129
130/* open a new tarfile handle */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500131int tar_open(TAR **t, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500132 int oflags, int mode, int options);
133
134/* make a tarfile handle out of a previously-opened descriptor */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500135int tar_fdopen(TAR **t, int fd, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500136 int oflags, int mode, int options);
137
138/* returns the descriptor associated with t */
139int tar_fd(TAR *t);
140
141/* close tarfile handle */
142int tar_close(TAR *t);
143
144
145/***** append.c ************************************************************/
146
147/* forward declaration to appease the compiler */
148struct tar_dev;
149
150/* cleanup function */
151void tar_dev_free(struct tar_dev *tdp);
152
153/* Appends a file to the tar archive.
154 * Arguments:
155 * t = TAR handle to append to
156 * realname = path of file to append
157 * savename = name to save the file under in the archive
158 */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500159int tar_append_file(TAR *t, const char *realname, const char *savename);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500160
161/* write EOF indicator */
162int tar_append_eof(TAR *t);
163
164/* add file contents to a tarchive */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500165int tar_append_regfile(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500166
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500167/* Appends in-memory file contents to a tarchive.
168 * Arguments:
169 * t = TAR handle to append to
170 * savename = name to save the file under in the archive
171 * mode = mode
172 * uid, gid = owner
173 * buf, len = in-memory buffer
174 */
175int tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
176 uid_t uid, gid_t gid, void *buf, size_t len);
177
178/* add buffer to a tarchive */
179int tar_append_buffer(TAR *t, void *buf, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500180
181/***** block.c *************************************************************/
182
183/* macros for reading/writing tarchive blocks */
184#define tar_block_read(t, buf) \
185 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
186#define tar_block_write(t, buf) \
187 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
188
189/* read/write a header block */
190int th_read(TAR *t);
191int th_write(TAR *t);
192
193
194/***** decode.c ************************************************************/
195
196/* determine file type */
197#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
198 || (t)->th_buf.typeflag == AREGTYPE \
199 || (t)->th_buf.typeflag == CONTTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500200 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500201 && (t)->th_buf.typeflag != LNKTYPE))
202#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
203#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500204 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500205#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500206 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500207#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500208 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500209#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500210 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211 || ((t)->th_buf.typeflag == AREGTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500212 && strnlen((t)->th_buf.name, T_NAMELEN) \
213 && ((t)->th_buf.name[strnlen((t)->th_buf.name, T_NAMELEN) - 1] == '/')))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500214#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500215 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500216#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
217#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200218#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
Ethan Yonker71187742017-01-13 13:30:10 -0600219#define TH_ISPOLHEADER(t) ((t)->th_buf.typeflag == TH_POL_TYPE_DO_NOT_USE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500220
221/* decode tar header info */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500222#define th_get_crc(t) oct_to_int((t)->th_buf.chksum, sizeof((t)->th_buf.chksum))
223#define th_get_size(t) oct_to_int_ex((t)->th_buf.size, sizeof((t)->th_buf.size))
224#define th_get_mtime(t) oct_to_int_ex((t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
225#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor, sizeof((t)->th_buf.devmajor))
226#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor, sizeof((t)->th_buf.devminor))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500227#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
228 ? (t)->th_buf.gnu_longlink \
229 : (t)->th_buf.linkname)
230char *th_get_pathname(TAR *t);
231mode_t th_get_mode(TAR *t);
232uid_t th_get_uid(TAR *t);
233gid_t th_get_gid(TAR *t);
234
235
236/***** encode.c ************************************************************/
237
238/* encode file info in th_header */
239void th_set_type(TAR *t, mode_t mode);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500240void th_set_path(TAR *t, const char *pathname);
241void th_set_link(TAR *t, const char *linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500242void th_set_device(TAR *t, dev_t device);
243void th_set_user(TAR *t, uid_t uid);
244void th_set_group(TAR *t, gid_t gid);
245void th_set_mode(TAR *t, mode_t fmode);
246#define th_set_mtime(t, fmtime) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500247 int_to_oct_ex((fmtime), (t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500248#define th_set_size(t, fsize) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500249 int_to_oct_ex((fsize), (t)->th_buf.size, sizeof((t)->th_buf.size))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500250
251/* encode everything at once (except the pathname and linkname) */
252void th_set_from_stat(TAR *t, struct stat *s);
253
254/* encode magic, version, and crc - must be done after everything else is set */
255void th_finish(TAR *t);
256
257
258/***** extract.c ***********************************************************/
259
260/* sequentially extract next file from t */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500261int tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500262
263/* extract different file types */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500264int tar_extract_dir(TAR *t, const char *realname);
265int tar_extract_hardlink(TAR *t, const char *realname, const char *prefix);
266int tar_extract_symlink(TAR *t, const char *realname);
267int tar_extract_chardev(TAR *t, const char *realname);
268int tar_extract_blockdev(TAR *t, const char *realname);
269int tar_extract_fifo(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500270
271/* for regfiles, we need to extract the content blocks as well */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500272int tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500273int tar_skip_regfile(TAR *t);
274
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500275/* extract regfile to buffer */
276int tar_extract_file_contents(TAR *t, void *buf, size_t *lenp);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500277
278/***** output.c ************************************************************/
279
280/* print the tar header */
281void th_print(TAR *t);
282
283/* print "ls -l"-like output for the file described by th */
284void th_print_long_ls(TAR *t);
285
286
287/***** util.c *************************************************************/
288
289/* hashing function for pathnames */
290int path_hashfunc(char *key, int numbuckets);
291
292/* matching function for dev_t's */
293int dev_match(dev_t *dev1, dev_t *dev2);
294
295/* matching function for ino_t's */
296int ino_match(ino_t *ino1, ino_t *ino2);
297
298/* hashing function for dev_t's */
299int dev_hash(dev_t *dev);
300
301/* hashing function for ino_t's */
302int ino_hash(ino_t *inode);
303
304/* create any necessary dirs */
305int mkdirhier(char *path);
306
307/* calculate header checksum */
308int th_crc_calc(TAR *t);
309
310/* calculate a signed header checksum */
311int th_signed_crc_calc(TAR *t);
312
313/* compare checksums in a forgiving way */
314#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
315
316/* string-octal to integer conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500317int64_t oct_to_int(char *oct, size_t len);
318
319/* string-octal or binary to integer conversion */
320int64_t oct_to_int_ex(char *oct, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500321
322/* integer to NULL-terminated string-octal conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500323void int_to_oct(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500324
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500325/* integer to string-octal conversion, or binary as necessary */
326void int_to_oct_ex(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500327
Ethan Yonker71187742017-01-13 13:30:10 -0600328/* prints posix file capabilities */
329void print_caps(struct vfs_cap_data *cap_data);
330
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500331
332/***** wrapper.c **********************************************************/
333
334/* extract groups of files */
335int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500336int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500337
338/* add a whole tree of files */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500339int tar_append_tree(TAR *t, char *realdir, char *savedir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500340
n0d33b511632013-03-06 21:14:15 +0200341/* find an entry */
342int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500343
344#ifdef __cplusplus
345}
346#endif
347
348#endif /* ! LIBTAR_H */
349