po: Update German man pages translation
[dpkg.git] / lib / dpkg / fdio.c
blob8e61b5d882d6ac93fe52be18fa6ddf2c490771c9
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * fdio.c - safe file descriptor based input/output
5 * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <unistd.h>
28 #include <dpkg/fdio.h>
30 ssize_t
31 fd_read(int fd, void *buf, size_t len)
33 ssize_t total = 0;
34 char *ptr = buf;
36 while (len > 0) {
37 ssize_t n;
39 n = read(fd, ptr + total, len);
40 if (n == -1) {
41 if (errno == EINTR || errno == EAGAIN)
42 continue;
43 return total ? -total : n;
45 if (n == 0)
46 break;
48 total += n;
49 len -= n;
52 return total;
55 ssize_t
56 fd_write(int fd, const void *buf, size_t len)
58 ssize_t total = 0;
59 const char *ptr = buf;
61 while (len > 0) {
62 ssize_t n;
64 n = write(fd, ptr + total, len);
65 if (n == -1) {
66 if (errno == EINTR || errno == EAGAIN)
67 continue;
68 return total ? -total : n;
70 if (n == 0)
71 break;
73 total += n;
74 len -= n;
77 return total;
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)
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;
91 #endif
93 /**
94 * Request the kernel to allocate the specified size for a file descriptor.
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).
102 fd_allocate_size(int fd, off_t offset, off_t len)
104 int rc;
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;
111 #if defined(HAVE_F_PREALLOCATE)
112 /* On Mac OS X. */
113 fstore_t fs;
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);
123 #elif defined(HAVE_F_ALLOCSP64)
124 /* On Solaris. */
125 struct flock64 fl;
127 fl.l_whence = SEEK_SET;
128 fl.l_start = offset;
129 fl.l_len = len;
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__))
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.
148 rc = posix_fallocate(fd, offset, len);
149 #else
150 errno = ENOSYS;
151 rc = -1;
152 #endif
154 return rc;
156 #else
158 fd_allocate_size(int fd, off_t offset, off_t len)
160 errno = ENOSYS;
161 return -1;
163 #endif