8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / cmd / svr4pkg / libinst / copyf.c
blob265341357c668c510951560e878264ac145f0f1f
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
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
32 #include <stdio.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <libgen.h>
38 #include <fcntl.h>
39 #include <sys/types.h>
40 #include <utime.h>
41 #include <sys/stat.h>
42 #include <locale.h>
43 #include <libintl.h>
44 #include <sys/mman.h>
47 * consolidation pkg command library includes
50 #include "pkglib.h"
53 * local pkg command library includes
55 #include "libinst.h"
56 #include "libadm.h"
57 #include "messages.h"
60 * MAXMAPSIZE controls the largest mapping to use at a time; please refer
61 * to mmap(2) for details of how this size is incremented and rounded; briefly
62 * each mapping request has an additional 16Kb added to it - mappings over
63 * 4Mb will be rounded to a 4Mb boundary - thus if there were 8mb, adding
64 * in the 16Kb overhead the mapping would use another 4Mb-16kb - that is
65 * why there is 16Kb subtracted from the total
68 #define MAXMAPSIZE (1024*1024*8)-(1024*16) /* map at most 8MB */
69 #define SMALLFILESIZE (32*1024) /* dont mmap files less than 32kb */
72 * Name: copyF
73 * Description: fast copy of file - use mmap()/write() loop if possible
74 * Arguments: char *srcPath - name of source file to copy from
75 * char *dstPath - name of target file to copy to
76 * time_t a_mytime: control setting of access/modification times:
77 * == 0 - replicate source file access/modification times
78 * != 0 - use specified time for access/modification times
79 * Returns: int
80 * == 0 - successful
81 * != 0 - failure
84 int
85 copyf(char *a_srcPath, char *a_dstPath, time_t a_mytime)
87 struct stat srcStatbuf;
88 struct utimbuf times;
89 int srcFd;
90 int dstFd;
91 int status;
92 char *pt;
94 /* open source file for reading */
96 srcFd = open(a_srcPath, O_RDONLY, 0);
97 if (srcFd < 0) {
98 progerr(ERR_OPEN_READ, a_srcPath, errno, strerror(errno));
99 return (-1);
102 /* obtain file status of source file */
104 if (fstat(srcFd, &srcStatbuf) != 0) {
105 progerr(ERR_FSTAT, srcFd, a_srcPath, errno, strerror(errno));
106 (void) close(srcFd);
107 return (-1);
110 /* open target file for writing */
112 dstFd = open(a_dstPath, O_WRONLY | O_TRUNC | O_CREAT,
113 srcStatbuf.st_mode);
114 if (dstFd < 0) {
115 /* create directory structure if missing */
116 pt = a_dstPath;
117 while (pt = strchr(pt+1, '/')) {
118 *pt = '\0';
119 if (isdir(a_dstPath)) {
120 if (mkdir(a_dstPath, 0755)) {
121 progerr(ERR_NODIR, a_dstPath,
122 errno, strerror(errno));
123 *pt = '/';
124 (void) close(srcFd);
125 return (-1);
128 *pt = '/';
131 /* attempt to create target file again */
132 dstFd = open(a_dstPath, O_WRONLY | O_TRUNC | O_CREAT,
133 srcStatbuf.st_mode);
134 if (dstFd < 0) {
135 progerr(ERR_OPEN_WRITE, a_dstPath, errno,
136 strerror(errno));
137 (void) close(srcFd);
138 return (-1);
143 * source and target files are open: copy data
146 status = copyFile(srcFd, dstFd, a_srcPath, a_dstPath, &srcStatbuf, 0);
148 (void) close(srcFd);
149 (void) close(dstFd);
152 * determine how to set access/modification times for target:
153 * -- a_mytime == 0: replicate source file access/modification times
154 * -- otherwise: use a_mytime for file access/modification times
157 if (a_mytime == 0) {
158 times.actime = srcStatbuf.st_atime;
159 times.modtime = srcStatbuf.st_mtime;
160 } else {
161 times.actime = a_mytime;
162 times.modtime = a_mytime;
165 /* set access/modification times for target */
167 if (utime(a_dstPath, &times) != 0) {
168 progerr(ERR_MODTIM, a_dstPath, errno, strerror(errno));
169 return (-1);
172 /* return error if copy failed */
174 if (status != 0) {
175 progerr(ERR_READ, a_srcPath, errno, strerror(errno));
176 return (-1);
179 /* success! */
181 return (0);
185 * Name: copyFile
186 * Description: fast copy of file - use mmap()/write() loop if possible
187 * Arguments: int srcFd - file descriptor open on source file
188 * int dstFd - file descriptor open on target file
189 * char *srcPath - name of source file (for error messages)
190 * char *dstPath - name of target file (for error messages)
191 * struct stat *a_srcStatbuf - stat structure for source file
192 * long a_iosize - preferred io size for read/write loop
193 * Returns: int
194 * == 0 - successful
195 * != 0 - failure
199 copyFile(int a_srcFd, int a_dstFd, char *a_srcPath, char *a_dstPath,
200 struct stat *a_srcStatbuf, long a_iosize)
202 caddr_t cp;
203 off_t filesize = a_srcStatbuf->st_size;
204 size_t mapsize = 0;
205 size_t munmapsize = 0;
206 off_t offset = 0;
208 echoDebug(DBG_COPY_FILE, a_srcPath, a_dstPath);
211 * if the source is a regular file and is not "too small", then cause
212 * the file to be mapped into memory
215 if (S_ISREG(a_srcStatbuf->st_mode) && (filesize > SMALLFILESIZE)) {
217 * Determine size of initial mapping. This will determine the
218 * size of the address space chunk we work with. This initial
219 * mapping size will be used to perform munmap() in the future.
222 mapsize = MAXMAPSIZE;
223 if (filesize < mapsize) {
224 mapsize = filesize;
228 * remember size of mapping to "unmap" - if the source file
229 * exceeds MAXMAPSIZE bytes, then the final mapping of the
230 * source file will be less than MAXMAPSIZE, and we need to
231 * make sure that the entire mapping is unmapped when done.
234 munmapsize = mapsize;
236 /* map the first segment of the source into memory */
238 cp = mmap((caddr_t)NULL, mapsize, PROT_READ,
239 (MAP_SHARED|MAP_ALIGN), a_srcFd, (off_t)0);
240 if (cp == MAP_FAILED) {
241 mapsize = 0; /* can't mmap today */
246 * if the source was not mapped into memory, copy via read/write loop
249 if (mapsize == 0) {
250 char *buf = (char *)NULL;
251 size_t blocksize;
252 int pagesize = getpagesize();
254 /* set blocksize for copy */
256 blocksize = a_iosize;
257 if ((blocksize == 0) || (blocksize > SMALLFILESIZE)) {
258 blocksize = SMALLFILESIZE;
259 } else if (blocksize < pagesize) {
260 blocksize = pagesize;
263 /* allocate i/o transfer buffer */
265 buf = memalign((size_t)pagesize, blocksize);
266 if (buf == (char *)NULL) {
267 progerr(ERR_COPY_MEMORY, a_srcPath, errno,
268 strerror(errno));
269 return (1);
272 /* copy the file contents */
274 for (;;) {
275 ssize_t n;
277 /* read next block of data */
279 n = read(a_srcFd, buf, blocksize);
280 if (n == 0) {
281 /* end of file - return success */
282 (void) free(buf);
283 return (0);
284 } else if (n < 0) {
285 /* read error - return error */
286 progerr(ERR_READ, a_srcPath,
287 errno, strerror(errno));
288 (void) free(buf);
289 return (1);
292 /* write out block of data just read in */
294 if (vfpSafeWrite(a_dstFd, buf, (size_t)n) != n) {
295 /* short write/write error - return error */
296 progerr(ERR_WRITE, a_dstPath,
297 errno, strerror(errno));
298 (void) free(buf);
299 return (1);
305 * the source has been mapped into memory, copy via mappings
308 for (;;) {
309 ssize_t nbytes;
311 /* write first mappings worth of data */
313 nbytes = write(a_dstFd, cp, mapsize);
316 * if we write less than the mmaped size it's due to a
317 * media error on the input file or out of space on
318 * the output file. So, try again, and look for errno.
321 if ((nbytes >= 0) && (nbytes != (ssize_t)mapsize)) {
322 size_t remains;
324 remains = mapsize - nbytes;
325 while (remains > 0) {
326 nbytes = write(a_dstFd,
327 (cp + mapsize - remains), remains);
328 if (nbytes >= 0) {
329 remains -= nbytes;
330 if (remains == 0) {
331 nbytes = mapsize;
333 continue;
336 /* i/o error - report and exit */
338 if (errno == ENOSPC) {
339 progerr(ERR_WRITE, a_dstPath,
340 errno, strerror(errno));
341 } else {
342 progerr(ERR_READ, a_srcPath,
343 errno, strerror(errno));
346 /* unmap source file mapping */
347 (void) munmap(cp, munmapsize);
348 return (1);
353 * although the write manual page doesn't specify this
354 * as a possible errno, it is set when the nfs read
355 * via the mmap'ed file is accessed, so report the
356 * problem as a source access problem, not a target file
357 * problem
360 if (nbytes < 0) {
361 if (errno == EACCES) {
362 progerr(ERR_READ, a_srcPath,
363 errno, strerror(errno));
364 } else {
365 progerr(ERR_WRITE, a_dstPath,
366 errno, strerror(errno));
369 /* unmap source file mapping */
370 (void) munmap(cp, munmapsize);
371 return (1);
374 filesize -= nbytes;
375 if (filesize == 0) {
376 break;
379 offset += nbytes;
380 if (filesize < mapsize) {
381 mapsize = filesize;
384 /* map next segment of file on top of existing mapping */
386 cp = mmap(cp, mapsize, PROT_READ, (MAP_SHARED|MAP_FIXED),
387 a_srcFd, offset);
389 if (cp == MAP_FAILED) {
390 progerr(ERR_MAPFAILED, a_srcPath, errno,
391 strerror(errno));
392 /* unmap source file mapping */
393 (void) munmap(cp, munmapsize);
394 return (1);
398 /* unmap source file mapping */
400 (void) munmap(cp, munmapsize);
402 return (0);
406 * Name: openLocal
407 * Description: open a file and assure that the descriptor returned is open on
408 * a file that is local to the current system - if the file is not
409 * local to this system, copy the file to a temporary file first,
410 * and then pass a handle back opened on the temporary file
411 * Arguments: a_path - [RO, *RO] - (char *)
412 * Pointer to string representing the path to the file
413 * to open
414 * a_oflag - [RO, *RO] - (int)
415 * Integer representing the "mode" bits for an open(2) call
416 * a_tmpdir - [RO, *RO] - (char *)
417 * Pointer to string representing the path to a directory
418 * where a temporary copy of the file can be placed if
419 * the source file is not local to this system. If this is
420 * NULL or does not exist, P_tmpdir is used.
421 * Returns: int
422 * >= 0 - file descriptor opened on the file
423 * == -1 - failed to open - errno contains error code
424 * NOTE: If the file is not local and is copied locally, the file is
425 * setup in such a way that it will be removed when the last
426 * file descriptor opened on the file is closed - there is no need
427 * to know the path to the temporary file or to remove it
428 * when done.
432 openLocal(char *a_path, int a_oflag, char *a_tmpdir)
434 char *bn;
435 char template[PATH_MAX];
436 int fd;
437 int lerrno;
438 int n;
439 int tmpFd;
440 struct stat statbuf;
442 /* open source file */
444 fd = open(a_path, a_oflag);
445 if (fd < 0) {
446 return (fd);
449 /* return open fd if the source file is not remote */
451 if (!isFdRemote(fd)) {
452 return (fd);
456 * source file is remote - must make a local copy
459 /* get the source file's status */
461 n = fstat(fd, &statbuf);
462 if (n < 0) {
463 lerrno = errno;
464 (void) close(fd);
465 errno = lerrno;
466 return (-1);
469 /* generate unique temporary file name */
471 if ((a_tmpdir == (char *)NULL) || (*a_tmpdir == '\0') ||
472 (isdir(a_tmpdir) != 0)) {
473 a_tmpdir = P_tmpdir;
475 bn = basename(a_path);
476 n = strlen(a_tmpdir);
477 n = snprintf(template, sizeof (template), "%s%s%sXXXXXX",
478 a_tmpdir, a_tmpdir[n-1] == '/' ? "" : "/", bn);
479 if (n > sizeof (template)) {
480 (void) close(fd);
481 return (EINVAL);
484 /* create the temporary file and open it */
486 tmpFd = mkstemp(template);
487 if (tmpFd < 0) {
488 lerrno = errno;
489 (void) close(fd);
490 errno = lerrno;
491 return (tmpFd);
494 /* unlink the file so when it is closed it is automatically deleted */
496 (void) unlink(template);
498 /* copy the source file to the temporary file */
500 n = copyFile(fd, tmpFd, a_path, template, &statbuf, 0L);
501 lerrno = errno;
502 (void) close(fd);
503 if (n != 0) {
504 (void) close(tmpFd);
505 errno = lerrno;
506 return (-1);
509 /* return handle to temporary file created */
511 return (tmpFd);