Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / cpio / utils.c
blob5717b3292374d7a58346058435e9eb4910f78455
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <ctype.h>
41 #include <limits.h>
42 #include <errno.h>
43 #include <fcntl.h>
45 #include "cpio.h"
48 * Allocation wrappers. Used to centralize error handling for
49 * failed allocations.
51 static void *
52 e_alloc_fail(int flag)
54 if (flag == E_EXIT)
55 msg(EXTN, "Out of memory");
57 return (NULL);
61 * Note: unlike the other e_*lloc functions, e_realloc does not zero out the
62 * additional memory it returns. Ensure that you do not trust its contents
63 * when you call it.
65 void *
66 e_realloc(int flag, void *old, size_t newsize)
68 void *ret = realloc(old, newsize);
70 if (ret == NULL) {
71 return (e_alloc_fail(flag));
74 return (ret);
77 char *
78 e_strdup(int flag, const char *arg)
80 char *ret = strdup(arg);
82 if (ret == NULL) {
83 return (e_alloc_fail(flag));
86 return (ret);
89 void *
90 e_valloc(int flag, size_t size)
92 void *ret = valloc(size);
94 if (ret == NULL) {
95 return (e_alloc_fail(flag));
98 return (ret);
101 void *
102 e_zalloc(int flag, size_t size)
104 void *ret = malloc(size);
106 if (ret == NULL) {
107 return (e_alloc_fail(flag));
110 (void) memset(ret, 0, size);
111 return (ret);
115 * Simple printf() which only support "%s" conversion.
116 * We need secure version of printf since format string can be supplied
117 * from gettext().
119 void
120 str_fprintf(FILE *fp, const char *fmt, ...)
122 const char *s = fmt;
123 va_list ap;
125 va_start(ap, fmt);
126 while (*s != '\0') {
127 if (*s != '%') {
128 (void) fputc(*s++, fp);
129 continue;
131 s++;
132 if (*s != 's') {
133 (void) fputc(*(s - 1), fp);
134 (void) fputc(*s++, fp);
135 continue;
137 (void) fputs(va_arg(ap, char *), fp);
138 s++;
140 va_end(ap);
144 * Step through a file discovering and recording pairs of data and hole
145 * offsets. Returns a linked list of data/hole offset pairs of a file.
146 * If there is no holes found, NULL is returned.
148 * Note: According to lseek(2), only filesystems which support
149 * fpathconf(_PC_MIN_HOLE_SIZE) support SEEK_HOLE. For filesystems
150 * that do not supply information about holes, the file will be
151 * represented as one entire data region.
153 static holes_list_t *
154 get_holes_list(int fd, off_t filesz, size_t *countp)
156 off_t data, hole;
157 holes_list_t *hlh, *hl, **hlp;
158 size_t cnt;
160 if (filesz == 0 || fpathconf(fd, _PC_MIN_HOLE_SIZE) < 0)
161 return (NULL);
163 cnt = 0;
164 hole = 0;
165 hlh = NULL;
166 hlp = &hlh;
168 while (hole < filesz) {
169 if ((data = lseek(fd, hole, SEEK_DATA)) == -1) {
170 /* no more data till the end of file */
171 if (errno == ENXIO) {
172 data = filesz;
173 } else {
174 /* assume data starts from the * beginning */
175 data = 0;
178 if ((hole = lseek(fd, data, SEEK_HOLE)) == -1) {
179 /* assume that data ends at the end of file */
180 hole = filesz;
182 if (data == 0 && hole == filesz) {
183 /* no holes */
184 break;
186 hl = e_zalloc(E_EXIT, sizeof (holes_list_t));
187 hl->hl_next = NULL;
189 /* set data and hole */
190 hl->hl_data = data;
191 hl->hl_hole = hole;
193 *hlp = hl;
194 hlp = &hl->hl_next;
195 cnt++;
197 if (countp != NULL)
198 *countp = cnt;
201 * reset to the beginning, otherwise subsequent read calls would
202 * get EOF
204 (void) lseek(fd, 0, SEEK_SET);
206 return (hlh);
210 * Calculate the real data size in the sparse file.
212 static off_t
213 get_compressed_filesz(holes_list_t *hlh)
215 holes_list_t *hl;
216 off_t size;
218 size = 0;
219 for (hl = hlh; hl != NULL; hl = hl->hl_next) {
220 size += (hl->hl_hole - hl->hl_data);
222 return (size);
226 * Convert val to digit string and put it in str. The next address
227 * of the last digit is returned.
229 static char *
230 put_value(off_t val, char *str)
232 size_t len;
233 char *digp, dbuf[ULL_MAX_SIZE + 1];
235 dbuf[ULL_MAX_SIZE] = '\0';
236 digp = ulltostr((u_longlong_t)val, &dbuf[ULL_MAX_SIZE]);
237 len = &dbuf[ULL_MAX_SIZE] - digp;
238 (void) memcpy(str, digp, len);
240 return (str + len);
244 * Put data/hole offset pair into string in the following
245 * sequence.
246 * <data> <sp> <hole> <sp>
248 static void
249 store_sparse_string(holes_list_t *hlh, char *str, size_t *szp)
251 holes_list_t *hl;
252 char *p;
254 p = str;
255 for (hl = hlh; hl != NULL; hl = hl->hl_next) {
256 p = put_value(hl->hl_data, p);
257 *p++ = ' ';
258 p = put_value(hl->hl_hole, p);
259 *p++ = ' ';
261 *--p = '\0';
262 if (szp != NULL)
263 *szp = p - str;
267 * Convert decimal str into unsigned long long value. The end pointer
268 * is returned.
270 static const char *
271 get_ull_tok(const char *str, uint64_t *ulp)
273 uint64_t ul;
274 char *np;
276 while (isspace(*str))
277 str++;
278 if (!isdigit(*str))
279 return (NULL);
281 errno = 0;
282 ul = strtoull(str, &np, 10);
283 if (ul == ULLONG_MAX && errno == ERANGE)
284 return (NULL); /* invalid value */
285 if (*np != ' ' && *np != '\0')
286 return (NULL); /* invalid input */
288 *ulp = ul;
289 return (np);
292 static void
293 free_holesdata(holes_info_t *hi)
295 holes_list_t *hl, *nhl;
297 for (hl = hi->holes_list; hl != NULL; hl = nhl) {
298 nhl = hl->hl_next;
299 free(hl);
301 hi->holes_list = NULL;
303 free(hi->holesdata);
304 hi->holesdata = NULL;
308 * When a hole is detected, non NULL holes_info pointer is returned.
309 * If we are in copy-out mode, holes_list is converted to string (holesdata)
310 * which will be prepended to file contents. The holesdata is a character
311 * string and in the format of:
313 * <data size(%10u)><SP><file size(%llu)><SP>
314 * <SP><data off><SP><hole off><SP><data off><SP><hole off> ...
316 * This string is parsed by parse_holesholes() in copy-in mode to restore
317 * the sparse info.
319 holes_info_t *
320 get_holes_info(int fd, off_t filesz, boolean_t pass_mode)
322 holes_info_t *hi;
323 holes_list_t *hl;
324 char *str, hstr[MIN_HOLES_HDRSIZE + 1];
325 size_t ninfo, len;
327 if ((hl = get_holes_list(fd, filesz, &ninfo)) == NULL)
328 return (NULL);
330 hi = e_zalloc(E_EXIT, sizeof (holes_info_t));
331 hi->holes_list = hl;
333 if (!pass_mode) {
334 str = e_zalloc(E_EXIT,
335 MIN_HOLES_HDRSIZE + ninfo * (ULL_MAX_SIZE * 2));
337 * Convert into string data, and place it to after
338 * the first 2 fixed entries.
340 store_sparse_string(hl, str + MIN_HOLES_HDRSIZE, &len);
343 * Add the first two fixed entries. The size of holesdata
344 * includes '\0' at the end of data
346 (void) sprintf(hstr, "%10lu %20llu ",
347 (ulong_t)MIN_HOLES_HDRSIZE + len + 1, filesz);
348 (void) memcpy(str, hstr, MIN_HOLES_HDRSIZE);
350 /* calc real file size without holes */
351 hi->data_size = get_compressed_filesz(hl);
352 hi->holesdata = str;
353 hi->holesdata_sz = MIN_HOLES_HDRSIZE + len + 1;
355 return (hi);
359 * The holesdata information is in the following format:
360 * <data size(%10u)><SP><file size(%llu)><SP>
361 * <SP><data off><SP><hole off><SP><data off><SP><hole off> ...
362 * read_holes_header() allocates holes_info_t, and read the first 2
363 * entries (data size and file size). The rest of holesdata is
364 * read by parse_holesdata().
366 holes_info_t *
367 read_holes_header(const char *str, off_t filesz)
369 holes_info_t *hi;
370 uint64_t ull;
372 hi = e_zalloc(E_EXIT, sizeof (holes_info_t));
374 /* read prepended holes data size */
375 if ((str = get_ull_tok(str, &ull)) == NULL || *str != ' ') {
376 bad:
377 free(hi);
378 return (NULL);
380 hi->holesdata_sz = (size_t)ull;
382 /* read original(expanded) file size */
383 if (get_ull_tok(str, &ull) == NULL)
384 goto bad;
385 hi->orig_size = (off_t)ull;
387 /* sanity check */
388 if (hi->holesdata_sz > filesz ||
389 hi->holesdata_sz <= MIN_HOLES_HDRSIZE) {
390 goto bad;
392 return (hi);
396 parse_holesdata(holes_info_t *hi, const char *str)
398 holes_list_t *hl, **hlp;
399 uint64_t ull;
400 off_t loff;
402 /* create hole list */
403 hlp = &hi->holes_list;
404 while (*str != '\0') {
405 hl = e_zalloc(E_EXIT, sizeof (holes_list_t));
406 /* link list */
407 hl->hl_next = NULL;
408 *hlp = hl;
409 hlp = &hl->hl_next;
411 /* read the string token for data */
412 if ((str = get_ull_tok(str, &ull)) == NULL)
413 goto bad;
414 hl->hl_data = (off_t)ull;
416 /* there must be single blank space in between */
417 if (*str != ' ')
418 goto bad;
420 /* read the string token for hole */
421 if ((str = get_ull_tok(str, &ull)) == NULL)
422 goto bad;
423 hl->hl_hole = (off_t)ull;
426 /* check to see if offset is in ascending order */
427 loff = -1;
428 for (hl = hi->holes_list; hl != NULL; hl = hl->hl_next) {
429 if (loff >= hl->hl_data)
430 goto bad;
431 loff = hl->hl_data;
432 /* data and hole can be equal */
433 if (loff > hl->hl_hole)
434 goto bad;
435 loff = hl->hl_hole;
437 /* The last hole offset should match original file size */
438 if (hi->orig_size != loff) {
439 bad:
440 free_holesdata(hi);
441 return (1);
444 hi->data_size = get_compressed_filesz(hi->holes_list);
446 return (0);
449 void
450 free_holes_info(holes_info_t *hi)
452 free_holesdata(hi);
453 free(hi);