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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include "cpio.h"
/*
* Allocation wrappers. Used to centralize error handling for
* failed allocations.
*/
static void *
e_alloc_fail(int flag)
{
if (flag == E_EXIT)
msg(EXTN, "Out of memory");
return (NULL);
}
/*
* Note: unlike the other e_*lloc functions, e_realloc does not zero out the
* additional memory it returns. Ensure that you do not trust its contents
* when you call it.
*/
void *
e_realloc(int flag, void *old, size_t newsize)
{
void *ret = realloc(old, newsize);
if (ret == NULL) {
return (e_alloc_fail(flag));
}
return (ret);
}
char *
e_strdup(int flag, const char *arg)
{
char *ret = strdup(arg);
if (ret == NULL) {
return (e_alloc_fail(flag));
}
return (ret);
}
void *
e_valloc(int flag, size_t size)
{
void *ret = valloc(size);
if (ret == NULL) {
return (e_alloc_fail(flag));
}
return (ret);
}
void *
e_zalloc(int flag, size_t size)
{
void *ret = malloc(size);
if (ret == NULL) {
return (e_alloc_fail(flag));
}
(void) memset(ret, 0, size);
return (ret);
}
/*
* Simple printf() which only support "%s" conversion.
* We need secure version of printf since format string can be supplied
* from gettext().
*/
void
str_fprintf(FILE *fp, const char *fmt, ...)
{
const char *s = fmt;
va_list ap;
va_start(ap, fmt);
while (*s != '\0') {
if (*s != '%') {
(void) fputc(*s++, fp);
continue;
}
s++;
if (*s != 's') {
(void) fputc(*(s - 1), fp);
(void) fputc(*s++, fp);
continue;
}
(void) fputs(va_arg(ap, char *), fp);
s++;
}
va_end(ap);
}
/*
* Step through a file discovering and recording pairs of data and hole
* offsets. Returns a linked list of data/hole offset pairs of a file.
* If there is no holes found, NULL is returned.
*
* Note: According to lseek(2), only filesystems which support
* fpathconf(_PC_MIN_HOLE_SIZE) support SEEK_HOLE. For filesystems
* that do not supply information about holes, the file will be
* represented as one entire data region.
*/
static holes_list_t *
get_holes_list(int fd, off_t filesz, size_t *countp)
{
off_t data, hole;
holes_list_t *hlh, *hl, **hlp;
size_t cnt;
if (filesz == 0 || fpathconf(fd, _PC_MIN_HOLE_SIZE) < 0)
return (NULL);
cnt = 0;
hole = 0;
hlh = NULL;
hlp = &hlh;
while (hole < filesz) {
if ((data = lseek(fd, hole, SEEK_DATA)) == -1) {
/* no more data till the end of file */
if (errno == ENXIO) {
data = filesz;
} else {
/* assume data starts from the * beginning */
data = 0;
}
}
if ((hole = lseek(fd, data, SEEK_HOLE)) == -1) {
/* assume that data ends at the end of file */
hole = filesz;
}
if (data == 0 && hole == filesz) {
/* no holes */
break;
}
hl = e_zalloc(E_EXIT, sizeof (holes_list_t));
hl->hl_next = NULL;
/* set data and hole */
hl->hl_data = data;
hl->hl_hole = hole;
*hlp = hl;
hlp = &hl->hl_next;
cnt++;
}
if (countp != NULL)
*countp = cnt;
/*
* reset to the beginning, otherwise subsequent read calls would
* get EOF
*/
(void) lseek(fd, 0, SEEK_SET);
return (hlh);
}
/*
* Calculate the real data size in the sparse file.
*/
static off_t
get_compressed_filesz(holes_list_t *hlh)
{
holes_list_t *hl;
off_t size;
size = 0;
for (hl = hlh; hl != NULL; hl = hl->hl_next) {
size += (hl->hl_hole - hl->hl_data);
}
return (size);
}
/*
* Convert val to digit string and put it in str. The next address
* of the last digit is returned.
*/
static char *
put_value(off_t val, char *str)
{
size_t len;
char *digp, dbuf[ULL_MAX_SIZE + 1];
dbuf[ULL_MAX_SIZE] = '\0';
digp = ulltostr((u_longlong_t)val, &dbuf[ULL_MAX_SIZE]);
len = &dbuf[ULL_MAX_SIZE] - digp;
(void) memcpy(str, digp, len);
return (str + len);
}
/*
* Put data/hole offset pair into string in the following
* sequence.
* <data> <sp> <hole> <sp>
*/
static void
store_sparse_string(holes_list_t *hlh, char *str, size_t *szp)
{
holes_list_t *hl;
char *p;
p = str;
for (hl = hlh; hl != NULL; hl = hl->hl_next) {
p = put_value(hl->hl_data, p);
*p++ = ' ';
p = put_value(hl->hl_hole, p);
*p++ = ' ';
}
*--p = '\0';
if (szp != NULL)
*szp = p - str;
}
/*
* Convert decimal str into unsigned long long value. The end pointer
* is returned.
*/
static const char *
get_ull_tok(const char *str, uint64_t *ulp)
{
uint64_t ul;
char *np;
while (isspace(*str))
str++;
if (!isdigit(*str))
return (NULL);
errno = 0;
ul = strtoull(str, &np, 10);
if (ul == ULLONG_MAX && errno == ERANGE)
return (NULL); /* invalid value */
if (*np != ' ' && *np != '\0')
return (NULL); /* invalid input */
*ulp = ul;
return (np);
}
static void
free_holesdata(holes_info_t *hi)
{
holes_list_t *hl, *nhl;
for (hl = hi->holes_list; hl != NULL; hl = nhl) {
nhl = hl->hl_next;
free(hl);
}
hi->holes_list = NULL;
if (hi->holesdata != NULL)
free(hi->holesdata);
hi->holesdata = NULL;
}
/*
* When a hole is detected, non NULL holes_info pointer is returned.
* If we are in copy-out mode, holes_list is converted to string (holesdata)
* which will be prepended to file contents. The holesdata is a character
* string and in the format of:
*
* <data size(%10u)><SP><file size(%llu)><SP>
* <SP><data off><SP><hole off><SP><data off><SP><hole off> ...
*
* This string is parsed by parse_holesholes() in copy-in mode to restore
* the sparse info.
*/
holes_info_t *
get_holes_info(int fd, off_t filesz, boolean_t pass_mode)
{
holes_info_t *hi;
holes_list_t *hl;
char *str, hstr[MIN_HOLES_HDRSIZE + 1];
size_t ninfo, len;
if ((hl = get_holes_list(fd, filesz, &ninfo)) == NULL)
return (NULL);
hi = e_zalloc(E_EXIT, sizeof (holes_info_t));
hi->holes_list = hl;
if (!pass_mode) {
str = e_zalloc(E_EXIT,
MIN_HOLES_HDRSIZE + ninfo * (ULL_MAX_SIZE * 2));
/*
* Convert into string data, and place it to after
* the first 2 fixed entries.
*/
store_sparse_string(hl, str + MIN_HOLES_HDRSIZE, &len);
/*
* Add the first two fixed entries. The size of holesdata
* includes '\0' at the end of data
*/
(void) sprintf(hstr, "%10lu %20llu ",
(ulong_t)MIN_HOLES_HDRSIZE + len + 1, filesz);
(void) memcpy(str, hstr, MIN_HOLES_HDRSIZE);
/* calc real file size without holes */
hi->data_size = get_compressed_filesz(hl);
hi->holesdata = str;
hi->holesdata_sz = MIN_HOLES_HDRSIZE + len + 1;
}
return (hi);
}
/*
* The holesdata information is in the following format:
* <data size(%10u)><SP><file size(%llu)><SP>
* <SP><data off><SP><hole off><SP><data off><SP><hole off> ...
* read_holes_header() allocates holes_info_t, and read the first 2
* entries (data size and file size). The rest of holesdata is
* read by parse_holesdata().
*/
holes_info_t *
read_holes_header(const char *str, off_t filesz)
{
holes_info_t *hi;
uint64_t ull;
hi = e_zalloc(E_EXIT, sizeof (holes_info_t));
/* read prepended holes data size */
if ((str = get_ull_tok(str, &ull)) == NULL || *str != ' ') {
bad:
free(hi);
return (NULL);
}
hi->holesdata_sz = (size_t)ull;
/* read original(expanded) file size */
if (get_ull_tok(str, &ull) == NULL)
goto bad;
hi->orig_size = (off_t)ull;
/* sanity check */
if (hi->holesdata_sz > filesz ||
hi->holesdata_sz <= MIN_HOLES_HDRSIZE) {
goto bad;
}
return (hi);
}
int
parse_holesdata(holes_info_t *hi, const char *str)
{
holes_list_t *hl, **hlp;
uint64_t ull;
off_t loff;
/* create hole list */
hlp = &hi->holes_list;
while (*str != '\0') {
hl = e_zalloc(E_EXIT, sizeof (holes_list_t));
/* link list */
hl->hl_next = NULL;
*hlp = hl;
hlp = &hl->hl_next;
/* read the string token for data */
if ((str = get_ull_tok(str, &ull)) == NULL)
goto bad;
hl->hl_data = (off_t)ull;
/* there must be single blank space in between */
if (*str != ' ')
goto bad;
/* read the string token for hole */
if ((str = get_ull_tok(str, &ull)) == NULL)
goto bad;
hl->hl_hole = (off_t)ull;
}
/* check to see if offset is in ascending order */
loff = -1;
for (hl = hi->holes_list; hl != NULL; hl = hl->hl_next) {
if (loff >= hl->hl_data)
goto bad;
loff = hl->hl_data;
/* data and hole can be equal */
if (loff > hl->hl_hole)
goto bad;
loff = hl->hl_hole;
}
/* The last hole offset should match original file size */
if (hi->orig_size != loff) {
bad:
free_holesdata(hi);
return (1);
}
hi->data_size = get_compressed_filesz(hi->holes_list);
return (0);
}
void
free_holes_info(holes_info_t *hi)
{
free_holesdata(hi);
free(hi);
}
|