Branch data Line data Source code
1 : : /*
2 : : * libdpkg - Debian packaging suite library routines
3 : : * fdio.c - safe file descriptor based input/output
4 : : *
5 : : * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
6 : : *
7 : : * This is free software; you can redistribute it and/or modify
8 : : * it under the terms of the GNU General Public License as published by
9 : : * the Free Software Foundation; either version 2 of the License, or
10 : : * (at your option) any later version.
11 : : *
12 : : * This is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : : * GNU General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU General Public License
18 : : * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include <config.h>
22 : : #include <compat.h>
23 : :
24 : : #include <errno.h>
25 : : #include <limits.h>
26 : : #include <fcntl.h>
27 : : #include <unistd.h>
28 : :
29 : : #include <dpkg/fdio.h>
30 : : #include <dpkg/ehandle.h>
31 : :
32 : : ssize_t
33 : 1428 : fd_read(int fd, void *buf, size_t len)
34 : : {
35 : 1428 : ssize_t total = 0;
36 : 1428 : char *ptr = buf;
37 : :
38 [ - + ]: 1428 : if (len > SSIZE_MAX)
39 : 0 : internerr("len=%zu exceeds SSIZE_MAX=%zd", len, SSIZE_MAX);
40 : :
41 [ + + ]: 2841 : while (len > 0) {
42 : : ssize_t n;
43 : :
44 : 1558 : n = read(fd, ptr + total, len);
45 [ - + ]: 1558 : if (n < 0) {
46 [ # # # # ]: 0 : if (errno == EINTR || errno == EAGAIN)
47 : 0 : continue;
48 [ # # ]: 0 : return total ? -total : n;
49 : : }
50 [ + + ]: 1558 : if (n == 0)
51 : 145 : break;
52 : :
53 : 1413 : total += n;
54 : 1413 : len -= n;
55 : : }
56 : :
57 : 1428 : return total;
58 : : }
59 : :
60 : : ssize_t
61 : 1278 : fd_write(int fd, const void *buf, size_t len)
62 : : {
63 : 1278 : ssize_t total = 0;
64 : 1278 : const char *ptr = buf;
65 : :
66 [ - + ]: 1278 : if (len > SSIZE_MAX)
67 : 0 : internerr("len=%zu exceeds SSIZE_MAX=%zd", len, SSIZE_MAX);
68 : :
69 [ + + ]: 2556 : while (len > 0) {
70 : : ssize_t n;
71 : :
72 : 1278 : n = write(fd, ptr + total, len);
73 [ - + ]: 1278 : if (n < 0) {
74 [ # # # # ]: 0 : if (errno == EINTR || errno == EAGAIN)
75 : 0 : continue;
76 [ # # ]: 0 : return total ? -total : n;
77 : : }
78 [ - + ]: 1278 : if (n == 0)
79 : 0 : break;
80 : :
81 : 1278 : total += n;
82 : 1278 : len -= n;
83 : : }
84 : :
85 : 1278 : return total;
86 : : }
87 : :
88 : : #ifdef USE_DISK_PREALLOCATE
89 : : #ifdef HAVE_F_PREALLOCATE
90 : : static void
91 : : fd_preallocate_setup(fstore_t *fs, int flags, off_t offset, off_t len)
92 : : {
93 : : fs->fst_flags = flags;
94 : : fs->fst_posmode = F_PEOFPOSMODE;
95 : : fs->fst_offset = offset;
96 : : fs->fst_length = len;
97 : : fs->fst_bytesalloc = 0;
98 : : }
99 : : #endif
100 : :
101 : : /**
102 : : * Request the kernel to allocate the specified size for a file descriptor.
103 : : *
104 : : * We only want to send a hint that we will be using the requested size. But
105 : : * we do not want to unnecessarily write the file contents. That is why we
106 : : * are not using posix_fallocate(3) directly if possible, and not at all
107 : : * on glibc based systems (except on GNU/kFreeBSD).
108 : : */
109 : : int
110 : : fd_allocate_size(int fd, off_t offset, off_t len)
111 : : {
112 : : int rc;
113 : :
114 : : /* Do not preallocate on very small files as that degrades performance
115 : : * on some filesystems. */
116 : : if (len < (4 * 4096) - 1)
117 : : return 0;
118 : :
119 : : #if defined(HAVE_F_PREALLOCATE)
120 : : /* On macOS. */
121 : : fstore_t fs;
122 : :
123 : : fd_preallocate_setup(&fs, F_ALLOCATECONTIG, offset, len);
124 : : rc = fcntl(fd, F_PREALLOCATE, &fs);
125 : : if (rc < 0 && errno == ENOSPC) {
126 : : /* If we cannot get a contiguous allocation, then try
127 : : * non-contiguous. */
128 : : fd_preallocate_setup(&fs, F_ALLOCATEALL, offset, len);
129 : : rc = fcntl(fd, F_PREALLOCATE, &fs);
130 : : }
131 : : #elif defined(HAVE_F_ALLOCSP64)
132 : : /* On Solaris. */
133 : : struct flock64 fl;
134 : :
135 : : fl.l_whence = SEEK_SET;
136 : : fl.l_start = offset;
137 : : fl.l_len = len;
138 : :
139 : : rc = fcntl(fd, F_ALLOCSP64, &fl);
140 : : #elif defined(HAVE_FALLOCATE)
141 : : /* On Linux. */
142 : : do {
143 : : rc = fallocate(fd, 0, offset, len);
144 : : } while (rc < 0 && errno == EINTR);
145 : : #elif defined(HAVE_POSIX_FALLOCATE) && \
146 : : ((defined(__GLIBC__) && defined(__FreeBSD_kernel__)) || \
147 : : !defined(__GLIBC__))
148 : : /*
149 : : * On BSDs, newer GNU/kFreeBSD and other non-glibc based systems
150 : : * we can use posix_fallocate(2) which should be a simple syscall
151 : : * wrapper. But not on other glibc systems, as there the function
152 : : * will try to allocate the size by writing a '\0' to each block
153 : : * if the syscall is not implemented or not supported by the
154 : : * kernel or the filesystem, which we do not want.
155 : : */
156 : : rc = posix_fallocate(fd, offset, len);
157 : : #else
158 : : errno = ENOSYS;
159 : : rc = -1;
160 : : #endif
161 : :
162 : : return rc;
163 : : }
164 : : #else
165 : : int
166 : 0 : fd_allocate_size(int fd, off_t offset, off_t len)
167 : : {
168 : 0 : errno = ENOSYS;
169 : 0 : return -1;
170 : : }
171 : : #endif
|