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