4
0

tar.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* GNU tar Archive Format description.
  2. Copyright 1988-2020 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* tar Header Block, from POSIX 1003.1-1990. */
  15. /* POSIX header. */
  16. struct posix_header
  17. { /* byte offset */
  18. char name[100]; /* 0 */
  19. char mode[8]; /* 100 */
  20. char uid[8]; /* 108 */
  21. char gid[8]; /* 116 */
  22. char size[12]; /* 124 */
  23. char mtime[12]; /* 136 */
  24. char chksum[8]; /* 148 */
  25. char typeflag; /* 156 */
  26. char linkname[100]; /* 157 */
  27. char magic[6]; /* 257 */
  28. char version[2]; /* 263 */
  29. char uname[32]; /* 265 */
  30. char gname[32]; /* 297 */
  31. char devmajor[8]; /* 329 */
  32. char devminor[8]; /* 337 */
  33. char prefix[155]; /* 345 */
  34. /* 500 */
  35. };
  36. #define TMAGIC "ustar" /* ustar and a null */
  37. #define TMAGLEN 6
  38. #define TVERSION "00" /* 00 and no null */
  39. #define TVERSLEN 2
  40. /* Values used in typeflag field. */
  41. #define REGTYPE '0' /* regular file */
  42. #define AREGTYPE '\0' /* regular file */
  43. #define LNKTYPE '1' /* link */
  44. #define SYMTYPE '2' /* reserved */
  45. #define CHRTYPE '3' /* character special */
  46. #define BLKTYPE '4' /* block special */
  47. #define DIRTYPE '5' /* directory */
  48. #define FIFOTYPE '6' /* FIFO special */
  49. #define CONTTYPE '7' /* reserved */
  50. #define XHDTYPE 'x' /* Extended header referring to the
  51. next file in the archive */
  52. #define XGLTYPE 'g' /* Global extended header */
  53. /* Bits used in the mode field, values in octal. */
  54. #define TSUID 04000 /* set UID on execution */
  55. #define TSGID 02000 /* set GID on execution */
  56. #define TSVTX 01000 /* reserved */
  57. /* file permissions */
  58. #define TUREAD 00400 /* read by owner */
  59. #define TUWRITE 00200 /* write by owner */
  60. #define TUEXEC 00100 /* execute/search by owner */
  61. #define TGREAD 00040 /* read by group */
  62. #define TGWRITE 00020 /* write by group */
  63. #define TGEXEC 00010 /* execute/search by group */
  64. #define TOREAD 00004 /* read by other */
  65. #define TOWRITE 00002 /* write by other */
  66. #define TOEXEC 00001 /* execute/search by other */
  67. /* tar Header Block, GNU extensions. */
  68. /* In GNU tar, SYMTYPE is for to symbolic links, and CONTTYPE is for
  69. contiguous files, so maybe disobeying the "reserved" comment in POSIX
  70. header description. I suspect these were meant to be used this way, and
  71. should not have really been "reserved" in the published standards. */
  72. /* *BEWARE* *BEWARE* *BEWARE* that the following information is still
  73. boiling, and may change. Even if the OLDGNU format description should be
  74. accurate, the so-called GNU format is not yet fully decided. It is
  75. surely meant to use only extensions allowed by POSIX, but the sketch
  76. below repeats some ugliness from the OLDGNU format, which should rather
  77. go away. Sparse files should be saved in such a way that they do *not*
  78. require two passes at archive creation time. Huge files get some POSIX
  79. fields to overflow, alternate solutions have to be sought for this. */
  80. /* Descriptor for a single file hole. */
  81. struct sparse
  82. { /* byte offset */
  83. char offset[12]; /* 0 */
  84. char numbytes[12]; /* 12 */
  85. /* 24 */
  86. };
  87. /* Sparse files are not supported in POSIX ustar format. For sparse files
  88. with a POSIX header, a GNU extra header is provided which holds overall
  89. sparse information and a few sparse descriptors. When an old GNU header
  90. replaces both the POSIX header and the GNU extra header, it holds some
  91. sparse descriptors too. Whether POSIX or not, if more sparse descriptors
  92. are still needed, they are put into as many successive sparse headers as
  93. necessary. The following constants tell how many sparse descriptors fit
  94. in each kind of header able to hold them. */
  95. #define SPARSES_IN_EXTRA_HEADER 16
  96. #define SPARSES_IN_OLDGNU_HEADER 4
  97. #define SPARSES_IN_SPARSE_HEADER 21
  98. /* Extension header for sparse files, used immediately after the GNU extra
  99. header, and used only if all sparse information cannot fit into that
  100. extra header. There might even be many such extension headers, one after
  101. the other, until all sparse information has been recorded. */
  102. struct sparse_header
  103. { /* byte offset */
  104. struct sparse sp[SPARSES_IN_SPARSE_HEADER];
  105. /* 0 */
  106. char isextended; /* 504 */
  107. /* 505 */
  108. };
  109. /* The old GNU format header conflicts with POSIX format in such a way that
  110. POSIX archives may fool old GNU tar's, and POSIX tar's might well be
  111. fooled by old GNU tar archives. An old GNU format header uses the space
  112. used by the prefix field in a POSIX header, and cumulates information
  113. normally found in a GNU extra header. With an old GNU tar header, we
  114. never see any POSIX header nor GNU extra header. Supplementary sparse
  115. headers are allowed, however. */
  116. struct oldgnu_header
  117. { /* byte offset */
  118. char unused_pad1[345]; /* 0 */
  119. char atime[12]; /* 345 Incr. archive: atime of the file */
  120. char ctime[12]; /* 357 Incr. archive: ctime of the file */
  121. char offset[12]; /* 369 Multivolume archive: the offset of
  122. the start of this volume */
  123. char longnames[4]; /* 381 Not used */
  124. char unused_pad2; /* 385 */
  125. struct sparse sp[SPARSES_IN_OLDGNU_HEADER];
  126. /* 386 */
  127. char isextended; /* 482 Sparse file: Extension sparse header
  128. follows */
  129. char realsize[12]; /* 483 Sparse file: Real size*/
  130. /* 495 */
  131. };
  132. /* OLDGNU_MAGIC uses both magic and version fields, which are contiguous.
  133. Found in an archive, it indicates an old GNU header format, which will be
  134. hopefully become obsolescent. With OLDGNU_MAGIC, uname and gname are
  135. valid, though the header is not truly POSIX conforming. */
  136. #define OLDGNU_MAGIC "ustar " /* 7 chars and a null */
  137. /* The standards committee allows only capital A through capital Z for
  138. user-defined expansion. Other letters in use include:
  139. 'A' Solaris Access Control List
  140. 'E' Solaris Extended Attribute File
  141. 'I' Inode only, as in 'star'
  142. 'N' Obsolete GNU tar, for file names that do not fit into the main header.
  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. /* This is for sparse files. */
  154. #define GNUTYPE_SPARSE 'S'
  155. /* This file is a tape/volume header. Ignore it on extraction. */
  156. #define GNUTYPE_VOLHDR 'V'
  157. /* Solaris extended header */
  158. #define SOLARIS_XHDTYPE 'X'
  159. /* J@"org 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. {
  186. char fill[345]; /* 0 Everything that is before t_prefix */
  187. char prefix[1]; /* 345 t_name prefix */
  188. char fill2; /* 346 */
  189. char fill3[8]; /* 347 */
  190. char isextended; /* 355 */
  191. struct sparse sp[SPARSES_IN_STAR_HEADER]; /* 356 */
  192. char realsize[12]; /* 452 Actual size of the file */
  193. char offset[12]; /* 464 Offset of multivolume contents */
  194. char atime[12]; /* 476 */
  195. char ctime[12]; /* 488 */
  196. char mfill[8]; /* 500 */
  197. char xmagic[4]; /* 508 "tar" */
  198. };
  199. struct star_ext_header
  200. {
  201. struct sparse sp[SPARSES_IN_STAR_EXT_HEADER];
  202. char isextended;
  203. };
  204. /* END */
  205. /* tar Header Block, overall structure. */
  206. /* tar files are made in basic blocks of this size. */
  207. #define BLOCKSIZE 512
  208. enum archive_format
  209. {
  210. DEFAULT_FORMAT, /* format to be decided later */
  211. V7_FORMAT, /* old V7 tar format */
  212. OLDGNU_FORMAT, /* GNU format as per before tar 1.12 */
  213. USTAR_FORMAT, /* POSIX.1-1988 (ustar) format */
  214. POSIX_FORMAT, /* POSIX.1-2001 format */
  215. STAR_FORMAT, /* Star format defined in 1994 */
  216. GNU_FORMAT /* Same as OLDGNU_FORMAT with one exception:
  217. see FIXME note for to_chars() function
  218. (create.c:189) */
  219. };
  220. /* Information about a sparse file. */
  221. struct sp_array
  222. {
  223. off_t offset;
  224. off_t numbytes;
  225. };
  226. struct xheader
  227. {
  228. struct obstack *stk;
  229. size_t size;
  230. char *buffer;
  231. uintmax_t string_length;
  232. };
  233. /* Information about xattrs for a file. */
  234. struct xattr_array
  235. {
  236. char *xkey;
  237. char *xval_ptr;
  238. size_t xval_len;
  239. };
  240. struct tar_stat_info
  241. {
  242. char *orig_file_name; /* name of file read from the archive header */
  243. char *file_name; /* name of file for the current archive entry
  244. after being normalized. */
  245. bool had_trailing_slash; /* true if the current archive entry had a
  246. trailing slash before it was normalized. */
  247. char *link_name; /* name of link for the current archive entry. */
  248. char *uname; /* user name of owner */
  249. char *gname; /* group name of owner */
  250. char *cntx_name; /* SELinux context for the current archive entry. */
  251. char *acls_a_ptr; /* Access ACLs for the current archive entry. */
  252. size_t acls_a_len; /* Access ACLs for the current archive entry. */
  253. char *acls_d_ptr; /* Default ACLs for the current archive entry. */
  254. size_t acls_d_len; /* Default ACLs for the current archive entry. */
  255. struct stat stat; /* regular filesystem stat */
  256. /* STAT doesn't always have access, data modification, and status
  257. change times in a convenient form, so store them separately. */
  258. struct timespec atime;
  259. struct timespec mtime;
  260. struct timespec ctime;
  261. off_t archive_file_size; /* Size of file as stored in the archive.
  262. Equals stat.st_size for non-sparse files */
  263. bool is_sparse; /* Is the file sparse */
  264. /* For sparse files: */
  265. unsigned sparse_major;
  266. unsigned sparse_minor;
  267. size_t sparse_map_avail; /* Index to the first unused element in
  268. sparse_map array. Zero if the file is
  269. not sparse */
  270. size_t sparse_map_size; /* Size of the sparse map */
  271. struct sp_array *sparse_map;
  272. off_t real_size; /* The real size of sparse file */
  273. bool real_size_set; /* True when GNU.sparse.realsize is set in
  274. archived file */
  275. bool sparse_name_done; /* Set to true if 'GNU.sparse.name' header was
  276. processed pax header parsing. Following 'path'
  277. header (lower priority) will be ignored. */
  278. size_t xattr_map_size; /* Size of the xattr map */
  279. struct xattr_array *xattr_map;
  280. /* Extended headers */
  281. struct xheader xhdr;
  282. /* For dumpdirs */
  283. bool is_dumpdir; /* Is the member a dumpdir? */
  284. bool skipped; /* The member contents is already read
  285. (for GNUTYPE_DUMPDIR) */
  286. char *dumpdir; /* Contents of the dump directory */
  287. /* Parent directory, if creating an archive. This is null if the
  288. file is at the top level. */
  289. struct tar_stat_info *parent;
  290. /* Directory stream. If this is not null, it is in control of FD,
  291. and should be closed instead of FD. */
  292. DIR *dirstream;
  293. /* File descriptor, if creating an archive, and if a directory or a
  294. regular file or a contiguous file.
  295. It is zero if no file descriptor is available, either because it
  296. was never needed or because it was open and then closed to
  297. conserve on file descriptors. (Standard input is never used
  298. here, so zero cannot be a valid file descriptor.)
  299. It is negative if it could not be reopened after it was closed.
  300. Negate it to find out what errno was when the reopen failed. */
  301. int fd;
  302. /* Exclusion list */
  303. struct exclist *exclude_list;
  304. };
  305. union block
  306. {
  307. char buffer[BLOCKSIZE];
  308. struct posix_header header;
  309. struct star_header star_header;
  310. struct oldgnu_header oldgnu_header;
  311. struct sparse_header sparse_header;
  312. struct star_in_header star_in_header;
  313. struct star_ext_header star_ext_header;
  314. };