libtar.h 8.5 KB

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