"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/qtsingleapplication/src/qtlockedfile_unix.cpp" (12 Feb 2021, 3456 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "qtlockedfile_unix.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.9.9_vs_2.0.0.

    1 /****************************************************************************
    2 **
    3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
    4 ** Contact: http://www.qt-project.org/legal
    5 **
    6 ** This file is part of the Qt Solutions component.
    7 **
    8 ** $QT_BEGIN_LICENSE:BSD$
    9 ** You may use this file under the terms of the BSD license as follows:
   10 **
   11 ** "Redistribution and use in source and binary forms, with or without
   12 ** modification, are permitted provided that the following conditions are
   13 ** met:
   14 **   * Redistributions of source code must retain the above copyright
   15 **     notice, this list of conditions and the following disclaimer.
   16 **   * Redistributions in binary form must reproduce the above copyright
   17 **     notice, this list of conditions and the following disclaimer in
   18 **     the documentation and/or other materials provided with the
   19 **     distribution.
   20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
   21 **     of its contributors may be used to endorse or promote products derived
   22 **     from this software without specific prior written permission.
   23 **
   24 **
   25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
   35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
   36 **
   37 ** $QT_END_LICENSE$
   38 **
   39 ****************************************************************************/
   40 
   41 #include <string.h>
   42 #include <errno.h>
   43 #include <unistd.h>
   44 #include <fcntl.h>
   45 
   46 #include "qtlockedfile.h"
   47 
   48 bool QtLockedFile::lock(LockMode mode, bool block)
   49 {
   50     if (!isOpen())
   51     {
   52         qWarning("QtLockedFile::lock(): file is not opened");
   53         return false;
   54     }
   55 
   56     if (mode == NoLock)
   57         return unlock();
   58 
   59     if (mode == m_lock_mode)
   60         return true;
   61 
   62     if (m_lock_mode != NoLock)
   63         unlock();
   64 
   65     struct flock fl;
   66     fl.l_whence = SEEK_SET;
   67     fl.l_start = 0;
   68     fl.l_len = 0;
   69     fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK;
   70     int cmd = block ? F_SETLKW : F_SETLK;
   71     int ret = fcntl(handle(), cmd, &fl);
   72 
   73     if (ret == -1)
   74     {
   75         if (errno != EINTR && errno != EAGAIN)
   76             qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
   77         return false;
   78     }
   79 
   80 
   81     m_lock_mode = mode;
   82     return true;
   83 }
   84 
   85 
   86 bool QtLockedFile::unlock()
   87 {
   88     if (!isOpen())
   89     {
   90         qWarning("QtLockedFile::unlock(): file is not opened");
   91         return false;
   92     }
   93 
   94     if (!isLocked())
   95         return true;
   96 
   97     struct flock fl;
   98     fl.l_whence = SEEK_SET;
   99     fl.l_start = 0;
  100     fl.l_len = 0;
  101     fl.l_type = F_UNLCK;
  102     int ret = fcntl(handle(), F_SETLKW, &fl);
  103 
  104     if (ret == -1)
  105     {
  106         qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
  107         return false;
  108     }
  109 
  110     m_lock_mode = NoLock;
  111     return true;
  112 }
  113 
  114 QtLockedFile::~QtLockedFile()
  115 {
  116     if (isOpen())
  117         unlock();
  118 }
  119