tar.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* GNU tar Archive Format description.
  2. Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
  3. 2000, 2001, 2003 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* tar Header Block, from POSIX 1003.1-1990. */
  16. /* POSIX header. */
  17. struct posix_header
  18. { /* byte offset */
  19. char name[100]; /* 0 */
  20. char mode[8]; /* 100 */
  21. char uid[8]; /* 108 */
  22. char gid[8]; /* 116 */
  23. char size[12]; /* 124 */
  24. char mtime[12]; /* 136 */
  25. char chksum[8]; /* 148 */
  26. char typeflag; /* 156 */
  27. char linkname[100]; /* 157 */
  28. char magic[6]; /* 257 */
  29. char version[2]; /* 263 */
  30. char uname[32]; /* 265 */
  31. char gname[32]; /* 297 */
  32. char devmajor[8]; /* 329 */
  33. char devminor[8]; /* 337 */
  34. char prefix[155]; /* 345 */
  35. /* 500 */
  36. };
  37. #define TMAGIC "ustar" /* ustar and a null */
  38. #define TMAGLEN 6
  39. #define TVERSION "00" /* 00 and no null */
  40. #define TVERSLEN 2
  41. /* Values used in typeflag field. */
  42. #define REGTYPE '0' /* regular file */
  43. #define AREGTYPE '\0' /* regular file */
  44. #define LNKTYPE '1' /* link */
  45. #define SYMTYPE '2' /* reserved */
  46. #define CHRTYPE '3' /* character special */
  47. #define BLKTYPE '4' /* block special */
  48. #define DIRTYPE '5' /* directory */
  49. #define FIFOTYPE '6' /* FIFO special */
  50. #define CONTTYPE '7' /* reserved */
  51. #define XHDTYPE 'x' /* Extended header referring to the
  52. next file in the archive */
  53. #define XGLTYPE 'g' /* Global extended header */
  54. /* Bits used in the mode field, values in octal. */
  55. #define TSUID 04000 /* set UID on execution */
  56. #define TSGID 02000 /* set GID on execution */
  57. #define TSVTX 01000 /* reserved */
  58. /* file permissions */
  59. #define TUREAD 00400 /* read by owner */
  60. #define TUWRITE 00200 /* write by owner */
  61. #define TUEXEC 00100 /* execute/search by owner */
  62. #define TGREAD 00040 /* read by group */
  63. #define TGWRITE 00020 /* write by group */
  64. #define TGEXEC 00010 /* execute/search by group */
  65. #define TOREAD 00004 /* read by other */
  66. #define TOWRITE 00002 /* write by other */
  67. #define TOEXEC 00001 /* execute/search by other */
  68. /* tar Header Block, GNU extensions. */
  69. /* In GNU tar, SYMTYPE is for to symbolic links, and CONTTYPE is for
  70. contiguous files, so maybe disobeying the `reserved' comment in POSIX
  71. header description. I suspect these were meant to be used this way, and
  72. should not have really been `reserved' in the published standards. */
  73. /* *BEWARE* *BEWARE* *BEWARE* that the following information is still
  74. boiling, and may change. Even if the OLDGNU format description should be
  75. accurate, the so-called GNU format is not yet fully decided. It is
  76. surely meant to use only extensions allowed by POSIX, but the sketch
  77. below repeats some ugliness from the OLDGNU format, which should rather
  78. go away. Sparse files should be saved in such a way that they do *not*
  79. require two passes at archive creation time. Huge files get some POSIX
  80. fields to overflow, alternate solutions have to be sought for this. */
  81. /* Descriptor for a single file hole. */
  82. struct sparse
  83. { /* byte offset */
  84. char offset[12]; /* 0 */
  85. char numbytes[12]; /* 12 */
  86. /* 24 */
  87. };
  88. /* Sparse files are not supported in POSIX ustar format. For sparse files
  89. with a POSIX header, a GNU extra header is provided which holds overall
  90. sparse information and a few sparse descriptors. When an old GNU header
  91. replaces both the POSIX header and the GNU extra header, it holds some
  92. sparse descriptors too. Whether POSIX or not, if more sparse descriptors
  93. are still needed, they are put into as many successive sparse headers as
  94. necessary. The following constants tell how many sparse descriptors fit
  95. in each kind of header able to hold them. */
  96. #define SPARSES_IN_EXTRA_HEADER 16
  97. #define SPARSES_IN_OLDGNU_HEADER 4
  98. #define SPARSES_IN_SPARSE_HEADER 21
  99. /* Extension header for sparse files, used immediately after the GNU extra
  100. header, and used only if all sparse information cannot fit into that
  101. extra header. There might even be many such extension headers, one after
  102. the other, until all sparse information has been recorded. */
  103. struct sparse_header
  104. { /* byte offset */
  105. struct sparse sp[SPARSES_IN_SPARSE_HEADER];
  106. /* 0 */
  107. char isextended; /* 504 */
  108. /* 505 */
  109. };
  110. /* The old GNU format header conflicts with POSIX format in such a way that
  111. POSIX archives may fool old GNU tar's, and POSIX tar's might well be
  112. fooled by old GNU tar archives. An old GNU format header uses the space
  113. used by the prefix field in a POSIX header, and cumulates information
  114. normally found in a GNU extra header. With an old GNU tar header, we
  115. never see any POSIX header nor GNU extra header. Supplementary sparse
  116. headers are allowed, however. */
  117. struct oldgnu_header
  118. { /* byte offset */
  119. char unused_pad1[345]; /* 0 */
  120. char atime[12]; /* 345 Incr. archive: atime of the file */
  121. char ctime[12]; /* 357 Incr. archive: ctime of the file */
  122. char offset[12]; /* 369 Multivolume archive: the offset of
  123. the start of this volume */
  124. char longnames[4]; /* 381 Not used */
  125. char unused_pad2; /* 385 */
  126. struct sparse sp[SPARSES_IN_OLDGNU_HEADER];
  127. /* 386 */
  128. char isextended; /* 482 Sparse file: Extension sparse header
  129. follows */
  130. char realsize[12]; /* 483 Sparse file: Real size*/
  131. /* 495 */
  132. };
  133. /* OLDGNU_MAGIC uses both magic and version fields, which are contiguous.
  134. Found in an archive, it indicates an old GNU header format, which will be
  135. hopefully become obsolescent. With OLDGNU_MAGIC, uname and gname are
  136. valid, though the header is not truly POSIX conforming. */
  137. #define OLDGNU_MAGIC "ustar " /* 7 chars and a null */
  138. /* The standards committee allows only capital A through capital Z for
  139. user-defined expansion. Other letters in use include:
  140. 'A' Solaris Access Control List
  141. 'E' Solaris Extended Attribute File
  142. 'I' Inode only, as in 'star'
  143. 'X' POSIX 1003.1-2001 eXtended (VU version) */
  144. /* This is a dir entry that contains the names of files that were in the
  145. dir at the time the dump was made. */
  146. #define GNUTYPE_DUMPDIR 'D'
  147. /* Identifies the *next* file on the tape as having a long linkname. */
  148. #define GNUTYPE_LONGLINK 'K'
  149. /* Identifies the *next* file on the tape as having a long name. */
  150. #define GNUTYPE_LONGNAME 'L'
  151. /* This is the continuation of a file that began on another volume. */
  152. #define GNUTYPE_MULTIVOL 'M'
  153. /* For storing filenames that do not fit into the main header. */
  154. #define GNUTYPE_NAMES 'N'
  155. /* This is for sparse files. */
  156. #define GNUTYPE_SPARSE 'S'
  157. /* This file is a tape/volume header. Ignore it on extraction. */
  158. #define GNUTYPE_VOLHDR 'V'
  159. /* Jörg Schilling star header */
  160. struct star_header
  161. { /* byte offset */
  162. char name[100]; /* 0 */
  163. char mode[8]; /* 100 */
  164. char uid[8]; /* 108 */
  165. char gid[8]; /* 116 */
  166. char size[12]; /* 124 */
  167. char mtime[12]; /* 136 */
  168. char chksum[8]; /* 148 */
  169. char typeflag; /* 156 */
  170. char linkname[100]; /* 157 */
  171. char magic[6]; /* 257 */
  172. char version[2]; /* 263 */
  173. char uname[32]; /* 265 */
  174. char gname[32]; /* 297 */
  175. char devmajor[8]; /* 329 */
  176. char devminor[8]; /* 337 */
  177. char prefix[131]; /* 345 */
  178. char atime[12]; /* 476 */
  179. char ctime[12]; /* 488 */
  180. /* 500 */
  181. };
  182. #define SPARSES_IN_STAR_HEADER 4
  183. #define SPARSES_IN_STAR_EXT_HEADER 21
  184. struct star_in_header {
  185. char fill[345]; /* 0 Everything that is before t_prefix */
  186. char prefix[1]; /* 345 t_name prefix */
  187. char fill2; /* 346 */
  188. char fill3[8]; /* 347 */
  189. char isextended; /* 355 */
  190. struct sparse sp[SPARSES_IN_STAR_HEADER]; /* 356 */
  191. char realsize[12]; /* 452 Actual size of the file */
  192. char offset[12]; /* 464 Offset of multivolume contents */
  193. char atime[12]; /* 476 */
  194. char ctime[12]; /* 488 */
  195. char mfill[8]; /* 500 */
  196. char xmagic[4]; /* 508 "tar" */
  197. };
  198. struct star_ext_header {
  199. struct sparse sp[SPARSES_IN_STAR_EXT_HEADER];
  200. char isextended;
  201. };
  202. /* tar Header Block, overall structure. */
  203. /* tar files are made in basic blocks of this size. */
  204. #define BLOCKSIZE 512
  205. enum archive_format
  206. {
  207. DEFAULT_FORMAT, /* format to be decided later */
  208. V7_FORMAT, /* old V7 tar format */
  209. OLDGNU_FORMAT, /* GNU format as per before tar 1.12 */
  210. USTAR_FORMAT, /* POSIX.1-1988 (ustar) format */
  211. POSIX_FORMAT, /* POSIX.1-2001 format */
  212. STAR_FORMAT, /* Star format defined in 1994 */
  213. GNU_FORMAT /* POSIX format with GNU extensions */
  214. };
  215. /* Information about a sparse file. */
  216. struct sp_array
  217. {
  218. off_t offset;
  219. size_t numbytes;
  220. };
  221. struct tar_stat_info
  222. {
  223. char *orig_file_name; /* name of file read from the archive header */
  224. char *file_name; /* name of file for the current archive entry
  225. after being normalized. */
  226. int had_trailing_slash; /* nonzero if the current archive entry had a
  227. trailing slash before it was normalized. */
  228. char *link_name; /* name of link for the current archive entry. */
  229. unsigned int devminor; /* device minor number */
  230. unsigned int devmajor; /* device major number */
  231. char *uname; /* user name of owner */
  232. char *gname; /* group name of owner */
  233. struct stat stat; /* regular filesystem stat */
  234. off_t archive_file_size; /* Size of file as stored in the archive.
  235. Equals stat.st_size for non-sparse files */
  236. size_t sparse_map_avail; /* Index to the first unused element in
  237. sparse_map array. Zero if the file is
  238. not sparse */
  239. size_t sparse_map_size; /* Size of the sparse map */
  240. struct sp_array *sparse_map;
  241. };
  242. union block
  243. {
  244. char buffer[BLOCKSIZE];
  245. struct posix_header header;
  246. struct star_header star_header;
  247. struct oldgnu_header oldgnu_header;
  248. struct sparse_header sparse_header;
  249. struct star_in_header star_in_header;
  250. struct star_ext_header star_ext_header;
  251. };
  252. /* End of Format description. */