1 /* xsparse - expands compressed sparse file images extracted from GNU tar
4 Copyright (C) 2006 Free Software Foundation, Inc.
6 Written by Sergey Poznyakoff
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
32 /* Bound on length of the string representing an off_t.
33 See INT_STRLEN_BOUND in intprops.h for explanation */
34 #define OFF_T_STRLEN_BOUND ((sizeof (off_t) * CHAR_BIT) * 146 / 485 + 1)
35 #define OFF_T_STRSIZE_BOUND (OFF_T_STRLEN_BOUND+1)
49 die (int code, char *fmt, ...)
53 fprintf (stderr, "%s: ", progname);
55 vfprintf (stderr, fmt, ap);
57 fprintf (stderr, "\n");
64 char *p = malloc (size);
66 die (1, "not enough memory");
71 string_to_off (char *p, char **endp)
79 if (9 < (unsigned) digit)
86 die (1, "number parse error near %s", p);
89 die (1, "number out of allowed range, near %s", p);
92 die (1, "negative number");
100 string_to_size (char *p, char **endp)
102 off_t v = string_to_off (p, endp);
105 die (1, "number too big");
109 size_t sparse_map_size;
110 struct sp_array *sparse_map;
113 get_line (char *s, int size, FILE *stream)
115 char *p = fgets (s, size, stream);
119 die (1, "unexpected end of file");
121 if (s[len - 1] != '\n')
122 die (1, "buffer overflow");
127 get_var (FILE *fp, char **name, char **value)
130 static size_t bufsize = OFF_T_STRSIZE_BOUND;
133 buffer = emalloc (bufsize);
138 if (!fgets (buffer, bufsize, fp))
140 len = strlen (buffer);
144 s = string_to_size (buffer, &p);
146 die (1, "malformed header: expected space but found %s", p);
147 if (buffer[len-1] != '\n')
152 buffer = realloc (buffer, bufsize);
154 die (1, "not enough memory");
156 if (!fgets (buffer + len, s - len + 1, fp))
157 die (1, "unexpected end of file or read error");
161 while (memcmp (p, "GNU.sparse.", 11));
166 die (1, "malformed header: expected `=' not found");
168 q[strlen (q) - 1] = 0;
176 unsigned version_major;
177 unsigned version_minor;
180 read_xheader (char *name)
183 FILE *fp = fopen (name, "r");
188 printf ("Reading extended header file\n");
190 while (get_var (fp, &kw, &val))
193 printf ("Found variable GNU.sparse.%s = %s\n", kw, val);
195 if (expect && strcmp (kw, expect))
196 die (1, "bad keyword sequence: expected `%s' but found `%s'",
199 if (strcmp (kw, "name") == 0)
201 outname = emalloc (strlen (val) + 1);
202 strcpy (outname, val);
204 else if (strcmp (kw, "major") == 0)
206 version_major = string_to_size (val, NULL);
208 else if (strcmp (kw, "minor") == 0)
210 version_minor = string_to_size (val, NULL);
212 else if (strcmp (kw, "realsize") == 0
213 || strcmp (kw, "size") == 0)
215 outsize = string_to_off (val, NULL);
217 else if (strcmp (kw, "numblocks") == 0)
219 sparse_map_size = string_to_size (val, NULL);
220 sparse_map = emalloc (sparse_map_size * sizeof *sparse_map);
222 else if (strcmp (kw, "offset") == 0)
224 sparse_map[i].offset = string_to_off (val, NULL);
227 else if (strcmp (kw, "numbytes") == 0)
229 sparse_map[i++].numbytes = string_to_size (val, NULL);
231 else if (strcmp (kw, "map") == 0)
233 for (i = 0; i < sparse_map_size; i++)
235 sparse_map[i].offset = string_to_off (val, &val);
237 die (1, "bad GNU.sparse.map: expected `,' but found `%c'",
239 sparse_map[i].numbytes = string_to_size (val+1, &val);
242 if (!(*val == 0 && i == sparse_map_size-1))
243 die (1, "bad GNU.sparse.map: expected `,' but found `%c'",
250 die (1, "bad GNU.sparse.map: garbage at the end");
254 die (1, "bad keyword sequence: expected `%s' not found", expect);
255 if (version_major == 0 && sparse_map_size == 0)
256 die (1, "size of the sparse map unknown");
257 if (i != sparse_map_size)
258 die (1, "not all sparse entries supplied");
266 char nbuf[OFF_T_STRSIZE_BOUND];
269 printf ("Reading v.1.0 sparse map\n");
271 get_line (nbuf, sizeof nbuf, ifp);
272 sparse_map_size = string_to_size (nbuf, NULL);
273 sparse_map = emalloc (sparse_map_size * sizeof *sparse_map);
275 for (i = 0; i < sparse_map_size; i++)
277 get_line (nbuf, sizeof nbuf, ifp);
278 sparse_map[i].offset = string_to_off (nbuf, NULL);
279 get_line (nbuf, sizeof nbuf, ifp);
280 sparse_map[i].numbytes = string_to_size (nbuf, NULL);
283 fseek (ifp, ((ftell (ifp) + BLOCKSIZE - 1) / BLOCKSIZE) * BLOCKSIZE,
288 expand_sparse (FILE *sfp, int ofd)
294 for (i = 0; i < sparse_map_size; i++)
295 if (maxbytes < sparse_map[i].numbytes)
296 maxbytes = sparse_map[i].numbytes;
298 for (buffer = malloc (maxbytes); !buffer; maxbytes /= 2)
300 die (1, "not enough memory");
302 for (i = 0; i < sparse_map_size; i++)
304 size_t size = sparse_map[i].numbytes;
306 lseek (ofd, sparse_map[i].offset, SEEK_SET);
309 size_t rdsize = (size < maxbytes) ? size : maxbytes;
310 if (rdsize != fread (buffer, 1, rdsize, sfp))
311 die (1, "read error (%d)", errno);
312 if (rdsize != write (ofd, buffer, rdsize))
313 die (1, "write error (%d)", errno);
323 printf ("Usage: %s [OPTIONS] infile [outfile]\n", progname);
324 printf ("%s: expand sparse files extracted from GNU archives\n",
326 printf ("\nOPTIONS are:\n\n");
327 printf (" -h Display this help list\n");
328 printf (" -n Dry run: do nothing, print what would have been done\n");
329 printf (" -v Increase verbosity level\n");
330 printf (" -x FILE Parse extended header FILE\n\n");
336 guess_outname (char *name)
341 if (name[0] == '.' && name[1] == '/')
344 p = name + strlen (name) - 1;
347 for (; p > name && *p != '/'; p--)
353 for (p--; p > name && *p != '/'; p--)
363 outname = emalloc (4 + strlen (name));
364 strcpy (outname, "../");
365 strcpy (outname + 3, name);
370 size_t len = p - name + 1;
371 outname = emalloc (len + strlen (s) + 1);
372 memcpy (outname, name, len);
373 strcpy (outname + len, s);
378 main (int argc, char **argv)
382 char *xheader_file = NULL;
389 while ((c = getopt (argc, argv, "hnvx:")) != EOF)
398 xheader_file = optarg;
415 if (argc == 0 || argc > 2)
419 read_xheader (xheader_file);
425 if (stat (inname, &st))
426 die (1, "cannot stat %s (%d)", inname, errno);
428 ifp = fopen (inname, "r");
430 die (1, "cannot open file %s (%d)", inname, errno);
432 if (!xheader_file || version_major == 1)
436 guess_outname (inname);
438 ofd = open (outname, O_RDWR|O_CREAT|O_TRUNC, st.st_mode);
440 die (1, "cannot open file %s (%d)", outname, errno);
443 printf ("Expanding file `%s' to `%s'\n", inname, outname);
447 printf ("Finished dry run\n");
451 expand_sparse (ifp, ofd);
461 if (stat (outname, &st))
462 die (1, "cannot stat output file %s (%d)", outname, errno);
463 if (st.st_size != outsize)
464 die (1, "expanded file has wrong size");