blob: e3154ae905055cf20a129b112f7684a5ca4369d8 [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 */
29#define T_BLOCKSIZE 512
30#define T_NAMELEN 100
31#define T_PREFIXLEN 155
32#define T_MAXPATHLEN (T_NAMELEN + T_PREFIXLEN)
33
34/* GNU extensions for typeflag */
35#define GNU_LONGNAME_TYPE 'L'
36#define GNU_LONGLINK_TYPE 'K'
37
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020038/* extended metadata for next file - used to store selinux_context */
39#define TH_EXT_TYPE 'x'
40
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041/* our version of the tar header structure */
42struct tar_header
43{
44 char name[100];
45 char mode[8];
46 char uid[8];
47 char gid[8];
48 char size[12];
49 char mtime[12];
50 char chksum[8];
51 char typeflag;
52 char linkname[100];
53 char magic[6];
54 char version[2];
55 char uname[32];
56 char gname[32];
57 char devmajor[8];
58 char devminor[8];
59 char prefix[155];
60 char padding[12];
61 char *gnu_longname;
62 char *gnu_longlink;
Vojtech Bocek25fd68d2013-08-27 03:10:10 +020063#ifdef HAVE_SELINUX
64 char *selinux_context;
65#endif
bigbiff bigbiff9c754052013-01-09 09:09:08 -050066};
67
68
69/***** handle.c ************************************************************/
70
71typedef int (*openfunc_t)(const char *, int, ...);
72typedef int (*closefunc_t)(int);
73typedef ssize_t (*readfunc_t)(int, void *, size_t);
74typedef ssize_t (*writefunc_t)(int, const void *, size_t);
75
76typedef struct
77{
78 openfunc_t openfunc;
79 closefunc_t closefunc;
80 readfunc_t readfunc;
81 writefunc_t writefunc;
82}
83tartype_t;
84
85typedef struct
86{
87 tartype_t *type;
88 char *pathname;
89 long fd;
90 int oflags;
91 int options;
92 struct tar_header th_buf;
93 libtar_hash_t *h;
94}
95TAR;
96
97/* constant values for the TAR options field */
98#define TAR_GNU 1 /* use GNU extensions */
99#define TAR_VERBOSE 2 /* output file info to stdout */
100#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
101#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
102#define TAR_CHECK_MAGIC 16 /* check magic in file header */
103#define TAR_CHECK_VERSION 32 /* check version in file header */
104#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200105#define TAR_STORE_SELINUX 128 /* store selinux context */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500106
107/* this is obsolete - it's here for backwards-compatibility only */
108#define TAR_IGNORE_MAGIC 0
109
110extern const char libtar_version[];
111
112
113/* open a new tarfile handle */
114int tar_open(TAR **t, char *pathname, tartype_t *type,
115 int oflags, int mode, int options);
116
117/* make a tarfile handle out of a previously-opened descriptor */
118int tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type,
119 int oflags, int mode, int options);
120
121/* returns the descriptor associated with t */
122int tar_fd(TAR *t);
123
124/* close tarfile handle */
125int tar_close(TAR *t);
126
127
128/***** append.c ************************************************************/
129
130/* forward declaration to appease the compiler */
131struct tar_dev;
132
133/* cleanup function */
134void tar_dev_free(struct tar_dev *tdp);
135
136/* Appends a file to the tar archive.
137 * Arguments:
138 * t = TAR handle to append to
139 * realname = path of file to append
140 * savename = name to save the file under in the archive
141 */
142int tar_append_file(TAR *t, char *realname, char *savename);
143
144/* write EOF indicator */
145int tar_append_eof(TAR *t);
146
147/* add file contents to a tarchive */
148int tar_append_regfile(TAR *t, char *realname);
149
150
151/***** block.c *************************************************************/
152
153/* macros for reading/writing tarchive blocks */
154#define tar_block_read(t, buf) \
155 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
156#define tar_block_write(t, buf) \
157 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
158
159/* read/write a header block */
160int th_read(TAR *t);
161int th_write(TAR *t);
162
163
164/***** decode.c ************************************************************/
165
166/* determine file type */
167#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
168 || (t)->th_buf.typeflag == AREGTYPE \
169 || (t)->th_buf.typeflag == CONTTYPE \
170 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode)) \
171 && (t)->th_buf.typeflag != LNKTYPE))
172#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
173#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
174 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode)))
175#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
176 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode)))
177#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
178 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode)))
179#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
180 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode)) \
181 || ((t)->th_buf.typeflag == AREGTYPE \
182 && ((t)->th_buf.name[strlen((t)->th_buf.name) - 1] == '/')))
183#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
184 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode)))
185#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
186#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
Vojtech Bocek25fd68d2013-08-27 03:10:10 +0200187#define TH_ISEXTHEADER(t) ((t)->th_buf.typeflag == TH_EXT_TYPE)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500188
189/* decode tar header info */
190#define th_get_crc(t) oct_to_int((t)->th_buf.chksum)
191#define th_get_size(t) oct_to_int((t)->th_buf.size)
192#define th_get_mtime(t) oct_to_int((t)->th_buf.mtime)
193#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor)
194#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor)
195#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
196 ? (t)->th_buf.gnu_longlink \
197 : (t)->th_buf.linkname)
198char *th_get_pathname(TAR *t);
199mode_t th_get_mode(TAR *t);
200uid_t th_get_uid(TAR *t);
201gid_t th_get_gid(TAR *t);
202
203
204/***** encode.c ************************************************************/
205
206/* encode file info in th_header */
207void th_set_type(TAR *t, mode_t mode);
208void th_set_path(TAR *t, char *pathname);
209void th_set_link(TAR *t, char *linkname);
210void th_set_device(TAR *t, dev_t device);
211void th_set_user(TAR *t, uid_t uid);
212void th_set_group(TAR *t, gid_t gid);
213void th_set_mode(TAR *t, mode_t fmode);
214#define th_set_mtime(t, fmtime) \
215 int_to_oct_nonull((fmtime), (t)->th_buf.mtime, 12)
216#define th_set_size(t, fsize) \
217 int_to_oct_nonull((fsize), (t)->th_buf.size, 12)
218
219/* encode everything at once (except the pathname and linkname) */
220void th_set_from_stat(TAR *t, struct stat *s);
221
222/* encode magic, version, and crc - must be done after everything else is set */
223void th_finish(TAR *t);
224
225
226/***** extract.c ***********************************************************/
227
228/* sequentially extract next file from t */
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500229int tar_extract_file(TAR *t, char *realname, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500230
231/* extract different file types */
232int tar_extract_dir(TAR *t, char *realname);
Dees_Troyee6632c2013-02-27 18:07:32 +0000233int tar_extract_hardlink(TAR *t, char *realname, char *prefix);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500234int tar_extract_symlink(TAR *t, char *realname);
235int tar_extract_chardev(TAR *t, char *realname);
236int tar_extract_blockdev(TAR *t, char *realname);
237int tar_extract_fifo(TAR *t, char *realname);
238
239/* for regfiles, we need to extract the content blocks as well */
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500240int tar_extract_regfile(TAR *t, char *realname, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500241int tar_skip_regfile(TAR *t);
242
243
244/***** output.c ************************************************************/
245
246/* print the tar header */
247void th_print(TAR *t);
248
249/* print "ls -l"-like output for the file described by th */
250void th_print_long_ls(TAR *t);
251
252
253/***** util.c *************************************************************/
254
255/* hashing function for pathnames */
256int path_hashfunc(char *key, int numbuckets);
257
258/* matching function for dev_t's */
259int dev_match(dev_t *dev1, dev_t *dev2);
260
261/* matching function for ino_t's */
262int ino_match(ino_t *ino1, ino_t *ino2);
263
264/* hashing function for dev_t's */
265int dev_hash(dev_t *dev);
266
267/* hashing function for ino_t's */
268int ino_hash(ino_t *inode);
269
270/* create any necessary dirs */
271int mkdirhier(char *path);
272
273/* calculate header checksum */
274int th_crc_calc(TAR *t);
275
276/* calculate a signed header checksum */
277int th_signed_crc_calc(TAR *t);
278
279/* compare checksums in a forgiving way */
280#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
281
282/* string-octal to integer conversion */
283int oct_to_int(char *oct);
284
285/* integer to NULL-terminated string-octal conversion */
286#define int_to_oct(num, oct, octlen) \
287 snprintf((oct), (octlen), "%*lo ", (octlen) - 2, (unsigned long)(num))
288
289/* integer to string-octal conversion, no NULL */
290void int_to_oct_nonull(int num, char *oct, size_t octlen);
291
292
293/***** wrapper.c **********************************************************/
294
295/* extract groups of files */
296int tar_extract_glob(TAR *t, char *globname, char *prefix);
Ethan Yonker1b7a31b2014-07-03 15:09:22 -0500297int tar_extract_all(TAR *t, char *prefix, const int *progress_fd);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500298
299/* add a whole tree of files */
Dees_Troy83bd4832013-05-04 12:39:56 +0000300int tar_append_tree(TAR *t, char *realdir, char *savedir, char *exclude);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500301
n0d33b511632013-03-06 21:14:15 +0200302/* find an entry */
303int tar_find(TAR *t, char *searchstr);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500304
305#ifdef __cplusplus
306}
307#endif
308
309#endif /* ! LIBTAR_H */
310