]>
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 static struct xhdr_tab
const xhdr_tab
[];
41 static struct xhdr_tab
const *
42 locate_handler (char const *keyword
)
44 struct xhdr_tab
const *p
;
46 for (p
= xhdr_tab
; p
->keyword
; p
++)
47 if (strcmp (p
->keyword
, keyword
) == 0)
52 /* Decodes a single extended header record. Advances P to the next
54 Returns true on success, false otherwise. */
56 decode_record (char **p
, struct tar_stat_info
*st
)
62 struct xhdr_tab
const *t
;
67 len
= strtoul (*p
, p
, 10);
70 ERROR ((0, 0, _("Malformed extended header: missing whitespace after the length")));
75 for (;*p
< start
+ len
; ++*p
)
81 ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
87 t
= locate_handler (keyword
);
97 t
->decoder (st
, value
);
106 xheader_decode (struct tar_stat_info
*st
)
108 char *p
= extended_header
.buffer
+ BLOCKSIZE
;
109 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
112 if (!decode_record (&p
, st
))
117 xheader_store (char const *keyword
, struct tar_stat_info
const *st
)
119 struct xhdr_tab
const *t
;
121 if (extended_header
.buffer
)
123 t
= locate_handler (keyword
);
126 if (!extended_header
.stk
)
128 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
129 obstack_init (extended_header
.stk
);
131 t
->coder (st
, keyword
, &extended_header
);
135 xheader_read (union block
*p
, size_t size
)
140 free (extended_header
.buffer
);
142 extended_header
.size
= size
;
143 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
144 extended_header
.buffer
= xmalloc (size
+ 1);
153 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
154 set_next_block_after (p
);
156 p
= find_next_block ();
165 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
172 while ((val
/= 10) != 0);
176 char *p
= buf
+ s
- 1;
180 *p
-- = val
% 10 + '0';
182 while ((val
/= 10) != 0);
191 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
193 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
200 n
= format_uintmax (len
+ p
, NULL
, 0);
204 format_uintmax (len
+ n
, nbuf
, n
);
205 obstack_grow (xhdr
->stk
, nbuf
, n
);
206 obstack_1grow (xhdr
->stk
, ' ');
207 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
208 obstack_1grow (xhdr
->stk
, '=');
209 obstack_grow (xhdr
->stk
, value
, strlen (value
));
210 obstack_1grow (xhdr
->stk
, '\n');
214 xheader_finish (struct xheader
*xhdr
)
216 obstack_1grow (xhdr
->stk
, 0);
217 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
218 xhdr
->size
= strlen (xhdr
->buffer
);
222 xheader_destroy (struct xheader
*xhdr
)
226 obstack_free (xhdr
->stk
, NULL
);
237 /* Implementations */
239 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
241 xheader_print (xhdr
, keyword
, string
);
245 code_time (time_t t
, char const *keyword
, struct xheader
*xhdr
)
248 size_t s
= format_uintmax (t
, NULL
, 0);
249 format_uintmax (t
, sbuf
, s
);
251 format_uintmax (0, sbuf
+ s
, 9);
253 xheader_print (xhdr
, keyword
, sbuf
);
257 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
260 size_t s
= format_uintmax (value
, NULL
, 0);
261 format_uintmax (value
, sbuf
, s
);
263 xheader_print (xhdr
, keyword
, sbuf
);
267 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
268 struct xheader
*xhdr
)
273 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
278 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
279 struct xheader
*xhdr
)
281 code_time (st
->stat
.st_atime
, keyword
, xhdr
);
285 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
287 st
->stat
.st_atime
= strtoul (arg
, NULL
, 0);
291 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
292 struct xheader
*xhdr
)
294 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
298 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
300 st
->stat
.st_gid
= strtoul (arg
, NULL
, 0);
304 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
305 struct xheader
*xhdr
)
307 code_string (st
->gname
, keyword
, xhdr
);
311 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
313 assign_string (&st
->gname
, arg
);
317 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
318 struct xheader
*xhdr
)
320 code_string (st
->link_name
, keyword
, xhdr
);
324 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
326 assign_string (&st
->link_name
, arg
);
330 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
331 struct xheader
*xhdr
)
333 code_time (st
->stat
.st_ctime
, keyword
, xhdr
);
337 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
339 st
->stat
.st_ctime
= strtoul (arg
, NULL
, 0);
343 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
344 struct xheader
*xhdr
)
346 code_time (st
->stat
.st_mtime
, keyword
, xhdr
);
350 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
352 st
->stat
.st_mtime
= strtoul (arg
, NULL
, 0);
356 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
357 struct xheader
*xhdr
)
359 code_string (st
->file_name
, keyword
, xhdr
);
363 path_decoder (struct tar_stat_info
*st
, char const *arg
)
365 assign_string (&st
->orig_file_name
, arg
);
366 assign_string (&st
->file_name
, arg
);
367 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
371 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
372 struct xheader
*xhdr
)
374 code_num (st
->stat
.st_size
, keyword
, xhdr
);
378 size_decoder (struct tar_stat_info
*st
, char const *arg
)
380 st
->stat
.st_size
= strtoul (arg
, NULL
, 0);
384 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
385 struct xheader
*xhdr
)
387 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
391 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
393 st
->stat
.st_uid
= strtoul (arg
, NULL
, 0);
397 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
398 struct xheader
*xhdr
)
400 code_string (st
->uname
, keyword
, xhdr
);
404 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
406 assign_string (&st
->uname
, arg
);
409 static struct xhdr_tab
const xhdr_tab
[] = {
410 { "atime", atime_coder
, atime_decoder
},
411 { "comment", dummy_coder
, dummy_decoder
},
412 { "charset", dummy_coder
, dummy_decoder
},
413 { "ctime", ctime_coder
, ctime_decoder
},
414 { "gid", gid_coder
, gid_decoder
},
415 { "gname", gname_coder
, gname_decoder
},
416 { "linkpath", linkpath_coder
, linkpath_decoder
},
417 { "mtime", mtime_coder
, mtime_decoder
},
418 { "path", path_coder
, path_decoder
},
419 { "size", size_coder
, size_decoder
},
420 { "uid", uid_coder
, uid_decoder
},
421 { "uname", uname_coder
, uname_decoder
},
423 #if 0 /* GNU private keywords (not yet implemented) */
424 /* Sparse file handling */
425 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
},
426 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
},
428 /* The next directory entry actually contains the names of files
429 that were in the directory at the time the dump was made.
430 Supersedes GNUTYPE_DUMPDIR header type. */
431 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
433 /* Keeps the tape/volume header. May be present only in the global headers.
434 Equivalent to GNUTYPE_VOLHDR. */
435 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
437 /* These may be present in a first global header of the archive.
438 They provide the same functionality as GNUTYPE_MULTIVOL header.
439 The GNU.volume.size keeps the real_s_sizeleft value, which is
440 otherwise kept in the size field of a multivolume header. The
441 GNU.volume.offset keeps the offset of the start of this volume,
442 otherwise kept in oldgnu_header.offset. */
443 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
444 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
This page took 0.055697 seconds and 5 git commands to generate.