xheader.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* POSIX extended and STAR headers.
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. #include "system.h"
  15. #include <grp.h>
  16. #include <hash.h>
  17. #include <pwd.h>
  18. #include <quotearg.h>
  19. #include "common.h"
  20. #define obstack_chunk_alloc xmalloc
  21. #define obstack_chunk_free free
  22. #include <obstack.h>
  23. /* General Interface */
  24. struct xhdr_tab
  25. {
  26. char const *keyword;
  27. void (*coder) (struct tar_stat_info const *, char const *, struct xheader *);
  28. void (*decoder) (struct tar_stat_info *, char const *);
  29. };
  30. static struct xhdr_tab const xhdr_tab[];
  31. static struct xhdr_tab const *
  32. locate_handler (char const *keyword)
  33. {
  34. struct xhdr_tab const *p;
  35. for (p = xhdr_tab; p->keyword; p++)
  36. if (strcmp (p->keyword, keyword) == 0)
  37. return p;
  38. return NULL;
  39. }
  40. /* Decodes a single extended header record. Advances P to the next
  41. record.
  42. Returns true on success, false otherwise. */
  43. static bool
  44. decode_record (char **p, struct tar_stat_info *st)
  45. {
  46. size_t len;
  47. char const *keyword;
  48. char *eqp;
  49. char *start = *p;
  50. struct xhdr_tab const *t;
  51. if (**p == 0)
  52. return false;
  53. len = strtoul (*p, p, 10);
  54. if (**p != ' ')
  55. {
  56. ERROR ((0, 0, _("Malformed extended header: missing whitespace after the length")));
  57. return false;
  58. }
  59. keyword = ++*p;
  60. for (;*p < start + len; ++*p)
  61. if (**p == '=')
  62. break;
  63. if (**p != '=')
  64. {
  65. ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
  66. return false;
  67. }
  68. eqp = *p;
  69. **p = 0;
  70. t = locate_handler (keyword);
  71. if (t)
  72. {
  73. char endc;
  74. char *value;
  75. value = ++*p;
  76. endc = start[len-1];
  77. start[len-1] = 0;
  78. t->decoder (st, value);
  79. start[len-1] = endc;
  80. }
  81. *eqp = '=';
  82. *p = &start[len];
  83. return true;
  84. }
  85. void
  86. xheader_decode (struct tar_stat_info *st)
  87. {
  88. char *p = extended_header.buffer + BLOCKSIZE;
  89. char *endp = &extended_header.buffer[extended_header.size-1];
  90. while (p < endp)
  91. if (!decode_record (&p, st))
  92. break;
  93. }
  94. void
  95. xheader_store (char const *keyword, struct tar_stat_info const *st)
  96. {
  97. struct xhdr_tab const *t;
  98. if (extended_header.buffer)
  99. return;
  100. t = locate_handler (keyword);
  101. if (!t)
  102. return;
  103. if (!extended_header.stk)
  104. {
  105. extended_header.stk = xmalloc (sizeof *extended_header.stk);
  106. obstack_init (extended_header.stk);
  107. }
  108. t->coder (st, keyword, &extended_header);
  109. }
  110. void
  111. xheader_read (union block *p, size_t size)
  112. {
  113. size_t j = 0;
  114. size_t nblocks;
  115. free (extended_header.buffer);
  116. size += BLOCKSIZE;
  117. extended_header.size = size;
  118. nblocks = (size + BLOCKSIZE - 1) / BLOCKSIZE;
  119. extended_header.buffer = xmalloc (size + 1);
  120. do
  121. {
  122. size_t len = size;
  123. if (len > BLOCKSIZE)
  124. len = BLOCKSIZE;
  125. memcpy (&extended_header.buffer[j], p->buffer, len);
  126. set_next_block_after (p);
  127. p = find_next_block ();
  128. j += len;
  129. size -= len;
  130. }
  131. while (size > 0);
  132. }
  133. static size_t
  134. format_uintmax (uintmax_t val, char *buf, size_t s)
  135. {
  136. if (!buf)
  137. {
  138. s = 0;
  139. do
  140. s++;
  141. while ((val /= 10) != 0);
  142. }
  143. else
  144. {
  145. char *p = buf + s - 1;
  146. do
  147. {
  148. *p-- = val % 10 + '0';
  149. }
  150. while ((val /= 10) != 0);
  151. while (p >= buf)
  152. *p-- = '0';
  153. }
  154. return s;
  155. }
  156. static void
  157. xheader_print (struct xheader *xhdr, char const *keyword, char const *value)
  158. {
  159. size_t len = strlen (keyword) + strlen (value) + 3; /* ' ' + '=' + '\n' */
  160. size_t p, n = 0;
  161. char nbuf[100];
  162. do
  163. {
  164. p = n;
  165. n = format_uintmax (len + p, NULL, 0);
  166. }
  167. while (n != p);
  168. format_uintmax (len + n, nbuf, n);
  169. obstack_grow (xhdr->stk, nbuf, n);
  170. obstack_1grow (xhdr->stk, ' ');
  171. obstack_grow (xhdr->stk, keyword, strlen (keyword));
  172. obstack_1grow (xhdr->stk, '=');
  173. obstack_grow (xhdr->stk, value, strlen (value));
  174. obstack_1grow (xhdr->stk, '\n');
  175. }
  176. void
  177. xheader_finish (struct xheader *xhdr)
  178. {
  179. obstack_1grow (xhdr->stk, 0);
  180. xhdr->buffer = obstack_finish (xhdr->stk);
  181. xhdr->size = strlen (xhdr->buffer);
  182. }
  183. void
  184. xheader_destroy (struct xheader *xhdr)
  185. {
  186. if (xhdr->stk)
  187. {
  188. obstack_free (xhdr->stk, NULL);
  189. free (xhdr->stk);
  190. xhdr->stk = NULL;
  191. }
  192. else
  193. free (xhdr->buffer);
  194. xhdr->buffer = 0;
  195. xhdr->size = 0;
  196. }
  197. /* Implementations */
  198. static void
  199. code_string (char const *string, char const *keyword, struct xheader *xhdr)
  200. {
  201. xheader_print (xhdr, keyword, string);
  202. }
  203. static void
  204. code_time (time_t t, char const *keyword, struct xheader *xhdr)
  205. {
  206. char sbuf[100];
  207. size_t s = format_uintmax (t, NULL, 0);
  208. format_uintmax (t, sbuf, s);
  209. sbuf[s++] = '.';
  210. format_uintmax (0, sbuf + s, 9);
  211. sbuf[s+9] = 0;
  212. xheader_print (xhdr, keyword, sbuf);
  213. }
  214. static void
  215. code_num (uintmax_t value, char const *keyword, struct xheader *xhdr)
  216. {
  217. char sbuf[100];
  218. size_t s = format_uintmax (value, NULL, 0);
  219. format_uintmax (value, sbuf, s);
  220. sbuf[s] = 0;
  221. xheader_print (xhdr, keyword, sbuf);
  222. }
  223. static void
  224. dummy_coder (struct tar_stat_info const *st, char const *keyword,
  225. struct xheader *xhdr)
  226. {
  227. }
  228. static void
  229. dummy_decoder (struct tar_stat_info *st, char const *arg)
  230. {
  231. }
  232. static void
  233. atime_coder (struct tar_stat_info const *st, char const *keyword,
  234. struct xheader *xhdr)
  235. {
  236. code_time (st->stat.st_atime, keyword, xhdr);
  237. }
  238. static void
  239. atime_decoder (struct tar_stat_info *st, char const *arg)
  240. {
  241. st->stat.st_atime = strtoul (arg, NULL, 0);
  242. }
  243. static void
  244. gid_coder (struct tar_stat_info const *st, char const *keyword,
  245. struct xheader *xhdr)
  246. {
  247. code_num (st->stat.st_gid, keyword, xhdr);
  248. }
  249. static void
  250. gid_decoder (struct tar_stat_info *st, char const *arg)
  251. {
  252. st->stat.st_gid = strtoul (arg, NULL, 0);
  253. }
  254. static void
  255. gname_coder (struct tar_stat_info const *st, char const *keyword,
  256. struct xheader *xhdr)
  257. {
  258. code_string (st->gname, keyword, xhdr);
  259. }
  260. static void
  261. gname_decoder (struct tar_stat_info *st, char const *arg)
  262. {
  263. assign_string (&st->gname, arg);
  264. }
  265. static void
  266. linkpath_coder (struct tar_stat_info const *st, char const *keyword,
  267. struct xheader *xhdr)
  268. {
  269. code_string (st->link_name, keyword, xhdr);
  270. }
  271. static void
  272. linkpath_decoder (struct tar_stat_info *st, char const *arg)
  273. {
  274. assign_string (&st->link_name, arg);
  275. }
  276. static void
  277. ctime_coder (struct tar_stat_info const *st, char const *keyword,
  278. struct xheader *xhdr)
  279. {
  280. code_time (st->stat.st_ctime, keyword, xhdr);
  281. }
  282. static void
  283. ctime_decoder (struct tar_stat_info *st, char const *arg)
  284. {
  285. st->stat.st_ctime = strtoul (arg, NULL, 0);
  286. }
  287. static void
  288. mtime_coder (struct tar_stat_info const *st, char const *keyword,
  289. struct xheader *xhdr)
  290. {
  291. code_time (st->stat.st_mtime, keyword, xhdr);
  292. }
  293. static void
  294. mtime_decoder (struct tar_stat_info *st, char const *arg)
  295. {
  296. st->stat.st_mtime = strtoul (arg, NULL, 0);
  297. }
  298. static void
  299. path_coder (struct tar_stat_info const *st, char const *keyword,
  300. struct xheader *xhdr)
  301. {
  302. code_string (st->file_name, keyword, xhdr);
  303. }
  304. static void
  305. path_decoder (struct tar_stat_info *st, char const *arg)
  306. {
  307. assign_string (&st->orig_file_name, arg);
  308. assign_string (&st->file_name, arg);
  309. st->had_trailing_slash = strip_trailing_slashes (st->file_name);
  310. }
  311. static void
  312. size_coder (struct tar_stat_info const *st, char const *keyword,
  313. struct xheader *xhdr)
  314. {
  315. code_num (st->stat.st_size, keyword, xhdr);
  316. }
  317. static void
  318. size_decoder (struct tar_stat_info *st, char const *arg)
  319. {
  320. st->stat.st_size = strtoul (arg, NULL, 0);
  321. }
  322. static void
  323. uid_coder (struct tar_stat_info const *st, char const *keyword,
  324. struct xheader *xhdr)
  325. {
  326. code_num (st->stat.st_uid, keyword, xhdr);
  327. }
  328. static void
  329. uid_decoder (struct tar_stat_info *st, char const *arg)
  330. {
  331. st->stat.st_uid = strtoul (arg, NULL, 0);
  332. }
  333. static void
  334. uname_coder (struct tar_stat_info const *st, char const *keyword,
  335. struct xheader *xhdr)
  336. {
  337. code_string (st->uname, keyword, xhdr);
  338. }
  339. static void
  340. uname_decoder (struct tar_stat_info *st, char const *arg)
  341. {
  342. assign_string (&st->uname, arg);
  343. }
  344. static struct xhdr_tab const xhdr_tab[] = {
  345. { "atime", atime_coder, atime_decoder },
  346. { "comment", dummy_coder, dummy_decoder },
  347. { "charset", dummy_coder, dummy_decoder },
  348. { "ctime", ctime_coder, ctime_decoder },
  349. { "gid", gid_coder, gid_decoder },
  350. { "gname", gname_coder, gname_decoder },
  351. { "linkpath", linkpath_coder, linkpath_decoder},
  352. { "mtime", mtime_coder, mtime_decoder },
  353. { "path", path_coder, path_decoder },
  354. { "size", size_coder, size_decoder },
  355. { "uid", uid_coder, uid_decoder },
  356. { "uname", uname_coder, uname_decoder },
  357. #if 0 /* GNU private keywords (not yet implemented) */
  358. /* Sparse file handling */
  359. { "GNU.sparse.offset", sparse_offset_coder, sparse_offset_decoder },
  360. { "GNU.sparse.numbytes", sparse_numbytes_coder, sparse_numbytes_decoder },
  361. /* The next directory entry actually contains the names of files
  362. that were in the directory at the time the dump was made.
  363. Supersedes GNUTYPE_DUMPDIR header type. */
  364. { "GNU.dumpdir", dumpdir_coder, dumpdir_decoder },
  365. /* Keeps the tape/volume header. May be present only in the global headers.
  366. Equivalent to GNUTYPE_VOLHDR. */
  367. { "GNU.volume.header", volume_header_coder, volume_header_decoder },
  368. /* These may be present in a first global header of the archive.
  369. They provide the same functionality as GNUTYPE_MULTIVOL header.
  370. The GNU.volume.size keeps the real_s_sizeleft value, which is
  371. otherwise kept in the size field of a multivolume header. The
  372. GNU.volume.offset keeps the offset of the start of this volume,
  373. otherwise kept in oldgnu_header.offset. */
  374. { "GNU.volume.size", volume_size_coder, volume_size_decoder },
  375. { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder },
  376. #endif
  377. { NULL, NULL, NULL }
  378. };