copyf.c revision 5c51f1241dbbdf2656d0e10011981411ed0c9673
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <fcntl.h>
#include <utime.h>
#include <locale.h>
#include <libintl.h>
/*
* consolidation pkg command library includes
*/
#include "pkglib.h"
/*
* local pkg command library includes
*/
#include "libinst.h"
#include "libadm.h"
#include "messages.h"
/*
* MAXMAPSIZE controls the largest mapping to use at a time; please refer
* to mmap(2) for details of how this size is incremented and rounded; briefly
* each mapping request has an additional 16Kb added to it - mappings over
* 4Mb will be rounded to a 4Mb boundary - thus if there were 8mb, adding
* in the 16Kb overhead the mapping would use another 4Mb-16kb - that is
* why there is 16Kb subtracted from the total
*/
/*
* Name: copyF
* Description: fast copy of file - use mmap()/write() loop if possible
* Arguments: char *srcPath - name of source file to copy from
* char *dstPath - name of target file to copy to
* time_t a_mytime: control setting of access/modification times:
* == 0 - replicate source file access/modification times
* != 0 - use specified time for access/modification times
* Returns: int
* == 0 - successful
* != 0 - failure
*/
int
{
struct stat srcStatbuf;
int srcFd;
int dstFd;
int status;
char *pt;
/* open source file for reading */
if (srcFd < 0) {
return (-1);
}
/* obtain file status of source file */
return (-1);
}
/* open target file for writing */
if (dstFd < 0) {
/* create directory structure if missing */
*pt = '\0';
*pt = '/';
return (-1);
}
}
*pt = '/';
}
/* attempt to create target file again */
if (dstFd < 0) {
return (-1);
}
}
/*
* source and target files are open: copy data
*/
/*
* determine how to set access/modification times for target:
* -- a_mytime == 0: replicate source file access/modification times
* -- otherwise: use a_mytime for file access/modification times
*/
if (a_mytime == 0) {
} else {
}
/* set access/modification times for target */
return (-1);
}
/* return error if copy failed */
if (status != 0) {
return (-1);
}
/* success! */
return (0);
}
/*
* Name: copyFile
* Description: fast copy of file - use mmap()/write() loop if possible
* Arguments: int srcFd - file descriptor open on source file
* int dstFd - file descriptor open on target file
* char *srcPath - name of source file (for error messages)
* char *dstPath - name of target file (for error messages)
* struct stat *a_srcStatbuf - stat structure for source file
* Returns: int
* == 0 - successful
* != 0 - failure
*/
int
{
size_t munmapsize = 0;
/*
* if the source is a regular file and is not "too small", then cause
* the file to be mapped into memory
*/
/*
* Determine size of initial mapping. This will determine the
* size of the address space chunk we work with. This initial
* mapping size will be used to perform munmap() in the future.
*/
}
/*
* remember size of mapping to "unmap" - if the source file
* exceeds MAXMAPSIZE bytes, then the final mapping of the
* source file will be less than MAXMAPSIZE, and we need to
* make sure that the entire mapping is unmapped when done.
*/
/* map the first segment of the source into memory */
if (cp == MAP_FAILED) {
mapsize = 0; /* can't mmap today */
}
}
/*
*/
if (mapsize == 0) {
int pagesize = getpagesize();
/* set blocksize for copy */
}
/* allocate i/o transfer buffer */
return (1);
}
/* copy the file contents */
for (;;) {
ssize_t n;
/* read next block of data */
if (n == 0) {
/* end of file - return success */
return (0);
} else if (n < 0) {
/* read error - return error */
return (1);
}
/* write out block of data just read in */
return (1);
}
}
}
/*
* the source has been mapped into memory, copy via mappings
*/
for (;;) {
/* write first mappings worth of data */
/*
* if we write less than the mmaped size it's due to a
* media error on the input file or out of space on
* the output file. So, try again, and look for errno.
*/
while (remains > 0) {
if (nbytes >= 0) {
if (remains == 0) {
}
continue;
}
/* i/o error - report and exit */
} else {
}
/* unmap source file mapping */
return (1);
}
}
/*
* although the write manual page doesn't specify this
* as a possible errno, it is set when the nfs read
* via the mmap'ed file is accessed, so report the
* problem as a source access problem, not a target file
* problem
*/
if (nbytes < 0) {
} else {
}
/* unmap source file mapping */
return (1);
}
if (filesize == 0) {
break;
}
}
/* map next segment of file on top of existing mapping */
if (cp == MAP_FAILED) {
/* unmap source file mapping */
return (1);
}
}
/* unmap source file mapping */
return (0);
}
/*
* Name: openLocal
* Description: open a file and assure that the descriptor returned is open on
* a file that is local to the current system - if the file is not
* local to this system, copy the file to a temporary file first,
* and then pass a handle back opened on the temporary file
* Arguments: a_path - [RO, *RO] - (char *)
* Pointer to string representing the path to the file
* to open
* a_oflag - [RO, *RO] - (int)
* Integer representing the "mode" bits for an open(2) call
* a_tmpdir - [RO, *RO] - (char *)
* Pointer to string representing the path to a directory
* where a temporary copy of the file can be placed if
* the source file is not local to this system. If this is
* NULL or does not exist, P_tmpdir is used.
* Returns: int
* >= 0 - file descriptor opened on the file
* == -1 - failed to open - errno contains error code
* NOTE: If the file is not local and is copied locally, the file is
* setup in such a way that it will be removed when the last
* file descriptor opened on the file is closed - there is no need
* to know the path to the temporary file or to remove it
* when done.
*/
int
{
char *bn;
int fd;
int lerrno;
int n;
int tmpFd;
/* open source file */
if (fd < 0) {
return (fd);
}
/* return open fd if the source file is not remote */
if (!isFdRemote(fd)) {
return (fd);
}
/*
* source file is remote - must make a local copy
*/
/* get the source file's status */
if (n < 0) {
return (-1);
}
/* generate unique temporary file name */
}
if (n > sizeof (template)) {
return (EINVAL);
}
/* create the temporary file and open it */
if (tmpFd < 0) {
return (tmpFd);
}
/* unlink the file so when it is closed it is automatically deleted */
/* copy the source file to the temporary file */
if (n != 0) {
return (-1);
}
/* return handle to temporary file created */
return (tmpFd);
}