Commit | Line | Data |
---|---|---|
1479465f GJ |
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 <fcntl.h> | |
26 | #include <unistd.h> | |
27 | ||
28 | #include <dpkg/fdio.h> | |
29 | ||
30 | ssize_t | |
31 | fd_read(int fd, void *buf, size_t len) | |
32 | { | |
33 | ssize_t total = 0; | |
34 | char *ptr = buf; | |
35 | ||
36 | while (len > 0) { | |
37 | ssize_t n; | |
38 | ||
39 | n = read(fd, ptr + total, len); | |
40 | if (n == -1) { | |
41 | if (errno == EINTR || errno == EAGAIN) | |
42 | continue; | |
43 | return total ? -total : n; | |
44 | } | |
45 | if (n == 0) | |
46 | break; | |
47 | ||
48 | total += n; | |
49 | len -= n; | |
50 | } | |
51 | ||
52 | return total; | |
53 | } | |
54 | ||
55 | ssize_t | |
56 | fd_write(int fd, const void *buf, size_t len) | |
57 | { | |
58 | ssize_t total = 0; | |
59 | const char *ptr = buf; | |
60 | ||
61 | while (len > 0) { | |
62 | ssize_t n; | |
63 | ||
64 | n = write(fd, ptr + total, len); | |
65 | if (n == -1) { | |
66 | if (errno == EINTR || errno == EAGAIN) | |
67 | continue; | |
68 | return total ? -total : n; | |
69 | } | |
70 | if (n == 0) | |
71 | break; | |
72 | ||
73 | total += n; | |
74 | len -= n; | |
75 | } | |
76 | ||
77 | return total; | |
78 | } | |
79 | ||
80 | #ifdef USE_DISK_PREALLOCATE | |
81 | #ifdef HAVE_F_PREALLOCATE | |
82 | static void | |
83 | fd_preallocate_setup(fstore_t *fs, int flags, off_t offset, off_t len) | |
84 | { | |
85 | fs->fst_flags = flags; | |
86 | fs->fst_posmode = F_PEOFPOSMODE; | |
87 | fs->fst_offset = offset; | |
88 | fs->fst_length = len; | |
89 | fs->fst_bytesalloc = 0; | |
90 | } | |
91 | #endif | |
92 | ||
93 | /** | |
94 | * Request the kernel to allocate the specified size for a file descriptor. | |
95 | * | |
96 | * We only want to send a hint that we will be using the requested size. But | |
97 | * we do not want to unnecessarily write the file contents. That is why we | |
98 | * are not using posix_fallocate(3) directly if possible, and not at all | |
99 | * on glibc based systems (except on GNU/kFreeBSD). | |
100 | */ | |
101 | int | |
102 | fd_allocate_size(int fd, off_t offset, off_t len) | |
103 | { | |
104 | int rc; | |
105 | ||
106 | /* Do not preallocate on very small files as that degrades performance | |
107 | * on some filesystems. */ | |
108 | if (len < (4 * 4096) - 1) | |
109 | return 0; | |
110 | ||
111 | #if defined(HAVE_F_PREALLOCATE) | |
112 | /* On Mac OS X. */ | |
113 | fstore_t fs; | |
114 | ||
115 | fd_preallocate_setup(&fs, F_ALLOCATECONTIG, offset, len); | |
116 | rc = fcntl(fd, F_PREALLOCATE, &fs); | |
117 | if (rc < 0 && errno == ENOSPC) { | |
118 | /* If we cannot get a contiguous allocation, then try | |
119 | * non-contiguous. */ | |
120 | fd_preallocate_setup(&fs, F_ALLOCATEALL, offset, len); | |
121 | rc = fcntl(fd, F_PREALLOCATE, &fs); | |
122 | } | |
123 | #elif defined(HAVE_F_ALLOCSP64) | |
124 | /* On Solaris. */ | |
125 | struct flock64 fl; | |
126 | ||
127 | fl.l_whence = SEEK_SET; | |
128 | fl.l_start = offset; | |
129 | fl.l_len = len; | |
130 | ||
131 | rc = fcntl(fd, F_ALLOCSP64, &fl); | |
132 | #elif defined(HAVE_FALLOCATE) | |
133 | /* On Linux. */ | |
134 | do { | |
135 | rc = fallocate(fd, 0, offset, len); | |
136 | } while (rc < 0 && errno == EINTR); | |
137 | #elif defined(HAVE_POSIX_FALLOCATE) && \ | |
138 | ((defined(__GLIBC__) && defined(__FreeBSD_kernel__)) || \ | |
139 | !defined(__GLIBC__)) | |
140 | /* | |
141 | * On BSDs, newer GNU/kFreeBSD and other non-glibc based systems | |
142 | * we can use posix_fallocate(2) which should be a simple syscall | |
143 | * wrapper. But not on other glibc systems, as there the function | |
144 | * will try to allocate the size by writing a '\0' to each block | |
145 | * if the syscall is not implemented or not supported by the | |
146 | * kernel or the filesystem, which we do not want. | |
147 | */ | |
148 | rc = posix_fallocate(fd, offset, len); | |
149 | #else | |
150 | errno = ENOSYS; | |
151 | rc = -1; | |
152 | #endif | |
153 | ||
154 | return rc; | |
155 | } | |
156 | #else | |
157 | int | |
158 | fd_allocate_size(int fd, off_t offset, off_t len) | |
159 | { | |
160 | errno = ENOSYS; | |
161 | return -1; | |
162 | } | |
163 | #endif |