blob: 8c4202817acaafba101239c848059104b4cbb46b [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#ifdef HAVE_SELINUX
72 char *selinux_context;
73#endif
Ethan Yonker79f88bd2016-12-09 14:52:12 -060074#ifdef HAVE_EXT4_CRYPT
75 char *e4crypt_policy;
76#endif
Ethan Yonker71187742017-01-13 13:30:10 -060077 int has_cap_data;
78 struct vfs_cap_data cap_data;
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 */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500125
126/* this is obsolete - it's here for backwards-compatibility only */
127#define TAR_IGNORE_MAGIC 0
128
129extern const char libtar_version[];
130
131
132/* open a new tarfile handle */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500133int tar_open(TAR **t, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500134 int oflags, int mode, int options);
135
136/* make a tarfile handle out of a previously-opened descriptor */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500137int tar_fdopen(TAR **t, int fd, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500138 int oflags, int mode, int options);
139
140/* returns the descriptor associated with t */
141int tar_fd(TAR *t);
142
143/* close tarfile handle */
144int tar_close(TAR *t);
145
146
147/***** append.c ************************************************************/
148
149/* forward declaration to appease the compiler */
150struct tar_dev;
151
152/* cleanup function */
153void tar_dev_free(struct tar_dev *tdp);
154
155/* Appends a file to the tar archive.
156 * Arguments:
157 * t = TAR handle to append to
158 * realname = path of file to append
159 * savename = name to save the file under in the archive
160 */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500161int tar_append_file(TAR *t, const char *realname, const char *savename);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500162
163/* write EOF indicator */
164int tar_append_eof(TAR *t);
165
166/* add file contents to a tarchive */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500167int tar_append_regfile(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500168
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500169/* Appends in-memory file contents to a tarchive.
170 * Arguments:
171 * t = TAR handle to append to
172 * savename = name to save the file under in the archive
173 * mode = mode
174 * uid, gid = owner
175 * buf, len = in-memory buffer
176 */
177int tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
178 uid_t uid, gid_t gid, void *buf, size_t len);
179
180/* add buffer to a tarchive */
181int tar_append_buffer(TAR *t, void *buf, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500182
183/***** block.c *************************************************************/
184
185/* macros for reading/writing tarchive blocks */
186#define tar_block_read(t, buf) \
187 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
188#define tar_block_write(t, buf) \
189 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
190
191/* read/write a header block */
192int th_read(TAR *t);
193int th_write(TAR *t);
194
195
196/***** decode.c ************************************************************/
197
198/* determine file type */
199#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
200 || (t)->th_buf.typeflag == AREGTYPE \
201 || (t)->th_buf.typeflag == CONTTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500202 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500203 && (t)->th_buf.typeflag != LNKTYPE))
204#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
205#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500206 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500207#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500208 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500209#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500210 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500211#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500212 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500213 || ((t)->th_buf.typeflag == AREGTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500214 && strnlen((t)->th_buf.name, T_NAMELEN) \
215 && ((t)->th_buf.name[strnlen((t)->th_buf.name, T_NAMELEN) - 1] == '/')))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500216#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500217 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500218#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
219#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200220#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
Ethan Yonker71187742017-01-13 13:30:10 -0600221#define TH_ISPOLHEADER(t) ((t)->th_buf.typeflag == TH_POL_TYPE_DO_NOT_USE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500222
223/* decode tar header info */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500224#define th_get_crc(t) oct_to_int((t)->th_buf.chksum, sizeof((t)->th_buf.chksum))
225#define th_get_size(t) oct_to_int_ex((t)->th_buf.size, sizeof((t)->th_buf.size))
226#define th_get_mtime(t) oct_to_int_ex((t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
227#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor, sizeof((t)->th_buf.devmajor))
228#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor, sizeof((t)->th_buf.devminor))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500229#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
230 ? (t)->th_buf.gnu_longlink \
231 : (t)->th_buf.linkname)
232char *th_get_pathname(TAR *t);
233mode_t th_get_mode(TAR *t);
234uid_t th_get_uid(TAR *t);
235gid_t th_get_gid(TAR *t);
236
237
238/***** encode.c ************************************************************/
239
240/* encode file info in th_header */
241void th_set_type(TAR *t, mode_t mode);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500242void th_set_path(TAR *t, const char *pathname);
243void th_set_link(TAR *t, const char *linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500244void th_set_device(TAR *t, dev_t device);
245void th_set_user(TAR *t, uid_t uid);
246void th_set_group(TAR *t, gid_t gid);
247void th_set_mode(TAR *t, mode_t fmode);
248#define th_set_mtime(t, fmtime) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500249 int_to_oct_ex((fmtime), (t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500250#define th_set_size(t, fsize) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500251 int_to_oct_ex((fsize), (t)->th_buf.size, sizeof((t)->th_buf.size))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500252
253/* encode everything at once (except the pathname and linkname) */
254void th_set_from_stat(TAR *t, struct stat *s);
255
256/* encode magic, version, and crc - must be done after everything else is set */
257void th_finish(TAR *t);
258
259
260/***** extract.c ***********************************************************/
261
262/* sequentially extract next file from t */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500263int tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500264
265/* extract different file types */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500266int tar_extract_dir(TAR *t, const char *realname);
267int tar_extract_hardlink(TAR *t, const char *realname, const char *prefix);
268int tar_extract_symlink(TAR *t, const char *realname);
269int tar_extract_chardev(TAR *t, const char *realname);
270int tar_extract_blockdev(TAR *t, const char *realname);
271int tar_extract_fifo(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500272
273/* for regfiles, we need to extract the content blocks as well */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500274int tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500275int tar_skip_regfile(TAR *t);
276
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500277/* extract regfile to buffer */
278int tar_extract_file_contents(TAR *t, void *buf, size_t *lenp);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500279
280/***** output.c ************************************************************/
281
282/* print the tar header */
283void th_print(TAR *t);
284
285/* print "ls -l"-like output for the file described by th */
286void th_print_long_ls(TAR *t);
287
288
289/***** util.c *************************************************************/
290
291/* hashing function for pathnames */
292int path_hashfunc(char *key, int numbuckets);
293
294/* matching function for dev_t's */
295int dev_match(dev_t *dev1, dev_t *dev2);
296
297/* matching function for ino_t's */
298int ino_match(ino_t *ino1, ino_t *ino2);
299
300/* hashing function for dev_t's */
301int dev_hash(dev_t *dev);
302
303/* hashing function for ino_t's */
304int ino_hash(ino_t *inode);
305
306/* create any necessary dirs */
307int mkdirhier(char *path);
308
309/* calculate header checksum */
310int th_crc_calc(TAR *t);
311
312/* calculate a signed header checksum */
313int th_signed_crc_calc(TAR *t);
314
315/* compare checksums in a forgiving way */
316#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
317
318/* string-octal to integer conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500319int64_t oct_to_int(char *oct, size_t len);
320
321/* string-octal or binary to integer conversion */
322int64_t oct_to_int_ex(char *oct, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500323
324/* integer to NULL-terminated string-octal conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500325void int_to_oct(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500326
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500327/* integer to string-octal conversion, or binary as necessary */
328void int_to_oct_ex(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500329
Ethan Yonker71187742017-01-13 13:30:10 -0600330/* prints posix file capabilities */
331void print_caps(struct vfs_cap_data *cap_data);
332
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500333
334/***** wrapper.c **********************************************************/
335
336/* extract groups of files */
337int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500338int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500339
340/* add a whole tree of files */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500341int tar_append_tree(TAR *t, char *realdir, char *savedir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500342
n0d33b511632013-03-06 21:14:15 +0200343/* find an entry */
344int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500345
346#ifdef __cplusplus
347}
348#endif
349
350#endif /* ! LIBTAR_H */
351