libtar.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #ifndef LIBTAR_H
  13. #define LIBTAR_H
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <libtar/tar.h>
  17. #include <libtar/libtar_listhash.h>
  18. #include <libtar/compat.h>
  19. #ifdef __cplusplus
  20. extern "C"
  21. {
  22. #endif
  23. /* useful constants */
  24. #define T_BLOCKSIZE 512
  25. #define T_NAMELEN 100
  26. #define T_PREFIXLEN 155
  27. #define T_MAXPATHLEN (T_NAMELEN + T_PREFIXLEN)
  28. /* GNU extensions for typeflag */
  29. #define GNU_LONGNAME_TYPE 'L'
  30. #define GNU_LONGLINK_TYPE 'K'
  31. /* our version of the tar header structure */
  32. struct tar_header
  33. {
  34. char name[100];
  35. char mode[8];
  36. char uid[8];
  37. char gid[8];
  38. char size[12];
  39. char mtime[12];
  40. char chksum[8];
  41. char typeflag;
  42. char linkname[100];
  43. char magic[6];
  44. char version[2];
  45. char uname[32];
  46. char gname[32];
  47. char devmajor[8];
  48. char devminor[8];
  49. char prefix[155];
  50. char padding[12];
  51. char *gnu_longname;
  52. char *gnu_longlink;
  53. };
  54. /***** handle.c ************************************************************/
  55. typedef int (*openfunc_t)(const char *, int, ...);
  56. typedef int (*closefunc_t)(int);
  57. typedef ssize_t (*readfunc_t)(int, void *, size_t);
  58. typedef ssize_t (*writefunc_t)(int, const void *, size_t);
  59. typedef struct
  60. {
  61. openfunc_t openfunc;
  62. closefunc_t closefunc;
  63. readfunc_t readfunc;
  64. writefunc_t writefunc;
  65. }
  66. tartype_t;
  67. typedef struct
  68. {
  69. tartype_t *type;
  70. char *pathname;
  71. long fd;
  72. int oflags;
  73. int options;
  74. struct tar_header th_buf;
  75. libtar_hash_t *h;
  76. }
  77. TAR;
  78. /* constant values for the TAR options field */
  79. #define TAR_GNU 1 /* use GNU extensions */
  80. #define TAR_VERBOSE 2 /* output file info to stdout */
  81. #define TAR_NOOVERWRITE 4 /* don't overwrite existing files */
  82. #define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */
  83. #define TAR_CHECK_MAGIC 16 /* check magic in file header */
  84. #define TAR_CHECK_VERSION 32 /* check version in file header */
  85. #define TAR_IGNORE_CRC 64 /* ignore CRC in file header */
  86. /* this is obsolete - it's here for backwards-compatibility only */
  87. #define TAR_IGNORE_MAGIC 0
  88. extern const char libtar_version[];
  89. /* open a new tarfile handle */
  90. int tar_open(TAR **t, char *pathname, tartype_t *type,
  91. int oflags, int mode, int options);
  92. /* make a tarfile handle out of a previously-opened descriptor */
  93. int tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type,
  94. int oflags, int mode, int options);
  95. /* returns the descriptor associated with t */
  96. int tar_fd(TAR *t);
  97. /* close tarfile handle */
  98. int tar_close(TAR *t);
  99. /***** append.c ************************************************************/
  100. /* forward declaration to appease the compiler */
  101. struct tar_dev;
  102. /* cleanup function */
  103. void tar_dev_free(struct tar_dev *tdp);
  104. /* Appends a file to the tar archive.
  105. * Arguments:
  106. * t = TAR handle to append to
  107. * realname = path of file to append
  108. * savename = name to save the file under in the archive
  109. */
  110. int tar_append_file(TAR *t, char *realname, char *savename);
  111. /* write EOF indicator */
  112. int tar_append_eof(TAR *t);
  113. /* add file contents to a tarchive */
  114. int tar_append_regfile(TAR *t, char *realname);
  115. /***** block.c *************************************************************/
  116. /* macros for reading/writing tarchive blocks */
  117. #define tar_block_read(t, buf) \
  118. (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
  119. #define tar_block_write(t, buf) \
  120. (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE)
  121. /* read/write a header block */
  122. int th_read(TAR *t);
  123. int th_write(TAR *t);
  124. /***** decode.c ************************************************************/
  125. /* determine file type */
  126. #define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \
  127. || (t)->th_buf.typeflag == AREGTYPE \
  128. || (t)->th_buf.typeflag == CONTTYPE \
  129. || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode)) \
  130. && (t)->th_buf.typeflag != LNKTYPE))
  131. #define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE)
  132. #define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \
  133. || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode)))
  134. #define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \
  135. || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode)))
  136. #define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \
  137. || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode)))
  138. #define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \
  139. || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode)) \
  140. || ((t)->th_buf.typeflag == AREGTYPE \
  141. && ((t)->th_buf.name[strlen((t)->th_buf.name) - 1] == '/')))
  142. #define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \
  143. || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode)))
  144. #define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE)
  145. #define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE)
  146. /* decode tar header info */
  147. #define th_get_crc(t) oct_to_int((t)->th_buf.chksum)
  148. #define th_get_size(t) oct_to_int((t)->th_buf.size)
  149. #define th_get_mtime(t) oct_to_int((t)->th_buf.mtime)
  150. #define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor)
  151. #define th_get_devminor(t) oct_to_int((t)->th_buf.devminor)
  152. #define th_get_linkname(t) ((t)->th_buf.gnu_longlink \
  153. ? (t)->th_buf.gnu_longlink \
  154. : (t)->th_buf.linkname)
  155. char *th_get_pathname(TAR *t);
  156. mode_t th_get_mode(TAR *t);
  157. uid_t th_get_uid(TAR *t);
  158. gid_t th_get_gid(TAR *t);
  159. /***** encode.c ************************************************************/
  160. /* encode file info in th_header */
  161. void th_set_type(TAR *t, mode_t mode);
  162. void th_set_path(TAR *t, char *pathname);
  163. void th_set_link(TAR *t, char *linkname);
  164. void th_set_device(TAR *t, dev_t device);
  165. void th_set_user(TAR *t, uid_t uid);
  166. void th_set_group(TAR *t, gid_t gid);
  167. void th_set_mode(TAR *t, mode_t fmode);
  168. #define th_set_mtime(t, fmtime) \
  169. int_to_oct_nonull((fmtime), (t)->th_buf.mtime, 12)
  170. #define th_set_size(t, fsize) \
  171. int_to_oct_nonull((fsize), (t)->th_buf.size, 12)
  172. /* encode everything at once (except the pathname and linkname) */
  173. void th_set_from_stat(TAR *t, struct stat *s);
  174. /* encode magic, version, and crc - must be done after everything else is set */
  175. void th_finish(TAR *t);
  176. /***** extract.c ***********************************************************/
  177. /* sequentially extract next file from t */
  178. int tar_extract_file(TAR *t, char *realname);
  179. /* extract different file types */
  180. int tar_extract_dir(TAR *t, char *realname);
  181. int tar_extract_hardlink(TAR *t, char *realname);
  182. int tar_extract_symlink(TAR *t, char *realname);
  183. int tar_extract_chardev(TAR *t, char *realname);
  184. int tar_extract_blockdev(TAR *t, char *realname);
  185. int tar_extract_fifo(TAR *t, char *realname);
  186. /* for regfiles, we need to extract the content blocks as well */
  187. int tar_extract_regfile(TAR *t, char *realname);
  188. int tar_skip_regfile(TAR *t);
  189. /***** output.c ************************************************************/
  190. /* print the tar header */
  191. void th_print(TAR *t);
  192. /* print "ls -l"-like output for the file described by th */
  193. void th_print_long_ls(TAR *t);
  194. /***** util.c *************************************************************/
  195. /* hashing function for pathnames */
  196. int path_hashfunc(char *key, int numbuckets);
  197. /* matching function for dev_t's */
  198. int dev_match(dev_t *dev1, dev_t *dev2);
  199. /* matching function for ino_t's */
  200. int ino_match(ino_t *ino1, ino_t *ino2);
  201. /* hashing function for dev_t's */
  202. int dev_hash(dev_t *dev);
  203. /* hashing function for ino_t's */
  204. int ino_hash(ino_t *inode);
  205. /* create any necessary dirs */
  206. int mkdirhier(char *path);
  207. /* calculate header checksum */
  208. int th_crc_calc(TAR *t);
  209. #define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t))
  210. /* string-octal to integer conversion */
  211. int oct_to_int(char *oct);
  212. /* integer to NULL-terminated string-octal conversion */
  213. #define int_to_oct(num, oct, octlen) \
  214. snprintf((oct), (octlen), "%*lo ", (octlen) - 2, (unsigned long)(num))
  215. /* integer to string-octal conversion, no NULL */
  216. void int_to_oct_nonull(int num, char *oct, size_t octlen);
  217. /***** wrapper.c **********************************************************/
  218. /* extract groups of files */
  219. int tar_extract_glob(TAR *t, char *globname, char *prefix);
  220. int tar_extract_all(TAR *t, char *prefix);
  221. /* add a whole tree of files */
  222. int tar_append_tree(TAR *t, char *realdir, char *savedir);
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif /* ! LIBTAR_H */