re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / src / tar.h
1 /* GNU tar Archive Format description.
2
3    Copyright 1988-1989, 1991-1997, 2000-2001, 2003-2007, 2012-2014, 2016
4    Free Software Foundation, Inc.
5
6    This file is part of GNU tar.
7
8    GNU tar is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    GNU tar is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* tar Header Block, from POSIX 1003.1-1990.  */
22
23 /* POSIX header.  */
24
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 */
44 };
45
46 #define TMAGIC   "ustar"        /* ustar and a null */
47 #define TMAGLEN  6
48 #define TVERSION "00"           /* 00 and no null */
49 #define TVERSLEN 2
50
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 */
61
62 #define XHDTYPE  'x'            /* Extended header referring to the
63                                    next file in the archive */
64 #define XGLTYPE  'g'            /* Global extended header */
65
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 */
80
81 /* tar Header Block, GNU extensions.  */
82
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.  */
87
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.  */
96
97 /* Descriptor for a single file hole.  */
98
99 struct sparse
100 {                               /* byte offset */
101   char offset[12];              /*   0 */
102   char numbytes[12];            /*  12 */
103                                 /*  24 */
104 };
105
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.  */
114
115 #define SPARSES_IN_EXTRA_HEADER  16
116 #define SPARSES_IN_OLDGNU_HEADER 4
117 #define SPARSES_IN_SPARSE_HEADER 21
118
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.  */
123
124 struct sparse_header
125 {                               /* byte offset */
126   struct sparse sp[SPARSES_IN_SPARSE_HEADER];
127                                 /*   0 */
128   char isextended;              /* 504 */
129                                 /* 505 */
130 };
131
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.  */
139
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 */
155 };
156
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 */
162
163 /* The standards committee allows only capital A through capital Z for
164    user-defined expansion.  Other letters in use include:
165
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)  */
171
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'
175
176 /* Identifies the *next* file on the tape as having a long linkname.  */
177 #define GNUTYPE_LONGLINK 'K'
178
179 /* Identifies the *next* file on the tape as having a long name.  */
180 #define GNUTYPE_LONGNAME 'L'
181
182 /* This is the continuation of a file that began on another volume.  */
183 #define GNUTYPE_MULTIVOL 'M'
184
185 /* This is for sparse files.  */
186 #define GNUTYPE_SPARSE 'S'
187
188 /* This file is a tape/volume header.  Ignore it on extraction.  */
189 #define GNUTYPE_VOLHDR 'V'
190
191 /* Solaris extended header */
192 #define SOLARIS_XHDTYPE 'X'
193
194 /* J@"org Schilling star header */
195
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 */
217 };
218
219 #define SPARSES_IN_STAR_HEADER      4
220 #define SPARSES_IN_STAR_EXT_HEADER  21
221
222 struct star_in_header
223 {
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" */
236 };
237
238 struct star_ext_header
239 {
240   struct sparse sp[SPARSES_IN_STAR_EXT_HEADER];
241   char isextended;
242 };
243
244 /* END */
245 \f
246
247 /* tar Header Block, overall structure.  */
248
249 /* tar files are made in basic blocks of this size.  */
250 #define BLOCKSIZE 512
251
252 enum archive_format
253 {
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) */
263 };
264
265 /* Information about a sparse file.  */
266 struct sp_array
267 {
268   off_t offset;
269   off_t numbytes;
270 };
271
272 struct xheader
273 {
274   struct obstack *stk;
275   size_t size;
276   char *buffer;
277   uintmax_t string_length;
278 };
279
280 /* Information about xattrs for a file.  */
281 struct xattr_array
282   {
283     char *xkey;
284     char *xval_ptr;
285     size_t xval_len;
286   };
287
288 struct tar_stat_info
289 {
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.  */
296
297   char          *uname;     /* user name of owner */
298   char          *gname;     /* group name of owner */
299
300   char *cntx_name;          /* SELinux context for the current archive entry. */
301
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. */
304
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. */
307
308   struct stat   stat;       /* regular filesystem stat */
309
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;
315
316   off_t archive_file_size;  /* Size of file as stored in the archive.
317                                Equals stat.st_size for non-sparse files */
318
319   bool   is_sparse;         /* Is the file sparse */
320
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;
329
330   off_t real_size;          /* The real size of sparse file */
331   int   real_size_set;      /* True when GNU.sparse.realsize is set in
332                                archived file */
333
334   size_t xattr_map_size;   /* Size of the xattr map */
335   struct xattr_array *xattr_map;
336
337   /* Extended headers */
338   struct xheader xhdr;
339
340   /* For dumpdirs */
341   bool is_dumpdir;          /* Is the member a dumpdir? */
342   bool skipped;             /* The member contents is already read
343                                (for GNUTYPE_DUMPDIR) */
344   char *dumpdir;            /* Contents of the dump directory */
345
346   /* Parent directory, if creating an archive.  This is null if the
347      file is at the top level.  */
348   struct tar_stat_info *parent;
349
350   /* Directory stream.  If this is not null, it is in control of FD,
351      and should be closed instead of FD.  */
352   DIR *dirstream;
353
354   /* File descriptor, if creating an archive, and if a directory or a
355      regular file or a contiguous file.
356
357      It is zero if no file descriptor is available, either because it
358      was never needed or because it was open and then closed to
359      conserve on file descriptors.  (Standard input is never used
360      here, so zero cannot be a valid file descriptor.)
361
362      It is negative if it could not be reopened after it was closed.
363      Negate it to find out what errno was when the reopen failed.  */
364   int fd;
365
366   /* Exclusion list */
367   struct exclist *exclude_list;
368 };
369
370 union block
371 {
372   char buffer[BLOCKSIZE];
373   struct posix_header header;
374   struct star_header star_header;
375   struct oldgnu_header oldgnu_header;
376   struct sparse_header sparse_header;
377   struct star_in_header star_in_header;
378   struct star_ext_header star_ext_header;
379 };