summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/fdio.c
blob: 8e61b5d882d6ac93fe52be18fa6ddf2c490771c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * libdpkg - Debian packaging suite library routines
 * fdio.c - safe file descriptor based input/output
 *
 * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <dpkg/fdio.h>

ssize_t
fd_read(int fd, void *buf, size_t len)
{
	ssize_t total = 0;
	char *ptr = buf;

	while (len > 0) {
		ssize_t n;

		n = read(fd, ptr + total, len);
		if (n == -1) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
			return total ? -total : n;
		}
		if (n == 0)
			break;

		total += n;
		len -= n;
	}

	return total;
}

ssize_t
fd_write(int fd, const void *buf, size_t len)
{
	ssize_t total = 0;
	const char *ptr = buf;

	while (len > 0) {
		ssize_t n;

		n = write(fd, ptr + total, len);
		if (n == -1) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
			return total ? -total : n;
		}
		if (n == 0)
			break;

		total += n;
		len -= n;
	}

	return total;
}

#ifdef USE_DISK_PREALLOCATE
#ifdef HAVE_F_PREALLOCATE
static void
fd_preallocate_setup(fstore_t *fs, int flags, off_t offset, off_t len)
{
	fs->fst_flags = flags;
	fs->fst_posmode = F_PEOFPOSMODE;
	fs->fst_offset = offset;
	fs->fst_length = len;
	fs->fst_bytesalloc = 0;
}
#endif

/**
 * Request the kernel to allocate the specified size for a file descriptor.
 *
 * We only want to send a hint that we will be using the requested size. But
 * we do not want to unnecessarily write the file contents. That is why we
 * are not using posix_fallocate(3) directly if possible, and not at all
 * on glibc based systems (except on GNU/kFreeBSD).
 */
int
fd_allocate_size(int fd, off_t offset, off_t len)
{
	int rc;

	/* Do not preallocate on very small files as that degrades performance
	 * on some filesystems. */
	if (len < (4 * 4096) - 1)
		return 0;

#if defined(HAVE_F_PREALLOCATE)
	/* On Mac OS X. */
	fstore_t fs;

	fd_preallocate_setup(&fs, F_ALLOCATECONTIG, offset, len);
	rc = fcntl(fd, F_PREALLOCATE, &fs);
	if (rc < 0 && errno == ENOSPC) {
		/* If we cannot get a contiguous allocation, then try
		 * non-contiguous. */
		fd_preallocate_setup(&fs, F_ALLOCATEALL, offset, len);
		rc = fcntl(fd, F_PREALLOCATE, &fs);
	}
#elif defined(HAVE_F_ALLOCSP64)
	/* On Solaris. */
	struct flock64 fl;

	fl.l_whence = SEEK_SET;
	fl.l_start = offset;
	fl.l_len = len;

	rc = fcntl(fd, F_ALLOCSP64, &fl);
#elif defined(HAVE_FALLOCATE)
	/* On Linux. */
	do {
		rc = fallocate(fd, 0, offset, len);
	} while (rc < 0 && errno == EINTR);
#elif defined(HAVE_POSIX_FALLOCATE) && \
      ((defined(__GLIBC__) && defined(__FreeBSD_kernel__)) || \
       !defined(__GLIBC__))
	/*
	 * On BSDs, newer GNU/kFreeBSD and other non-glibc based systems
	 * we can use posix_fallocate(2) which should be a simple syscall
	 * wrapper. But not on other glibc systems, as there the function
	 * will try to allocate the size by writing a '\0' to each block
	 * if the syscall is not implemented or not supported by the
	 * kernel or the filesystem, which we do not want.
	 */
	rc = posix_fallocate(fd, offset, len);
#else
	errno = ENOSYS;
	rc = -1;
#endif

	return rc;
}
#else
int
fd_allocate_size(int fd, off_t offset, off_t len)
{
	errno = ENOSYS;
	return -1;
}
#endif