xheader.c 11 KB

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