]>
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. */
26 #define obstack_chunk_alloc xmalloc
27 #define obstack_chunk_free free
30 /* General Interface */
35 void (*coder
) (struct tar_stat_info
const *, char const *, struct xheader
*);
36 void (*decoder
) (struct tar_stat_info
*, char const *);
39 /* This declaration must be extern, because ISO C99 section 6.9.2
40 prohibits a tentative definition that has both internal linkage and
41 incomplete type. If we made it static, we'd have to declare its
42 size which would be a maintenance pain; if we put its initializer
43 here, we'd need a boatload of forward declarations, which would be
44 even more of a pain. */
45 extern struct xhdr_tab
const xhdr_tab
[];
47 static struct xhdr_tab
const *
48 locate_handler (char const *keyword
)
50 struct xhdr_tab
const *p
;
52 for (p
= xhdr_tab
; p
->keyword
; p
++)
53 if (strcmp (p
->keyword
, keyword
) == 0)
58 /* Decodes a single extended header record. Advances P to the next
60 Returns true on success, false otherwise. */
62 decode_record (char **p
, struct tar_stat_info
*st
)
68 struct xhdr_tab
const *t
;
73 len
= strtoul (*p
, p
, 10);
76 ERROR ((0, 0, _("Malformed extended header: missing whitespace after the length")));
81 for (;*p
< start
+ len
; ++*p
)
87 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
93 t
= locate_handler (keyword
);
103 t
->decoder (st
, value
);
112 xheader_decode (struct tar_stat_info
*st
)
114 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
115 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
118 if (!decode_record (&p
, st
))
123 xheader_store (char const *keyword
, struct tar_stat_info
const *st
)
125 struct xhdr_tab
const *t
;
127 if (extended_header
.buffer
)
129 t
= locate_handler (keyword
);
132 if (!extended_header
.stk
)
134 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
135 obstack_init (extended_header
.stk
);
137 t
->coder (st
, keyword
, &extended_header
);
141 xheader_read (union block
*p
, size_t size
)
146 free (extended_header
.buffer
);
148 extended_header
.size
= size
;
149 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
150 extended_header
.buffer
= xmalloc (size
+ 1);
159 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
160 set_next_block_after (p
);
162 p
= find_next_block ();
171 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
178 while ((val
/= 10) != 0);
182 char *p
= buf
+ s
- 1;
186 *p
-- = val
% 10 + '0';
188 while ((val
/= 10) != 0);
197 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
199 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
206 n
= format_uintmax (len
+ p
, NULL
, 0);
210 format_uintmax (len
+ n
, nbuf
, n
);
211 obstack_grow (xhdr
->stk
, nbuf
, n
);
212 obstack_1grow (xhdr
->stk
, ' ');
213 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
214 obstack_1grow (xhdr
->stk
, '=');
215 obstack_grow (xhdr
->stk
, value
, strlen (value
));
216 obstack_1grow (xhdr
->stk
, '\n');
220 xheader_finish (struct xheader
*xhdr
)
222 obstack_1grow (xhdr
->stk
, 0);
223 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
224 xhdr
->size
= strlen (xhdr
->buffer
);
228 xheader_destroy (struct xheader
*xhdr
)
232 obstack_free (xhdr
->stk
, NULL
);
243 /* Implementations */
245 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
247 xheader_print (xhdr
, keyword
, string
);
251 code_time (time_t t
, char const *keyword
, struct xheader
*xhdr
)
254 size_t s
= format_uintmax (t
, NULL
, 0);
255 format_uintmax (t
, sbuf
, s
);
257 format_uintmax (0, sbuf
+ s
, 9);
259 xheader_print (xhdr
, keyword
, sbuf
);
263 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
266 size_t s
= format_uintmax (value
, NULL
, 0);
267 format_uintmax (value
, sbuf
, s
);
269 xheader_print (xhdr
, keyword
, sbuf
);
273 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
274 struct xheader
*xhdr
)
279 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
284 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
285 struct xheader
*xhdr
)
287 code_time (st
->stat
.st_atime
, keyword
, xhdr
);
291 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
293 st
->stat
.st_atime
= strtoul (arg
, NULL
, 0);
297 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
298 struct xheader
*xhdr
)
300 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
304 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
306 st
->stat
.st_gid
= strtoul (arg
, NULL
, 0);
310 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
311 struct xheader
*xhdr
)
313 code_string (st
->gname
, keyword
, xhdr
);
317 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
319 assign_string (&st
->gname
, arg
);
323 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
324 struct xheader
*xhdr
)
326 code_string (st
->link_name
, keyword
, xhdr
);
330 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
332 assign_string (&st
->link_name
, arg
);
336 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
337 struct xheader
*xhdr
)
339 code_time (st
->stat
.st_ctime
, keyword
, xhdr
);
343 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
345 st
->stat
.st_ctime
= strtoul (arg
, NULL
, 0);
349 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
350 struct xheader
*xhdr
)
352 code_time (st
->stat
.st_mtime
, keyword
, xhdr
);
356 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
358 st
->stat
.st_mtime
= strtoul (arg
, NULL
, 0);
362 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
363 struct xheader
*xhdr
)
365 code_string (st
->file_name
, keyword
, xhdr
);
369 path_decoder (struct tar_stat_info
*st
, char const *arg
)
371 assign_string (&st
->orig_file_name
, arg
);
372 assign_string (&st
->file_name
, arg
);
373 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
377 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
378 struct xheader
*xhdr
)
380 code_num (st
->stat
.st_size
, keyword
, xhdr
);
384 size_decoder (struct tar_stat_info
*st
, char const *arg
)
386 st
->stat
.st_size
= strtoul (arg
, NULL
, 0);
390 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
391 struct xheader
*xhdr
)
393 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
397 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
399 st
->stat
.st_uid
= strtoul (arg
, NULL
, 0);
403 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
404 struct xheader
*xhdr
)
406 code_string (st
->uname
, keyword
, xhdr
);
410 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
412 assign_string (&st
->uname
, arg
);
415 struct xhdr_tab
const xhdr_tab
[] = {
416 { "atime", atime_coder
, atime_decoder
},
417 { "comment", dummy_coder
, dummy_decoder
},
418 { "charset", dummy_coder
, dummy_decoder
},
419 { "ctime", ctime_coder
, ctime_decoder
},
420 { "gid", gid_coder
, gid_decoder
},
421 { "gname", gname_coder
, gname_decoder
},
422 { "linkpath", linkpath_coder
, linkpath_decoder
},
423 { "mtime", mtime_coder
, mtime_decoder
},
424 { "path", path_coder
, path_decoder
},
425 { "size", size_coder
, size_decoder
},
426 { "uid", uid_coder
, uid_decoder
},
427 { "uname", uname_coder
, uname_decoder
},
429 /* The number of entries in xhdr_tab must agree with the array
430 bounds in xhdr_tab's forward declaration. */
432 #if 0 /* GNU private keywords (not yet implemented) */
433 /* Sparse file handling */
434 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
},
435 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
},
437 /* The next directory entry actually contains the names of files
438 that were in the directory at the time the dump was made.
439 Supersedes GNUTYPE_DUMPDIR header type. */
440 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
442 /* Keeps the tape/volume header. May be present only in the global headers.
443 Equivalent to GNUTYPE_VOLHDR. */
444 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
446 /* These may be present in a first global header of the archive.
447 They provide the same functionality as GNUTYPE_MULTIVOL header.
448 The GNU.volume.size keeps the real_s_sizeleft value, which is
449 otherwise kept in the size field of a multivolume header. The
450 GNU.volume.offset keeps the offset of the start of this volume,
451 otherwise kept in oldgnu_header.offset. */
452 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
453 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
This page took 0.055353 seconds and 4 git commands to generate.