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