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