]>
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 *,
37 struct xheader
*, void *data
);
38 void (*decoder
) (struct tar_stat_info
*, char const *);
41 /* This declaration must be extern, because ISO C99 section 6.9.2
42 prohibits a tentative definition that has both internal linkage and
43 incomplete type. If we made it static, we'd have to declare its
44 size which would be a maintenance pain; if we put its initializer
45 here, we'd need a boatload of forward declarations, which would be
46 even more of a pain. */
47 extern struct xhdr_tab
const xhdr_tab
[];
49 static struct xhdr_tab
const *
50 locate_handler (char const *keyword
)
52 struct xhdr_tab
const *p
;
54 for (p
= xhdr_tab
; p
->keyword
; p
++)
55 if (strcmp (p
->keyword
, keyword
) == 0)
60 /* Decodes a single extended header record. Advances P to the next
62 Returns true on success, false otherwise. */
64 decode_record (char **p
, struct tar_stat_info
*st
)
70 struct xhdr_tab
const *t
;
75 len
= strtoul (*p
, p
, 10);
78 ERROR ((0, 0, _("Malformed extended header: missing whitespace after the length")));
83 for (;*p
< start
+ len
; ++*p
)
89 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
95 t
= locate_handler (keyword
);
105 t
->decoder (st
, value
);
114 xheader_decode (struct tar_stat_info
*st
)
116 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
117 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
120 if (!decode_record (&p
, st
))
125 xheader_store (char const *keyword
, struct tar_stat_info
const *st
, void *data
)
127 struct xhdr_tab
const *t
;
129 if (extended_header
.buffer
)
131 t
= locate_handler (keyword
);
134 if (!extended_header
.stk
)
136 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
137 obstack_init (extended_header
.stk
);
139 t
->coder (st
, keyword
, &extended_header
, data
);
143 xheader_read (union block
*p
, size_t size
)
148 free (extended_header
.buffer
);
150 extended_header
.size
= size
;
151 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
152 extended_header
.buffer
= xmalloc (size
+ 1);
161 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
162 set_next_block_after (p
);
164 p
= find_next_block ();
173 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
180 while ((val
/= 10) != 0);
184 char *p
= buf
+ s
- 1;
188 *p
-- = val
% 10 + '0';
190 while ((val
/= 10) != 0);
199 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
201 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
208 n
= format_uintmax (len
+ p
, NULL
, 0);
212 format_uintmax (len
+ n
, nbuf
, n
);
213 obstack_grow (xhdr
->stk
, nbuf
, n
);
214 obstack_1grow (xhdr
->stk
, ' ');
215 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
216 obstack_1grow (xhdr
->stk
, '=');
217 obstack_grow (xhdr
->stk
, value
, strlen (value
));
218 obstack_1grow (xhdr
->stk
, '\n');
222 xheader_finish (struct xheader
*xhdr
)
224 obstack_1grow (xhdr
->stk
, 0);
225 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
226 xhdr
->size
= strlen (xhdr
->buffer
);
230 xheader_destroy (struct xheader
*xhdr
)
234 obstack_free (xhdr
->stk
, NULL
);
245 /* Implementations */
247 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
249 xheader_print (xhdr
, keyword
, string
);
253 code_time (time_t t
, char const *keyword
, struct xheader
*xhdr
)
256 size_t s
= format_uintmax (t
, NULL
, 0);
257 format_uintmax (t
, sbuf
, s
);
259 format_uintmax (0, sbuf
+ s
, 9);
261 xheader_print (xhdr
, keyword
, sbuf
);
265 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
268 size_t s
= format_uintmax (value
, NULL
, 0);
269 format_uintmax (value
, sbuf
, s
);
271 xheader_print (xhdr
, keyword
, sbuf
);
275 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
276 struct xheader
*xhdr
, void *data
)
281 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
286 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
287 struct xheader
*xhdr
, void *data
)
289 code_time (st
->stat
.st_atime
, keyword
, xhdr
);
293 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
296 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
297 st
->stat
.st_atime
= u
;
301 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
302 struct xheader
*xhdr
, void *data
)
304 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
308 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
311 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
316 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
317 struct xheader
*xhdr
, void *data
)
319 code_string (st
->gname
, keyword
, xhdr
);
323 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
325 assign_string (&st
->gname
, arg
);
329 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
330 struct xheader
*xhdr
, void *data
)
332 code_string (st
->link_name
, keyword
, xhdr
);
336 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
338 assign_string (&st
->link_name
, arg
);
342 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
343 struct xheader
*xhdr
, void *data
)
345 code_time (st
->stat
.st_ctime
, keyword
, xhdr
);
349 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
352 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
353 st
->stat
.st_ctime
= u
;
357 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
358 struct xheader
*xhdr
, void *data
)
360 code_time (st
->stat
.st_mtime
, keyword
, xhdr
);
364 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
367 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
368 st
->stat
.st_mtime
= u
;
372 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
373 struct xheader
*xhdr
, void *data
)
375 code_string (st
->file_name
, keyword
, xhdr
);
379 path_decoder (struct tar_stat_info
*st
, char const *arg
)
381 assign_string (&st
->orig_file_name
, arg
);
382 assign_string (&st
->file_name
, arg
);
383 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
387 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
388 struct xheader
*xhdr
, void *data
)
390 code_num (st
->stat
.st_size
, keyword
, xhdr
);
394 size_decoder (struct tar_stat_info
*st
, char const *arg
)
397 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
398 st
->stat
.st_size
= u
;
402 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
403 struct xheader
*xhdr
, void *data
)
405 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
409 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
412 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
417 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
418 struct xheader
*xhdr
, void *data
)
420 code_string (st
->uname
, keyword
, xhdr
);
424 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
426 assign_string (&st
->uname
, arg
);
430 sparse_size_coder (struct tar_stat_info
const *st
, char const *keyword
,
431 struct xheader
*xhdr
, void *data
)
433 size_coder (st
, keyword
, xhdr
, data
);
437 sparse_size_decoder (struct tar_stat_info
*st
, char const *arg
)
440 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
441 st
->archive_file_size
= u
;
445 sparse_numblocks_coder (struct tar_stat_info
const *st
, char const *keyword
,
446 struct xheader
*xhdr
, void *data
)
448 code_num (st
->sparse_map_avail
, keyword
, xhdr
);
452 sparse_numblocks_decoder (struct tar_stat_info
*st
, char const *arg
)
455 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
457 st
->sparse_map_size
= u
;
458 st
->sparse_map
= calloc(st
->sparse_map_size
, sizeof(st
->sparse_map
[0]));
459 st
->sparse_map_avail
= 0;
464 sparse_offset_coder (struct tar_stat_info
const *st
, char const *keyword
,
465 struct xheader
*xhdr
, void *data
)
467 size_t i
= *(size_t*)data
;
468 code_num (st
->sparse_map
[i
].offset
, keyword
, xhdr
);
472 sparse_offset_decoder (struct tar_stat_info
*st
, char const *arg
)
475 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
476 st
->sparse_map
[st
->sparse_map_avail
].offset
= u
;
480 sparse_numbytes_coder (struct tar_stat_info
const *st
, char const *keyword
,
481 struct xheader
*xhdr
, void *data
)
483 size_t i
= *(size_t*)data
;
484 code_num (st
->sparse_map
[i
].numbytes
, keyword
, xhdr
);
488 sparse_numbytes_decoder (struct tar_stat_info
*st
, char const *arg
)
491 if (xstrtoumax (arg
, 0, 10, &u
, "") == LONGINT_OK
)
493 if (st
->sparse_map_avail
== st
->sparse_map_size
)
495 size_t newsize
= st
->sparse_map_size
*= 2;
496 st
->sparse_map
= xrealloc (st
->sparse_map
,
498 * sizeof st
->sparse_map
[0]);
500 st
->sparse_map
[st
->sparse_map_avail
++].numbytes
= u
;
504 struct xhdr_tab
const xhdr_tab
[] = {
505 { "atime", atime_coder
, atime_decoder
},
506 { "comment", dummy_coder
, dummy_decoder
},
507 { "charset", dummy_coder
, dummy_decoder
},
508 { "ctime", ctime_coder
, ctime_decoder
},
509 { "gid", gid_coder
, gid_decoder
},
510 { "gname", gname_coder
, gname_decoder
},
511 { "linkpath", linkpath_coder
, linkpath_decoder
},
512 { "mtime", mtime_coder
, mtime_decoder
},
513 { "path", path_coder
, path_decoder
},
514 { "size", size_coder
, size_decoder
},
515 { "uid", uid_coder
, uid_decoder
},
516 { "uname", uname_coder
, uname_decoder
},
518 /* Sparse file handling */
519 { "GNU.sparse.size", sparse_size_coder
, sparse_size_decoder
},
520 { "GNU.sparse.numblocks", sparse_numblocks_coder
, sparse_numblocks_decoder
},
521 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
},
522 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
},
524 #if 0 /* GNU private keywords (not yet implemented) */
526 /* The next directory entry actually contains the names of files
527 that were in the directory at the time the dump was made.
528 Supersedes GNUTYPE_DUMPDIR header type. */
529 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
531 /* Keeps the tape/volume header. May be present only in the global headers.
532 Equivalent to GNUTYPE_VOLHDR. */
533 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
535 /* These may be present in a first global header of the archive.
536 They provide the same functionality as GNUTYPE_MULTIVOL header.
537 The GNU.volume.size keeps the real_s_sizeleft value, which is
538 otherwise kept in the size field of a multivolume header. The
539 GNU.volume.offset keeps the offset of the start of this volume,
540 otherwise kept in oldgnu_header.offset. */
541 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
542 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
This page took 0.057221 seconds and 4 git commands to generate.