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