LCOV - code coverage report
Current view: top level - backends - flint_lock.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 7822d31adece Lines: 6 6 100.0 %
Date: 2019-05-23 11:15:29 Functions: 3 3 100.0 %
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file flint_lock.h
       2                 :            :  * @brief Flint-compatible database locking.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2005,2006,2007,2008,2009,2012,2014,2016,2017 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      19                 :            :  * USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef XAPIAN_INCLUDED_FLINT_LOCK_H
      23                 :            : #define XAPIAN_INCLUDED_FLINT_LOCK_H
      24                 :            : 
      25                 :            : #ifndef PACKAGE
      26                 :            : # error config.h must be included first in each C++ source file
      27                 :            : #endif
      28                 :            : 
      29                 :            : #include <string>
      30                 :            : 
      31                 :            : #if defined __CYGWIN__ || defined __WIN32__
      32                 :            : # include "safewindows.h"
      33                 :            : #else
      34                 :            : # include <sys/types.h>
      35                 :            : #endif
      36                 :            : 
      37                 :            : class FlintLock {
      38                 :            :     std::string filename;
      39                 :            : #if defined __CYGWIN__ || defined __WIN32__
      40                 :            :     HANDLE hFile = INVALID_HANDLE_VALUE;
      41                 :            : #elif defined FLINTLOCK_USE_FLOCK
      42                 :            :     int fd = -1;
      43                 :            : #else
      44                 :            :     int fd = -1;
      45                 :            :     pid_t pid;
      46                 :            : #endif
      47                 :            : 
      48                 :            :   public:
      49                 :            :     typedef enum {
      50                 :            :         SUCCESS, // We got the lock!
      51                 :            :         INUSE, // Already locked by someone else.
      52                 :            :         UNSUPPORTED, // Locking probably not supported (e.g. NFS without lockd).
      53                 :            :         FDLIMIT, // Process hit its file descriptor limit.
      54                 :            :         UNKNOWN // The attempt failed for some unspecified reason.
      55                 :            :     } reason;
      56                 :            : 
      57                 :            :     /** Standard constructor. */
      58                 :       4088 :     explicit FlintLock(const std::string &filename_)
      59                 :       4088 :         : filename(filename_) {
      60                 :            :         // Keep the same lockfile name as flint since the locking is compatible
      61                 :            :         // and this avoids the possibility of creating two databases in the
      62                 :            :         // same directory using different backends.
      63         [ +  - ]:       4088 :         filename += "/flintlock";
      64                 :       4088 :     }
      65                 :            : 
      66                 :            :     /** Constructor for use in read-only cases (like single-file glass). */
      67                 :        616 :     FlintLock() {}
      68                 :            : 
      69                 :            :     operator bool() const {
      70                 :            : #if defined __CYGWIN__ || defined __WIN32__
      71                 :            :         return hFile != INVALID_HANDLE_VALUE;
      72                 :            : #else
      73                 :            :         return fd != -1;
      74                 :            : #endif
      75                 :            :     }
      76                 :            : 
      77                 :            :     // Release any lock held when we're destroyed.
      78                 :       8786 :     ~FlintLock() { release(); }
      79                 :            : 
      80                 :            :     /** Test if the lock is held.
      81                 :            :      *
      82                 :            :      *  If this object holds the lock, just returns true.  Otherwise it will
      83                 :            :      *  try to test taking the lock, if that is possible to do on the current
      84                 :            :      *  platform without actually take it (fcntl() locks support this).
      85                 :            :      *
      86                 :            :      *  Throws Xapian::UnimplemenetedError if the platform doesn't support
      87                 :            :      *  testing the lock in this way, or Xapian::DatabaseLockError if there's
      88                 :            :      *  an error while trying to perform the test.
      89                 :            :      */
      90                 :            :     bool test() const;
      91                 :            : 
      92                 :            :     /** Attempt to obtain the lock.
      93                 :            :      *
      94                 :            :      *  If the attempt fails with code "UNKNOWN", the string supplied in the
      95                 :            :      *  explanation parameter will be set to contain any details available of
      96                 :            :      *  the reason for the failure.
      97                 :            :      *
      98                 :            :      *     @param exclusive  Get an exclusive lock?  Value currently ignored,
      99                 :            :      *                       and the lock is always exclusive.
     100                 :            :      *     @param wait       Wait until we can get the lock?
     101                 :            :      */
     102                 :            :     reason lock(bool exclusive, bool wait, std::string & explanation);
     103                 :            : 
     104                 :            :     /// Release the lock.
     105                 :            :     void release();
     106                 :            : 
     107                 :            :     /// Throw Xapian::DatabaseLockError.
     108                 :            :     [[noreturn]]
     109                 :            :     void throw_databaselockerror(FlintLock::reason why,
     110                 :            :                                  const std::string & db_dir,
     111                 :            :                                  const std::string & explanation) const;
     112                 :            : };
     113                 :            : 
     114                 :            : #endif // XAPIAN_INCLUDED_FLINT_LOCK_H

Generated by: LCOV version 1.11