xref: /onnv-gate/usr/src/cmd/svr4pkg/libinst/copyf.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*9781SMoriah.Waterland@Sun.COM /* All Rights Reserved */
29*9781SMoriah.Waterland@Sun.COM 
30*9781SMoriah.Waterland@Sun.COM 
31*9781SMoriah.Waterland@Sun.COM 
32*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
33*9781SMoriah.Waterland@Sun.COM #include <string.h>
34*9781SMoriah.Waterland@Sun.COM #include <errno.h>
35*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
36*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
37*9781SMoriah.Waterland@Sun.COM #include <libgen.h>
38*9781SMoriah.Waterland@Sun.COM #include <fcntl.h>
39*9781SMoriah.Waterland@Sun.COM #include <sys/types.h>
40*9781SMoriah.Waterland@Sun.COM #include <utime.h>
41*9781SMoriah.Waterland@Sun.COM #include <sys/stat.h>
42*9781SMoriah.Waterland@Sun.COM #include <locale.h>
43*9781SMoriah.Waterland@Sun.COM #include <libintl.h>
44*9781SMoriah.Waterland@Sun.COM #include <sys/mman.h>
45*9781SMoriah.Waterland@Sun.COM 
46*9781SMoriah.Waterland@Sun.COM /*
47*9781SMoriah.Waterland@Sun.COM  * consolidation pkg command library includes
48*9781SMoriah.Waterland@Sun.COM  */
49*9781SMoriah.Waterland@Sun.COM 
50*9781SMoriah.Waterland@Sun.COM #include "pkglib.h"
51*9781SMoriah.Waterland@Sun.COM 
52*9781SMoriah.Waterland@Sun.COM /*
53*9781SMoriah.Waterland@Sun.COM  * local pkg command library includes
54*9781SMoriah.Waterland@Sun.COM  */
55*9781SMoriah.Waterland@Sun.COM #include "libinst.h"
56*9781SMoriah.Waterland@Sun.COM #include "libadm.h"
57*9781SMoriah.Waterland@Sun.COM #include "messages.h"
58*9781SMoriah.Waterland@Sun.COM 
59*9781SMoriah.Waterland@Sun.COM /*
60*9781SMoriah.Waterland@Sun.COM  * MAXMAPSIZE controls the largest mapping to use at a time; please refer
61*9781SMoriah.Waterland@Sun.COM  * to mmap(2) for details of how this size is incremented and rounded; briefly
62*9781SMoriah.Waterland@Sun.COM  * each mapping request has an additional 16Kb added to it - mappings over
63*9781SMoriah.Waterland@Sun.COM  * 4Mb will be rounded to a 4Mb boundary - thus if there were 8mb, adding
64*9781SMoriah.Waterland@Sun.COM  * in the 16Kb overhead the mapping would use another 4Mb-16kb - that is
65*9781SMoriah.Waterland@Sun.COM  * why there is 16Kb subtracted from the total
66*9781SMoriah.Waterland@Sun.COM  */
67*9781SMoriah.Waterland@Sun.COM 
68*9781SMoriah.Waterland@Sun.COM #define	MAXMAPSIZE	(1024*1024*8)-(1024*16)	/* map at most 8MB */
69*9781SMoriah.Waterland@Sun.COM #define	SMALLFILESIZE	(32*1024)	/* dont mmap files less than 32kb */
70*9781SMoriah.Waterland@Sun.COM 
71*9781SMoriah.Waterland@Sun.COM /*
72*9781SMoriah.Waterland@Sun.COM  * Name:	copyF
73*9781SMoriah.Waterland@Sun.COM  * Description:	fast copy of file - use mmap()/write() loop if possible
74*9781SMoriah.Waterland@Sun.COM  * Arguments:	char *srcPath - name of source file to copy from
75*9781SMoriah.Waterland@Sun.COM  *		char *dstPath - name of target file to copy to
76*9781SMoriah.Waterland@Sun.COM  *		time_t a_mytime: control setting of access/modification times:
77*9781SMoriah.Waterland@Sun.COM  *			== 0 - replicate source file access/modification times
78*9781SMoriah.Waterland@Sun.COM  *			!= 0 - use specified time for access/modification times
79*9781SMoriah.Waterland@Sun.COM  * Returns:	int
80*9781SMoriah.Waterland@Sun.COM  *		== 0 - successful
81*9781SMoriah.Waterland@Sun.COM  *		!= 0 - failure
82*9781SMoriah.Waterland@Sun.COM  */
83*9781SMoriah.Waterland@Sun.COM 
84*9781SMoriah.Waterland@Sun.COM int
copyf(char * a_srcPath,char * a_dstPath,time_t a_mytime)85*9781SMoriah.Waterland@Sun.COM copyf(char *a_srcPath, char *a_dstPath, time_t a_mytime)
86*9781SMoriah.Waterland@Sun.COM {
87*9781SMoriah.Waterland@Sun.COM 	struct stat	srcStatbuf;
88*9781SMoriah.Waterland@Sun.COM 	struct utimbuf	times;
89*9781SMoriah.Waterland@Sun.COM 	int		srcFd;
90*9781SMoriah.Waterland@Sun.COM 	int		dstFd;
91*9781SMoriah.Waterland@Sun.COM 	int		status;
92*9781SMoriah.Waterland@Sun.COM 	char		*pt;
93*9781SMoriah.Waterland@Sun.COM 
94*9781SMoriah.Waterland@Sun.COM 	/* open source file for reading */
95*9781SMoriah.Waterland@Sun.COM 
96*9781SMoriah.Waterland@Sun.COM 	srcFd = open(a_srcPath, O_RDONLY, 0);
97*9781SMoriah.Waterland@Sun.COM 	if (srcFd < 0) {
98*9781SMoriah.Waterland@Sun.COM 		progerr(ERR_OPEN_READ, a_srcPath, errno, strerror(errno));
99*9781SMoriah.Waterland@Sun.COM 		return (-1);
100*9781SMoriah.Waterland@Sun.COM 	}
101*9781SMoriah.Waterland@Sun.COM 
102*9781SMoriah.Waterland@Sun.COM 	/* obtain file status of source file */
103*9781SMoriah.Waterland@Sun.COM 
104*9781SMoriah.Waterland@Sun.COM 	if (fstat(srcFd, &srcStatbuf) != 0) {
105*9781SMoriah.Waterland@Sun.COM 		progerr(ERR_FSTAT, srcFd, a_srcPath, errno, strerror(errno));
106*9781SMoriah.Waterland@Sun.COM 		(void) close(srcFd);
107*9781SMoriah.Waterland@Sun.COM 		return (-1);
108*9781SMoriah.Waterland@Sun.COM 	}
109*9781SMoriah.Waterland@Sun.COM 
110*9781SMoriah.Waterland@Sun.COM 	/* open target file for writing */
111*9781SMoriah.Waterland@Sun.COM 
112*9781SMoriah.Waterland@Sun.COM 	dstFd = open(a_dstPath, O_WRONLY | O_TRUNC | O_CREAT,
113*9781SMoriah.Waterland@Sun.COM 		srcStatbuf.st_mode);
114*9781SMoriah.Waterland@Sun.COM 	if (dstFd < 0) {
115*9781SMoriah.Waterland@Sun.COM 		/* create directory structure if missing */
116*9781SMoriah.Waterland@Sun.COM 		pt = a_dstPath;
117*9781SMoriah.Waterland@Sun.COM 		while (pt = strchr(pt+1, '/')) {
118*9781SMoriah.Waterland@Sun.COM 			*pt = '\0';
119*9781SMoriah.Waterland@Sun.COM 			if (isdir(a_dstPath)) {
120*9781SMoriah.Waterland@Sun.COM 				if (mkdir(a_dstPath, 0755)) {
121*9781SMoriah.Waterland@Sun.COM 					progerr(ERR_NODIR, a_dstPath,
122*9781SMoriah.Waterland@Sun.COM 						errno, strerror(errno));
123*9781SMoriah.Waterland@Sun.COM 					*pt = '/';
124*9781SMoriah.Waterland@Sun.COM 					(void) close(srcFd);
125*9781SMoriah.Waterland@Sun.COM 					return (-1);
126*9781SMoriah.Waterland@Sun.COM 				}
127*9781SMoriah.Waterland@Sun.COM 			}
128*9781SMoriah.Waterland@Sun.COM 			*pt = '/';
129*9781SMoriah.Waterland@Sun.COM 		}
130*9781SMoriah.Waterland@Sun.COM 
131*9781SMoriah.Waterland@Sun.COM 		/* attempt to create target file again */
132*9781SMoriah.Waterland@Sun.COM 		dstFd = open(a_dstPath, O_WRONLY | O_TRUNC | O_CREAT,
133*9781SMoriah.Waterland@Sun.COM 				srcStatbuf.st_mode);
134*9781SMoriah.Waterland@Sun.COM 		if (dstFd < 0) {
135*9781SMoriah.Waterland@Sun.COM 			progerr(ERR_OPEN_WRITE, a_dstPath, errno,
136*9781SMoriah.Waterland@Sun.COM 							strerror(errno));
137*9781SMoriah.Waterland@Sun.COM 			(void) close(srcFd);
138*9781SMoriah.Waterland@Sun.COM 			return (-1);
139*9781SMoriah.Waterland@Sun.COM 		}
140*9781SMoriah.Waterland@Sun.COM 	}
141*9781SMoriah.Waterland@Sun.COM 
142*9781SMoriah.Waterland@Sun.COM 	/*
143*9781SMoriah.Waterland@Sun.COM 	 * source and target files are open: copy data
144*9781SMoriah.Waterland@Sun.COM 	 */
145*9781SMoriah.Waterland@Sun.COM 
146*9781SMoriah.Waterland@Sun.COM 	status = copyFile(srcFd, dstFd, a_srcPath, a_dstPath, &srcStatbuf, 0);
147*9781SMoriah.Waterland@Sun.COM 
148*9781SMoriah.Waterland@Sun.COM 	(void) close(srcFd);
149*9781SMoriah.Waterland@Sun.COM 	(void) close(dstFd);
150*9781SMoriah.Waterland@Sun.COM 
151*9781SMoriah.Waterland@Sun.COM 	/*
152*9781SMoriah.Waterland@Sun.COM 	 * determine how to set access/modification times for target:
153*9781SMoriah.Waterland@Sun.COM 	 * -- a_mytime == 0: replicate source file access/modification times
154*9781SMoriah.Waterland@Sun.COM 	 * -- otherwise: use a_mytime for file access/modification times
155*9781SMoriah.Waterland@Sun.COM 	 */
156*9781SMoriah.Waterland@Sun.COM 
157*9781SMoriah.Waterland@Sun.COM 	if (a_mytime == 0) {
158*9781SMoriah.Waterland@Sun.COM 		times.actime = srcStatbuf.st_atime;
159*9781SMoriah.Waterland@Sun.COM 		times.modtime = srcStatbuf.st_mtime;
160*9781SMoriah.Waterland@Sun.COM 	} else {
161*9781SMoriah.Waterland@Sun.COM 		times.actime = a_mytime;
162*9781SMoriah.Waterland@Sun.COM 		times.modtime = a_mytime;
163*9781SMoriah.Waterland@Sun.COM 	}
164*9781SMoriah.Waterland@Sun.COM 
165*9781SMoriah.Waterland@Sun.COM 	/* set access/modification times for target */
166*9781SMoriah.Waterland@Sun.COM 
167*9781SMoriah.Waterland@Sun.COM 	if (utime(a_dstPath, &times) != 0) {
168*9781SMoriah.Waterland@Sun.COM 		progerr(ERR_MODTIM, a_dstPath, errno, strerror(errno));
169*9781SMoriah.Waterland@Sun.COM 		return (-1);
170*9781SMoriah.Waterland@Sun.COM 	}
171*9781SMoriah.Waterland@Sun.COM 
172*9781SMoriah.Waterland@Sun.COM 	/* return error if copy failed */
173*9781SMoriah.Waterland@Sun.COM 
174*9781SMoriah.Waterland@Sun.COM 	if (status != 0) {
175*9781SMoriah.Waterland@Sun.COM 		progerr(ERR_READ, a_srcPath, errno, strerror(errno));
176*9781SMoriah.Waterland@Sun.COM 		return (-1);
177*9781SMoriah.Waterland@Sun.COM 	}
178*9781SMoriah.Waterland@Sun.COM 
179*9781SMoriah.Waterland@Sun.COM 	/* success! */
180*9781SMoriah.Waterland@Sun.COM 
181*9781SMoriah.Waterland@Sun.COM 	return (0);
182*9781SMoriah.Waterland@Sun.COM }
183*9781SMoriah.Waterland@Sun.COM 
184*9781SMoriah.Waterland@Sun.COM /*
185*9781SMoriah.Waterland@Sun.COM  * Name:	copyFile
186*9781SMoriah.Waterland@Sun.COM  * Description:	fast copy of file - use mmap()/write() loop if possible
187*9781SMoriah.Waterland@Sun.COM  * Arguments:	int srcFd - file descriptor open on source file
188*9781SMoriah.Waterland@Sun.COM  *		int dstFd - file descriptor open on target file
189*9781SMoriah.Waterland@Sun.COM  *		char *srcPath - name of source file (for error messages)
190*9781SMoriah.Waterland@Sun.COM  *		char *dstPath - name of target file (for error messages)
191*9781SMoriah.Waterland@Sun.COM  *		struct stat *a_srcStatbuf - stat structure for source file
192*9781SMoriah.Waterland@Sun.COM  *		long a_iosize - preferred io size for read/write loop
193*9781SMoriah.Waterland@Sun.COM  * Returns:	int
194*9781SMoriah.Waterland@Sun.COM  *		== 0 - successful
195*9781SMoriah.Waterland@Sun.COM  *		!= 0 - failure
196*9781SMoriah.Waterland@Sun.COM  */
197*9781SMoriah.Waterland@Sun.COM 
198*9781SMoriah.Waterland@Sun.COM int
copyFile(int a_srcFd,int a_dstFd,char * a_srcPath,char * a_dstPath,struct stat * a_srcStatbuf,long a_iosize)199*9781SMoriah.Waterland@Sun.COM copyFile(int a_srcFd, int a_dstFd, char *a_srcPath, char *a_dstPath,
200*9781SMoriah.Waterland@Sun.COM 	struct stat *a_srcStatbuf, long a_iosize)
201*9781SMoriah.Waterland@Sun.COM {
202*9781SMoriah.Waterland@Sun.COM 	caddr_t	cp;
203*9781SMoriah.Waterland@Sun.COM 	off_t	filesize = a_srcStatbuf->st_size;
204*9781SMoriah.Waterland@Sun.COM 	size_t	mapsize = 0;
205*9781SMoriah.Waterland@Sun.COM 	size_t	munmapsize = 0;
206*9781SMoriah.Waterland@Sun.COM 	off_t	offset = 0;
207*9781SMoriah.Waterland@Sun.COM 
208*9781SMoriah.Waterland@Sun.COM 	echoDebug(DBG_COPY_FILE, a_srcPath, a_dstPath);
209*9781SMoriah.Waterland@Sun.COM 
210*9781SMoriah.Waterland@Sun.COM 	/*
211*9781SMoriah.Waterland@Sun.COM 	 * if the source is a regular file and is not "too small", then cause
212*9781SMoriah.Waterland@Sun.COM 	 * the file to be mapped into memory
213*9781SMoriah.Waterland@Sun.COM 	 */
214*9781SMoriah.Waterland@Sun.COM 
215*9781SMoriah.Waterland@Sun.COM 	if (S_ISREG(a_srcStatbuf->st_mode) && (filesize > SMALLFILESIZE)) {
216*9781SMoriah.Waterland@Sun.COM 		/*
217*9781SMoriah.Waterland@Sun.COM 		 * Determine size of initial mapping.  This will determine the
218*9781SMoriah.Waterland@Sun.COM 		 * size of the address space chunk we work with.  This initial
219*9781SMoriah.Waterland@Sun.COM 		 * mapping size will be used to perform munmap() in the future.
220*9781SMoriah.Waterland@Sun.COM 		 */
221*9781SMoriah.Waterland@Sun.COM 
222*9781SMoriah.Waterland@Sun.COM 		mapsize = MAXMAPSIZE;
223*9781SMoriah.Waterland@Sun.COM 		if (filesize < mapsize) {
224*9781SMoriah.Waterland@Sun.COM 			mapsize = filesize;
225*9781SMoriah.Waterland@Sun.COM 		}
226*9781SMoriah.Waterland@Sun.COM 
227*9781SMoriah.Waterland@Sun.COM 		/*
228*9781SMoriah.Waterland@Sun.COM 		 * remember size of mapping to "unmap" - if the source file
229*9781SMoriah.Waterland@Sun.COM 		 * exceeds MAXMAPSIZE bytes, then the final mapping of the
230*9781SMoriah.Waterland@Sun.COM 		 * source file will be less than MAXMAPSIZE, and we need to
231*9781SMoriah.Waterland@Sun.COM 		 * make sure that the entire mapping is unmapped when done.
232*9781SMoriah.Waterland@Sun.COM 		 */
233*9781SMoriah.Waterland@Sun.COM 
234*9781SMoriah.Waterland@Sun.COM 		munmapsize = mapsize;
235*9781SMoriah.Waterland@Sun.COM 
236*9781SMoriah.Waterland@Sun.COM 		/* map the first segment of the source into memory */
237*9781SMoriah.Waterland@Sun.COM 
238*9781SMoriah.Waterland@Sun.COM 		cp = mmap((caddr_t)NULL, mapsize, PROT_READ,
239*9781SMoriah.Waterland@Sun.COM 			(MAP_SHARED|MAP_ALIGN), a_srcFd, (off_t)0);
240*9781SMoriah.Waterland@Sun.COM 		if (cp == MAP_FAILED) {
241*9781SMoriah.Waterland@Sun.COM 			mapsize = 0;   /* can't mmap today */
242*9781SMoriah.Waterland@Sun.COM 		}
243*9781SMoriah.Waterland@Sun.COM 	}
244*9781SMoriah.Waterland@Sun.COM 
245*9781SMoriah.Waterland@Sun.COM 	/*
246*9781SMoriah.Waterland@Sun.COM 	 * if the source was not mapped into memory, copy via read/write loop
247*9781SMoriah.Waterland@Sun.COM 	 */
248*9781SMoriah.Waterland@Sun.COM 
249*9781SMoriah.Waterland@Sun.COM 	if (mapsize == 0) {
250*9781SMoriah.Waterland@Sun.COM 		char	*buf = (char *)NULL;
251*9781SMoriah.Waterland@Sun.COM 		size_t	blocksize;
252*9781SMoriah.Waterland@Sun.COM 		int	pagesize = getpagesize();
253*9781SMoriah.Waterland@Sun.COM 
254*9781SMoriah.Waterland@Sun.COM 		/* set blocksize for copy */
255*9781SMoriah.Waterland@Sun.COM 
256*9781SMoriah.Waterland@Sun.COM 		blocksize = a_iosize;
257*9781SMoriah.Waterland@Sun.COM 		if ((blocksize == 0) || (blocksize > SMALLFILESIZE)) {
258*9781SMoriah.Waterland@Sun.COM 			blocksize = SMALLFILESIZE;
259*9781SMoriah.Waterland@Sun.COM 		} else if (blocksize < pagesize) {
260*9781SMoriah.Waterland@Sun.COM 			blocksize = pagesize;
261*9781SMoriah.Waterland@Sun.COM 		}
262*9781SMoriah.Waterland@Sun.COM 
263*9781SMoriah.Waterland@Sun.COM 		/* allocate i/o transfer buffer */
264*9781SMoriah.Waterland@Sun.COM 
265*9781SMoriah.Waterland@Sun.COM 		buf = memalign((size_t)pagesize, blocksize);
266*9781SMoriah.Waterland@Sun.COM 		if (buf == (char *)NULL) {
267*9781SMoriah.Waterland@Sun.COM 			progerr(ERR_COPY_MEMORY, a_srcPath, errno,
268*9781SMoriah.Waterland@Sun.COM 				strerror(errno));
269*9781SMoriah.Waterland@Sun.COM 			return (1);
270*9781SMoriah.Waterland@Sun.COM 		}
271*9781SMoriah.Waterland@Sun.COM 
272*9781SMoriah.Waterland@Sun.COM 		/* copy the file contents */
273*9781SMoriah.Waterland@Sun.COM 
274*9781SMoriah.Waterland@Sun.COM 		for (;;) {
275*9781SMoriah.Waterland@Sun.COM 			ssize_t	n;
276*9781SMoriah.Waterland@Sun.COM 
277*9781SMoriah.Waterland@Sun.COM 			/* read next block of data */
278*9781SMoriah.Waterland@Sun.COM 
279*9781SMoriah.Waterland@Sun.COM 			n = read(a_srcFd, buf, blocksize);
280*9781SMoriah.Waterland@Sun.COM 			if (n == 0) {
281*9781SMoriah.Waterland@Sun.COM 				/* end of file - return success */
282*9781SMoriah.Waterland@Sun.COM 				(void) free(buf);
283*9781SMoriah.Waterland@Sun.COM 				return (0);
284*9781SMoriah.Waterland@Sun.COM 			} else if (n < 0) {
285*9781SMoriah.Waterland@Sun.COM 				/* read error - return error */
286*9781SMoriah.Waterland@Sun.COM 				progerr(ERR_READ, a_srcPath,
287*9781SMoriah.Waterland@Sun.COM 						errno, strerror(errno));
288*9781SMoriah.Waterland@Sun.COM 				(void) free(buf);
289*9781SMoriah.Waterland@Sun.COM 				return (1);
290*9781SMoriah.Waterland@Sun.COM 			}
291*9781SMoriah.Waterland@Sun.COM 
292*9781SMoriah.Waterland@Sun.COM 			/* write out block of data just read in */
293*9781SMoriah.Waterland@Sun.COM 
294*9781SMoriah.Waterland@Sun.COM 			if (vfpSafeWrite(a_dstFd, buf, (size_t)n) != n) {
295*9781SMoriah.Waterland@Sun.COM 				/* short write/write error - return error */
296*9781SMoriah.Waterland@Sun.COM 				progerr(ERR_WRITE, a_dstPath,
297*9781SMoriah.Waterland@Sun.COM 						errno, strerror(errno));
298*9781SMoriah.Waterland@Sun.COM 				(void) free(buf);
299*9781SMoriah.Waterland@Sun.COM 				return (1);
300*9781SMoriah.Waterland@Sun.COM 			}
301*9781SMoriah.Waterland@Sun.COM 		}
302*9781SMoriah.Waterland@Sun.COM 	}
303*9781SMoriah.Waterland@Sun.COM 
304*9781SMoriah.Waterland@Sun.COM 	/*
305*9781SMoriah.Waterland@Sun.COM 	 * the source has been mapped into memory, copy via mappings
306*9781SMoriah.Waterland@Sun.COM 	 */
307*9781SMoriah.Waterland@Sun.COM 
308*9781SMoriah.Waterland@Sun.COM 	for (;;) {
309*9781SMoriah.Waterland@Sun.COM 		ssize_t	nbytes;
310*9781SMoriah.Waterland@Sun.COM 
311*9781SMoriah.Waterland@Sun.COM 		/* write first mappings worth of data */
312*9781SMoriah.Waterland@Sun.COM 
313*9781SMoriah.Waterland@Sun.COM 		nbytes = write(a_dstFd, cp, mapsize);
314*9781SMoriah.Waterland@Sun.COM 
315*9781SMoriah.Waterland@Sun.COM 		/*
316*9781SMoriah.Waterland@Sun.COM 		 * if we write less than the mmaped size it's due to a
317*9781SMoriah.Waterland@Sun.COM 		 * media error on the input file or out of space on
318*9781SMoriah.Waterland@Sun.COM 		 * the output file.  So, try again, and look for errno.
319*9781SMoriah.Waterland@Sun.COM 		 */
320*9781SMoriah.Waterland@Sun.COM 
321*9781SMoriah.Waterland@Sun.COM 		if ((nbytes >= 0) && (nbytes != (ssize_t)mapsize)) {
322*9781SMoriah.Waterland@Sun.COM 			size_t	remains;
323*9781SMoriah.Waterland@Sun.COM 
324*9781SMoriah.Waterland@Sun.COM 			remains = mapsize - nbytes;
325*9781SMoriah.Waterland@Sun.COM 			while (remains > 0) {
326*9781SMoriah.Waterland@Sun.COM 				nbytes = write(a_dstFd,
327*9781SMoriah.Waterland@Sun.COM 					(cp + mapsize - remains), remains);
328*9781SMoriah.Waterland@Sun.COM 				if (nbytes >= 0) {
329*9781SMoriah.Waterland@Sun.COM 					remains -= nbytes;
330*9781SMoriah.Waterland@Sun.COM 					if (remains == 0) {
331*9781SMoriah.Waterland@Sun.COM 						nbytes = mapsize;
332*9781SMoriah.Waterland@Sun.COM 					}
333*9781SMoriah.Waterland@Sun.COM 					continue;
334*9781SMoriah.Waterland@Sun.COM 				}
335*9781SMoriah.Waterland@Sun.COM 
336*9781SMoriah.Waterland@Sun.COM 				/* i/o error - report and exit */
337*9781SMoriah.Waterland@Sun.COM 
338*9781SMoriah.Waterland@Sun.COM 				if (errno == ENOSPC) {
339*9781SMoriah.Waterland@Sun.COM 					progerr(ERR_WRITE, a_dstPath,
340*9781SMoriah.Waterland@Sun.COM 						errno, strerror(errno));
341*9781SMoriah.Waterland@Sun.COM 				} else {
342*9781SMoriah.Waterland@Sun.COM 					progerr(ERR_READ, a_srcPath,
343*9781SMoriah.Waterland@Sun.COM 						errno, strerror(errno));
344*9781SMoriah.Waterland@Sun.COM 				}
345*9781SMoriah.Waterland@Sun.COM 
346*9781SMoriah.Waterland@Sun.COM 				/* unmap source file mapping */
347*9781SMoriah.Waterland@Sun.COM 				(void) munmap(cp, munmapsize);
348*9781SMoriah.Waterland@Sun.COM 				return (1);
349*9781SMoriah.Waterland@Sun.COM 			}
350*9781SMoriah.Waterland@Sun.COM 		}
351*9781SMoriah.Waterland@Sun.COM 
352*9781SMoriah.Waterland@Sun.COM 		/*
353*9781SMoriah.Waterland@Sun.COM 		 * although the write manual page doesn't specify this
354*9781SMoriah.Waterland@Sun.COM 		 * as a possible errno, it is set when the nfs read
355*9781SMoriah.Waterland@Sun.COM 		 * via the mmap'ed file is accessed, so report the
356*9781SMoriah.Waterland@Sun.COM 		 * problem as a source access problem, not a target file
357*9781SMoriah.Waterland@Sun.COM 		 * problem
358*9781SMoriah.Waterland@Sun.COM 		 */
359*9781SMoriah.Waterland@Sun.COM 
360*9781SMoriah.Waterland@Sun.COM 		if (nbytes < 0) {
361*9781SMoriah.Waterland@Sun.COM 			if (errno == EACCES) {
362*9781SMoriah.Waterland@Sun.COM 				progerr(ERR_READ, a_srcPath,
363*9781SMoriah.Waterland@Sun.COM 					errno, strerror(errno));
364*9781SMoriah.Waterland@Sun.COM 			} else {
365*9781SMoriah.Waterland@Sun.COM 				progerr(ERR_WRITE, a_dstPath,
366*9781SMoriah.Waterland@Sun.COM 					errno, strerror(errno));
367*9781SMoriah.Waterland@Sun.COM 			}
368*9781SMoriah.Waterland@Sun.COM 
369*9781SMoriah.Waterland@Sun.COM 			/* unmap source file mapping */
370*9781SMoriah.Waterland@Sun.COM 			(void) munmap(cp, munmapsize);
371*9781SMoriah.Waterland@Sun.COM 			return (1);
372*9781SMoriah.Waterland@Sun.COM 		}
373*9781SMoriah.Waterland@Sun.COM 
374*9781SMoriah.Waterland@Sun.COM 		filesize -= nbytes;
375*9781SMoriah.Waterland@Sun.COM 		if (filesize == 0) {
376*9781SMoriah.Waterland@Sun.COM 			break;
377*9781SMoriah.Waterland@Sun.COM 		}
378*9781SMoriah.Waterland@Sun.COM 
379*9781SMoriah.Waterland@Sun.COM 		offset += nbytes;
380*9781SMoriah.Waterland@Sun.COM 		if (filesize < mapsize) {
381*9781SMoriah.Waterland@Sun.COM 			mapsize = filesize;
382*9781SMoriah.Waterland@Sun.COM 		}
383*9781SMoriah.Waterland@Sun.COM 
384*9781SMoriah.Waterland@Sun.COM 		/* map next segment of file on top of existing mapping */
385*9781SMoriah.Waterland@Sun.COM 
386*9781SMoriah.Waterland@Sun.COM 		cp = mmap(cp, mapsize, PROT_READ, (MAP_SHARED|MAP_FIXED),
387*9781SMoriah.Waterland@Sun.COM 			a_srcFd, offset);
388*9781SMoriah.Waterland@Sun.COM 
389*9781SMoriah.Waterland@Sun.COM 		if (cp == MAP_FAILED) {
390*9781SMoriah.Waterland@Sun.COM 			progerr(ERR_MAPFAILED, a_srcPath, errno,
391*9781SMoriah.Waterland@Sun.COM 						strerror(errno));
392*9781SMoriah.Waterland@Sun.COM 			/* unmap source file mapping */
393*9781SMoriah.Waterland@Sun.COM 			(void) munmap(cp, munmapsize);
394*9781SMoriah.Waterland@Sun.COM 			return (1);
395*9781SMoriah.Waterland@Sun.COM 		}
396*9781SMoriah.Waterland@Sun.COM 	}
397*9781SMoriah.Waterland@Sun.COM 
398*9781SMoriah.Waterland@Sun.COM 	/* unmap source file mapping */
399*9781SMoriah.Waterland@Sun.COM 
400*9781SMoriah.Waterland@Sun.COM 	(void) munmap(cp, munmapsize);
401*9781SMoriah.Waterland@Sun.COM 
402*9781SMoriah.Waterland@Sun.COM 	return (0);
403*9781SMoriah.Waterland@Sun.COM }
404*9781SMoriah.Waterland@Sun.COM 
405*9781SMoriah.Waterland@Sun.COM /*
406*9781SMoriah.Waterland@Sun.COM  * Name:	openLocal
407*9781SMoriah.Waterland@Sun.COM  * Description:	open a file and assure that the descriptor returned is open on
408*9781SMoriah.Waterland@Sun.COM  *		a file that is local to the current system - if the file is not
409*9781SMoriah.Waterland@Sun.COM  *		local to this system, copy the file to a temporary file first,
410*9781SMoriah.Waterland@Sun.COM  *		and then pass a handle back opened on the temporary file
411*9781SMoriah.Waterland@Sun.COM  * Arguments:	a_path - [RO, *RO] - (char *)
412*9781SMoriah.Waterland@Sun.COM  *			Pointer to string representing the path to the file
413*9781SMoriah.Waterland@Sun.COM  *			to open
414*9781SMoriah.Waterland@Sun.COM  *		a_oflag - [RO, *RO] - (int)
415*9781SMoriah.Waterland@Sun.COM  *			Integer representing the "mode" bits for an open(2) call
416*9781SMoriah.Waterland@Sun.COM  *		a_tmpdir - [RO, *RO] - (char *)
417*9781SMoriah.Waterland@Sun.COM  *			Pointer to string representing the path to a directory
418*9781SMoriah.Waterland@Sun.COM  *			where a temporary copy of the file can be placed if
419*9781SMoriah.Waterland@Sun.COM  *			the source file is not local to this system. If this is
420*9781SMoriah.Waterland@Sun.COM  *			NULL or does not exist, P_tmpdir is used.
421*9781SMoriah.Waterland@Sun.COM  * Returns:	int
422*9781SMoriah.Waterland@Sun.COM  *			>= 0 - file descriptor opened on the file
423*9781SMoriah.Waterland@Sun.COM  *			== -1 - failed to open - errno contains error code
424*9781SMoriah.Waterland@Sun.COM  * NOTE:	If the file is not local and is copied locally, the file is
425*9781SMoriah.Waterland@Sun.COM  *		setup in such a way that it will be removed when the last
426*9781SMoriah.Waterland@Sun.COM  *		file descriptor opened on the file is closed - there is no need
427*9781SMoriah.Waterland@Sun.COM  *		to know the path to the temporary file or to remove it
428*9781SMoriah.Waterland@Sun.COM  *		when done.
429*9781SMoriah.Waterland@Sun.COM  */
430*9781SMoriah.Waterland@Sun.COM 
431*9781SMoriah.Waterland@Sun.COM int
openLocal(char * a_path,int a_oflag,char * a_tmpdir)432*9781SMoriah.Waterland@Sun.COM openLocal(char *a_path, int a_oflag, char *a_tmpdir)
433*9781SMoriah.Waterland@Sun.COM {
434*9781SMoriah.Waterland@Sun.COM 	char		*bn;
435*9781SMoriah.Waterland@Sun.COM 	char		template[PATH_MAX];
436*9781SMoriah.Waterland@Sun.COM 	int		fd;
437*9781SMoriah.Waterland@Sun.COM 	int		lerrno;
438*9781SMoriah.Waterland@Sun.COM 	int		n;
439*9781SMoriah.Waterland@Sun.COM 	int		tmpFd;
440*9781SMoriah.Waterland@Sun.COM 	struct stat	statbuf;
441*9781SMoriah.Waterland@Sun.COM 
442*9781SMoriah.Waterland@Sun.COM 	/* open source file */
443*9781SMoriah.Waterland@Sun.COM 
444*9781SMoriah.Waterland@Sun.COM 	fd = open(a_path, a_oflag);
445*9781SMoriah.Waterland@Sun.COM 	if (fd < 0) {
446*9781SMoriah.Waterland@Sun.COM 		return (fd);
447*9781SMoriah.Waterland@Sun.COM 	}
448*9781SMoriah.Waterland@Sun.COM 
449*9781SMoriah.Waterland@Sun.COM 	/* return open fd if the source file is not remote */
450*9781SMoriah.Waterland@Sun.COM 
451*9781SMoriah.Waterland@Sun.COM 	if (!isFdRemote(fd)) {
452*9781SMoriah.Waterland@Sun.COM 		return (fd);
453*9781SMoriah.Waterland@Sun.COM 	}
454*9781SMoriah.Waterland@Sun.COM 
455*9781SMoriah.Waterland@Sun.COM 	/*
456*9781SMoriah.Waterland@Sun.COM 	 * source file is remote - must make a local copy
457*9781SMoriah.Waterland@Sun.COM 	 */
458*9781SMoriah.Waterland@Sun.COM 
459*9781SMoriah.Waterland@Sun.COM 	/* get the source file's status */
460*9781SMoriah.Waterland@Sun.COM 
461*9781SMoriah.Waterland@Sun.COM 	n = fstat(fd, &statbuf);
462*9781SMoriah.Waterland@Sun.COM 	if (n < 0) {
463*9781SMoriah.Waterland@Sun.COM 		lerrno = errno;
464*9781SMoriah.Waterland@Sun.COM 		(void) close(fd);
465*9781SMoriah.Waterland@Sun.COM 		errno = lerrno;
466*9781SMoriah.Waterland@Sun.COM 		return (-1);
467*9781SMoriah.Waterland@Sun.COM 	}
468*9781SMoriah.Waterland@Sun.COM 
469*9781SMoriah.Waterland@Sun.COM 	/* generate unique temporary file name */
470*9781SMoriah.Waterland@Sun.COM 
471*9781SMoriah.Waterland@Sun.COM 	if ((a_tmpdir == (char *)NULL) || (*a_tmpdir == '\0') ||
472*9781SMoriah.Waterland@Sun.COM 		(isdir(a_tmpdir) != 0)) {
473*9781SMoriah.Waterland@Sun.COM 		a_tmpdir = P_tmpdir;
474*9781SMoriah.Waterland@Sun.COM 	}
475*9781SMoriah.Waterland@Sun.COM 	bn = basename(a_path);
476*9781SMoriah.Waterland@Sun.COM 	n = strlen(a_tmpdir);
477*9781SMoriah.Waterland@Sun.COM 	n = snprintf(template, sizeof (template), "%s%s%sXXXXXX",
478*9781SMoriah.Waterland@Sun.COM 		a_tmpdir, a_tmpdir[n-1] == '/' ? "" : "/", bn);
479*9781SMoriah.Waterland@Sun.COM 	if (n > sizeof (template)) {
480*9781SMoriah.Waterland@Sun.COM 		(void) close(fd);
481*9781SMoriah.Waterland@Sun.COM 		return (EINVAL);
482*9781SMoriah.Waterland@Sun.COM 	}
483*9781SMoriah.Waterland@Sun.COM 
484*9781SMoriah.Waterland@Sun.COM 	/* create the temporary file and open it */
485*9781SMoriah.Waterland@Sun.COM 
486*9781SMoriah.Waterland@Sun.COM 	tmpFd = mkstemp(template);
487*9781SMoriah.Waterland@Sun.COM 	if (tmpFd < 0) {
488*9781SMoriah.Waterland@Sun.COM 		lerrno = errno;
489*9781SMoriah.Waterland@Sun.COM 		(void) close(fd);
490*9781SMoriah.Waterland@Sun.COM 		errno = lerrno;
491*9781SMoriah.Waterland@Sun.COM 		return (tmpFd);
492*9781SMoriah.Waterland@Sun.COM 	}
493*9781SMoriah.Waterland@Sun.COM 
494*9781SMoriah.Waterland@Sun.COM 	/* unlink the file so when it is closed it is automatically deleted */
495*9781SMoriah.Waterland@Sun.COM 
496*9781SMoriah.Waterland@Sun.COM 	(void) unlink(template);
497*9781SMoriah.Waterland@Sun.COM 
498*9781SMoriah.Waterland@Sun.COM 	/* copy the source file to the temporary file */
499*9781SMoriah.Waterland@Sun.COM 
500*9781SMoriah.Waterland@Sun.COM 	n = copyFile(fd, tmpFd, a_path, template, &statbuf, 0L);
501*9781SMoriah.Waterland@Sun.COM 	lerrno = errno;
502*9781SMoriah.Waterland@Sun.COM 	(void) close(fd);
503*9781SMoriah.Waterland@Sun.COM 	if (n != 0) {
504*9781SMoriah.Waterland@Sun.COM 		(void) close(tmpFd);
505*9781SMoriah.Waterland@Sun.COM 		errno = lerrno;
506*9781SMoriah.Waterland@Sun.COM 		return (-1);
507*9781SMoriah.Waterland@Sun.COM 	}
508*9781SMoriah.Waterland@Sun.COM 
509*9781SMoriah.Waterland@Sun.COM 	/* return handle to temporary file created */
510*9781SMoriah.Waterland@Sun.COM 
511*9781SMoriah.Waterland@Sun.COM 	return (tmpFd);
512*9781SMoriah.Waterland@Sun.COM }
513