2 * @brief Wrappers for low-level POSIX I/O routines.
4 /* Copyright (C) 2004,2006,2007,2008,2009,2011,2012,2014,2015,2016 Olly Betts
5 * Copyright (C) 2010 Richard Boulton
7 * This program 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 program 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "posixy_wrapper.h"
27 #include "safeunistd.h"
33 #include <xapian/error.h>
39 // Trying to include the correct headers with the correct defines set to
40 // get pread() and pwrite() prototyped on every platform without breaking any
41 // other platform is a real can of worms. So instead we probe for what
42 // prototypes (if any) are required in configure and put them into
43 // PREAD_PROTOTYPE and PWRITE_PROTOTYPE.
44 #if defined HAVE_PREAD && defined PREAD_PROTOTYPE
47 #if defined HAVE_PWRITE && defined PWRITE_PROTOTYPE
52 io_unlink(const std::string
& filename
)
54 if (posixy_unlink(filename
.c_str()) == 0) {
57 if (errno
!= ENOENT
) {
58 throw Xapian::DatabaseError(filename
+ ": delete failed", errno
);
63 // The smallest fd we want to use for a writable handle.
64 const int MIN_WRITE_FD
= 3;
67 io_open_block_wr(const char * fname
, bool anew
)
69 // Use auto because on AIX O_CLOEXEC may be a 64-bit integer constant.
70 auto flags
= O_RDWR
| O_BINARY
| O_CLOEXEC
;
71 if (anew
) flags
|= O_CREAT
| O_TRUNC
;
72 int fd
= ::open(fname
, flags
, 0666);
73 if (fd
>= MIN_WRITE_FD
|| fd
< 0) return fd
;
75 // We want to avoid using fd < MIN_WRITE_FD, in case some other code in
76 // the same process tries to write to stdout or stderr, which would end up
77 // corrupting our database.
79 #ifdef F_DUPFD_CLOEXEC
80 // dup to the first unused fd >= MIN_WRITE_FD.
81 fd
= fcntl(badfd
, F_DUPFD_CLOEXEC
, MIN_WRITE_FD
);
82 // F_DUPFD_CLOEXEC may not be supported.
83 if (fd
< 0 && errno
== EINVAL
)
87 fd
= fcntl(badfd
, F_DUPFD
, MIN_WRITE_FD
);
90 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
93 int save_errno
= errno
;
98 char toclose
[MIN_WRITE_FD
];
99 memset(toclose
, 0, sizeof(toclose
));
104 } while (fd
>= 0 && fd
< MIN_WRITE_FD
);
105 int save_errno
= errno
;
106 for (badfd
= 0; badfd
!= MIN_WRITE_FD
; ++badfd
)
113 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
118 Assert(fd
>= MIN_WRITE_FD
|| fd
< 0);
123 io_read(int fd
, char * p
, size_t n
, size_t min
)
127 ssize_t c
= read(fd
, p
, n
);
130 if (total
>= min
) break;
131 throw Xapian::DatabaseCorruptError("Couldn't read enough (EOF)");
133 if (errno
== EINTR
) continue;
134 throw Xapian::DatabaseError("Error reading from file", errno
);
143 /** Write n bytes from block pointed to by p to file descriptor fd. */
145 io_write(int fd
, const char * p
, size_t n
)
148 ssize_t c
= write(fd
, p
, n
);
150 if (errno
== EINTR
) continue;
151 throw Xapian::DatabaseError("Error writing to file", errno
);
159 static void throw_block_error(const char * s
, off_t b
, int e
= 0));
161 throw_block_error(const char * s
, off_t b
, int e
)
165 throw Xapian::DatabaseError(m
, e
);
168 #ifdef HAVE_POSIX_FADVISE
170 io_readahead_block(int fd
, size_t n
, off_t b
, off_t o
)
173 // Assume that any failure is likely to also happen for another call with
175 return posix_fadvise(fd
, o
, n
, POSIX_FADV_WILLNEED
) == 0;
180 io_read_block(int fd
, char * p
, size_t n
, off_t b
, off_t o
)
183 // Prefer pread if available since it's typically implemented as a
184 // separate syscall, and that eliminates the overhead of an extra syscall
188 ssize_t c
= pread(fd
, p
, n
, o
);
189 // We should get a full read most of the time, so streamline that case.
190 if (usual(c
== ssize_t(n
)))
192 // -1 is error, 0 is EOF
195 throw_block_error("EOF reading block ", b
);
196 // We get EINTR if the syscall was interrupted by a signal.
197 // In this case we should retry the read.
198 if (errno
== EINTR
) continue;
199 throw_block_error("Error reading block ", b
, errno
);
206 if (rare(lseek(fd
, o
, SEEK_SET
) < 0))
207 throw_block_error("Error seeking to block ", b
, errno
);
209 ssize_t c
= read(fd
, p
, n
);
210 // We should get a full read most of the time, so streamline that case.
211 if (usual(c
== ssize_t(n
)))
215 throw_block_error("EOF reading block ", b
);
216 // We get EINTR if the syscall was interrupted by a signal.
217 // In this case we should retry the read.
218 if (errno
== EINTR
) continue;
219 throw_block_error("Error reading block ", b
, errno
);
228 io_write_block(int fd
, const char * p
, size_t n
, off_t b
, off_t o
)
231 // Prefer pwrite if available since it's typically implemented as a
232 // separate syscall, and that eliminates the overhead of an extra syscall
236 ssize_t c
= pwrite(fd
, p
, n
, o
);
237 // We should get a full write most of the time, so streamline that case.
238 if (usual(c
== ssize_t(n
)))
241 // We get EINTR if the syscall was interrupted by a signal.
242 // In this case we should retry the write.
243 if (errno
== EINTR
) continue;
244 throw_block_error("Error writing block ", b
, errno
);
251 if (rare(lseek(fd
, o
, SEEK_SET
) < 0))
252 throw_block_error("Error seeking to block ", b
, errno
);
254 ssize_t c
= write(fd
, p
, n
);
255 // We should get a full write most of the time, so streamline that case.
256 if (usual(c
== ssize_t(n
)))
259 // We get EINTR if the syscall was interrupted by a signal.
260 // In this case we should retry the write.
261 if (errno
== EINTR
) continue;
262 throw_block_error("Error writing block ", b
, errno
);
271 io_tmp_rename(const std::string
& tmp_file
, const std::string
& real_file
)
274 // We retry on EXDEV a few times as some older Linux kernels are buggy and
275 // fail with EXDEV when the two files are on the same device (as they
276 // always ought to be when this function is used). Don't retry forever in
277 // case someone calls this with files on different devices.
279 // We're not sure exactly which kernels are buggy in this way, but there's
280 // discussion here: https://www.spinics.net/lists/linux-nfs/msg17306.html
282 // Reported at: https://trac.xapian.org/ticket/698
286 if (posixy_rename(tmp_file
.c_str(), real_file
.c_str()) < 0) {
288 if (errno
== EXDEV
&& --retries
> 0) goto retry
;
290 // With NFS, rename() failing may just mean that the server crashed
291 // after successfully renaming, but before reporting this, and then
292 // the retried operation fails. So we need to check if the source
293 // file still exists, which we do by calling unlink(), since we want
294 // to remove the temporary file anyway.
295 int saved_errno
= errno
;
296 if (unlink(tmp_file
.c_str()) == 0 || errno
!= ENOENT
) {