xapian-core  1.4.25
io_utils.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2009,2011,2014,2015,2016 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, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 * fname) {
39  return ::open(fname, O_RDONLY | O_BINARY | O_CLOEXEC);
40 }
41 
46 inline int io_open_block_rd(const std::string & fname)
47 {
48  return io_open_block_rd(fname.c_str());
49 }
50 
56 int io_open_block_wr(const char * fname, bool anew);
57 
63 inline int io_open_block_wr(const std::string & fname, bool anew)
64 {
65  return io_open_block_wr(fname.c_str(), anew);
66 }
67 
73 inline bool io_sync(int fd)
74 {
75 #if defined HAVE_FDATASYNC
76  // If we have it, prefer fdatasync() over fsync() as the former avoids
77  // updating the access time so is probably a little more efficient.
78  return fdatasync(fd) == 0;
79 #elif defined HAVE_FSYNC
80  return fsync(fd) == 0;
81 #elif defined __WIN32__
82  return _commit(fd) == 0;
83 #else
84 # error Cannot implement io_sync() without fdatasync(), fsync(), or _commit()
85 #endif
86 }
87 
88 inline bool io_full_sync(int fd)
89 {
90 #ifdef F_FULLFSYNC
91  /* Only supported on macOS (at the time of writing at least).
92  *
93  * This call ensures that data has actually been written to disk, not just
94  * to the drive's write cache, so it provides better protection from power
95  * failures, etc. It does take longer though.
96  *
97  * According to the sqlite sources, this shouldn't fail on a local FS so
98  * a failure means that the file system doesn't support this operation and
99  * therefore it's best to fallback to fdatasync()/fsync().
100  */
101  if (fcntl(fd, F_FULLFSYNC, 0) == 0)
102  return true;
103 #endif
104  return io_sync(fd);
105 }
106 
117 size_t io_read(int fd, char * p, size_t n, size_t min = 0);
118 
120 void io_write(int fd, const char * p, size_t n);
121 
122 inline void io_write(int fd, const unsigned char * p, size_t n) {
123  io_write(fd, reinterpret_cast<const char *>(p), n);
124 }
125 
130 #ifdef HAVE_POSIX_FADVISE
131 bool io_readahead_block(int fd, size_t n, off_t b, off_t o = 0);
132 #else
133 inline bool io_readahead_block(int, size_t, off_t, off_t = 0) { return false; }
134 #endif
135 
137 void io_read_block(int fd, char * p, size_t n, off_t b, off_t o = 0);
138 
140 void io_write_block(int fd, const char * p, size_t n, off_t b, off_t o = 0);
141 
142 inline void io_write_block(int fd, const unsigned char * p, size_t n, off_t b) {
143  io_write_block(fd, reinterpret_cast<const char *>(p), n, b);
144 }
145 
158 bool io_unlink(const std::string & filename);
159 
168 bool io_tmp_rename(const std::string & tmp_file, const std::string & real_file);
169 
170 #endif // XAPIAN_INCLUDED_IO_UTILS_H
bool io_unlink(const std::string &filename)
Delete a file.
Definition: io_utils.cc:52
int io_open_block_rd(const char *fname)
Open a block-based file for reading.
Definition: io_utils.h:38
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:228
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:123
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:180
bool io_sync(int fd)
Ensure all data previously written to file descriptor fd has been written to disk.
Definition: io_utils.h:73
#define O_BINARY
Definition: safefcntl.h:81
WritableDatabase open()
Construct a WritableDatabase object for a new, empty InMemory database.
Definition: dbfactory.h:104
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:133
#define O_CLOEXEC
Definition: safefcntl.h:90
int io_open_block_wr(const char *fname, bool anew)
Open a block-based file for writing.
Definition: io_utils.cc:67
bool io_full_sync(int fd)
Definition: io_utils.h:88
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:271
<unistd.h>, but with compat.
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:145
include <fcntl.h>, but working around broken platforms.