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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
/** @file
* @brief Wrappers for low-level POSIX I/O routines.
*/
/* Copyright (C) 2004-2025 Olly Betts
* Copyright (C) 2010 Richard Boulton
*
* This program 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 program 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 "io_utils.h"
#include "posixy_wrapper.h"
#include "safeunistd.h"
#include <cerrno>
#include <cstring>
#include <limits>
#include <string>
#include <xapian/error.h>
#include "omassert.h"
#include "str.h"
#ifdef __WIN32__
# include "safewindows.h"
#endif
// Trying to include the correct headers with the correct defines set to
// get pread() and pwrite() prototyped on every platform without breaking any
// other platform is a real can of worms. So instead we probe for what
// prototypes (if any) are required in configure and put them into
// PREAD_PROTOTYPE and PWRITE_PROTOTYPE.
#if defined HAVE_PREAD && defined PREAD_PROTOTYPE
PREAD_PROTOTYPE
#endif
#if defined HAVE_PWRITE && defined PWRITE_PROTOTYPE
PWRITE_PROTOTYPE
#endif
bool
io_unlink(const std::string & filename)
{
if (posixy_unlink(filename.c_str()) == 0) {
return true;
}
if (errno != ENOENT) {
throw Xapian::DatabaseError(filename + ": delete failed", errno);
}
return false;
}
// The smallest fd we want to use for a writable handle.
//
// We want to avoid using fd < MIN_WRITE_FD, in case some other code in the
// same process tries to write to stdout or stderr, which would end up
// corrupting our database.
const int MIN_WRITE_FD = 3;
static int
move_to_higher_fd_(int fd)
{
int badfd = fd;
#ifdef F_DUPFD_CLOEXEC
// dup to the first unused fd >= MIN_WRITE_FD.
fd = fcntl(badfd, F_DUPFD_CLOEXEC, MIN_WRITE_FD);
// F_DUPFD_CLOEXEC may not be supported.
if (fd < 0 && errno == EINVAL)
#endif
#ifdef F_DUPFD
{
fd = fcntl(badfd, F_DUPFD, MIN_WRITE_FD);
# ifdef FD_CLOEXEC
if (fd >= 0)
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
# endif
}
int save_errno = errno;
(void)close(badfd);
errno = save_errno;
#else
{
char toclose[MIN_WRITE_FD];
memset(toclose, 0, sizeof(toclose));
fd = badfd;
do {
toclose[fd] = 1;
fd = dup(fd);
} while (fd >= 0 && fd < MIN_WRITE_FD);
int save_errno = errno;
for (badfd = 0; badfd != MIN_WRITE_FD; ++badfd)
if (toclose[badfd])
close(badfd);
if (fd < 0) {
errno = save_errno;
} else {
# ifdef FD_CLOEXEC
(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
# endif
}
}
#endif
Assert(fd >= MIN_WRITE_FD || fd < 0);
return fd;
}
static inline int
move_to_higher_fd(int fd)
{
if (usual(fd >= MIN_WRITE_FD || fd < 0)) return fd;
return move_to_higher_fd_(fd);
}
/** Protect against stray writes to fds we use pwrite() on.
*
* Set the file position high to protect against user code or other libraries
* accidentally trying to write to our fd. To avoid problems we're rolling
* this out gradually on platforms we've tested it on.
*/
static inline void
protect_from_write(int fd)
{
#if !defined HAVE_PREAD || !defined HAVE_PWRITE
// No point setting the file position high here as it'll just get reset
// by the first block read or write.
(void)fd;
#elif defined __linux__
// The maximum off_t value works for at least btrfs.
if (lseek(fd, std::numeric_limits<off_t>::max(), SEEK_SET) < 0) {
if constexpr (sizeof(off_t) > 4) {
// Try the actual maximum for ext4 (which matches the documented
// maximum filesize) since ext4 is very widely used.
(void)lseek(fd, off_t(0xffffffff000), SEEK_SET);
}
}
#elif defined _AIX
// It seems prudent to try the maximum off_t value first.
if (lseek(fd, std::numeric_limits<off_t>::max(), SEEK_SET) < 0) {
if constexpr (sizeof(off_t) > 4) {
// Actual maximum seen in testing AIX 7.1 and 7.3 on JFS.
(void)lseek(fd, off_t(0xffffffff000), SEEK_SET);
}
}
#elif defined __CYGWIN__ || \
defined __DragonFly__ || \
defined __FreeBSD__ || \
defined __APPLE__ || \
defined __NetBSD__ || \
defined __OpenBSD__ || \
defined __sun__
// The maximum off_t value worked in testing on:
// * Cygwin 3.6.5
// * DragonFlyBSD 6.4.2
// * FreeBSD 14.0 and 15.0
// * macOS 10.10 and 12.6
// * NetBSD 10.0
// * OpenBSD 7.5
// * Solaris 10 and 11.4
(void)lseek(fd, std::numeric_limits<off_t>::max(), SEEK_SET);
#elif defined __EMSCRIPTEN__
if constexpr (sizeof(off_t) > 4) {
// Anything larger fails with EOVERFLOW (tested with Emscripten SDK
// 4.0.19).
(void)lseek(fd, off_t(0x20000000000000), SEEK_SET);
}
#elif defined __WIN32__
// For Microsoft Windows we open the file with CreateFile() and
// FILE_FLAG_OVERLAPPED so write() will always fail with EINVAL which
// protects us from accidental writes. Tested on Microsoft Windows Server
// 2025 10.0.26100.
(void)fd;
#else
(void)fd;
#endif
}
int
io_open_block_wr(const char* filename, bool anew)
{
#ifndef __WIN32__
// Use auto because on AIX O_CLOEXEC may be a 64-bit integer constant.
auto flags = O_RDWR | O_BINARY | O_CLOEXEC;
if (anew) flags |= O_CREAT | O_TRUNC;
int fd = ::open(filename, flags, 0666);
#else
// Microsoft Windows lacks the POSIX standard function pwrite().
// We can still protect the fd from accidental writes by opening the file
// with CreateFile() and specifying FILE_FLAG_OVERLAPPED, then wrapping the
// returned handle in a file descriptor to give a file descriptor for which
// write() fails with EINVAL. We implement an equivalent to pwrite() using
// WriteFile().
HANDLE handleWin =
CreateFile(filename,
GENERIC_READ | GENERIC_WRITE,
/* Subsequent operations may open this file to read, write
* or delete it */
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
anew ? CREATE_ALWAYS : OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (handleWin == INVALID_HANDLE_VALUE) {
return posixy_set_errno_from_getlasterror();
}
// Wrap in a standard file descriptor.
int fd = _open_osfhandle((intptr_t)(handleWin), O_RDWR|O_BINARY);
#endif
if (fd >= 0) {
fd = move_to_higher_fd(fd);
protect_from_write(fd);
}
return fd;
}
int
io_open_stream_wr(const char* filename, bool anew)
{
// Use auto because on AIX O_CLOEXEC may be a 64-bit integer constant.
auto flags = O_RDWR | O_BINARY | O_CLOEXEC;
if (anew) flags |= O_CREAT | O_TRUNC;
int fd = ::open(filename, flags, 0666);
return move_to_higher_fd(fd);
}
size_t
io_read(int fd, char * p, size_t n, size_t min)
{
size_t total = 0;
while (n) {
ssize_t c = read(fd, p, n);
if (c <= 0) {
if (c == 0) {
if (total >= min) break;
throw Xapian::DatabaseCorruptError("Couldn't read enough (EOF)");
}
if (errno == EINTR) continue;
throw Xapian::DatabaseError("Error reading from file", errno);
}
p += c;
total += c;
n -= c;
}
return total;
}
/** Write n bytes from block pointed to by p to file descriptor fd. */
void
io_write(int fd, const char * p, size_t n)
{
while (n) {
ssize_t c = write(fd, p, n);
if (c < 0) {
if (errno == EINTR) continue;
throw Xapian::DatabaseError("Error writing to file", errno);
}
p += c;
n -= c;
}
}
size_t
io_pread(int fd, char * p, size_t n, off_t o, size_t min)
{
#ifdef HAVE_PREAD
size_t total = 0;
while (true) {
ssize_t c = pread(fd, p, n, o);
// We should get a full read most of the time, so streamline that case.
if (usual(c == ssize_t(n)))
return total + n;
// -1 is error, 0 is EOF
if (c <= 0) {
if (c == 0) {
if (min == 0)
return total;
throw Xapian::DatabaseError("EOF reading database");
}
// We get EINTR if the syscall was interrupted by a signal.
// In this case we should retry the read.
if (errno == EINTR) continue;
throw Xapian::DatabaseError("Error reading database", errno);
}
total += c;
if (total >= min)
return total;
p += c;
n -= c;
o += c;
}
#elif defined __WIN32__
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
// _get_osfhandle() sets errno to EBADF.
throw Xapian::DatabaseError("Error reading database", errno);
}
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.Offset = (DWORD)o;
if constexpr (sizeof(off_t) > 4) {
overlapped.OffsetHigh = o >> 32;
}
DWORD c;
if (!ReadFile(h, p, n, &c, &overlapped)) {
if (GetLastError() != ERROR_IO_PENDING ||
!GetOverlappedResult(h,
&overlapped,
&c,
TRUE)) {
throw Xapian::DatabaseError("Error reading database",
-GetLastError());
}
}
if (c < min) {
throw Xapian::DatabaseError("EOF reading database");
}
return c;
#else
size_t total = 0;
if (rare(lseek(fd, o, SEEK_SET) < 0))
throw Xapian::DatabaseError("Error seeking database", errno);
while (true) {
ssize_t c = read(fd, p, n);
// We should get a full read most of the time, so streamline that case.
if (usual(c == ssize_t(n)))
return total + n;
if (c <= 0) {
if (c == 0) {
if (min == 0)
return total;
throw Xapian::DatabaseError("EOF reading database");
}
// We get EINTR if the syscall was interrupted by a signal.
// In this case we should retry the read.
if (errno == EINTR) continue;
throw Xapian::DatabaseError("Error reading database", errno);
}
total += c;
if (total >= min)
return total;
p += c;
n -= c;
}
#endif
}
void
io_pwrite(int fd, const char * p, size_t n, off_t o)
{
#ifdef HAVE_PWRITE
while (n) {
ssize_t c = pwrite(fd, p, n, o);
// We should get a full write most of the time, so streamline that
// case.
if (usual(c == ssize_t(n)))
return;
if (c < 0) {
if (errno == EINTR) continue;
throw Xapian::DatabaseError("Error writing database", errno);
}
p += c;
n -= c;
o += c;
}
#elif defined __WIN32__
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
// _get_osfhandle() sets errno to EBADF.
throw Xapian::DatabaseError("Error writing database", errno);
}
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.Offset = (DWORD)o;
if constexpr (sizeof(off_t) > 4) {
overlapped.OffsetHigh = o >> 32;
}
DWORD c;
if (!WriteFile(h, p, n, &c, &overlapped)) {
if (GetLastError() != ERROR_IO_PENDING ||
!GetOverlappedResult(h,
&overlapped,
&c,
TRUE)) {
throw Xapian::DatabaseError("Error writing database",
-GetLastError());
}
}
#else
if (rare(lseek(fd, o, SEEK_SET) < 0))
throw Xapian::DatabaseError("Error seeking database", errno);
io_write(fd, p, n);
#endif
}
[[noreturn]]
static void
throw_block_error(const char * s, off_t b, int e = 0)
{
std::string m = s;
m += str(b);
throw Xapian::DatabaseError(m, e);
}
#ifdef HAVE_POSIX_FADVISE
bool
io_readahead_block(int fd, size_t n, off_t b, off_t o)
{
o += b * n;
// Assume that any failure is likely to also happen for another call with
// the same fd.
return posix_fadvise(fd, o, n, POSIX_FADV_WILLNEED) == 0;
}
#endif
void
io_read_block(int fd, char * p, size_t n, off_t b, off_t o)
{
o += b * n;
// Prefer pread if available since it's typically implemented as a
// separate syscall, and that eliminates the overhead of an extra syscall
// per block read.
#ifdef HAVE_PREAD
while (true) {
ssize_t c = pread(fd, p, n, o);
// We should get a full read most of the time, so streamline that case.
if (usual(c == ssize_t(n)))
return;
// -1 is error, 0 is EOF
if (c <= 0) {
if (c == 0)
throw_block_error("EOF reading block ", b);
// We get EINTR if the syscall was interrupted by a signal.
// In this case we should retry the read.
if (errno == EINTR) continue;
throw_block_error("Error reading block ", b, errno);
}
p += c;
n -= c;
o += c;
}
#elif defined __WIN32__
// Using ReadFile() seems to be faster than lseek() and read().
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
// _get_osfhandle() sets errno to EBADF.
throw_block_error("Error reading block ", b, errno);
}
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.Offset = (DWORD)o;
if constexpr (sizeof(off_t) > 4) {
overlapped.OffsetHigh = o >> 32;
}
DWORD c;
if (!ReadFile(h, p, n, &c, &overlapped)) {
if (GetLastError() != ERROR_IO_PENDING ||
!GetOverlappedResult(h,
&overlapped,
&c,
TRUE)) {
throw_block_error("Error reading block ", b, -GetLastError());
}
}
if (c != n) {
throw_block_error("EOF reading block ", b);
}
#else
if (rare(lseek(fd, o, SEEK_SET) < 0))
throw_block_error("Error seeking to block ", b, errno);
while (true) {
ssize_t c = read(fd, p, n);
// We should get a full read most of the time, so streamline that case.
if (usual(c == ssize_t(n)))
return;
if (c <= 0) {
if (c == 0)
throw_block_error("EOF reading block ", b);
// We get EINTR if the syscall was interrupted by a signal.
// In this case we should retry the read.
if (errno == EINTR) continue;
throw_block_error("Error reading block ", b, errno);
}
p += c;
n -= c;
}
#endif
}
void
io_write_block(int fd, const char * p, size_t n, off_t b, off_t o)
{
o += b * n;
// Prefer pwrite if available since it's typically implemented as a
// separate syscall, and that eliminates the overhead of an extra syscall
// per block write.
#ifdef HAVE_PWRITE
while (true) {
ssize_t c = pwrite(fd, p, n, o);
// We should get a full write most of the time, so streamline that case.
if (usual(c == ssize_t(n)))
return;
if (c < 0) {
// We get EINTR if the syscall was interrupted by a signal.
// In this case we should retry the write.
if (errno == EINTR) continue;
throw_block_error("Error writing block ", b, errno);
}
p += c;
n -= c;
o += c;
}
#elif defined __WIN32__
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
// _get_osfhandle() sets errno to EBADF.
throw_block_error("Error writing block ", b, errno);
}
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.Offset = (DWORD)o;
if constexpr (sizeof(off_t) > 4) {
overlapped.OffsetHigh = o >> 32;
}
DWORD c;
if (!WriteFile(h, p, n, &c, &overlapped)) {
if (GetLastError() != ERROR_IO_PENDING ||
!GetOverlappedResult(h,
&overlapped,
&c,
TRUE)) {
throw_block_error("Error writing block ", b, -GetLastError());
}
}
#else
if (rare(lseek(fd, o, SEEK_SET) < 0))
throw_block_error("Error seeking to block ", b, errno);
while (true) {
ssize_t c = write(fd, p, n);
// We should get a full write most of the time, so streamline that case.
if (usual(c == ssize_t(n)))
return;
if (c < 0) {
// We get EINTR if the syscall was interrupted by a signal.
// In this case we should retry the write.
if (errno == EINTR) continue;
throw_block_error("Error writing block ", b, errno);
}
p += c;
n -= c;
}
#endif
}
bool
io_tmp_rename(const std::string & tmp_file, const std::string & real_file)
{
#ifdef EXDEV
// We retry on EXDEV a few times as some older Linux kernels are buggy and
// fail with EXDEV when the two files are on the same device (as they
// always ought to be when this function is used). Don't retry forever in
// case someone calls this with files on different devices.
//
// We're not sure exactly which kernels are buggy in this way, but there's
// discussion here: https://www.spinics.net/lists/linux-nfs/msg17306.html
//
// Reported at: https://trac.xapian.org/ticket/698
int retries = 5;
retry:
#endif
if (posixy_rename(tmp_file.c_str(), real_file.c_str()) < 0) {
#ifdef EXDEV
if (errno == EXDEV && --retries > 0) goto retry;
#endif
// With NFS, rename() failing may just mean that the server crashed
// after successfully renaming, but before reporting this, and then
// the retried operation fails. So we need to check if the source
// file still exists, which we do by calling unlink(), since we want
// to remove the temporary file anyway.
int saved_errno = errno;
if (unlink(tmp_file.c_str()) == 0 || errno != ENOENT) {
errno = saved_errno;
return false;
}
}
return true;
}
|