]>
Dogcows Code - chaz/tar/blob - src/xheader.c
1 /* This file is part of GNU Tar
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 /* Forward declarations */
33 static void dummy_coder (struct tar_stat_info
*st
, char *keyword
,
34 struct xheader
*xhdr
);
35 static void dummy_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
37 static void atime_coder (struct tar_stat_info
*st
, char *keyword
,
38 struct xheader
*xhdr
);
39 static void atime_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
41 static void gid_coder (struct tar_stat_info
*st
, char *keyword
,
42 struct xheader
*xhdr
);
43 static void gid_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
45 static void gname_coder (struct tar_stat_info
*st
, char *keyword
,
46 struct xheader
*xhdr
);
47 static void gname_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
49 static void linkpath_coder (struct tar_stat_info
*st
, char *keyword
,
50 struct xheader
*xhdr
);
51 static void linkpath_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
53 static void mtime_coder (struct tar_stat_info
*st
, char *keyword
,
54 struct xheader
*xhdr
);
55 static void mtime_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
57 static void ctime_coder (struct tar_stat_info
*st
, char *keyword
,
58 struct xheader
*xhdr
);
59 static void ctime_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
61 static void path_coder (struct tar_stat_info
*st
, char *keyword
,
62 struct xheader
*xhdr
);
63 static void path_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
65 static void size_coder (struct tar_stat_info
*st
, char *keyword
,
66 struct xheader
*xhdr
);
67 static void size_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
69 static void uid_coder (struct tar_stat_info
*st
, char *keyword
,
70 struct xheader
*xhdr
);
71 static void uid_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
73 static void uname_coder (struct tar_stat_info
*st
, char *keyword
,
74 struct xheader
*xhdr
);
75 static void uname_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
);
77 /* General Interface */
82 void (*coder
) (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
);
83 void (*decoder
) (struct tar_stat_info
*st
, char *keyword
, char *arg
);
86 struct xhdr_tab xhdr_tab
[] = {
87 { "atime", atime_coder
, atime_decoder
},
88 { "comment", dummy_coder
, dummy_decoder
},
89 { "charset", dummy_coder
, dummy_decoder
},
90 { "ctime", ctime_coder
, ctime_decoder
},
91 { "gid", gid_coder
, gid_decoder
},
92 { "gname", gname_coder
, gname_decoder
},
93 { "linkpath", linkpath_coder
, linkpath_decoder
},
94 { "mtime", mtime_coder
, mtime_decoder
},
95 { "path", path_coder
, path_decoder
},
96 { "size", size_coder
, size_decoder
},
97 { "uid", uid_coder
, uid_decoder
},
98 { "uname", uname_coder
, uname_decoder
},
99 #if 0 /* GNU private keywords (not yet implemented) */
100 /* Sparse file handling */
101 { "GNU.sparse.offset", sparse_offset_coder
, sparse_offset_decoder
},
102 { "GNU.sparse.numbytes", sparse_numbytes_coder
, sparse_numbytes_decoder
},
104 /* The next directory entry actually contains the names of files that were
105 in the directory at the time the dump was made. Supersedes
106 GNUTYPE_DUMPDIR header type */
107 { "GNU.dumpdir", dumpdir_coder
, dumpdir_decoder
},
109 /* Keeps the tape/volume header. May be present only in the global headers.
110 Equivalent to GNUTYPE_VOLHDR */
111 { "GNU.volume.header", volume_header_coder
, volume_header_decoder
},
113 /* These may be present in a first global header of the archive. They
114 provide the same functionality as GNUTYPE_MULTIVOL header.
115 The GNU.volume.size keeps the real_s_sizeleft value (which is
116 otherwise kept in the size field of a multivolume header).
117 The GNU.volume.offset keeps the offset of the start of this
118 volume (otherwise kept in oldgnu_header.offset */
119 { "GNU.volume.size", volume_size_coder
, volume_size_decoder
},
120 { "GNU.volume.offset", volume_offset_coder
, volume_offset_decoder
},
125 static struct xhdr_tab
*
126 locate_handler (char *keyword
)
130 for (p
= xhdr_tab
; p
->keyword
; p
++)
131 if (strcmp (p
->keyword
, keyword
) == 0)
137 decode_record (char **p
, struct tar_stat_info
*st
)
147 len
= strtoul (*p
, p
, 10);
150 ERROR ((0, 0, _("Malformed extended headed")));
155 for (;*p
< start
+ len
; ++*p
)
161 ERROR ((0, 0, _("Malformed extended headed")));
167 t
= locate_handler (keyword
);
177 t
->decoder (st
, keyword
, value
);
186 xheader_decode (struct tar_stat_info
*st
)
190 p
= extended_header
.buffer
;
191 endp
= &extended_header
.buffer
[extended_header
.size
-1];
194 if (decode_record (&p
, st
))
199 xheader_store (char *keyword
, struct tar_stat_info
*st
)
203 if (extended_header
.buffer
)
205 t
= locate_handler (keyword
);
208 if (!extended_header
.stk
)
210 extended_header
.stk
= xmalloc (sizeof (*extended_header
.stk
));
211 obstack_init (extended_header
.stk
);
213 t
->coder (st
, keyword
, &extended_header
);
217 xheader_read (union block
*p
, size_t size
)
222 free (extended_header
.buffer
);
223 extended_header
.size
= size
;
224 nblocks
= (size
+ BLOCKSIZE
- 1) / BLOCKSIZE
;
225 extended_header
.buffer
= xmalloc (size
+ 1);
227 set_next_block_after (p
);
228 for (i
= j
= 0; i
< nblocks
; i
++)
232 p
= find_next_block ();
236 memcpy (&extended_header
.buffer
[j
], p
->buffer
, len
);
237 set_next_block_after (p
);
245 format_uintmax (uintmax_t val
, char *buf
, size_t s
)
252 while ((val
/= 10) != 0);
256 char *p
= buf
+ s
- 1;
260 *p
-- = val
% 10 + '0';
262 while ((val
/= 10) != 0);
271 xheader_print (struct xheader
*xhdr
, char *keyword
, char *value
)
273 size_t len
= strlen (keyword
) + strlen (value
) + 3; /* ' ' + '=' + '\n' */
280 n
= format_uintmax (len
+ p
, NULL
, 0);
284 format_uintmax (len
+ n
, nbuf
, n
);
285 obstack_grow (xhdr
->stk
, nbuf
, n
);
286 obstack_1grow (xhdr
->stk
, ' ');
287 obstack_grow (xhdr
->stk
, keyword
, strlen (keyword
));
288 obstack_1grow (xhdr
->stk
, '=');
289 obstack_grow (xhdr
->stk
, value
, strlen (value
));
290 obstack_1grow (xhdr
->stk
, '\n');
294 xheader_finish (struct xheader
*xhdr
)
296 obstack_1grow (xhdr
->stk
, 0);
297 xhdr
->buffer
= obstack_finish (xhdr
->stk
);
298 xhdr
->size
= strlen (xhdr
->buffer
);
302 xheader_destroy (struct xheader
*xhdr
)
306 obstack_free (xhdr
->stk
, NULL
);
317 /* Implementations */
319 code_string (char *string
, char *keyword
, struct xheader
*xhdr
)
321 xheader_print (xhdr
, keyword
, string
);
325 code_time (time_t t
, char *keyword
, struct xheader
*xhdr
)
328 size_t s
= format_uintmax (t
, NULL
, 0);
329 format_uintmax (t
, sbuf
, s
);
331 format_uintmax (0, sbuf
+ s
, 9);
333 xheader_print (xhdr
, keyword
, sbuf
);
337 code_num (uintmax_t value
, char *keyword
, struct xheader
*xhdr
)
340 size_t s
= format_uintmax (value
, NULL
, 0);
341 format_uintmax (value
, sbuf
, s
);
343 xheader_print (xhdr
, keyword
, sbuf
);
347 dummy_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
352 dummy_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
357 atime_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
359 code_time (st
->stat
.st_atime
, keyword
, xhdr
);
363 atime_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
365 st
->stat
.st_atime
= strtoul (arg
, NULL
, 0);
369 gid_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
371 code_num (st
->stat
.st_gid
, keyword
, xhdr
);
375 gid_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
377 st
->stat
.st_gid
= strtoul (arg
, NULL
, 0);
381 gname_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
383 code_string (st
->gname
, keyword
, xhdr
);
387 gname_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
389 assign_string (&st
->gname
, arg
);
393 linkpath_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
395 code_string (st
->link_name
, keyword
, xhdr
);
399 linkpath_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
401 assign_string (&st
->link_name
, arg
);
405 ctime_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
407 code_time (st
->stat
.st_ctime
, keyword
, xhdr
);
411 ctime_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
413 st
->stat
.st_ctime
= strtoul (arg
, NULL
, 0);
417 mtime_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
419 code_time (st
->stat
.st_mtime
, keyword
, xhdr
);
423 mtime_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
425 st
->stat
.st_mtime
= strtoul (arg
, NULL
, 0);
429 path_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
431 code_string (st
->file_name
, keyword
, xhdr
);
435 path_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
437 assign_string (&st
->orig_file_name
, arg
);
438 assign_string (&st
->file_name
, arg
);
439 st
->had_trailing_slash
= strip_trailing_slashes (st
->file_name
);
443 size_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
445 code_num (st
->stat
.st_size
, keyword
, xhdr
);
449 size_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
451 st
->stat
.st_size
= strtoul (arg
, NULL
, 0);
455 uid_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
457 code_num (st
->stat
.st_uid
, keyword
, xhdr
);
461 uid_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
463 st
->stat
.st_uid
= strtoul (arg
, NULL
, 0);
467 uname_coder (struct tar_stat_info
*st
, char *keyword
, struct xheader
*xhdr
)
469 code_string (st
->uname
, keyword
, xhdr
);
473 uname_decoder (struct tar_stat_info
*st
, char *keyword
, char *arg
)
475 assign_string (&st
->uname
, arg
);
This page took 0.060269 seconds and 5 git commands to generate.