]>
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. */
28 #define obstack_chunk_alloc xmalloc
29 #define obstack_chunk_free free
32 /* General Interface */
37 void (*coder
) (struct tar_stat_info
const *, char const *, struct xheader
*);
38 void (*decoder
) (struct tar_stat_info
*, char const *);
41 static struct xhdr_tab
const xhdr_tab
[];
43 static struct xhdr_tab
const *
44 locate_handler (char const *keyword
)
46 struct xhdr_tab
const *p
;
48 for (p
= xhdr_tab
; p
->keyword
; p
++)
49 if (strcmp (p
->keyword
, keyword
) == 0)
54 /* Decodes a single extended header record. Advances P to the next
56 Returns true on success, false otherwise. */
58 decode_record (char **p
, struct tar_stat_info
*st
)
64 struct xhdr_tab
const *t
;
69 len
= strtoul (*p
, p
, 10);
72 ERROR ((0, 0, _("Malformed extended header: missing whitespace after the length")));
77 for (;*p
< start
+ len
; ++*p
)
83 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
89 t
= locate_handler (keyword
);
99 t
->decoder (st
, value
);
108 xheader_decode (struct tar_stat_info
*st
)
110 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
111 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
114 if (!decode_record (&p
, st
))
119 xheader_store (char const *keyword
, struct tar_stat_info
const *st
)
121 struct xhdr_tab
const *t
;
123 if (extended_header
.buffer
)
125 t
= locate_handler (keyword
);
128 if (!extended_header
.stk
)
130 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
131 obstack_init (extended_header
.stk
);
133 t
->coder (st
, keyword
, &extended_header
);
137 xheader_read (union block
*p
, size_t size
)
142 free (extended_header
.buffer
);
144 extended_header
.size
= size
;
145 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
146 extended_header
.buffer
= xmalloc (size
+ 1);
155 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
156 set_next_block_after (p
);
158 p
= find_next_block ();
167 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
174 while ((val
/= 10) != 0);
178 char *p
= buf
+ s
- 1;
182 *p
-- = val
% 10 + '0';
184 while ((val
/= 10) != 0);
193 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
195 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
202 n
= format_uintmax (len
+ p
, NULL
, 0);
206 format_uintmax (len
+ n
, nbuf
, n
);
207 obstack_grow (xhdr
->stk
, nbuf
, n
);
208 obstack_1grow (xhdr
->stk
, ' ');
209 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
210 obstack_1grow (xhdr
->stk
, '=');
211 obstack_grow (xhdr
->stk
, value
, strlen (value
));
212 obstack_1grow (xhdr
->stk
, '\n');
216 xheader_finish (struct xheader
*xhdr
)
218 obstack_1grow (xhdr
->stk
, 0);
219 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
220 xhdr
->size
= strlen (xhdr
->buffer
);
224 xheader_destroy (struct xheader
*xhdr
)
228 obstack_free (xhdr
->stk
, NULL
);
239 /* Implementations */
241 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
243 xheader_print (xhdr
, keyword
, string
);
247 code_time (time_t t
, char const *keyword
, struct xheader
*xhdr
)
250 size_t s
= format_uintmax (t
, NULL
, 0);
251 format_uintmax (t
, sbuf
, s
);
253 format_uintmax (0, sbuf
+ s
, 9);
255 xheader_print (xhdr
, keyword
, sbuf
);
259 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
262 size_t s
= format_uintmax (value
, NULL
, 0);
263 format_uintmax (value
, sbuf
, s
);
265 xheader_print (xhdr
, keyword
, sbuf
);
269 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
270 struct xheader
*xhdr
)
275 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
280 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
281 struct xheader
*xhdr
)
283 code_time (st
->stat
.st_atime
, keyword
, xhdr
);
287 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
289 st
->stat
.st_atime
= strtoul (arg
, NULL
, 0);
293 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
294 struct xheader
*xhdr
)
296 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
300 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
302 st
->stat
.st_gid
= strtoul (arg
, NULL
, 0);
306 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
307 struct xheader
*xhdr
)
309 code_string (st
->gname
, keyword
, xhdr
);
313 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
315 assign_string (&st
->gname
, arg
);
319 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
320 struct xheader
*xhdr
)
322 code_string (st
->link_name
, keyword
, xhdr
);
326 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
328 assign_string (&st
->link_name
, arg
);
332 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
333 struct xheader
*xhdr
)
335 code_time (st
->stat
.st_ctime
, keyword
, xhdr
);
339 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
341 st
->stat
.st_ctime
= strtoul (arg
, NULL
, 0);
345 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
346 struct xheader
*xhdr
)
348 code_time (st
->stat
.st_mtime
, keyword
, xhdr
);
352 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
354 st
->stat
.st_mtime
= strtoul (arg
, NULL
, 0);
358 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
359 struct xheader
*xhdr
)
361 code_string (st
->file_name
, keyword
, xhdr
);
365 path_decoder (struct tar_stat_info
*st
, char const *arg
)
367 assign_string (&st
->orig_file_name
, arg
);
368 assign_string (&st
->file_name
, arg
);
369 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
373 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
374 struct xheader
*xhdr
)
376 code_num (st
->stat
.st_size
, keyword
, xhdr
);
380 size_decoder (struct tar_stat_info
*st
, char const *arg
)
382 st
->stat
.st_size
= strtoul (arg
, NULL
, 0);
386 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
387 struct xheader
*xhdr
)
389 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
393 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
395 st
->stat
.st_uid
= strtoul (arg
, NULL
, 0);
399 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
400 struct xheader
*xhdr
)
402 code_string (st
->uname
, keyword
, xhdr
);
406 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
408 assign_string (&st
->uname
, arg
);
411 static struct xhdr_tab
const xhdr_tab
[] = {
412 { "atime", atime_coder
, atime_decoder
},
413 { "comment", dummy_coder
, dummy_decoder
},
414 { "charset", dummy_coder
, dummy_decoder
},
415 { "ctime", ctime_coder
, ctime_decoder
},
416 { "gid", gid_coder
, gid_decoder
},
417 { "gname", gname_coder
, gname_decoder
},
418 { "linkpath", linkpath_coder
, linkpath_decoder
},
419 { "mtime", mtime_coder
, mtime_decoder
},
420 { "path", path_coder
, path_decoder
},
421 { "size", size_coder
, size_decoder
},
422 { "uid", uid_coder
, uid_decoder
},
423 { "uname", uname_coder
, uname_decoder
},
425 #if 0 /* GNU private keywords (not yet implemented) */
426 /* Sparse file handling */
427 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
},
428 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
},
430 /* The next directory entry actually contains the names of files
431 that were in the directory at the time the dump was made.
432 Supersedes GNUTYPE_DUMPDIR header type. */
433 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
435 /* Keeps the tape/volume header. May be present only in the global headers.
436 Equivalent to GNUTYPE_VOLHDR. */
437 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
439 /* These may be present in a first global header of the archive.
440 They provide the same functionality as GNUTYPE_MULTIVOL header.
441 The GNU.volume.size keeps the real_s_sizeleft value, which is
442 otherwise kept in the size field of a multivolume header. The
443 GNU.volume.offset keeps the offset of the start of this volume,
444 otherwise kept in oldgnu_header.offset. */
445 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
446 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
This page took 0.053925 seconds and 5 git commands to generate.