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/>.
28 #include <dpkg/fdio.h>
31 fd_read(int fd, void *buf, size_t len)
39 n = read(fd, ptr + total, len);
41 if (errno == EINTR || errno == EAGAIN)
43 return total ? -total : n;
56 fd_write(int fd, const void *buf, size_t len)
59 const char *ptr = buf;
64 n = write(fd, ptr + total, len);
66 if (errno == EINTR || errno == EAGAIN)
68 return total ? -total : n;
80 #ifdef USE_DISK_PREALLOCATE
81 #ifdef HAVE_F_PREALLOCATE
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;
89 fs->fst_bytesalloc = 0;
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)
106 /* Do not preallocate on very small files as that degrades performance
107 * on some filesystems. */
108 if (len < (4 * 4096) - 1)
111 #if defined(HAVE_F_PREALLOCATE)
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
120 fd_preallocate_setup(&fs, F_ALLOCATEALL, offset, len);
121 rc = fcntl(fd, F_PREALLOCATE, &fs);
123 #elif defined(HAVE_F_ALLOCSP64)
127 fl.l_whence = SEEK_SET;
131 rc = fcntl(fd, F_ALLOCSP64, &fl);
132 #elif defined(HAVE_FALLOCATE)
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__)) || \
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);
158 fd_allocate_size(int fd, off_t offset, off_t len)