]>
Dogcows Code - chaz/tar/blob - src/xheader.c
1 /* POSIX extended and STAR headers.
3 Copyright (C) 2003 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #define obstack_chunk_alloc xmalloc
28 #define obstack_chunk_free free
31 /* General Interface */
36 void (*coder
) (struct tar_stat_info
const *, char const *, struct xheader
*);
37 void (*decoder
) (struct tar_stat_info
*, char const *);
40 /* This declaration must be extern, because ISO C99 section 6.9.2
41 prohibits a tentative definition that has both internal linkage and
42 incomplete type. If we made it static, we'd have to declare its
43 size which would be a maintenance pain; if we put its initializer
44 here, we'd need a boatload of forward declarations, which would be
45 even more of a pain. */
46 extern struct xhdr_tab
const xhdr_tab
[];
48 static struct xhdr_tab
const *
49 locate_handler (char const *keyword
)
51 struct xhdr_tab
const *p
;
53 for (p
= xhdr_tab
; p
->keyword
; p
++)
54 if (strcmp (p
->keyword
, keyword
) == 0)
59 /* Decodes a single extended header record. Advances P to the next
61 Returns true on success, false otherwise. */
63 decode_record (char **p
, struct tar_stat_info
*st
)
69 struct xhdr_tab
const *t
;
74 len
= strtoul (*p
, p
, 10);
77 ERROR ((0, 0, _("Malformed extended header: missing whitespace after the length")));
82 for (;*p
< start
+ len
; ++*p
)
88 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
94 t
= locate_handler (keyword
);
104 t
->decoder (st
, value
);
113 xheader_decode (struct tar_stat_info
*st
)
115 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
116 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
119 if (!decode_record (&p
, st
))
124 xheader_store (char const *keyword
, struct tar_stat_info
const *st
)
126 struct xhdr_tab
const *t
;
128 if (extended_header
.buffer
)
130 t
= locate_handler (keyword
);
133 if (!extended_header
.stk
)
135 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
136 obstack_init (extended_header
.stk
);
138 t
->coder (st
, keyword
, &extended_header
);
142 xheader_read (union block
*p
, size_t size
)
147 free (extended_header
.buffer
);
149 extended_header
.size
= size
;
150 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
151 extended_header
.buffer
= xmalloc (size
+ 1);
160 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
161 set_next_block_after (p
);
163 p
= find_next_block ();
172 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
179 while ((val
/= 10) != 0);
183 char *p
= buf
+ s
- 1;
187 *p
-- = val
% 10 + '0';
189 while ((val
/= 10) != 0);
198 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
200 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
207 n
= format_uintmax (len
+ p
, NULL
, 0);
211 format_uintmax (len
+ n
, nbuf
, n
);
212 obstack_grow (xhdr
->stk
, nbuf
, n
);
213 obstack_1grow (xhdr
->stk
, ' ');
214 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
215 obstack_1grow (xhdr
->stk
, '=');
216 obstack_grow (xhdr
->stk
, value
, strlen (value
));
217 obstack_1grow (xhdr
->stk
, '\n');
221 xheader_finish (struct xheader
*xhdr
)
223 obstack_1grow (xhdr
->stk
, 0);
224 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
225 xhdr
->size
= strlen (xhdr
->buffer
);
229 xheader_destroy (struct xheader
*xhdr
)
233 obstack_free (xhdr
->stk
, NULL
);
244 /* Implementations */
246 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
248 xheader_print (xhdr
, keyword
, string
);
252 code_time (time_t t
, char const *keyword
, struct xheader
*xhdr
)
255 size_t s
= format_uintmax (t
, NULL
, 0);
256 format_uintmax (t
, sbuf
, s
);
258 format_uintmax (0, sbuf
+ s
, 9);
260 xheader_print (xhdr
, keyword
, sbuf
);
264 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
267 size_t s
= format_uintmax (value
, NULL
, 0);
268 format_uintmax (value
, sbuf
, s
);
270 xheader_print (xhdr
, keyword
, sbuf
);
274 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
275 struct xheader
*xhdr
)
280 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
285 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
286 struct xheader
*xhdr
)
288 code_time (st
->stat
.st_atime
, keyword
, xhdr
);
292 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
295 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
296 st
->stat
.st_atime
= u
;
300 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
301 struct xheader
*xhdr
)
303 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
307 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
310 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
315 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
316 struct xheader
*xhdr
)
318 code_string (st
->gname
, keyword
, xhdr
);
322 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
324 assign_string (&st
->gname
, arg
);
328 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
329 struct xheader
*xhdr
)
331 code_string (st
->link_name
, keyword
, xhdr
);
335 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
337 assign_string (&st
->link_name
, arg
);
341 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
342 struct xheader
*xhdr
)
344 code_time (st
->stat
.st_ctime
, keyword
, xhdr
);
348 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
351 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
352 st
->stat
.st_ctime
= u
;
356 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
357 struct xheader
*xhdr
)
359 code_time (st
->stat
.st_mtime
, keyword
, xhdr
);
363 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
366 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
367 st
->stat
.st_mtime
= u
;
371 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
372 struct xheader
*xhdr
)
374 code_string (st
->file_name
, keyword
, xhdr
);
378 path_decoder (struct tar_stat_info
*st
, char const *arg
)
380 assign_string (&st
->orig_file_name
, arg
);
381 assign_string (&st
->file_name
, arg
);
382 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
386 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
387 struct xheader
*xhdr
)
389 code_num (st
->stat
.st_size
, keyword
, xhdr
);
393 size_decoder (struct tar_stat_info
*st
, char const *arg
)
396 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
397 st
->stat
.st_size
= u
;
401 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
402 struct xheader
*xhdr
)
404 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
408 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
411 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
416 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
417 struct xheader
*xhdr
)
419 code_string (st
->uname
, keyword
, xhdr
);
423 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
425 assign_string (&st
->uname
, arg
);
428 struct xhdr_tab
const xhdr_tab
[] = {
429 { "atime", atime_coder
, atime_decoder
},
430 { "comment", dummy_coder
, dummy_decoder
},
431 { "charset", dummy_coder
, dummy_decoder
},
432 { "ctime", ctime_coder
, ctime_decoder
},
433 { "gid", gid_coder
, gid_decoder
},
434 { "gname", gname_coder
, gname_decoder
},
435 { "linkpath", linkpath_coder
, linkpath_decoder
},
436 { "mtime", mtime_coder
, mtime_decoder
},
437 { "path", path_coder
, path_decoder
},
438 { "size", size_coder
, size_decoder
},
439 { "uid", uid_coder
, uid_decoder
},
440 { "uname", uname_coder
, uname_decoder
},
442 /* The number of entries in xhdr_tab must agree with the array
443 bounds in xhdr_tab's forward declaration. */
445 #if 0 /* GNU private keywords (not yet implemented) */
446 /* Sparse file handling */
447 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
},
448 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
},
450 /* The next directory entry actually contains the names of files
451 that were in the directory at the time the dump was made.
452 Supersedes GNUTYPE_DUMPDIR header type. */
453 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
455 /* Keeps the tape/volume header. May be present only in the global headers.
456 Equivalent to GNUTYPE_VOLHDR. */
457 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
459 /* These may be present in a first global header of the archive.
460 They provide the same functionality as GNUTYPE_MULTIVOL header.
461 The GNU.volume.size keeps the real_s_sizeleft value, which is
462 otherwise kept in the size field of a multivolume header. The
463 GNU.volume.offset keeps the offset of the start of this volume,
464 otherwise kept in oldgnu_header.offset. */
465 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
466 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
This page took 0.054211 seconds and 4 git commands to generate.