xsparse.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* xsparse - expands compressed sparse file images extracted from GNU tar
  2. archives.
  3. Copyright 2006-2007, 2010, 2013-2014, 2016 Free Software Foundation,
  4. Inc.
  5. This file is part of GNU tar.
  6. GNU tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. GNU tar is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. Written by Sergey Poznyakoff */
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #include <limits.h>
  26. #include <errno.h>
  27. /* Bound on length of the string representing an off_t.
  28. See INT_STRLEN_BOUND in intprops.h for explanation */
  29. #define OFF_T_STRLEN_BOUND ((sizeof (off_t) * CHAR_BIT) * 146 / 485 + 1)
  30. #define OFF_T_STRSIZE_BOUND (OFF_T_STRLEN_BOUND+1)
  31. #define BLOCKSIZE 512
  32. struct sp_array
  33. {
  34. off_t offset;
  35. off_t numbytes;
  36. };
  37. char *progname;
  38. int verbose;
  39. void
  40. die (int code, char *fmt, ...)
  41. {
  42. va_list ap;
  43. fprintf (stderr, "%s: ", progname);
  44. va_start (ap, fmt);
  45. vfprintf (stderr, fmt, ap);
  46. va_end (ap);
  47. fprintf (stderr, "\n");
  48. exit (code);
  49. }
  50. void *
  51. emalloc (size_t size)
  52. {
  53. char *p = malloc (size);
  54. if (!p)
  55. die (1, "not enough memory");
  56. return p;
  57. }
  58. off_t
  59. string_to_off (char *p, char **endp)
  60. {
  61. off_t v = 0;
  62. for (; *p; p++)
  63. {
  64. int digit = *p - '0';
  65. off_t x = v * 10;
  66. if (9 < (unsigned) digit)
  67. {
  68. if (endp)
  69. {
  70. *endp = p;
  71. break;
  72. }
  73. die (1, "number parse error near %s", p);
  74. }
  75. else if (x / 10 != v)
  76. die (1, "number out of allowed range, near %s", p);
  77. v = x + digit;
  78. if (v < 0)
  79. die (1, "negative number");
  80. }
  81. if (endp)
  82. *endp = p;
  83. return v;
  84. }
  85. size_t
  86. string_to_size (char *p, char **endp)
  87. {
  88. off_t v = string_to_off (p, endp);
  89. size_t ret = v;
  90. if (ret != v)
  91. die (1, "number too big");
  92. return ret;
  93. }
  94. size_t sparse_map_size;
  95. struct sp_array *sparse_map;
  96. void
  97. get_line (char *s, int size, FILE *stream)
  98. {
  99. char *p = fgets (s, size, stream);
  100. size_t len;
  101. if (!p)
  102. die (1, "unexpected end of file");
  103. len = strlen (p);
  104. if (s[len - 1] != '\n')
  105. die (1, "buffer overflow");
  106. s[len - 1] = 0;
  107. }
  108. int
  109. get_var (FILE *fp, char **name, char **value)
  110. {
  111. static char *buffer;
  112. static size_t bufsize = OFF_T_STRSIZE_BOUND;
  113. char *p, *q;
  114. buffer = emalloc (bufsize);
  115. do
  116. {
  117. size_t len, s;
  118. if (!fgets (buffer, bufsize, fp))
  119. return 0;
  120. len = strlen (buffer);
  121. if (len == 0)
  122. return 0;
  123. s = string_to_size (buffer, &p);
  124. if (*p != ' ')
  125. die (1, "malformed header: expected space but found %s", p);
  126. if (buffer[len-1] != '\n')
  127. {
  128. if (bufsize < s + 1)
  129. {
  130. bufsize = s + 1;
  131. buffer = realloc (buffer, bufsize);
  132. if (!buffer)
  133. die (1, "not enough memory");
  134. }
  135. if (!fgets (buffer + len, s - len + 1, fp))
  136. die (1, "unexpected end of file or read error");
  137. }
  138. p++;
  139. }
  140. while (memcmp (p, "GNU.sparse.", 11));
  141. p += 11;
  142. q = strchr (p, '=');
  143. if (!q)
  144. die (1, "malformed header: expected '=' not found");
  145. *q++ = 0;
  146. q[strlen (q) - 1] = 0;
  147. *name = p;
  148. *value = q;
  149. return 1;
  150. }
  151. char *outname;
  152. off_t outsize;
  153. unsigned version_major;
  154. unsigned version_minor;
  155. void
  156. read_xheader (char *name)
  157. {
  158. char *kw, *val;
  159. FILE *fp = fopen (name, "r");
  160. char *expect = NULL;
  161. size_t i = 0;
  162. if (verbose)
  163. printf ("Reading extended header file\n");
  164. while (get_var (fp, &kw, &val))
  165. {
  166. if (verbose)
  167. printf ("Found variable GNU.sparse.%s = %s\n", kw, val);
  168. if (expect && strcmp (kw, expect))
  169. die (1, "bad keyword sequence: expected '%s' but found '%s'",
  170. expect, kw);
  171. expect = NULL;
  172. if (strcmp (kw, "name") == 0)
  173. {
  174. outname = emalloc (strlen (val) + 1);
  175. strcpy (outname, val);
  176. }
  177. else if (strcmp (kw, "major") == 0)
  178. {
  179. version_major = string_to_size (val, NULL);
  180. }
  181. else if (strcmp (kw, "minor") == 0)
  182. {
  183. version_minor = string_to_size (val, NULL);
  184. }
  185. else if (strcmp (kw, "realsize") == 0
  186. || strcmp (kw, "size") == 0)
  187. {
  188. outsize = string_to_off (val, NULL);
  189. }
  190. else if (strcmp (kw, "numblocks") == 0)
  191. {
  192. sparse_map_size = string_to_size (val, NULL);
  193. sparse_map = emalloc (sparse_map_size * sizeof *sparse_map);
  194. }
  195. else if (strcmp (kw, "offset") == 0)
  196. {
  197. sparse_map[i].offset = string_to_off (val, NULL);
  198. expect = "numbytes";
  199. }
  200. else if (strcmp (kw, "numbytes") == 0)
  201. {
  202. sparse_map[i++].numbytes = string_to_off (val, NULL);
  203. }
  204. else if (strcmp (kw, "map") == 0)
  205. {
  206. for (i = 0; i < sparse_map_size; i++)
  207. {
  208. sparse_map[i].offset = string_to_off (val, &val);
  209. if (*val != ',')
  210. die (1, "bad GNU.sparse.map: expected ',' but found '%c'",
  211. *val);
  212. sparse_map[i].numbytes = string_to_off (val+1, &val);
  213. if (*val != ',')
  214. {
  215. if (!(*val == 0 && i == sparse_map_size-1))
  216. die (1, "bad GNU.sparse.map: expected ',' but found '%c'",
  217. *val);
  218. }
  219. else
  220. val++;
  221. }
  222. if (*val)
  223. die (1, "bad GNU.sparse.map: garbage at the end");
  224. }
  225. }
  226. if (expect)
  227. die (1, "bad keyword sequence: expected '%s' not found", expect);
  228. if (version_major == 0 && sparse_map_size == 0)
  229. die (1, "size of the sparse map unknown");
  230. if (i != sparse_map_size)
  231. die (1, "not all sparse entries supplied");
  232. fclose (fp);
  233. }
  234. void
  235. read_map (FILE *ifp)
  236. {
  237. size_t i;
  238. char nbuf[OFF_T_STRSIZE_BOUND];
  239. if (verbose)
  240. printf ("Reading v.1.0 sparse map\n");
  241. get_line (nbuf, sizeof nbuf, ifp);
  242. sparse_map_size = string_to_size (nbuf, NULL);
  243. sparse_map = emalloc (sparse_map_size * sizeof *sparse_map);
  244. for (i = 0; i < sparse_map_size; i++)
  245. {
  246. get_line (nbuf, sizeof nbuf, ifp);
  247. sparse_map[i].offset = string_to_off (nbuf, NULL);
  248. get_line (nbuf, sizeof nbuf, ifp);
  249. sparse_map[i].numbytes = string_to_off (nbuf, NULL);
  250. }
  251. fseeko (ifp, ((ftell (ifp) + BLOCKSIZE - 1) / BLOCKSIZE) * BLOCKSIZE,
  252. SEEK_SET);
  253. }
  254. void
  255. expand_sparse (FILE *sfp, int ofd)
  256. {
  257. size_t i;
  258. off_t max_numbytes = 0;
  259. size_t maxbytes;
  260. char *buffer;
  261. for (i = 0; i < sparse_map_size; i++)
  262. if (max_numbytes < sparse_map[i].numbytes)
  263. max_numbytes = sparse_map[i].numbytes;
  264. maxbytes = max_numbytes < SIZE_MAX ? max_numbytes : SIZE_MAX;
  265. for (buffer = malloc (maxbytes); !buffer; maxbytes /= 2)
  266. if (maxbytes == 0)
  267. die (1, "not enough memory");
  268. for (i = 0; i < sparse_map_size; i++)
  269. {
  270. off_t size = sparse_map[i].numbytes;
  271. if (size == 0)
  272. ftruncate (ofd, sparse_map[i].offset);
  273. else
  274. {
  275. lseek (ofd, sparse_map[i].offset, SEEK_SET);
  276. while (size)
  277. {
  278. size_t rdsize = (size < maxbytes) ? size : maxbytes;
  279. if (rdsize != fread (buffer, 1, rdsize, sfp))
  280. die (1, "read error (%d)", errno);
  281. if (rdsize != write (ofd, buffer, rdsize))
  282. die (1, "write error (%d)", errno);
  283. size -= rdsize;
  284. }
  285. }
  286. }
  287. free (buffer);
  288. }
  289. void
  290. usage (int code)
  291. {
  292. printf ("Usage: %s [OPTIONS] infile [outfile]\n", progname);
  293. printf ("%s: expand sparse files extracted from GNU archives\n",
  294. progname);
  295. printf ("\nOPTIONS are:\n\n");
  296. printf (" -h Display this help list\n");
  297. printf (" -n Dry run: do nothing, print what would have been done\n");
  298. printf (" -v Increase verbosity level\n");
  299. printf (" -x FILE Parse extended header FILE\n\n");
  300. exit (code);
  301. }
  302. void
  303. guess_outname (char *name)
  304. {
  305. char *p;
  306. char *s;
  307. if (name[0] == '.' && name[1] == '/')
  308. name += 2;
  309. p = name + strlen (name) - 1;
  310. s = NULL;
  311. for (; p > name && *p != '/'; p--)
  312. ;
  313. if (*p == '/')
  314. s = p + 1;
  315. if (p != name)
  316. {
  317. for (p--; p > name && *p != '/'; p--)
  318. ;
  319. }
  320. if (*p != '/')
  321. {
  322. if (s)
  323. outname = s;
  324. else
  325. {
  326. outname = emalloc (4 + strlen (name));
  327. strcpy (outname, "../");
  328. strcpy (outname + 3, name);
  329. }
  330. }
  331. else
  332. {
  333. size_t len = p - name + 1;
  334. outname = emalloc (len + strlen (s) + 1);
  335. memcpy (outname, name, len);
  336. strcpy (outname + len, s);
  337. }
  338. }
  339. int
  340. main (int argc, char **argv)
  341. {
  342. int c;
  343. int dry_run = 0;
  344. char *xheader_file = NULL;
  345. char *inname;
  346. FILE *ifp;
  347. struct stat st;
  348. int ofd;
  349. progname = argv[0];
  350. while ((c = getopt (argc, argv, "hnvx:")) != EOF)
  351. {
  352. switch (c)
  353. {
  354. case 'h':
  355. usage (0);
  356. break;
  357. case 'x':
  358. xheader_file = optarg;
  359. break;
  360. case 'n':
  361. dry_run = 1;
  362. case 'v':
  363. verbose++;
  364. break;
  365. default:
  366. exit (1);
  367. }
  368. }
  369. argc -= optind;
  370. argv += optind;
  371. if (argc == 0 || argc > 2)
  372. usage (1);
  373. if (xheader_file)
  374. read_xheader (xheader_file);
  375. inname = argv[0];
  376. if (argv[1])
  377. outname = argv[1];
  378. if (stat (inname, &st))
  379. die (1, "cannot stat %s (%d)", inname, errno);
  380. ifp = fopen (inname, "r");
  381. if (ifp == NULL)
  382. die (1, "cannot open file %s (%d)", inname, errno);
  383. if (!xheader_file || version_major == 1)
  384. read_map (ifp);
  385. if (!outname)
  386. guess_outname (inname);
  387. ofd = open (outname, O_RDWR|O_CREAT|O_TRUNC, st.st_mode);
  388. if (ofd == -1)
  389. die (1, "cannot open file %s (%d)", outname, errno);
  390. if (verbose)
  391. printf ("Expanding file '%s' to '%s'\n", inname, outname);
  392. if (dry_run)
  393. {
  394. printf ("Finished dry run\n");
  395. return 0;
  396. }
  397. expand_sparse (ifp, ofd);
  398. fclose (ifp);
  399. close (ofd);
  400. if (verbose)
  401. printf ("Done\n");
  402. if (outsize)
  403. {
  404. if (stat (outname, &st))
  405. die (1, "cannot stat output file %s (%d)", outname, errno);
  406. if (st.st_size != outsize)
  407. die (1, "expanded file has wrong size");
  408. }
  409. return 0;
  410. }