1 /** @file
2  * @brief Wrappers for low-level POSIX I/O routines.
3  */
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 
34 /** Open a block-based file for reading.
35  *
36  *  @param fname  The path of the file to open.
37  */
io_open_block_rd(const char * fname)38 inline int io_open_block_rd(const char * fname) {
39     return ::open(fname, O_RDONLY | O_BINARY | O_CLOEXEC);
40 }
41 
42 /** Open a block-based file for reading.
43  *
44  *  @param fname  The path of the file to open.
45  */
io_open_block_rd(const std::string & fname)46 inline int io_open_block_rd(const std::string & fname)
47 {
48     return io_open_block_rd(fname.c_str());
49 }
50 
51 /** Open a block-based file for writing.
52  *
53  *  @param fname  The path of the file to open.
54  *  @param anew   If true, open the file anew (create or truncate it).
55  */
56 int io_open_block_wr(const char * fname, bool anew);
57 
58 /** Open a block-based file for writing.
59  *
60  *  @param fname  The path of the file to open.
61  *  @param anew  If true, open the file anew (create or truncate it).
62  */
io_open_block_wr(const std::string & fname,bool anew)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 
68 /** Ensure all data previously written to file descriptor fd has been written to
69  *  disk.
70  *
71  *  Returns false if this could not be done.
72  */
io_sync(int fd)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 
io_full_sync(int fd)88 inline bool io_full_sync(int fd)
89 {
90 #ifdef F_FULLFSYNC
91     /* Only supported on Mac OS X (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 
107 /** Read n bytes (or until EOF) into block pointed to by p from file descriptor
108  *  fd.
109  *
110  *  If a read error occurs, throws DatabaseError.
111  *
112  *  If @a min is specified and EOF is reached after less than @a min bytes,
113  *  throws DatabaseCorruptError.
114  *
115  *  Returns the number of bytes actually read.
116  */
117 size_t io_read(int fd, char * p, size_t n, size_t min = 0);
118 
119 /** Write n bytes from block pointed to by p to file descriptor fd. */
120 void io_write(int fd, const char * p, size_t n);
121 
io_write(int fd,const unsigned char * p,size_t n)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 
126 /** Readahead block b size n bytes from file descriptor fd.
127  *
128  *  Returns false if we can't readahead on this fd.
129  */
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 
136 /// Read block b size n bytes into buffer p from file descriptor fd, offset o.
137 void io_read_block(int fd, char * p, size_t n, off_t b, off_t o = 0);
138 
139 /// Write block b size n bytes from buffer p to file descriptor fd, offset o.
140 void io_write_block(int fd, const char * p, size_t n, off_t b, off_t o = 0);
141 
io_write_block(int fd,const unsigned char * p,size_t n,off_t b)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 
146 /** Delete a file.
147  *
148  *  @param	filename	The file to delete.
149  *
150  *  @exception	Xapian::DatabaseError is thrown if @a filename existed but
151  *		couldn't be unlinked.
152  *  @return	true if @a filename was successfully removed; false if it
153  *		didn't exist.  If the file is on NFS, false may be returned
154  *		even if the file was removed (if the server fails after
155  *		removing the file but before telling the client, and the
156  *		client then retries).
157  */
158 bool io_unlink(const std::string & filename);
159 
160 /** Rename a temporary file to its final position.
161  *
162  *  Attempts to deal with NFS infelicities.  If the rename fails, the temporary
163  *  file is removed.
164  *
165  *  @return	true if the rename succeeded; false if it failed (and errno will
166  *		be set appropriately).
167  */
168 bool io_tmp_rename(const std::string & tmp_file, const std::string & real_file);
169 
170 #endif // XAPIAN_INCLUDED_IO_UTILS_H
171