-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxfile.c
254 lines (218 loc) · 5.02 KB
/
xfile.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <errno.h>
#include "ct_assert.h"
#include "xfile.h"
// instead of #define _GNU_SOURCE/_BSD_SOURCE/_SVID_SOURCE and #include <stdio.h>
#undef fwrite_unlocked
extern size_t fwrite_unlocked(const void *ptr, size_t size, size_t n, FILE *stream);
int
xopen(const char *pathname, int flags, mode_t mode)
{
int fd;
do
fd = open(pathname, flags, mode);
while ((fd < 0) && (errno == EINTR));
return fd;
}
int xopen_append(const char *pathname) { return xopen(pathname, O_WRONLY|O_CREAT|O_APPEND, 0644); }
int xopen_read(const char *pathname) { return xopen(pathname, O_RDONLY, 0); }
int xopen_write(const char *pathname) { return xopen(pathname, O_WRONLY, 0644); }
int xopen_trunc(const char *pathname) { return xopen(pathname, O_WRONLY|O_CREAT|O_TRUNC, 0644); }
int xopen_readwrite(const char *pathname) { return xopen(pathname, O_RDWR|O_CREAT, 0644); }
// like read(2), but ignore EINTR
ssize_t
xread(int fd, void *buf, size_t count)
{
ssize_t nread;
for (;;) {
nread = read(fd, buf, count);
if ((nread < 0) && (errno == EINTR))
continue;
break;
}
return nread;
}
// like pread(2), but ignore EINTR
ssize_t
xpread(int fd, void *buf, size_t count, off_t offset)
{
ssize_t nread;
for (;;) {
nread = pread(fd, buf, count, offset);
if ((nread < 0) && (errno == EINTR))
continue;
break;
}
return nread;
}
// like write(2), but ignore EINTR
ssize_t
xwrite(int fd, const void *buf, size_t count)
{
ssize_t nwrite;
for (;;) {
nwrite = write(fd, buf, count);
if ((nwrite < 0) && (errno == EINTR))
continue;
break;
}
return nwrite;
}
// like pwrite(2), but ignore EINTR
ssize_t
xpwrite(int fd, const void *buf, size_t count, off_t offset)
{
ssize_t nwrite;
for (;;) {
nwrite = pwrite(fd, buf, count, offset);
if ((nwrite < 0) && (errno == EINTR))
continue;
break;
}
return nwrite;
}
ssize_t
xreadall(int fd, void *buf, size_t count)
{
char *bufp = buf;
size_t nleft = count;
ssize_t nread;
while (nleft > 0) {
nread = xread(fd, bufp, nleft);
if (nread < 0)
return -1;
if (nread == 0)
break;
bufp += nread;
nleft -= nread;
}
return count-nleft;
}
ssize_t
xpreadall(int fd, void *buf, size_t count, off_t offset)
{
char *bufp = buf;
size_t nleft = count;
ssize_t nread;
// use 64-bit file offsets (even on 32-bit builds)
ct_assert(sizeof(off_t) == 8);
while (nleft > 0) {
nread = xpread(fd, bufp, nleft, offset);
if (nread < 0)
return -1;
if (nread == 0)
break;
bufp += nread;
offset += nread;
nleft -= nread;
}
return count-nleft;
}
size_t
xwriteall(int fd, const void *buf, size_t count)
{
const char *bufp = buf;
size_t nleft = count;
ssize_t nwrite;
while (nleft > 0) {
nwrite = xwrite(fd, bufp, nleft);
if (nwrite < 0)
return count-nleft;
nleft -= nwrite;
bufp += nwrite;
}
return count;
}
size_t
xpwriteall(int fd, const void *buf, size_t count, off_t offset)
{
const char *bufp = buf;
size_t nleft = count;
ssize_t nwrite;
// use 64-bit file offsets (even on 32-bit builds)
ct_assert(sizeof(off_t) == 8);
while (nleft > 0) {
nwrite = xpwrite(fd, bufp, nleft, offset);
if (nwrite < 0)
return count-nleft;
nleft -= nwrite;
bufp += nwrite;
offset += nwrite;
}
return count;
}
size_t
xfwriteall(void *fp, const void *buf, size_t count)
{
const char *bufp = buf;
size_t nleft = count, nwrite;
while (nleft > 0) {
nwrite = fwrite_unlocked(bufp, 1, nleft, fp);
if (nwrite != nleft) {
if (errno != EINTR) {
nleft -= nwrite;
return count-nleft;
}
}
nleft -= nwrite;
bufp += nwrite;
}
return count;
}
int
xfsync(int fd)
{
int ret;
do
ret = fsync(fd);
while ((ret < 0) && (errno == EINTR));
return ret;
}
int
xfdatasync(int fd)
{
int ret;
do
ret = fdatasync(fd);
while ((ret < 0) && (errno == EINTR));
return ret;
}
int
xftruncate(int fd, off_t length)
{
int ret;
do
ret = ftruncate(fd, length);
while ((ret < 0) && (errno == EINTR));
return ret;
}
int
xtruncate(const char *pathname, off_t length)
{
int ret;
do
ret = truncate(pathname, length);
while ((ret < 0) && (errno == EINTR));
return ret;
}
int
xndelay_on(int fd)
{
int ret;
ret = fcntl(fd, F_GETFL);
return ret < 0 ? ret : fcntl(fd, F_SETFL, ret|O_NONBLOCK);
}
int
xflock_exnb(int fd)
{
int ret;
do
ret = flock(fd, LOCK_EX|LOCK_NB);
while ((ret < 0) && (errno == EINTR));
return ret;
}