]>
Dogcows Code - chaz/tar/blob - scripts/xsparse.c
1 /* xsparse - expands compressed sparse file images extracted from GNU tar
4 Copyright (C) 2006, 2007 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. */
31 /* Bound on length of the string representing an off_t.
32 See INT_STRLEN_BOUND in intprops.h for explanation */
33 #define OFF_T_STRLEN_BOUND ((sizeof (off_t) * CHAR_BIT) * 146 / 485 + 1)
34 #define OFF_T_STRSIZE_BOUND (OFF_T_STRLEN_BOUND+1)
48 die (int code
, char *fmt
, ...)
52 fprintf (stderr
, "%s: ", progname
);
54 vfprintf (stderr
, fmt
, ap
);
56 fprintf (stderr
, "\n");
63 char *p
= malloc (size
);
65 die (1, "not enough memory");
70 string_to_off (char *p
, char **endp
)
78 if (9 < (unsigned) digit
)
85 die (1, "number parse error near %s", p
);
88 die (1, "number out of allowed range, near %s", p
);
91 die (1, "negative number");
99 string_to_size (char *p
, char **endp
)
101 off_t v
= string_to_off (p
, endp
);
104 die (1, "number too big");
108 size_t sparse_map_size
;
109 struct sp_array
*sparse_map
;
112 get_line (char *s
, int size
, FILE *stream
)
114 char *p
= fgets (s
, size
, stream
);
118 die (1, "unexpected end of file");
120 if (s
[len
- 1] != '\n')
121 die (1, "buffer overflow");
126 get_var (FILE *fp
, char **name
, char **value
)
129 static size_t bufsize
= OFF_T_STRSIZE_BOUND
;
132 buffer
= emalloc (bufsize
);
137 if (!fgets (buffer
, bufsize
, fp
))
139 len
= strlen (buffer
);
143 s
= string_to_size (buffer
, &p
);
145 die (1, "malformed header: expected space but found %s", p
);
146 if (buffer
[len
-1] != '\n')
151 buffer
= realloc (buffer
, bufsize
);
153 die (1, "not enough memory");
155 if (!fgets (buffer
+ len
, s
- len
+ 1, fp
))
156 die (1, "unexpected end of file or read error");
160 while (memcmp (p
, "GNU.sparse.", 11));
165 die (1, "malformed header: expected `=' not found");
167 q
[strlen (q
) - 1] = 0;
175 unsigned version_major
;
176 unsigned version_minor
;
179 read_xheader (char *name
)
182 FILE *fp
= fopen (name
, "r");
187 printf ("Reading extended header file\n");
189 while (get_var (fp
, &kw
, &val
))
192 printf ("Found variable GNU.sparse.%s = %s\n", kw
, val
);
194 if (expect
&& strcmp (kw
, expect
))
195 die (1, "bad keyword sequence: expected `%s' but found `%s'",
198 if (strcmp (kw
, "name") == 0)
200 outname
= emalloc (strlen (val
) + 1);
201 strcpy (outname
, val
);
203 else if (strcmp (kw
, "major") == 0)
205 version_major
= string_to_size (val
, NULL
);
207 else if (strcmp (kw
, "minor") == 0)
209 version_minor
= string_to_size (val
, NULL
);
211 else if (strcmp (kw
, "realsize") == 0
212 || strcmp (kw
, "size") == 0)
214 outsize
= string_to_off (val
, NULL
);
216 else if (strcmp (kw
, "numblocks") == 0)
218 sparse_map_size
= string_to_size (val
, NULL
);
219 sparse_map
= emalloc (sparse_map_size
* sizeof *sparse_map
);
221 else if (strcmp (kw
, "offset") == 0)
223 sparse_map
[i
].offset
= string_to_off (val
, NULL
);
226 else if (strcmp (kw
, "numbytes") == 0)
228 sparse_map
[i
++].numbytes
= string_to_size (val
, NULL
);
230 else if (strcmp (kw
, "map") == 0)
232 for (i
= 0; i
< sparse_map_size
; i
++)
234 sparse_map
[i
].offset
= string_to_off (val
, &val
);
236 die (1, "bad GNU.sparse.map: expected `,' but found `%c'",
238 sparse_map
[i
].numbytes
= string_to_size (val
+1, &val
);
241 if (!(*val
== 0 && i
== sparse_map_size
-1))
242 die (1, "bad GNU.sparse.map: expected `,' but found `%c'",
249 die (1, "bad GNU.sparse.map: garbage at the end");
253 die (1, "bad keyword sequence: expected `%s' not found", expect
);
254 if (version_major
== 0 && sparse_map_size
== 0)
255 die (1, "size of the sparse map unknown");
256 if (i
!= sparse_map_size
)
257 die (1, "not all sparse entries supplied");
265 char nbuf
[OFF_T_STRSIZE_BOUND
];
268 printf ("Reading v.1.0 sparse map\n");
270 get_line (nbuf
, sizeof nbuf
, ifp
);
271 sparse_map_size
= string_to_size (nbuf
, NULL
);
272 sparse_map
= emalloc (sparse_map_size
* sizeof *sparse_map
);
274 for (i
= 0; i
< sparse_map_size
; i
++)
276 get_line (nbuf
, sizeof nbuf
, ifp
);
277 sparse_map
[i
].offset
= string_to_off (nbuf
, NULL
);
278 get_line (nbuf
, sizeof nbuf
, ifp
);
279 sparse_map
[i
].numbytes
= string_to_size (nbuf
, NULL
);
282 fseek (ifp
, ((ftell (ifp
) + BLOCKSIZE
- 1) / BLOCKSIZE
) * BLOCKSIZE
,
287 expand_sparse (FILE *sfp
, int ofd
)
293 for (i
= 0; i
< sparse_map_size
; i
++)
294 if (maxbytes
< sparse_map
[i
].numbytes
)
295 maxbytes
= sparse_map
[i
].numbytes
;
297 for (buffer
= malloc (maxbytes
); !buffer
; maxbytes
/= 2)
299 die (1, "not enough memory");
301 for (i
= 0; i
< sparse_map_size
; i
++)
303 size_t size
= sparse_map
[i
].numbytes
;
306 ftruncate (ofd
, sparse_map
[i
].offset
);
309 lseek (ofd
, sparse_map
[i
].offset
, SEEK_SET
);
312 size_t rdsize
= (size
< maxbytes
) ? size
: maxbytes
;
313 if (rdsize
!= fread (buffer
, 1, rdsize
, sfp
))
314 die (1, "read error (%d)", errno
);
315 if (rdsize
!= write (ofd
, buffer
, rdsize
))
316 die (1, "write error (%d)", errno
);
327 printf ("Usage: %s [OPTIONS] infile [outfile]\n", progname
);
328 printf ("%s: expand sparse files extracted from GNU archives\n",
330 printf ("\nOPTIONS are:\n\n");
331 printf (" -h Display this help list\n");
332 printf (" -n Dry run: do nothing, print what would have been done\n");
333 printf (" -v Increase verbosity level\n");
334 printf (" -x FILE Parse extended header FILE\n\n");
340 guess_outname (char *name
)
345 if (name
[0] == '.' && name
[1] == '/')
348 p
= name
+ strlen (name
) - 1;
351 for (; p
> name
&& *p
!= '/'; p
--)
357 for (p
--; p
> name
&& *p
!= '/'; p
--)
367 outname
= emalloc (4 + strlen (name
));
368 strcpy (outname
, "../");
369 strcpy (outname
+ 3, name
);
374 size_t len
= p
- name
+ 1;
375 outname
= emalloc (len
+ strlen (s
) + 1);
376 memcpy (outname
, name
, len
);
377 strcpy (outname
+ len
, s
);
382 main (int argc
, char **argv
)
386 char *xheader_file
= NULL
;
393 while ((c
= getopt (argc
, argv
, "hnvx:")) != EOF
)
402 xheader_file
= optarg
;
419 if (argc
== 0 || argc
> 2)
423 read_xheader (xheader_file
);
429 if (stat (inname
, &st
))
430 die (1, "cannot stat %s (%d)", inname
, errno
);
432 ifp
= fopen (inname
, "r");
434 die (1, "cannot open file %s (%d)", inname
, errno
);
436 if (!xheader_file
|| version_major
== 1)
440 guess_outname (inname
);
442 ofd
= open (outname
, O_RDWR
|O_CREAT
|O_TRUNC
, st
.st_mode
);
444 die (1, "cannot open file %s (%d)", outname
, errno
);
447 printf ("Expanding file `%s' to `%s'\n", inname
, outname
);
451 printf ("Finished dry run\n");
455 expand_sparse (ifp
, ofd
);
465 if (stat (outname
, &st
))
466 die (1, "cannot stat output file %s (%d)", outname
, errno
);
467 if (st
.st_size
!= outsize
)
468 die (1, "expanded file has wrong size");
This page took 0.052087 seconds and 4 git commands to generate.