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