writefile.c revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * or http://www.opensolaris.org/os/licensing.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fields enclosed by brackets "[]" replaced with your own identifying
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER END
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/* All Rights Reserved */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * University Copyright- Copyright (c) 1982, 1986, 1988
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The Regents of the University of California
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * All Rights Reserved
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw *
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * University Acknowledgment- Portions of this document are derived from
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * software developed by the University of California, Berkeley, and its
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * contributors.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw/*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use is subject to license terms.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#pragma ident "%Z%%M% %I% %E% SMI"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include <c_synonyms.h>
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw#include "libcmdutils.h"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwwritefile(int fi, int fo, char *infile, char *outfile, char *asfile,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw char *atfile, struct stat *s1p, struct stat *s2p)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw{
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int mapsize, munmapsize;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw caddr_t cp;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw off_t filesize = s1p->st_size;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw off_t offset;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int nbytes;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int remains;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw int n;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw size_t src_size;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw size_t targ_size;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw char *srcbuf;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw char *targbuf;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (asfile != NULL) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw src_size = strlen(infile) + strlen(asfile) +
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw strlen(dgettext(TEXT_DOMAIN, " attribute ")) + 1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw src_size = strlen(infile) + 1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw srcbuf = malloc(src_size);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (srcbuf == NULL) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fprintf(stderr,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw dgettext(TEXT_DOMAIN, "could not allocate memory"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw " for path buffer: "));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (asfile != NULL) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) snprintf(srcbuf, src_size, "%s%s%s",
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw infile, dgettext(TEXT_DOMAIN, " attribute "), asfile);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) snprintf(srcbuf, src_size, "%s", infile);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (atfile != NULL) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw targ_size = strlen(outfile) + strlen(atfile) +
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw strlen(dgettext(TEXT_DOMAIN, " attribute ")) + 1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw targ_size = strlen(outfile) + 1;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw targbuf = malloc(targ_size);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (targbuf == NULL) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) fprintf(stderr,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw dgettext(TEXT_DOMAIN, "could not allocate memory"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw " for path buffer: "));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (atfile != NULL) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) snprintf(targbuf, targ_size, "%s%s%s",
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw outfile, dgettext(TEXT_DOMAIN, " attribute "), atfile);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) snprintf(targbuf, targ_size, "%s", outfile);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ISREG(*s1p) && s1p->st_size > SMALLFILESIZE) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Determine size of initial mapping. This will determine the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * size of the address space chunk we work with. This initial
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * mapping size will be used to perform munmap() in the future.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mapsize = MAXMAPSIZE;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (s1p->st_size < mapsize) mapsize = s1p->st_size;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw munmapsize = mapsize;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Mmap time!
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((cp = mmap((caddr_t)NULL, mapsize, PROT_READ,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw MAP_SHARED, fi, (off_t)0)) == MAP_FAILED)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mapsize = 0; /* can't mmap today */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mapsize = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (mapsize != 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw offset = 0;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (;;) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw nbytes = write(fo, cp, mapsize);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * if we write less than the mmaped size it's due to a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * media error on the input file or out of space on
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the output file. So, try again, and look for errno.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if ((nbytes >= 0) && (nbytes != (int)mapsize)) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw remains = mapsize - nbytes;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (remains > 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw nbytes = write(fo,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw cp + mapsize - remains, remains);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (nbytes < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (errno == ENOSPC)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw perror(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw else
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw perror(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fi);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fo);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) munmap(cp, munmapsize);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ISREG(*s2p))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) unlink(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw remains -= nbytes;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (remains == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw nbytes = mapsize;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * although the write manual page doesn't specify this
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * as a possible errno, it is set when the nfs read
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * via the mmap'ed file is accessed, so report the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * problem as a source access problem, not a target file
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * problem
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (nbytes < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (errno == EACCES)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw perror(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw else
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw perror(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fi);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fo);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) munmap(cp, munmapsize);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ISREG(*s2p))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) unlink(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (srcbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (targbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw filesize -= nbytes;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (filesize == 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw break;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw offset += nbytes;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (filesize < mapsize)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw mapsize = filesize;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (mmap(cp, mapsize, PROT_READ, MAP_SHARED |
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw MAP_FIXED, fi, offset) == MAP_FAILED) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw perror(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fi);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fo);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) munmap(cp, munmapsize);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ISREG(*s2p))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) unlink(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (srcbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (targbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) munmap(cp, munmapsize);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw char buf[SMALLFILESIZE];
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (;;) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw n = read(fi, buf, sizeof (buf));
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (n == 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else if (n < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fi);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fo);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ISREG(*s2p))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) unlink(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (srcbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (targbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw } else if (write(fo, buf, n) != n) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fi);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) close(fo);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (ISREG(*s2p))
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw (void) unlink(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (srcbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (targbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw }
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (srcbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(srcbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (targbuf != NULL)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw free(targbuf);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw}