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