1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
|
/*
Virtual File System: GNU Tar file system.
Copyright (C) 2003-2023
Free Software Foundation, Inc.
Written by:
Andrew Borodin <aborodin@vmail.ru>, 2023
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file
* \brief Source: Virtual File System: GNU Tar file system
*/
/*
* Avoid following error:
* comparison of unsigned expression < 0 is always false [-Werror=type-limits]
*
* https://www.boost.org/doc/libs/1_55_0/libs/integer/test/cstdint_test.cpp
* We can't suppress this warning on the command line as not all GCC versions support -Wno-type-limits
*/
#if defined(__GNUC__) && (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4))
#pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#include <config.h>
#include <ctype.h> /* isdigit() */
#include <errno.h>
#include <inttypes.h> /* uintmax_t */
#include "lib/global.h"
#include "tar-internal.h"
/* Old GNU Format.
The sparse file information is stored in the oldgnu_header in the following manner:
The header is marked with type 'S'. Its 'size' field contains the cumulative size
of all non-empty blocks of the file. The actual file size is stored in `realsize'
member of oldgnu_header.
The map of the file is stored in a list of 'struct sparse'. Each struct contains
offset to the block of data and its size (both as octal numbers). The first file
header contains at most 4 such structs (SPARSES_IN_OLDGNU_HEADER). If the map
contains more structs, then the field 'isextended' of the main header is set to
1 (binary) and the 'struct sparse_header' header follows, containing at most
21 following structs (SPARSES_IN_SPARSE_HEADER). If more structs follow, 'isextended'
field of the extended header is set and next next extension header follows, etc...
*/
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/* The width in bits of the integer type or expression T.
Do not evaluate T. T must not be a bit-field expression.
Padding bits are not supported; this is checked at compile-time below. */
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
/* Bound on length of the string representing an unsigned integer
value representable in B bits. log10 (2.0) < 146/485. The
smallest value of B where this bound is not tight is 2621. */
#define INT_BITS_STRLEN_BOUND(b) (((b) * 146 + 484) / 485)
/* Does the __typeof__ keyword work? This could be done by
'configure', but for now it's easier to do it by hand. */
#if (2 <= __GNUC__ \
|| (4 <= __clang_major__) \
|| (1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
|| (0x5110 <= __SUNPRO_C && !__STDC__))
#define _GL_HAVE___TYPEOF__ 1
#else
#define _GL_HAVE___TYPEOF__ 0
#endif
/* Return 1 if the integer type or expression T might be signed. Return 0
if it is definitely unsigned. T must not be a bit-field expression.
This macro does not evaluate its argument, and expands to an
integer constant expression. */
#if _GL_HAVE___TYPEOF__
#define _GL_SIGNED_TYPE_OR_EXPR(t) TYPE_SIGNED (__typeof__ (t))
#else
#define _GL_SIGNED_TYPE_OR_EXPR(t) 1
#endif
/* Return a value with the common real type of E and V and the value of V.
Do not evaluate E. */
#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))
/* Act like _GL_INT_CONVERT (E, -V) but work around a bug in IRIX 6.5 cc; see
<https://lists.gnu.org/r/bug-gnulib/2011-05/msg00406.html>. */
#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))
/* Return 1 if the real expression E, after promotion, has a
signed or floating type. Do not evaluate E. */
#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)
#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)
/* The maximum and minimum values for the type of the expression E,
after integer promotion. E is not evaluated. */
#define _GL_INT_MINIMUM(e) \
(EXPR_SIGNED (e) \
? ~_GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_CONVERT (e, 0))
#define _GL_INT_MAXIMUM(e) \
(EXPR_SIGNED (e) \
? _GL_SIGNED_INT_MAXIMUM (e) \
: _GL_INT_NEGATE_CONVERT (e, 1))
/* Return 1 if the expression A <op> B would overflow,
where OP_RESULT_OVERFLOW (A, B, MIN, MAX) does the actual test,
assuming MIN and MAX are the minimum and maximum for the result type.
Arguments should be free of side effects. */
#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \
op_result_overflow (a, b, \
_GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
_GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))
#define INT_ADD_RANGE_OVERFLOW(a, b, min, max) \
((b) < 0 \
? (a) < (min) - (b) \
: (max) - (b) < (a))
/* True if __builtin_add_overflow_p (A, B, C) works, and similarly for
__builtin_sub_overflow_p and __builtin_mul_overflow_p. */
#if defined __clang__ || defined __ICC
/* Clang 11 lacks __builtin_mul_overflow_p, and even if it did it
would presumably run afoul of Clang bug 16404. ICC 2021.1's
__builtin_add_overflow_p etc. are not treated as integral constant
expressions even when all arguments are. */
#define _GL_HAS_BUILTIN_OVERFLOW_P 0
#elif defined __has_builtin
#define _GL_HAS_BUILTIN_OVERFLOW_P __has_builtin (__builtin_mul_overflow_p)
#else
#define _GL_HAS_BUILTIN_OVERFLOW_P (7 <= __GNUC__)
#endif
/* The _GL*_OVERFLOW macros have the same restrictions as the
*_RANGE_OVERFLOW macros, except that they do not assume that operands
(e.g., A and B) have the same type as MIN and MAX. Instead, they assume
that the result (e.g., A + B) has that type. */
#if _GL_HAS_BUILTIN_OVERFLOW_P
#define _GL_ADD_OVERFLOW(a, b, min, max) \
__builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
#else
#define _GL_ADD_OVERFLOW(a, b, min, max) \
((min) < 0 ? INT_ADD_RANGE_OVERFLOW (a, b, min, max) \
: (a) < 0 ? (b) <= (a) + (b) \
: (b) < 0 ? (a) <= (a) + (b) \
: (a) + (b) < (b))
#endif
/* Bound on length of the string representing an integer type or expression T.
T must not be a bit-field expression.
Subtract 1 for the sign bit if T is signed, and then add 1 more for
a minus sign if needed.
Because _GL_SIGNED_TYPE_OR_EXPR sometimes returns 1 when its argument is
unsigned, this macro may overestimate the true bound by one byte when
applied to unsigned types of size 2, 4, 16, ... bytes. */
#define INT_STRLEN_BOUND(t) \
(INT_BITS_STRLEN_BOUND (TYPE_WIDTH (t) - _GL_SIGNED_TYPE_OR_EXPR (t)) \
+ _GL_SIGNED_TYPE_OR_EXPR (t))
/* Bound on buffer size needed to represent an integer type or expression T,
including the terminating null. T must not be a bit-field expression. */
#define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
#define UINTMAX_STRSIZE_BOUND INT_BUFSIZE_BOUND (uintmax_t)
#define INT_ADD_OVERFLOW(a, b) \
_GL_BINARY_OP_OVERFLOW (a, b, _GL_ADD_OVERFLOW)
#define SPARSES_INIT_COUNT SPARSES_IN_SPARSE_HEADER
#define COPY_BUF(arch,b,buf,src) \
do \
{ \
char *endp = b->buffer + BLOCKSIZE; \
char *dst = buf; \
do \
{ \
if (dst == buf + UINTMAX_STRSIZE_BOUND - 1) \
/* numeric overflow in sparse archive member */ \
return FALSE; \
if (src == endp) \
{ \
tar_set_next_block_after (b); \
b = tar_find_next_block (arch); \
if (b == NULL) \
/* unexpected EOF in archive */ \
return FALSE; \
src = b->buffer; \
endp = b->buffer + BLOCKSIZE; \
} \
*dst = *src++; \
} \
while (*dst++ != '\n'); \
dst[-1] = '\0'; \
} \
while (FALSE)
/*** file scope type declarations ****************************************************************/
struct tar_sparse_file;
struct tar_sparse_optab
{
gboolean (*init) (struct tar_sparse_file * file);
gboolean (*done) (struct tar_sparse_file * file);
gboolean (*sparse_member_p) (struct tar_sparse_file * file);
gboolean (*fixup_header) (struct tar_sparse_file * file);
gboolean (*decode_header) (tar_super_t * archive, struct tar_sparse_file * file);
};
struct tar_sparse_file
{
int fd; /**< File descriptor */
off_t dumped_size; /**< Number of bytes actually written to the archive */
struct tar_stat_info *stat_info; /**< Information about the file */
struct tar_sparse_optab const *optab;
void *closure; /**< Any additional data optab calls might reqiure */
};
enum oldgnu_add_status
{
add_ok,
add_finish,
add_fail
};
/*** forward declarations (file scope functions) *************************************************/
static gboolean oldgnu_sparse_member_p (struct tar_sparse_file *file);
static gboolean oldgnu_fixup_header (struct tar_sparse_file *file);
static gboolean oldgnu_get_sparse_info (tar_super_t * archive, struct tar_sparse_file *file);
static gboolean star_sparse_member_p (struct tar_sparse_file *file);
static gboolean star_fixup_header (struct tar_sparse_file *file);
static gboolean star_get_sparse_info (tar_super_t * archive, struct tar_sparse_file *file);
static gboolean pax_sparse_member_p (struct tar_sparse_file *file);
static gboolean pax_decode_header (tar_super_t * archive, struct tar_sparse_file *file);
/*** file scope variables ************************************************************************/
/* *INDENT-OFF* */
static struct tar_sparse_optab const oldgnu_optab =
{
.init = NULL, /* No init function */
.done = NULL, /* No done function */
.sparse_member_p = oldgnu_sparse_member_p,
.fixup_header = oldgnu_fixup_header,
.decode_header = oldgnu_get_sparse_info
};
/* *INDENT-ON* */
/* *INDENT-OFF* */
static struct tar_sparse_optab const star_optab =
{
.init = NULL, /* No init function */
.done = NULL, /* No done function */
.sparse_member_p = star_sparse_member_p,
.fixup_header = star_fixup_header,
.decode_header = star_get_sparse_info
};
/* *INDENT-ON* */
/* GNU PAX sparse file format. There are several versions:
* 0.0
The initial version of sparse format used by tar 1.14-1.15.1.
The sparse file map is stored in x header:
GNU.sparse.size Real size of the stored file
GNU.sparse.numblocks Number of blocks in the sparse map repeat numblocks time
GNU.sparse.offset Offset of the next data block
GNU.sparse.numbytes Size of the next data block end repeat
This has been reported as conflicting with the POSIX specs. The reason is
that offsets and sizes of non-zero data blocks were stored in multiple instances
of GNU.sparse.offset/GNU.sparse.numbytes variables, whereas POSIX requires the
latest occurrence of the variable to override all previous occurrences.
To avoid this incompatibility two following versions were introduced.
* 0.1
Used by tar 1.15.2 -- 1.15.91 (alpha releases).
The sparse file map is stored in x header:
GNU.sparse.size Real size of the stored file
GNU.sparse.numblocks Number of blocks in the sparse map
GNU.sparse.map Map of non-null data chunks. A string consisting of comma-separated
values "offset,size[,offset,size]..."
The resulting GNU.sparse.map string can be *very* long. While POSIX does not impose
any limit on the length of a x header variable, this can confuse some tars.
* 1.0
Starting from this version, the exact sparse format version is specified explicitly
in the header using the following variables:
GNU.sparse.major Major version
GNU.sparse.minor Minor version
X header keeps the following variables:
GNU.sparse.name Real file name of the sparse file
GNU.sparse.realsize Real size of the stored file (corresponds to the old GNU.sparse.size
variable)
The name field of the ustar header is constructed using the pattern "%d/GNUSparseFile.%p/%f".
The sparse map itself is stored in the file data block, preceding the actual file data.
It consists of a series of octal numbers of arbitrary length, delimited by newlines.
The map is padded with nulls to the nearest block boundary.
The first number gives the number of entries in the map. Following are map entries, each one
consisting of two numbers giving the offset and size of the data block it describes.
The format is designed in such a way that non-posix aware tars and tars not supporting
GNU.sparse.* keywords will extract each sparse file in its condensed form with the file map
attached and will place it into a separate directory. Then, using a simple program it would be
possible to expand the file to its original form even without GNU tar.
Bu default, v.1.0 archives are created. To use other formats, --sparse-version option is provided.
Additionally, v.0.0 can be obtained by deleting GNU.sparse.map from 0.1 format:
--sparse-version 0.1 --pax-option delete=GNU.sparse.map
*/
static struct tar_sparse_optab const pax_optab = {
.init = NULL, /* No init function */
.done = NULL, /* No done function */
.sparse_member_p = pax_sparse_member_p,
.fixup_header = NULL, /* No fixup_header function */
.decode_header = pax_decode_header
};
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static gboolean
decode_num (uintmax_t * num, const char *arg, uintmax_t maxval)
{
uintmax_t u;
char *arg_lim;
if (!isdigit (*arg))
return FALSE;
errno = 0;
u = (uintmax_t) g_ascii_strtoll (arg, &arg_lim, 10);
if (!(u <= maxval && errno != ERANGE) || *arg_lim != '\0')
return FALSE;
*num = u;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
sparse_select_optab (const tar_super_t * archive, struct tar_sparse_file *file)
{
switch (archive->type)
{
case TAR_V7:
case TAR_USTAR:
return FALSE;
case TAR_OLDGNU:
case TAR_GNU: /* FIXME: This one should disappear? */
file->optab = &oldgnu_optab;
break;
case TAR_POSIX:
file->optab = &pax_optab;
break;
case TAR_STAR:
file->optab = &star_optab;
break;
default:
return FALSE;
}
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
sparse_init (tar_super_t * archive, struct tar_sparse_file *file)
{
memset (file, 0, sizeof (*file));
if (!sparse_select_optab (archive, file))
return FALSE;
if (file->optab->init != NULL)
return file->optab->init (file);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
sparse_done (struct tar_sparse_file *file)
{
if (file->optab->done != NULL)
return file->optab->done (file);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
sparse_member_p (struct tar_sparse_file *file)
{
if (file->optab->sparse_member_p != NULL)
return file->optab->sparse_member_p (file);
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
sparse_fixup_header (struct tar_sparse_file *file)
{
if (file->optab->fixup_header != NULL)
return file->optab->fixup_header (file);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
sparse_decode_header (tar_super_t * archive, struct tar_sparse_file *file)
{
if (file->optab->decode_header != NULL)
return file->optab->decode_header (archive, file);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static inline void
sparse_add_map (struct tar_stat_info *st, struct sp_array *sp)
{
if (st->sparse_map == NULL)
st->sparse_map = g_array_sized_new (FALSE, FALSE, sizeof (struct sp_array), 1);
g_array_append_val (st->sparse_map, *sp);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Add a sparse item to the sparse file
*/
static enum oldgnu_add_status
oldgnu_add_sparse (struct tar_sparse_file *file, struct sparse *s)
{
struct sp_array sp;
if (s->numbytes[0] == '\0')
return add_finish;
sp.offset = OFF_FROM_HEADER (s->offset);
sp.numbytes = OFF_FROM_HEADER (s->numbytes);
if (sp.offset < 0 || sp.numbytes < 0
|| INT_ADD_OVERFLOW (sp.offset, sp.numbytes)
|| file->stat_info->stat.st_size < sp.offset + sp.numbytes
|| file->stat_info->archive_file_size < 0)
return add_fail;
sparse_add_map (file->stat_info, &sp);
return add_ok;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
oldgnu_sparse_member_p (struct tar_sparse_file *file)
{
(void) file;
return current_header->header.typeflag == GNUTYPE_SPARSE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
oldgnu_fixup_header (struct tar_sparse_file *file)
{
/* NOTE! st_size was initialized from the header which actually contains archived size.
The following fixes it */
off_t realsize;
realsize = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
file->stat_info->archive_file_size = file->stat_info->stat.st_size;
file->stat_info->stat.st_size = MAX (0, realsize);
return (realsize >= 0);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Convert old GNU format sparse data to internal representation.
*/
static gboolean
oldgnu_get_sparse_info (tar_super_t * archive, struct tar_sparse_file *file)
{
size_t i;
union block *h = current_header;
int ext_p;
enum oldgnu_add_status rc;
if (file->stat_info->sparse_map != NULL)
g_array_set_size (file->stat_info->sparse_map, 0);
for (i = 0; i < SPARSES_IN_OLDGNU_HEADER; i++)
{
rc = oldgnu_add_sparse (file, &h->oldgnu_header.sp[i]);
if (rc != add_ok)
break;
}
for (ext_p = h->oldgnu_header.isextended ? 1 : 0; rc == add_ok && ext_p != 0;
ext_p = h->sparse_header.isextended ? 1 : 0)
{
h = tar_find_next_block (archive);
if (h == NULL)
return FALSE;
tar_set_next_block_after (h);
for (i = 0; i < SPARSES_IN_SPARSE_HEADER && rc == add_ok; i++)
rc = oldgnu_add_sparse (file, &h->sparse_header.sp[i]);
}
return (rc != add_fail);
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
star_sparse_member_p (struct tar_sparse_file *file)
{
(void) file;
return current_header->header.typeflag == GNUTYPE_SPARSE;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
star_fixup_header (struct tar_sparse_file *file)
{
/* NOTE! st_size was initialized from the header which actually contains archived size.
The following fixes it */
off_t realsize;
realsize = OFF_FROM_HEADER (current_header->star_in_header.realsize);
file->stat_info->archive_file_size = file->stat_info->stat.st_size;
file->stat_info->stat.st_size = MAX (0, realsize);
return (realsize >= 0);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Convert STAR format sparse data to internal representation
*/
static gboolean
star_get_sparse_info (tar_super_t * archive, struct tar_sparse_file *file)
{
size_t i;
union block *h = current_header;
int ext_p = 1;
enum oldgnu_add_status rc = add_ok;
if (file->stat_info->sparse_map != NULL)
g_array_set_size (file->stat_info->sparse_map, 0);
if (h->star_in_header.prefix[0] == '\0' && h->star_in_header.sp[0].offset[10] != '\0')
{
/* Old star format */
for (i = 0; i < SPARSES_IN_STAR_HEADER; i++)
{
rc = oldgnu_add_sparse (file, &h->star_in_header.sp[i]);
if (rc != add_ok)
break;
}
ext_p = h->star_in_header.isextended ? 1 : 0;
}
for (; rc == add_ok && ext_p != 0; ext_p = h->star_ext_header.isextended ? 1 : 0)
{
h = tar_find_next_block (archive);
if (h == NULL)
return FALSE;
tar_set_next_block_after (h);
for (i = 0; i < SPARSES_IN_STAR_EXT_HEADER && rc == add_ok; i++)
rc = oldgnu_add_sparse (file, &h->star_ext_header.sp[i]);
file->dumped_size += BLOCKSIZE;
}
return (rc != add_fail);
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
pax_sparse_member_p (struct tar_sparse_file *file)
{
return file->stat_info->sparse_map != NULL && file->stat_info->sparse_map->len > 0
&& file->stat_info->sparse_major > 0;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
pax_decode_header (tar_super_t * archive, struct tar_sparse_file *file)
{
if (file->stat_info->sparse_major > 0)
{
uintmax_t u;
char nbuf[UINTMAX_STRSIZE_BOUND];
union block *blk;
char *p;
size_t sparse_map_len;
size_t i;
off_t start;
start = tar_current_block_ordinal (archive);
tar_set_next_block_after (current_header);
blk = tar_find_next_block (archive);
if (blk == NULL)
/* unexpected EOF in archive */
return FALSE;
p = blk->buffer;
COPY_BUF (archive, blk, nbuf, p);
if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)))
{
/* malformed sparse archive member */
return FALSE;
}
if (file->stat_info->sparse_map == NULL)
file->stat_info->sparse_map =
g_array_sized_new (FALSE, FALSE, sizeof (struct sp_array), u);
else
g_array_set_size (file->stat_info->sparse_map, u);
sparse_map_len = u;
for (i = 0; i < sparse_map_len; i++)
{
struct sp_array sp;
COPY_BUF (archive, blk, nbuf, p);
if (!decode_num (&u, nbuf, TYPE_MAXIMUM (off_t)))
{
/* malformed sparse archive member */
return FALSE;
}
sp.offset = u;
COPY_BUF (archive, blk, nbuf, p);
if (!decode_num (&u, nbuf, TYPE_MAXIMUM (size_t)) || INT_ADD_OVERFLOW (sp.offset, u)
|| (uintmax_t) file->stat_info->stat.st_size < sp.offset + u)
{
/* malformed sparse archive member */
return FALSE;
}
sp.numbytes = u;
sparse_add_map (file->stat_info, &sp);
}
tar_set_next_block_after (blk);
file->dumped_size += BLOCKSIZE * (tar_current_block_ordinal (archive) - start);
}
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
gboolean
tar_sparse_member_p (tar_super_t * archive, struct tar_stat_info * st)
{
struct tar_sparse_file file;
if (!sparse_init (archive, &file))
return FALSE;
file.stat_info = st;
return sparse_member_p (&file);
}
/* --------------------------------------------------------------------------------------------- */
gboolean
tar_sparse_fixup_header (tar_super_t * archive, struct tar_stat_info * st)
{
struct tar_sparse_file file;
if (!sparse_init (archive, &file))
return FALSE;
file.stat_info = st;
return sparse_fixup_header (&file);
}
/* --------------------------------------------------------------------------------------------- */
enum dump_status
tar_sparse_skip_file (tar_super_t * archive, struct tar_stat_info *st)
{
gboolean rc = TRUE;
struct tar_sparse_file file;
if (!sparse_init (archive, &file))
return dump_status_not_implemented;
file.stat_info = st;
file.fd = -1;
rc = sparse_decode_header (archive, &file);
(void) tar_skip_file (archive, file.stat_info->archive_file_size - file.dumped_size);
return (sparse_done (&file) && rc) ? dump_status_ok : dump_status_short;
}
/* --------------------------------------------------------------------------------------------- */
|