LCOV - code coverage report
Current view: top level - common - io_utils.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 8 10 80.0 %
Date: 2019-05-16 09:13:18 Functions: 4 5 80.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /** @file io_utils.h
       2                 :            :  * @brief Wrappers for low-level POSIX I/O routines.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2006,2007,2008,2009,2011,2014,2015,2016,2018 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                 :            :  */
      38                 :      15066 : inline int io_open_block_rd(const char * fname) {
      39                 :      15066 :     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                 :            :  */
      46                 :      15066 : inline int io_open_block_rd(const std::string & fname)
      47                 :            : {
      48                 :      15066 :     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                 :            :  */
      63                 :       7199 : inline int io_open_block_wr(const std::string & fname, bool anew)
      64                 :            : {
      65                 :       7199 :     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                 :            :  */
      73                 :      58270 : 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                 :      58270 :     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                 :          0 : 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                 :          0 :     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                 :            : 
     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                 :            : /** Read n bytes (or until EOF) into block pointed to by p from file descriptor
     127                 :            :  *  fd starting at position o.
     128                 :            :  *
     129                 :            :  *  If a read error occurs, throws DatabaseError.
     130                 :            :  *
     131                 :            :  *  If @a min is specified and EOF is reached after less than @a min bytes,
     132                 :            :  *  throws DatabaseCorruptError.
     133                 :            :  *
     134                 :            :  *  The current file position may or may not be updated.
     135                 :            :  *
     136                 :            :  *  Returns the number of bytes actually read.
     137                 :            :  */
     138                 :            : size_t io_pread(int fd, char * p, size_t n, off_t o, size_t min = 0);
     139                 :            : 
     140                 :            : /** Write n bytes from block pointed to by p to file descriptor fd starting at
     141                 :            :  *  position o.
     142                 :            :  *
     143                 :            :  *  If a read error occurs, throws DatabaseError.
     144                 :            :  *
     145                 :            :  *  The current file position may or may not be updated.
     146                 :            :  */
     147                 :            : void io_pwrite(int fd, const char * p, size_t n, off_t o);
     148                 :            : 
     149                 :            : /** Readahead block b size n bytes from file descriptor fd.
     150                 :            :  *
     151                 :            :  *  Returns false if we can't readahead on this fd.
     152                 :            :  */
     153                 :            : #ifdef HAVE_POSIX_FADVISE
     154                 :            : bool io_readahead_block(int fd, size_t n, off_t b, off_t o = 0);
     155                 :            : #else
     156                 :            : inline bool io_readahead_block(int, size_t, off_t, off_t = 0) { return false; }
     157                 :            : #endif
     158                 :            : 
     159                 :            : /// Read block b size n bytes into buffer p from file descriptor fd, offset o.
     160                 :            : void io_read_block(int fd, char * p, size_t n, off_t b, off_t o = 0);
     161                 :            : 
     162                 :            : /// Write block b size n bytes from buffer p to file descriptor fd, offset o.
     163                 :            : void io_write_block(int fd, const char * p, size_t n, off_t b, off_t o = 0);
     164                 :            : 
     165                 :            : inline void io_write_block(int fd, const unsigned char * p, size_t n, off_t b) {
     166                 :            :     io_write_block(fd, reinterpret_cast<const char *>(p), n, b);
     167                 :            : }
     168                 :            : 
     169                 :            : /** Delete a file.
     170                 :            :  *
     171                 :            :  *  @param      filename        The file to delete.
     172                 :            :  *
     173                 :            :  *  @exception  Xapian::DatabaseError is thrown if @a filename existed but
     174                 :            :  *              couldn't be unlinked.
     175                 :            :  *  @return     true if @a filename was successfully removed; false if it
     176                 :            :  *              didn't exist.  If the file is on NFS, false may be returned
     177                 :            :  *              even if the file was removed (if the server fails after
     178                 :            :  *              removing the file but before telling the client, and the
     179                 :            :  *              client then retries).
     180                 :            :  */
     181                 :            : bool io_unlink(const std::string & filename);
     182                 :            : 
     183                 :            : /** Rename a temporary file to its final position.
     184                 :            :  *
     185                 :            :  *  Attempts to deal with NFS infelicities.  If the rename fails, the temporary
     186                 :            :  *  file is removed.
     187                 :            :  *
     188                 :            :  *  @return     true if the rename succeeded; false if it failed (and errno will
     189                 :            :  *              be set appropriately).
     190                 :            :  */
     191                 :            : bool io_tmp_rename(const std::string & tmp_file, const std::string & real_file);
     192                 :            : 
     193                 :            : #endif // XAPIAN_INCLUDED_IO_UTILS_H

Generated by: LCOV version 1.11