xapian-core  2.0.0
io_utils.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006-2025 Olly Betts
5  * Copyright (C) 2010 Richard Boulton
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef XAPIAN_INCLUDED_IO_UTILS_H
23 #define XAPIAN_INCLUDED_IO_UTILS_H
24 
25 #ifndef PACKAGE
26 # error config.h must be included first in each C++ source file
27 #endif
28 
29 #include <sys/types.h>
30 #include "safefcntl.h"
31 #include "safeunistd.h"
32 #include <string>
33 
38 inline int io_open_block_rd(const char* filename) {
39  return ::open(filename, O_RDONLY | O_BINARY | O_CLOEXEC);
40 }
41 
46 inline int io_open_block_rd(const std::string& filename)
47 {
48  return io_open_block_rd(filename.c_str());
49 }
50 
56 int io_open_block_wr(const char* filename, bool anew);
57 
63 inline int io_open_block_wr(const std::string& filename, bool anew)
64 {
65  return io_open_block_wr(filename.c_str(), anew);
66 }
67 
72 inline int io_open_stream_rd(const char* filename) {
73  return ::open(filename, O_RDONLY | O_BINARY | O_CLOEXEC);
74 }
75 
80 inline int io_open_stream_rd(const std::string& filename)
81 {
82  return io_open_stream_rd(filename.c_str());
83 }
84 
90 int io_open_stream_wr(const char* filename, bool anew);
91 
97 inline int io_open_stream_wr(const std::string& filename, bool anew)
98 {
99  return io_open_stream_wr(filename.c_str(), anew);
100 }
101 
107 inline bool io_sync(int fd)
108 {
109 #if defined HAVE_FDATASYNC
110  // If we have it, prefer fdatasync() over fsync() as the former avoids
111  // updating the access time so is probably a little more efficient.
112  return fdatasync(fd) == 0;
113 #elif defined HAVE_FSYNC
114  return fsync(fd) == 0;
115 #elif defined __WIN32__
116  return _commit(fd) == 0;
117 #else
118 # error Cannot implement io_sync() without fdatasync(), fsync(), or _commit()
119 #endif
120 }
121 
122 inline bool io_full_sync(int fd)
123 {
124 #ifdef F_FULLFSYNC
125  /* Only supported on macOS (at the time of writing at least).
126  *
127  * This call ensures that data has actually been written to disk, not just
128  * to the drive's write cache, so it provides better protection from power
129  * failures, etc. It does take longer though.
130  *
131  * According to the sqlite sources, this shouldn't fail on a local FS so
132  * a failure means that the file system doesn't support this operation and
133  * therefore it's best to fallback to fdatasync()/fsync().
134  */
135  if (fcntl(fd, F_FULLFSYNC, 0) == 0)
136  return true;
137 #endif
138  return io_sync(fd);
139 }
140 
151 size_t io_read(int fd, char * p, size_t n, size_t min = 0);
152 
154 void io_write(int fd, const char * p, size_t n);
155 
156 inline void io_write(int fd, const unsigned char * p, size_t n) {
157  io_write(fd, reinterpret_cast<const char *>(p), n);
158 }
159 
172 size_t io_pread(int fd, char * p, size_t n, off_t o, size_t min = 0);
173 
181 void io_pwrite(int fd, const char * p, size_t n, off_t o);
182 
187 #ifdef HAVE_POSIX_FADVISE
188 bool io_readahead_block(int fd, size_t n, off_t b, off_t o = 0);
189 #else
190 inline bool io_readahead_block(int, size_t, off_t, off_t = 0) { return false; }
191 #endif
192 
194 void io_read_block(int fd, char * p, size_t n, off_t b, off_t o = 0);
195 
197 void io_write_block(int fd, const char * p, size_t n, off_t b, off_t o = 0);
198 
199 inline void io_write_block(int fd, const unsigned char * p, size_t n, off_t b) {
200  io_write_block(fd, reinterpret_cast<const char *>(p), n, b);
201 }
202 
215 bool io_unlink(const std::string & filename);
216 
225 bool io_tmp_rename(const std::string & tmp_file, const std::string & real_file);
226 
227 #endif // XAPIAN_INCLUDED_IO_UTILS_H
PositionList * p
void io_write_block(int fd, const char *p, size_t n, off_t b, off_t o=0)
Write block b size n bytes from buffer p to file descriptor fd, offset o.
Definition: io_utils.cc:507
size_t io_read(int fd, char *p, size_t n, size_t min=0)
Read n bytes (or until EOF) into block pointed to by p from file descriptor fd.
Definition: io_utils.cc:241
int io_open_block_wr(const char *filename, bool anew)
Open a block-based file for writing.
Definition: io_utils.cc:192
bool io_unlink(const std::string &filename)
Delete a file.
Definition: io_utils.cc:56
void io_write(int fd, const char *p, size_t n)
Write n bytes from block pointed to by p to file descriptor fd.
Definition: io_utils.cc:263
int io_open_stream_rd(const char *filename)
Open a stream-based file for reading.
Definition: io_utils.h:72
bool io_sync(int fd)
Ensure all data previously written to file descriptor fd has been written to disk.
Definition: io_utils.h:107
void io_pwrite(int fd, const char *p, size_t n, off_t o)
Write n bytes from block pointed to by p to file descriptor fd starting at position o.
Definition: io_utils.cc:363
bool io_full_sync(int fd)
Definition: io_utils.h:122
void io_read_block(int fd, char *p, size_t n, off_t b, off_t o=0)
Read block b size n bytes into buffer p from file descriptor fd, offset o.
Definition: io_utils.cc:432
int io_open_stream_wr(const char *filename, bool anew)
Open a stream-based file for writing.
Definition: io_utils.cc:231
bool io_readahead_block(int, size_t, off_t, off_t=0)
Readahead block b size n bytes from file descriptor fd.
Definition: io_utils.h:190
bool io_tmp_rename(const std::string &tmp_file, const std::string &real_file)
Rename a temporary file to its final position.
Definition: io_utils.cc:573
size_t io_pread(int fd, char *p, size_t n, off_t o, size_t min=0)
Read n bytes (or until EOF) into block pointed to by p from file descriptor fd starting at position o...
Definition: io_utils.cc:277
int io_open_block_rd(const char *filename)
Open a block-based file for reading.
Definition: io_utils.h:38
Database open(std::string_view host, unsigned int port, unsigned timeout=10000, unsigned connect_timeout=10000)
Construct a Database object for read-only access to a remote database accessed via a TCP connection.
include <fcntl.h>, but working around broken platforms.
#define O_BINARY
Definition: safefcntl.h:80
#define O_CLOEXEC
Definition: safefcntl.h:89
<unistd.h>, but with compat.