blob: 4a513754fb5c0defc33bc803bf6d87bfc1cc1dc9 [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>
18#include "tar.h"
19
20#include "libtar_listhash.h"
21
22#ifdef __cplusplus
23extern "C"
24{
25#endif
26
27
28/* useful constants */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050029/* see FIXME note in block.c regarding T_BLOCKSIZE */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050030#define T_BLOCKSIZE 512
31#define T_NAMELEN 100
32#define T_PREFIXLEN 155
33#define T_MAXPATHLEN (T_NAMELEN + T_PREFIXLEN)
34
35/* GNU extensions for typeflag */
36#define GNU_LONGNAME_TYPE 'L'
37#define GNU_LONGLINK_TYPE 'K'
38
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020039/* extended metadata for next file - used to store selinux_context */
40#define TH_EXT_TYPE 'x'
41
bigbiff bigbiff9c754052013-01-09 09:09:08 -050042/* our version of the tar header structure */
43struct tar_header
44{
45 char name[100];
46 char mode[8];
47 char uid[8];
48 char gid[8];
49 char size[12];
50 char mtime[12];
51 char chksum[8];
52 char typeflag;
53 char linkname[100];
54 char magic[6];
55 char version[2];
56 char uname[32];
57 char gname[32];
58 char devmajor[8];
59 char devminor[8];
60 char prefix[155];
61 char padding[12];
62 char *gnu_longname;
63 char *gnu_longlink;
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020064#ifdef HAVE_SELINUX
65 char *selinux_context;
66#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -050067};
68
69
70/***** handle.c ************************************************************/
71
72typedef int (*openfunc_t)(const char *, int, ...);
73typedef int (*closefunc_t)(int);
74typedef ssize_t (*readfunc_t)(int, void *, size_t);
75typedef ssize_t (*writefunc_t)(int, const void *, size_t);
76
77typedef struct
78{
79 openfunc_t openfunc;
80 closefunc_t closefunc;
81 readfunc_t readfunc;
82 writefunc_t writefunc;
83}
84tartype_t;
85
86typedef struct
87{
88 tartype_t *type;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050089 const char *pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050090 long fd;
91 int oflags;
92 int options;
93 struct tar_header th_buf;
94 libtar_hash_t *h;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050095
96 /* introduced in libtar 1.2.21 */
97 char *th_pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050098}
99TAR;
100
101/* constant values for the TAR options field */
102#define TAR_GNU 1 /* use GNU extensions */
103#define TAR_VERBOSE 2 /* output file info to stdout */
104#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
105#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
106#define TAR_CHECK_MAGIC 16 /* check magic in file header */
107#define TAR_CHECK_VERSION 32 /* check version in file header */
108#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200109#define TAR_STORE_SELINUX 128 /* store selinux context */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500110#define TAR_USE_NUMERIC_ID 256 /* favor numeric owner over names */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500111
112/* this is obsolete - it's here for backwards-compatibility only */
113#define TAR_IGNORE_MAGIC 0
114
115extern const char libtar_version[];
116
117
118/* open a new tarfile handle */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500119int tar_open(TAR **t, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500120 int oflags, int mode, int options);
121
122/* make a tarfile handle out of a previously-opened descriptor */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500123int tar_fdopen(TAR **t, int fd, const char *pathname, tartype_t *type,
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500124 int oflags, int mode, int options);
125
126/* returns the descriptor associated with t */
127int tar_fd(TAR *t);
128
129/* close tarfile handle */
130int tar_close(TAR *t);
131
132
133/***** append.c ************************************************************/
134
135/* forward declaration to appease the compiler */
136struct tar_dev;
137
138/* cleanup function */
139void tar_dev_free(struct tar_dev *tdp);
140
141/* Appends a file to the tar archive.
142 * Arguments:
143 * t = TAR handle to append to
144 * realname = path of file to append
145 * savename = name to save the file under in the archive
146 */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500147int tar_append_file(TAR *t, const char *realname, const char *savename);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500148
149/* write EOF indicator */
150int tar_append_eof(TAR *t);
151
152/* add file contents to a tarchive */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500153int tar_append_regfile(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500154
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500155/* Appends in-memory file contents to a tarchive.
156 * Arguments:
157 * t = TAR handle to append to
158 * savename = name to save the file under in the archive
159 * mode = mode
160 * uid, gid = owner
161 * buf, len = in-memory buffer
162 */
163int tar_append_file_contents(TAR *t, const char *savename, mode_t mode,
164 uid_t uid, gid_t gid, void *buf, size_t len);
165
166/* add buffer to a tarchive */
167int tar_append_buffer(TAR *t, void *buf, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500168
169/***** block.c *************************************************************/
170
171/* macros for reading/writing tarchive blocks */
172#define tar_block_read(t, buf) \
173 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
174#define tar_block_write(t, buf) \
175 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
176
177/* read/write a header block */
178int th_read(TAR *t);
179int th_write(TAR *t);
180
181
182/***** decode.c ************************************************************/
183
184/* determine file type */
185#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
186 || (t)->th_buf.typeflag == AREGTYPE \
187 || (t)->th_buf.typeflag == CONTTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500188 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500189 && (t)->th_buf.typeflag != LNKTYPE))
190#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
191#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500192 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500193#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500194 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500195#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500196 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500197#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500198 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))) \
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500199 || ((t)->th_buf.typeflag == AREGTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500200 && strnlen((t)->th_buf.name, T_NAMELEN) \
201 && ((t)->th_buf.name[strnlen((t)->th_buf.name, T_NAMELEN) - 1] == '/')))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500202#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500203 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode, sizeof((t)->th_buf.mode))))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500204#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
205#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200206#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500207
208/* decode tar header info */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500209#define th_get_crc(t) oct_to_int((t)->th_buf.chksum, sizeof((t)->th_buf.chksum))
210#define th_get_size(t) oct_to_int_ex((t)->th_buf.size, sizeof((t)->th_buf.size))
211#define th_get_mtime(t) oct_to_int_ex((t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
212#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor, sizeof((t)->th_buf.devmajor))
213#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor, sizeof((t)->th_buf.devminor))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500214#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
215 ? (t)->th_buf.gnu_longlink \
216 : (t)->th_buf.linkname)
217char *th_get_pathname(TAR *t);
218mode_t th_get_mode(TAR *t);
219uid_t th_get_uid(TAR *t);
220gid_t th_get_gid(TAR *t);
221
222
223/***** encode.c ************************************************************/
224
225/* encode file info in th_header */
226void th_set_type(TAR *t, mode_t mode);
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500227void th_set_path(TAR *t, const char *pathname);
228void th_set_link(TAR *t, const char *linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500229void th_set_device(TAR *t, dev_t device);
230void th_set_user(TAR *t, uid_t uid);
231void th_set_group(TAR *t, gid_t gid);
232void th_set_mode(TAR *t, mode_t fmode);
233#define th_set_mtime(t, fmtime) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500234 int_to_oct_ex((fmtime), (t)->th_buf.mtime, sizeof((t)->th_buf.mtime))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500235#define th_set_size(t, fsize) \
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500236 int_to_oct_ex((fsize), (t)->th_buf.size, sizeof((t)->th_buf.size))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500237
238/* encode everything at once (except the pathname and linkname) */
239void th_set_from_stat(TAR *t, struct stat *s);
240
241/* encode magic, version, and crc - must be done after everything else is set */
242void th_finish(TAR *t);
243
244
245/***** extract.c ***********************************************************/
246
247/* sequentially extract next file from t */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500248int tar_extract_file(TAR *t, const char *realname, const char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500249
250/* extract different file types */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500251int tar_extract_dir(TAR *t, const char *realname);
252int tar_extract_hardlink(TAR *t, const char *realname, const char *prefix);
253int tar_extract_symlink(TAR *t, const char *realname);
254int tar_extract_chardev(TAR *t, const char *realname);
255int tar_extract_blockdev(TAR *t, const char *realname);
256int tar_extract_fifo(TAR *t, const char *realname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500257
258/* for regfiles, we need to extract the content blocks as well */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500259int tar_extract_regfile(TAR *t, const char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500260int tar_skip_regfile(TAR *t);
261
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500262/* extract regfile to buffer */
263int tar_extract_file_contents(TAR *t, void *buf, size_t *lenp);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500264
265/***** output.c ************************************************************/
266
267/* print the tar header */
268void th_print(TAR *t);
269
270/* print "ls -l"-like output for the file described by th */
271void th_print_long_ls(TAR *t);
272
273
274/***** util.c *************************************************************/
275
276/* hashing function for pathnames */
277int path_hashfunc(char *key, int numbuckets);
278
279/* matching function for dev_t's */
280int dev_match(dev_t *dev1, dev_t *dev2);
281
282/* matching function for ino_t's */
283int ino_match(ino_t *ino1, ino_t *ino2);
284
285/* hashing function for dev_t's */
286int dev_hash(dev_t *dev);
287
288/* hashing function for ino_t's */
289int ino_hash(ino_t *inode);
290
291/* create any necessary dirs */
292int mkdirhier(char *path);
293
294/* calculate header checksum */
295int th_crc_calc(TAR *t);
296
297/* calculate a signed header checksum */
298int th_signed_crc_calc(TAR *t);
299
300/* compare checksums in a forgiving way */
301#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
302
303/* string-octal to integer conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500304int64_t oct_to_int(char *oct, size_t len);
305
306/* string-octal or binary to integer conversion */
307int64_t oct_to_int_ex(char *oct, size_t len);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500308
309/* integer to NULL-terminated string-octal conversion */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500310void int_to_oct(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500311
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500312/* integer to string-octal conversion, or binary as necessary */
313void int_to_oct_ex(int64_t num, char *oct, size_t octlen);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500314
315
316/***** wrapper.c **********************************************************/
317
318/* extract groups of files */
319int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500320int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500321
322/* add a whole tree of files */
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500323int tar_append_tree(TAR *t, char *realdir, char *savedir);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500324
n0d33b511632013-03-06 21:14:15 +0200325/* find an entry */
326int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500327
328#ifdef __cplusplus
329}
330#endif
331
332#endif /* ! LIBTAR_H */
333