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]
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 */
39 #include <sys/types.h>
47 * consolidation pkg command library includes
53 * local pkg command library includes
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 */
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
85 copyf(char *a_srcPath
, char *a_dstPath
, time_t a_mytime
)
87 struct stat srcStatbuf
;
94 /* open source file for reading */
96 srcFd
= open(a_srcPath
, O_RDONLY
, 0);
98 progerr(ERR_OPEN_READ
, a_srcPath
, errno
, strerror(errno
));
102 /* obtain file status of source file */
104 if (fstat(srcFd
, &srcStatbuf
) != 0) {
105 progerr(ERR_FSTAT
, srcFd
, a_srcPath
, errno
, strerror(errno
));
110 /* open target file for writing */
112 dstFd
= open(a_dstPath
, O_WRONLY
| O_TRUNC
| O_CREAT
,
115 /* create directory structure if missing */
117 while (pt
= strchr(pt
+1, '/')) {
119 if (isdir(a_dstPath
)) {
120 if (mkdir(a_dstPath
, 0755)) {
121 progerr(ERR_NODIR
, a_dstPath
,
122 errno
, strerror(errno
));
131 /* attempt to create target file again */
132 dstFd
= open(a_dstPath
, O_WRONLY
| O_TRUNC
| O_CREAT
,
135 progerr(ERR_OPEN_WRITE
, a_dstPath
, errno
,
143 * source and target files are open: copy data
146 status
= copyFile(srcFd
, dstFd
, a_srcPath
, a_dstPath
, &srcStatbuf
, 0);
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
158 times
.actime
= srcStatbuf
.st_atime
;
159 times
.modtime
= srcStatbuf
.st_mtime
;
161 times
.actime
= a_mytime
;
162 times
.modtime
= a_mytime
;
165 /* set access/modification times for target */
167 if (utime(a_dstPath
, ×
) != 0) {
168 progerr(ERR_MODTIM
, a_dstPath
, errno
, strerror(errno
));
172 /* return error if copy failed */
175 progerr(ERR_READ
, a_srcPath
, errno
, strerror(errno
));
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
199 copyFile(int a_srcFd
, int a_dstFd
, char *a_srcPath
, char *a_dstPath
,
200 struct stat
*a_srcStatbuf
, long a_iosize
)
203 off_t filesize
= a_srcStatbuf
->st_size
;
205 size_t munmapsize
= 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
) {
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
250 char *buf
= (char *)NULL
;
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
,
272 /* copy the file contents */
277 /* read next block of data */
279 n
= read(a_srcFd
, buf
, blocksize
);
281 /* end of file - return success */
285 /* read error - return error */
286 progerr(ERR_READ
, a_srcPath
,
287 errno
, strerror(errno
));
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
));
305 * the source has been mapped into memory, copy via mappings
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
)) {
324 remains
= mapsize
- nbytes
;
325 while (remains
> 0) {
326 nbytes
= write(a_dstFd
,
327 (cp
+ mapsize
- remains
), remains
);
336 /* i/o error - report and exit */
338 if (errno
== ENOSPC
) {
339 progerr(ERR_WRITE
, a_dstPath
,
340 errno
, strerror(errno
));
342 progerr(ERR_READ
, a_srcPath
,
343 errno
, strerror(errno
));
346 /* unmap source file mapping */
347 (void) munmap(cp
, munmapsize
);
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
361 if (errno
== EACCES
) {
362 progerr(ERR_READ
, a_srcPath
,
363 errno
, strerror(errno
));
365 progerr(ERR_WRITE
, a_dstPath
,
366 errno
, strerror(errno
));
369 /* unmap source file mapping */
370 (void) munmap(cp
, munmapsize
);
380 if (filesize
< mapsize
) {
384 /* map next segment of file on top of existing mapping */
386 cp
= mmap(cp
, mapsize
, PROT_READ
, (MAP_SHARED
|MAP_FIXED
),
389 if (cp
== MAP_FAILED
) {
390 progerr(ERR_MAPFAILED
, a_srcPath
, errno
,
392 /* unmap source file mapping */
393 (void) munmap(cp
, munmapsize
);
398 /* unmap source file mapping */
400 (void) munmap(cp
, munmapsize
);
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
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.
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
432 openLocal(char *a_path
, int a_oflag
, char *a_tmpdir
)
435 char template[PATH_MAX
];
442 /* open source file */
444 fd
= open(a_path
, a_oflag
);
449 /* return open fd if the source file is not remote */
451 if (!isFdRemote(fd
)) {
456 * source file is remote - must make a local copy
459 /* get the source file's status */
461 n
= fstat(fd
, &statbuf
);
469 /* generate unique temporary file name */
471 if ((a_tmpdir
== (char *)NULL
) || (*a_tmpdir
== '\0') ||
472 (isdir(a_tmpdir
) != 0)) {
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)) {
484 /* create the temporary file and open it */
486 tmpFd
= mkstemp(template);
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);
509 /* return handle to temporary file created */