]>
Dogcows Code - chaz/tar/blob - scripts/xsparse.c
1 /* xsparse - expands compressed sparse file images extracted from GNU tar
4 Copyright (C) 2006, 2007, 2010 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 3, 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. */
33 /* Bound on length of the string representing an off_t.
34 See INT_STRLEN_BOUND in intprops.h for explanation */
35 #define OFF_T_STRLEN_BOUND ((sizeof (off_t) * CHAR_BIT) * 146 / 485 + 1)
36 #define OFF_T_STRSIZE_BOUND (OFF_T_STRLEN_BOUND+1)
50 die (int code
, char *fmt
, ...)
54 fprintf (stderr
, "%s: ", progname
);
56 vfprintf (stderr
, fmt
, ap
);
58 fprintf (stderr
, "\n");
65 char *p
= malloc (size
);
67 die (1, "not enough memory");
72 string_to_off (char *p
, char **endp
)
80 if (9 < (unsigned) digit
)
87 die (1, "number parse error near %s", p
);
90 die (1, "number out of allowed range, near %s", p
);
93 die (1, "negative number");
101 string_to_size (char *p
, char **endp
)
103 off_t v
= string_to_off (p
, endp
);
106 die (1, "number too big");
110 size_t sparse_map_size
;
111 struct sp_array
*sparse_map
;
114 get_line (char *s
, int size
, FILE *stream
)
116 char *p
= fgets (s
, size
, stream
);
120 die (1, "unexpected end of file");
122 if (s
[len
- 1] != '\n')
123 die (1, "buffer overflow");
128 get_var (FILE *fp
, char **name
, char **value
)
131 static size_t bufsize
= OFF_T_STRSIZE_BOUND
;
134 buffer
= emalloc (bufsize
);
139 if (!fgets (buffer
, bufsize
, fp
))
141 len
= strlen (buffer
);
145 s
= string_to_size (buffer
, &p
);
147 die (1, "malformed header: expected space but found %s", p
);
148 if (buffer
[len
-1] != '\n')
153 buffer
= realloc (buffer
, bufsize
);
155 die (1, "not enough memory");
157 if (!fgets (buffer
+ len
, s
- len
+ 1, fp
))
158 die (1, "unexpected end of file or read error");
162 while (memcmp (p
, "GNU.sparse.", 11));
167 die (1, "malformed header: expected `=' not found");
169 q
[strlen (q
) - 1] = 0;
177 unsigned version_major
;
178 unsigned version_minor
;
181 read_xheader (char *name
)
184 FILE *fp
= fopen (name
, "r");
189 printf ("Reading extended header file\n");
191 while (get_var (fp
, &kw
, &val
))
194 printf ("Found variable GNU.sparse.%s = %s\n", kw
, val
);
196 if (expect
&& strcmp (kw
, expect
))
197 die (1, "bad keyword sequence: expected `%s' but found `%s'",
200 if (strcmp (kw
, "name") == 0)
202 outname
= emalloc (strlen (val
) + 1);
203 strcpy (outname
, val
);
205 else if (strcmp (kw
, "major") == 0)
207 version_major
= string_to_size (val
, NULL
);
209 else if (strcmp (kw
, "minor") == 0)
211 version_minor
= string_to_size (val
, NULL
);
213 else if (strcmp (kw
, "realsize") == 0
214 || strcmp (kw
, "size") == 0)
216 outsize
= string_to_off (val
, NULL
);
218 else if (strcmp (kw
, "numblocks") == 0)
220 sparse_map_size
= string_to_size (val
, NULL
);
221 sparse_map
= emalloc (sparse_map_size
* sizeof *sparse_map
);
223 else if (strcmp (kw
, "offset") == 0)
225 sparse_map
[i
].offset
= string_to_off (val
, NULL
);
228 else if (strcmp (kw
, "numbytes") == 0)
230 sparse_map
[i
++].numbytes
= string_to_off (val
, NULL
);
232 else if (strcmp (kw
, "map") == 0)
234 for (i
= 0; i
< sparse_map_size
; i
++)
236 sparse_map
[i
].offset
= string_to_off (val
, &val
);
238 die (1, "bad GNU.sparse.map: expected `,' but found `%c'",
240 sparse_map
[i
].numbytes
= string_to_off (val
+1, &val
);
243 if (!(*val
== 0 && i
== sparse_map_size
-1))
244 die (1, "bad GNU.sparse.map: expected `,' but found `%c'",
251 die (1, "bad GNU.sparse.map: garbage at the end");
255 die (1, "bad keyword sequence: expected `%s' not found", expect
);
256 if (version_major
== 0 && sparse_map_size
== 0)
257 die (1, "size of the sparse map unknown");
258 if (i
!= sparse_map_size
)
259 die (1, "not all sparse entries supplied");
267 char nbuf
[OFF_T_STRSIZE_BOUND
];
270 printf ("Reading v.1.0 sparse map\n");
272 get_line (nbuf
, sizeof nbuf
, ifp
);
273 sparse_map_size
= string_to_size (nbuf
, NULL
);
274 sparse_map
= emalloc (sparse_map_size
* sizeof *sparse_map
);
276 for (i
= 0; i
< sparse_map_size
; i
++)
278 get_line (nbuf
, sizeof nbuf
, ifp
);
279 sparse_map
[i
].offset
= string_to_off (nbuf
, NULL
);
280 get_line (nbuf
, sizeof nbuf
, ifp
);
281 sparse_map
[i
].numbytes
= string_to_off (nbuf
, NULL
);
284 fseeko (ifp
, ((ftell (ifp
) + BLOCKSIZE
- 1) / BLOCKSIZE
) * BLOCKSIZE
,
289 expand_sparse (FILE *sfp
, int ofd
)
292 off_t max_numbytes
= 0;
296 for (i
= 0; i
< sparse_map_size
; i
++)
297 if (max_numbytes
< sparse_map
[i
].numbytes
)
298 max_numbytes
= sparse_map
[i
].numbytes
;
300 maxbytes
= max_numbytes
< SIZE_MAX
? max_numbytes
: SIZE_MAX
;
302 for (buffer
= malloc (maxbytes
); !buffer
; maxbytes
/= 2)
304 die (1, "not enough memory");
306 for (i
= 0; i
< sparse_map_size
; i
++)
308 off_t size
= sparse_map
[i
].numbytes
;
311 ftruncate (ofd
, sparse_map
[i
].offset
);
314 lseek (ofd
, sparse_map
[i
].offset
, SEEK_SET
);
317 size_t rdsize
= (size
< maxbytes
) ? size
: maxbytes
;
318 if (rdsize
!= fread (buffer
, 1, rdsize
, sfp
))
319 die (1, "read error (%d)", errno
);
320 if (rdsize
!= write (ofd
, buffer
, rdsize
))
321 die (1, "write error (%d)", errno
);
332 printf ("Usage: %s [OPTIONS] infile [outfile]\n", progname
);
333 printf ("%s: expand sparse files extracted from GNU archives\n",
335 printf ("\nOPTIONS are:\n\n");
336 printf (" -h Display this help list\n");
337 printf (" -n Dry run: do nothing, print what would have been done\n");
338 printf (" -v Increase verbosity level\n");
339 printf (" -x FILE Parse extended header FILE\n\n");
345 guess_outname (char *name
)
350 if (name
[0] == '.' && name
[1] == '/')
353 p
= name
+ strlen (name
) - 1;
356 for (; p
> name
&& *p
!= '/'; p
--)
362 for (p
--; p
> name
&& *p
!= '/'; p
--)
372 outname
= emalloc (4 + strlen (name
));
373 strcpy (outname
, "../");
374 strcpy (outname
+ 3, name
);
379 size_t len
= p
- name
+ 1;
380 outname
= emalloc (len
+ strlen (s
) + 1);
381 memcpy (outname
, name
, len
);
382 strcpy (outname
+ len
, s
);
387 main (int argc
, char **argv
)
391 char *xheader_file
= NULL
;
398 while ((c
= getopt (argc
, argv
, "hnvx:")) != EOF
)
407 xheader_file
= optarg
;
424 if (argc
== 0 || argc
> 2)
428 read_xheader (xheader_file
);
434 if (stat (inname
, &st
))
435 die (1, "cannot stat %s (%d)", inname
, errno
);
437 ifp
= fopen (inname
, "r");
439 die (1, "cannot open file %s (%d)", inname
, errno
);
441 if (!xheader_file
|| version_major
== 1)
445 guess_outname (inname
);
447 ofd
= open (outname
, O_RDWR
|O_CREAT
|O_TRUNC
, st
.st_mode
);
449 die (1, "cannot open file %s (%d)", outname
, errno
);
452 printf ("Expanding file `%s' to `%s'\n", inname
, outname
);
456 printf ("Finished dry run\n");
460 expand_sparse (ifp
, ofd
);
470 if (stat (outname
, &st
))
471 die (1, "cannot stat output file %s (%d)", outname
, errno
);
472 if (st
.st_size
!= outsize
)
473 die (1, "expanded file has wrong size");
This page took 0.059405 seconds and 4 git commands to generate.