2fefee0c814e564ba010a59a3244b5695fd5c8cc
2 ** Copyright 1998-2003 University of Illinois Board of Trustees
3 ** Copyright 1998-2003 Mark D. Roth
4 ** All rights reserved.
6 ** libtar.h - header file for libtar library
8 ** Mark D. Roth <roth@uiuc.edu>
9 ** Campus Information Technologies and Educational Services
10 ** University of Illinois at Urbana-Champaign
16 #include <sys/types.h>
20 #include <libtar_listhash.h>
28 /* useful constants */
29 /* see FIXME note in block.c regarding T_BLOCKSIZE */
30 #define T_BLOCKSIZE 512
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 */
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);
74 closefunc_t closefunc
;
76 writefunc_t writefunc
;
87 struct tar_header th_buf
;
90 /* introduced in libtar 1.2.21 */
95 /* constant values for the TAR options field */
96 #define TAR_GNU 1 /* use GNU extensions */
97 #define TAR_VERBOSE 2 /* output file info to stdout */
98 #define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
99 #define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
100 #define TAR_CHECK_MAGIC 16 /* check magic in file header */
101 #define TAR_CHECK_VERSION 32 /* check version in file header */
102 #define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
104 /* this is obsolete - it's here for backwards-compatibility only */
105 #define TAR_IGNORE_MAGIC 0
107 extern const char libtar_version
[];
110 /* open a new tarfile handle */
111 int tar_open(TAR
**t
, const char *pathname
, tartype_t
*type
,
112 int oflags
, int mode
, int options
);
114 /* make a tarfile handle out of a previously-opened descriptor */
115 int tar_fdopen(TAR
**t
, int fd
, const char *pathname
, tartype_t
*type
,
116 int oflags
, int mode
, int options
);
118 /* returns the descriptor associated with t */
121 /* close tarfile handle */
122 int tar_close(TAR
*t
);
125 /***** append.c ************************************************************/
127 /* forward declaration to appease the compiler */
130 /* cleanup function */
131 void tar_dev_free(struct tar_dev
*tdp
);
133 /* Appends a file to the tar archive.
135 * t = TAR handle to append to
136 * realname = path of file to append
137 * savename = name to save the file under in the archive
139 int tar_append_file(TAR
*t
, const char *realname
, const char *savename
);
141 /* write EOF indicator */
142 int tar_append_eof(TAR
*t
);
144 /* add file contents to a tarchive */
145 int tar_append_regfile(TAR
*t
, const char *realname
);
148 /***** block.c *************************************************************/
150 /* macros for reading/writing tarchive blocks */
151 #define tar_block_read(t, buf) \
152 (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
153 #define tar_block_write(t, buf) \
154 (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
156 /* read/write a header block */
158 int th_write(TAR
*t
);
161 /***** decode.c ************************************************************/
163 /* determine file type */
164 #define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
165 || (t)->th_buf.typeflag == AREGTYPE \
166 || (t)->th_buf.typeflag == CONTTYPE \
167 || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode)) \
168 && (t)->th_buf.typeflag != LNKTYPE))
169 #define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
170 #define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
171 || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode)))
172 #define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
173 || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode)))
174 #define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
175 || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode)))
176 #define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
177 || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode)) \
178 || ((t)->th_buf.typeflag == AREGTYPE \
179 && strlen((t)->th_buf.name) \
180 && ((t)->th_buf.name[strlen((t)->th_buf.name) - 1] == '/')))
181 #define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
182 || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode)))
183 #define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
184 #define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
186 /* decode tar header info */
187 #define th_get_crc(t) oct_to_int((t)->th_buf.chksum)
188 #define th_get_size(t) oct_to_int((t)->th_buf.size)
189 #define th_get_mtime(t) oct_to_int((t)->th_buf.mtime)
190 #define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor)
191 #define th_get_devminor(t) oct_to_int((t)->th_buf.devminor)
192 #define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
193 ? (t)->th_buf.gnu_longlink \
194 : (t)->th_buf.linkname)
195 char *th_get_pathname(TAR
*t
);
196 mode_t
th_get_mode(TAR
*t
);
197 uid_t
th_get_uid(TAR
*t
);
198 gid_t
th_get_gid(TAR
*t
);
201 /***** encode.c ************************************************************/
203 /* encode file info in th_header */
204 void th_set_type(TAR
*t
, mode_t mode
);
205 void th_set_path(TAR
*t
, const char *pathname
);
206 void th_set_link(TAR
*t
, const char *linkname
);
207 void th_set_device(TAR
*t
, dev_t device
);
208 void th_set_user(TAR
*t
, uid_t uid
);
209 void th_set_group(TAR
*t
, gid_t gid
);
210 void th_set_mode(TAR
*t
, mode_t fmode
);
211 #define th_set_mtime(t, fmtime) \
212 int_to_oct_nonull((fmtime), (t)->th_buf.mtime, 12)
213 #define th_set_size(t, fsize) \
214 int_to_oct_nonull((fsize), (t)->th_buf.size, 12)
216 /* encode everything at once (except the pathname and linkname) */
217 void th_set_from_stat(TAR
*t
, struct stat
*s
);
219 /* encode magic, version, and crc - must be done after everything else is set */
220 void th_finish(TAR
*t
);
223 /***** extract.c ***********************************************************/
225 /* sequentially extract next file from t */
226 int tar_extract_file(TAR
*t
, char *realname
);
228 /* extract different file types */
229 int tar_extract_dir(TAR
*t
, char *realname
);
230 int tar_extract_hardlink(TAR
*t
, char *realname
);
231 int tar_extract_symlink(TAR
*t
, char *realname
);
232 int tar_extract_chardev(TAR
*t
, char *realname
);
233 int tar_extract_blockdev(TAR
*t
, char *realname
);
234 int tar_extract_fifo(TAR
*t
, char *realname
);
236 /* for regfiles, we need to extract the content blocks as well */
237 int tar_extract_regfile(TAR
*t
, char *realname
);
238 int tar_skip_regfile(TAR
*t
);
241 /***** output.c ************************************************************/
243 /* print the tar header */
244 void th_print(TAR
*t
);
246 /* print "ls -l"-like output for the file described by th */
247 void th_print_long_ls(TAR
*t
);
250 /***** util.c *************************************************************/
252 /* hashing function for pathnames */
253 int path_hashfunc(char *key
, int numbuckets
);
255 /* matching function for dev_t's */
256 int dev_match(dev_t
*dev1
, dev_t
*dev2
);
258 /* matching function for ino_t's */
259 int ino_match(ino_t
*ino1
, ino_t
*ino2
);
261 /* hashing function for dev_t's */
262 int dev_hash(dev_t
*dev
);
264 /* hashing function for ino_t's */
265 int ino_hash(ino_t
*inode
);
267 /* create any necessary dirs */
268 int mkdirhier(char *path
);
270 /* calculate header checksum */
271 int th_crc_calc(TAR
*t
);
273 /* calculate a signed header checksum */
274 int th_signed_crc_calc(TAR
*t
);
276 /* compare checksums in a forgiving way */
277 #define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t))
279 /* string-octal to integer conversion */
280 int oct_to_int(char *oct
);
282 /* integer to NULL-terminated string-octal conversion */
283 #define int_to_oct(num, oct, octlen) \
284 snprintf((oct), (octlen), "%*lo ", (octlen) - 2, (unsigned long)(num))
286 /* integer to string-octal conversion, no NULL */
287 void int_to_oct_nonull(int num
, char *oct
, size_t octlen
);
290 /***** wrapper.c **********************************************************/
292 /* extract groups of files */
293 int tar_extract_glob(TAR
*t
, char *globname
, char *prefix
);
294 int tar_extract_all(TAR
*t
, char *prefix
);
296 /* add a whole tree of files */
297 int tar_append_tree(TAR
*t
, char *realdir
, char *savedir
);
304 #endif /* ! LIBTAR_H */