1*5331Samw /*
2*5331Samw  * CDDL HEADER START
3*5331Samw  *
4*5331Samw  * The contents of this file are subject to the terms of the
5*5331Samw  * Common Development and Distribution License (the "License").
6*5331Samw  * You may not use this file except in compliance with the License.
7*5331Samw  *
8*5331Samw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5331Samw  * or http://www.opensolaris.org/os/licensing.
10*5331Samw  * See the License for the specific language governing permissions
11*5331Samw  * and limitations under the License.
12*5331Samw  *
13*5331Samw  * When distributing Covered Code, include this CDDL HEADER in each
14*5331Samw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5331Samw  * If applicable, add the following below this CDDL HEADER, with the
16*5331Samw  * fields enclosed by brackets "[]" replaced with your own identifying
17*5331Samw  * information: Portions Copyright [yyyy] [name of copyright owner]
18*5331Samw  *
19*5331Samw  * CDDL HEADER END
20*5331Samw  */
21*5331Samw /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22*5331Samw /*	  All Rights Reserved  	*/
23*5331Samw 
24*5331Samw /*
25*5331Samw  * University Copyright- Copyright (c) 1982, 1986, 1988
26*5331Samw  * The Regents of the University of California
27*5331Samw  * All Rights Reserved
28*5331Samw  *
29*5331Samw  * University Acknowledgment- Portions of this document are derived from
30*5331Samw  * software developed by the University of California, Berkeley, and its
31*5331Samw  * contributors.
32*5331Samw  */
33*5331Samw /*
34*5331Samw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
35*5331Samw  * Use is subject to license terms.
36*5331Samw  */
37*5331Samw 
38*5331Samw #pragma ident	"%Z%%M%	%I%	%E% SMI"
39*5331Samw 
40*5331Samw #include <c_synonyms.h>
41*5331Samw #include "libcmdutils.h"
42*5331Samw 
43*5331Samw 
44*5331Samw int
45*5331Samw writefile(int fi, int fo, char *infile, char *outfile, char *asfile,
46*5331Samw     char *atfile, struct stat *s1p, struct stat *s2p)
47*5331Samw {
48*5331Samw 	int mapsize, munmapsize;
49*5331Samw 	caddr_t cp;
50*5331Samw 	off_t filesize = s1p->st_size;
51*5331Samw 	off_t offset;
52*5331Samw 	int nbytes;
53*5331Samw 	int remains;
54*5331Samw 	int n;
55*5331Samw 	size_t src_size;
56*5331Samw 	size_t targ_size;
57*5331Samw 	char *srcbuf;
58*5331Samw 	char *targbuf;
59*5331Samw 
60*5331Samw 	if (asfile != NULL) {
61*5331Samw 		src_size = strlen(infile) + strlen(asfile) +
62*5331Samw 		    strlen(dgettext(TEXT_DOMAIN, " attribute ")) + 1;
63*5331Samw 	} else {
64*5331Samw 		src_size = strlen(infile) + 1;
65*5331Samw 	}
66*5331Samw 	srcbuf = malloc(src_size);
67*5331Samw 	if (srcbuf == NULL) {
68*5331Samw 		(void) fprintf(stderr,
69*5331Samw 		    dgettext(TEXT_DOMAIN, "could not allocate memory"
70*5331Samw 		    " for path buffer: "));
71*5331Samw 		return (1);
72*5331Samw 	}
73*5331Samw 	if (asfile != NULL) {
74*5331Samw 		(void) snprintf(srcbuf, src_size, "%s%s%s",
75*5331Samw 		    infile, dgettext(TEXT_DOMAIN, " attribute "), asfile);
76*5331Samw 	} else {
77*5331Samw 		(void) snprintf(srcbuf, src_size, "%s", infile);
78*5331Samw 	}
79*5331Samw 
80*5331Samw 	if (atfile != NULL) {
81*5331Samw 		targ_size = strlen(outfile) + strlen(atfile) +
82*5331Samw 		    strlen(dgettext(TEXT_DOMAIN, " attribute ")) + 1;
83*5331Samw 	} else {
84*5331Samw 		targ_size = strlen(outfile) + 1;
85*5331Samw 	}
86*5331Samw 	targbuf = malloc(targ_size);
87*5331Samw 	if (targbuf == NULL) {
88*5331Samw 		(void) fprintf(stderr,
89*5331Samw 		    dgettext(TEXT_DOMAIN, "could not allocate memory"
90*5331Samw 		    " for path buffer: "));
91*5331Samw 		return (1);
92*5331Samw 	}
93*5331Samw 	if (atfile != NULL) {
94*5331Samw 		(void) snprintf(targbuf, targ_size, "%s%s%s",
95*5331Samw 		    outfile, dgettext(TEXT_DOMAIN, " attribute "), atfile);
96*5331Samw 	} else {
97*5331Samw 		(void) snprintf(targbuf, targ_size, "%s", outfile);
98*5331Samw 	}
99*5331Samw 
100*5331Samw 	if (ISREG(*s1p) && s1p->st_size > SMALLFILESIZE) {
101*5331Samw 		/*
102*5331Samw 		 * Determine size of initial mapping.  This will determine the
103*5331Samw 		 * size of the address space chunk we work with.  This initial
104*5331Samw 		 * mapping size will be used to perform munmap() in the future.
105*5331Samw 		 */
106*5331Samw 		mapsize = MAXMAPSIZE;
107*5331Samw 		if (s1p->st_size < mapsize) mapsize = s1p->st_size;
108*5331Samw 		munmapsize = mapsize;
109*5331Samw 
110*5331Samw 		/*
111*5331Samw 		 * Mmap time!
112*5331Samw 		 */
113*5331Samw 		if ((cp = mmap((caddr_t)NULL, mapsize, PROT_READ,
114*5331Samw 		    MAP_SHARED, fi, (off_t)0)) == MAP_FAILED)
115*5331Samw 			mapsize = 0;   /* can't mmap today */
116*5331Samw 	} else
117*5331Samw 		mapsize = 0;
118*5331Samw 
119*5331Samw 	if (mapsize != 0) {
120*5331Samw 		offset = 0;
121*5331Samw 
122*5331Samw 		for (;;) {
123*5331Samw 			nbytes = write(fo, cp, mapsize);
124*5331Samw 			/*
125*5331Samw 			 * if we write less than the mmaped size it's due to a
126*5331Samw 			 * media error on the input file or out of space on
127*5331Samw 			 * the output file.  So, try again, and look for errno.
128*5331Samw 			 */
129*5331Samw 			if ((nbytes >= 0) && (nbytes != (int)mapsize)) {
130*5331Samw 				remains = mapsize - nbytes;
131*5331Samw 				while (remains > 0) {
132*5331Samw 					nbytes = write(fo,
133*5331Samw 					    cp + mapsize - remains, remains);
134*5331Samw 					if (nbytes < 0) {
135*5331Samw 						if (errno == ENOSPC)
136*5331Samw 							perror(targbuf);
137*5331Samw 						else
138*5331Samw 							perror(srcbuf);
139*5331Samw 						(void) close(fi);
140*5331Samw 						(void) close(fo);
141*5331Samw 						(void) munmap(cp, munmapsize);
142*5331Samw 						if (ISREG(*s2p))
143*5331Samw 							(void) unlink(targbuf);
144*5331Samw 						return (1);
145*5331Samw 					}
146*5331Samw 					remains -= nbytes;
147*5331Samw 					if (remains == 0)
148*5331Samw 						nbytes = mapsize;
149*5331Samw 				}
150*5331Samw 			}
151*5331Samw 			/*
152*5331Samw 			 * although the write manual page doesn't specify this
153*5331Samw 			 * as a possible errno, it is set when the nfs read
154*5331Samw 			 * via the mmap'ed file is accessed, so report the
155*5331Samw 			 * problem as a source access problem, not a target file
156*5331Samw 			 * problem
157*5331Samw 			 */
158*5331Samw 			if (nbytes < 0) {
159*5331Samw 				if (errno == EACCES)
160*5331Samw 					perror(srcbuf);
161*5331Samw 				else
162*5331Samw 					perror(targbuf);
163*5331Samw 				(void) close(fi);
164*5331Samw 				(void) close(fo);
165*5331Samw 				(void) munmap(cp, munmapsize);
166*5331Samw 				if (ISREG(*s2p))
167*5331Samw 					(void) unlink(targbuf);
168*5331Samw 				if (srcbuf != NULL)
169*5331Samw 					free(srcbuf);
170*5331Samw 				if (targbuf != NULL)
171*5331Samw 					free(targbuf);
172*5331Samw 				return (1);
173*5331Samw 			}
174*5331Samw 			filesize -= nbytes;
175*5331Samw 			if (filesize == 0)
176*5331Samw 				break;
177*5331Samw 			offset += nbytes;
178*5331Samw 			if (filesize < mapsize)
179*5331Samw 				mapsize = filesize;
180*5331Samw 			if (mmap(cp, mapsize, PROT_READ, MAP_SHARED |
181*5331Samw 			    MAP_FIXED, fi, offset) == MAP_FAILED) {
182*5331Samw 				perror(srcbuf);
183*5331Samw 				(void) close(fi);
184*5331Samw 				(void) close(fo);
185*5331Samw 				(void) munmap(cp, munmapsize);
186*5331Samw 				if (ISREG(*s2p))
187*5331Samw 					(void) unlink(targbuf);
188*5331Samw 				if (srcbuf != NULL)
189*5331Samw 					free(srcbuf);
190*5331Samw 				if (targbuf != NULL)
191*5331Samw 					free(targbuf);
192*5331Samw 				return (1);
193*5331Samw 			}
194*5331Samw 		}
195*5331Samw 		(void) munmap(cp, munmapsize);
196*5331Samw 	} else {
197*5331Samw 		char buf[SMALLFILESIZE];
198*5331Samw 		for (;;) {
199*5331Samw 			n = read(fi, buf, sizeof (buf));
200*5331Samw 			if (n == 0) {
201*5331Samw 				return (0);
202*5331Samw 			} else if (n < 0) {
203*5331Samw 				(void) close(fi);
204*5331Samw 				(void) close(fo);
205*5331Samw 				if (ISREG(*s2p))
206*5331Samw 					(void) unlink(targbuf);
207*5331Samw 				if (srcbuf != NULL)
208*5331Samw 					free(srcbuf);
209*5331Samw 				if (targbuf != NULL)
210*5331Samw 					free(targbuf);
211*5331Samw 				return (1);
212*5331Samw 			} else if (write(fo, buf, n) != n) {
213*5331Samw 				(void) close(fi);
214*5331Samw 				(void) close(fo);
215*5331Samw 				if (ISREG(*s2p))
216*5331Samw 					(void) unlink(targbuf);
217*5331Samw 				if (srcbuf != NULL)
218*5331Samw 					free(srcbuf);
219*5331Samw 				if (targbuf != NULL)
220*5331Samw 					free(targbuf);
221*5331Samw 				return (1);
222*5331Samw 			}
223*5331Samw 		}
224*5331Samw 	}
225*5331Samw 	if (srcbuf != NULL)
226*5331Samw 		free(srcbuf);
227*5331Samw 	if (targbuf != NULL)
228*5331Samw 		free(targbuf);
229*5331Samw 	return (0);
230*5331Samw }
231