xref: /freebsd/contrib/elftoolchain/libelftc/elftc_copyfile.c (revision a85fe12e361c14018a24f0f7714663b490206c7f)
1*a85fe12eSEd Maste /*-
2*a85fe12eSEd Maste  * Copyright (c) 2011, Joseph Koshy
3*a85fe12eSEd Maste  * All rights reserved.
4*a85fe12eSEd Maste  *
5*a85fe12eSEd Maste  * Redistribution and use in source and binary forms, with or without
6*a85fe12eSEd Maste  * modification, are permitted provided that the following conditions
7*a85fe12eSEd Maste  * are met:
8*a85fe12eSEd Maste  * 1. Redistributions of source code must retain the above copyright
9*a85fe12eSEd Maste  *    notice, this list of conditions and the following disclaimer
10*a85fe12eSEd Maste  *    in this position and unchanged.
11*a85fe12eSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
12*a85fe12eSEd Maste  *    notice, this list of conditions and the following disclaimer in the
13*a85fe12eSEd Maste  *    documentation and/or other materials provided with the distribution.
14*a85fe12eSEd Maste  *
15*a85fe12eSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16*a85fe12eSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*a85fe12eSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*a85fe12eSEd Maste  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19*a85fe12eSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*a85fe12eSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*a85fe12eSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*a85fe12eSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*a85fe12eSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*a85fe12eSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*a85fe12eSEd Maste  */
26*a85fe12eSEd Maste 
27*a85fe12eSEd Maste #include <sys/types.h>
28*a85fe12eSEd Maste #include <sys/stat.h>
29*a85fe12eSEd Maste 
30*a85fe12eSEd Maste #include <stdlib.h>
31*a85fe12eSEd Maste #include <unistd.h>
32*a85fe12eSEd Maste 
33*a85fe12eSEd Maste #include "libelftc.h"
34*a85fe12eSEd Maste #include "_libelftc.h"
35*a85fe12eSEd Maste 
36*a85fe12eSEd Maste #if	ELFTC_HAVE_MMAP
37*a85fe12eSEd Maste #include <sys/mman.h>
38*a85fe12eSEd Maste #endif
39*a85fe12eSEd Maste 
40*a85fe12eSEd Maste ELFTC_VCSID("$Id: elftc_copyfile.c 2318 2011-12-11 10:54:27Z jkoshy $");
41*a85fe12eSEd Maste 
42*a85fe12eSEd Maste /*
43*a85fe12eSEd Maste  * Copy the contents referenced by 'ifd' to 'ofd'.  Returns 0 on
44*a85fe12eSEd Maste  * success and -1 on error.
45*a85fe12eSEd Maste  */
46*a85fe12eSEd Maste 
47*a85fe12eSEd Maste int
48*a85fe12eSEd Maste elftc_copyfile(int ifd, int ofd)
49*a85fe12eSEd Maste {
50*a85fe12eSEd Maste 	int buf_mmapped;
51*a85fe12eSEd Maste 	struct stat sb;
52*a85fe12eSEd Maste 	char *b, *buf;
53*a85fe12eSEd Maste 	ssize_t nw;
54*a85fe12eSEd Maste 	size_t n;
55*a85fe12eSEd Maste 
56*a85fe12eSEd Maste 	/* Determine the input file's size. */
57*a85fe12eSEd Maste 	if (fstat(ifd, &sb) < 0)
58*a85fe12eSEd Maste 		return (-1);
59*a85fe12eSEd Maste 
60*a85fe12eSEd Maste 	/* Skip files without content. */
61*a85fe12eSEd Maste 	if (sb.st_size == 0)
62*a85fe12eSEd Maste 		return (0);
63*a85fe12eSEd Maste 
64*a85fe12eSEd Maste 	buf = NULL;
65*a85fe12eSEd Maste 	buf_mmapped = 0;
66*a85fe12eSEd Maste 
67*a85fe12eSEd Maste #if	ELFTC_HAVE_MMAP
68*a85fe12eSEd Maste 	/*
69*a85fe12eSEd Maste 	 * Prefer mmap() if it is available.
70*a85fe12eSEd Maste 	 */
71*a85fe12eSEd Maste 	buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd, (off_t) 0);
72*a85fe12eSEd Maste 	if (buf != MAP_FAILED)
73*a85fe12eSEd Maste 		buf_mmapped = 1;
74*a85fe12eSEd Maste 	else
75*a85fe12eSEd Maste 		buf = NULL;
76*a85fe12eSEd Maste #endif
77*a85fe12eSEd Maste 
78*a85fe12eSEd Maste 	/*
79*a85fe12eSEd Maste 	 * If mmap() is not available, or if the mmap() operation
80*a85fe12eSEd Maste 	 * failed, allocate a buffer, and read in input data.
81*a85fe12eSEd Maste 	 */
82*a85fe12eSEd Maste 	if (buf == NULL) {
83*a85fe12eSEd Maste 		if ((buf = malloc(sb.st_size)) == NULL)
84*a85fe12eSEd Maste 			return (-1);
85*a85fe12eSEd Maste 		if (read(ifd, buf, sb.st_size) != sb.st_size)
86*a85fe12eSEd Maste 			return (-1);
87*a85fe12eSEd Maste 	}
88*a85fe12eSEd Maste 
89*a85fe12eSEd Maste 	/*
90*a85fe12eSEd Maste 	 * Write data to the output file descriptor.
91*a85fe12eSEd Maste 	 */
92*a85fe12eSEd Maste 	for (n = sb.st_size, b = buf; n > 0; n -= nw, b += nw)
93*a85fe12eSEd Maste 		if ((nw = write(ofd, b, n)) <= 0)
94*a85fe12eSEd Maste 			break;
95*a85fe12eSEd Maste 
96*a85fe12eSEd Maste 	/* Release the input buffer. */
97*a85fe12eSEd Maste #if	ELFTC_HAVE_MMAP
98*a85fe12eSEd Maste 	if (buf_mmapped && munmap(buf, sb.st_size) < 0)
99*a85fe12eSEd Maste 		return (-1);
100*a85fe12eSEd Maste #endif
101*a85fe12eSEd Maste 
102*a85fe12eSEd Maste 	if (!buf_mmapped)
103*a85fe12eSEd Maste 		free(buf);
104*a85fe12eSEd Maste 
105*a85fe12eSEd Maste 	return (n > 0 ? -1 : 0);
106*a85fe12eSEd Maste }
107*a85fe12eSEd Maste 
108