]>
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)
55 decode_record (char **p
, struct tar_stat_info
*st
)
61 struct xhdr_tab
const *t
;
66 len
= strtoul (*p
, p
, 10);
69 ERROR ((0, 0, _("Malformed extended header")));
74 for (;*p
< start
+ len
; ++*p
)
80 ERROR ((0, 0, _("Malformed extended header")));
86 t
= locate_handler (keyword
);
96 t
->decoder (st
, value
);
105 xheader_decode (struct tar_stat_info
*st
)
107 char *p
= extended_header
.buffer
;
108 char *endp
= &extended_header
.buffer
[extended_header
.size
-1];
111 if (decode_record (&p
, st
))
116 xheader_store (char const *keyword
, struct tar_stat_info
const *st
)
118 struct xhdr_tab
const *t
;
120 if (extended_header
.buffer
)
122 t
= locate_handler (keyword
);
125 if (!extended_header
.stk
)
127 extended_header
.stk
= xmalloc (sizeof *extended_header
.stk
);
128 obstack_init (extended_header
.stk
);
130 t
->coder (st
, keyword
, &extended_header
);
134 xheader_read (union block
*p
, size_t size
)
139 free (extended_header
.buffer
);
140 extended_header
.size
= size
;
141 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
142 extended_header
.buffer
= xmalloc (size
+ 1);
144 set_next_block_after (p
);
145 for (i
= j
= 0; i
< nblocks
; i
++)
149 p
= find_next_block ();
153 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
154 set_next_block_after (p
);
162 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
169 while ((val
/= 10) != 0);
173 char *p
= buf
+ s
- 1;
177 *p
-- = val
% 10 + '0';
179 while ((val
/= 10) != 0);
188 xheader_print (struct xheader
*xhdr
, char const *keyword
, char const *value
)
190 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
197 n
= format_uintmax (len
+ p
, NULL
, 0);
201 format_uintmax (len
+ n
, nbuf
, n
);
202 obstack_grow (xhdr
->stk
, nbuf
, n
);
203 obstack_1grow (xhdr
->stk
, ' ');
204 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
205 obstack_1grow (xhdr
->stk
, '=');
206 obstack_grow (xhdr
->stk
, value
, strlen (value
));
207 obstack_1grow (xhdr
->stk
, '\n');
211 xheader_finish (struct xheader
*xhdr
)
213 obstack_1grow (xhdr
->stk
, 0);
214 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
215 xhdr
->size
= strlen (xhdr
->buffer
);
219 xheader_destroy (struct xheader
*xhdr
)
223 obstack_free (xhdr
->stk
, NULL
);
234 /* Implementations */
236 code_string (char const *string
, char const *keyword
, struct xheader
*xhdr
)
238 xheader_print (xhdr
, keyword
, string
);
242 code_time (time_t t
, char const *keyword
, struct xheader
*xhdr
)
245 size_t s
= format_uintmax (t
, NULL
, 0);
246 format_uintmax (t
, sbuf
, s
);
248 format_uintmax (0, sbuf
+ s
, 9);
250 xheader_print (xhdr
, keyword
, sbuf
);
254 code_num (uintmax_t value
, char const *keyword
, struct xheader
*xhdr
)
257 size_t s
= format_uintmax (value
, NULL
, 0);
258 format_uintmax (value
, sbuf
, s
);
260 xheader_print (xhdr
, keyword
, sbuf
);
264 dummy_coder (struct tar_stat_info
const *st
, char const *keyword
,
265 struct xheader
*xhdr
)
270 dummy_decoder (struct tar_stat_info
*st
, char const *arg
)
275 atime_coder (struct tar_stat_info
const *st
, char const *keyword
,
276 struct xheader
*xhdr
)
278 code_time (st
->stat
.st_atime
, keyword
, xhdr
);
282 atime_decoder (struct tar_stat_info
*st
, char const *arg
)
284 st
->stat
.st_atime
= strtoul (arg
, NULL
, 0);
288 gid_coder (struct tar_stat_info
const *st
, char const *keyword
,
289 struct xheader
*xhdr
)
291 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
295 gid_decoder (struct tar_stat_info
*st
, char const *arg
)
297 st
->stat
.st_gid
= strtoul (arg
, NULL
, 0);
301 gname_coder (struct tar_stat_info
const *st
, char const *keyword
,
302 struct xheader
*xhdr
)
304 code_string (st
->gname
, keyword
, xhdr
);
308 gname_decoder (struct tar_stat_info
*st
, char const *arg
)
310 assign_string (&st
->gname
, arg
);
314 linkpath_coder (struct tar_stat_info
const *st
, char const *keyword
,
315 struct xheader
*xhdr
)
317 code_string (st
->link_name
, keyword
, xhdr
);
321 linkpath_decoder (struct tar_stat_info
*st
, char const *arg
)
323 assign_string (&st
->link_name
, arg
);
327 ctime_coder (struct tar_stat_info
const *st
, char const *keyword
,
328 struct xheader
*xhdr
)
330 code_time (st
->stat
.st_ctime
, keyword
, xhdr
);
334 ctime_decoder (struct tar_stat_info
*st
, char const *arg
)
336 st
->stat
.st_ctime
= strtoul (arg
, NULL
, 0);
340 mtime_coder (struct tar_stat_info
const *st
, char const *keyword
,
341 struct xheader
*xhdr
)
343 code_time (st
->stat
.st_mtime
, keyword
, xhdr
);
347 mtime_decoder (struct tar_stat_info
*st
, char const *arg
)
349 st
->stat
.st_mtime
= strtoul (arg
, NULL
, 0);
353 path_coder (struct tar_stat_info
const *st
, char const *keyword
,
354 struct xheader
*xhdr
)
356 code_string (st
->file_name
, keyword
, xhdr
);
360 path_decoder (struct tar_stat_info
*st
, char const *arg
)
362 assign_string (&st
->orig_file_name
, arg
);
363 assign_string (&st
->file_name
, arg
);
364 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
368 size_coder (struct tar_stat_info
const *st
, char const *keyword
,
369 struct xheader
*xhdr
)
371 code_num (st
->stat
.st_size
, keyword
, xhdr
);
375 size_decoder (struct tar_stat_info
*st
, char const *arg
)
377 st
->stat
.st_size
= strtoul (arg
, NULL
, 0);
381 uid_coder (struct tar_stat_info
const *st
, char const *keyword
,
382 struct xheader
*xhdr
)
384 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
388 uid_decoder (struct tar_stat_info
*st
, char const *arg
)
390 st
->stat
.st_uid
= strtoul (arg
, NULL
, 0);
394 uname_coder (struct tar_stat_info
const *st
, char const *keyword
,
395 struct xheader
*xhdr
)
397 code_string (st
->uname
, keyword
, xhdr
);
401 uname_decoder (struct tar_stat_info
*st
, char const *arg
)
403 assign_string (&st
->uname
, arg
);
406 static struct xhdr_tab
const xhdr_tab
[] = {
407 { "atime", atime_coder
, atime_decoder
},
408 { "comment", dummy_coder
, dummy_decoder
},
409 { "charset", dummy_coder
, dummy_decoder
},
410 { "ctime", ctime_coder
, ctime_decoder
},
411 { "gid", gid_coder
, gid_decoder
},
412 { "gname", gname_coder
, gname_decoder
},
413 { "linkpath", linkpath_coder
, linkpath_decoder
},
414 { "mtime", mtime_coder
, mtime_decoder
},
415 { "path", path_coder
, path_decoder
},
416 { "size", size_coder
, size_decoder
},
417 { "uid", uid_coder
, uid_decoder
},
418 { "uname", uname_coder
, uname_decoder
},
420 #if 0 /* GNU private keywords (not yet implemented) */
421 /* Sparse file handling */
422 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
},
423 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
},
425 /* The next directory entry actually contains the names of files
426 that were in the directory at the time the dump was made.
427 Supersedes GNUTYPE_DUMPDIR header type. */
428 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
430 /* Keeps the tape/volume header. May be present only in the global headers.
431 Equivalent to GNUTYPE_VOLHDR. */
432 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
434 /* These may be present in a first global header of the archive.
435 They provide the same functionality as GNUTYPE_MULTIVOL header.
436 The GNU.volume.size keeps the real_s_sizeleft value, which is
437 otherwise kept in the size field of a multivolume header. The
438 GNU.volume.offset keeps the offset of the start of this volume,
439 otherwise kept in oldgnu_header.offset. */
440 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
441 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
This page took 0.062671 seconds and 4 git commands to generate.