qBittorrent
qtlockedfile_unix.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2019 Mike Tzou (Chocobo1)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  *
28 ****************************************************************************
29 **
30 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
31 ** Contact: http://www.qt-project.org/legal
32 **
33 ** This file is part of the Qt Solutions component.
34 **
35 ** $QT_BEGIN_LICENSE:BSD$
36 ** You may use this file under the terms of the BSD license as follows:
37 **
38 ** "Redistribution and use in source and binary forms, with or without
39 ** modification, are permitted provided that the following conditions are
40 ** met:
41 ** * Redistributions of source code must retain the above copyright
42 ** notice, this list of conditions and the following disclaimer.
43 ** * Redistributions in binary form must reproduce the above copyright
44 ** notice, this list of conditions and the following disclaimer in
45 ** the documentation and/or other materials provided with the
46 ** distribution.
47 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
48 ** of its contributors may be used to endorse or promote products derived
49 ** from this software without specific prior written permission.
50 **
51 **
52 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
63 **
64 ** $QT_END_LICENSE$
65 **
66 ****************************************************************************
67 */
68 
69 #include "qtlockedfile.h"
70 
71 #include <errno.h>
72 #include <fcntl.h>
73 #include <string.h>
74 #include <unistd.h>
75 
76 bool QtLockedFile::lock(const LockMode mode, const bool block)
77 {
78  if (!isOpen())
79  {
80  qWarning("QtLockedFile::lock(): file is not opened");
81  return false;
82  }
83 
84  if (mode == NoLock)
85  return unlock();
86 
87  if (mode == m_lockMode)
88  return true;
89 
90  if (m_lockMode != NoLock)
91  unlock();
92 
93  struct flock fl;
94  fl.l_whence = SEEK_SET;
95  fl.l_start = 0;
96  fl.l_len = 0;
97  fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK;
98  int cmd = block ? F_SETLKW : F_SETLK;
99  int ret = fcntl(handle(), cmd, &fl);
100 
101  if (ret == -1)
102  {
103  if (errno != EINTR && errno != EAGAIN)
104  qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
105  return false;
106  }
107 
108  m_lockMode = mode;
109  return true;
110 }
111 
112 bool QtLockedFile::unlock()
113 {
114  if (!isOpen())
115  {
116  qWarning("QtLockedFile::unlock(): file is not opened");
117  return false;
118  }
119 
120  if (!isLocked())
121  return true;
122 
123  struct flock fl;
124  fl.l_whence = SEEK_SET;
125  fl.l_start = 0;
126  fl.l_len = 0;
127  fl.l_type = F_UNLCK;
128  int ret = fcntl(handle(), F_SETLKW, &fl);
129 
130  if (ret == -1)
131  {
132  qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
133  return false;
134  }
135 
136  m_lockMode = NoLock;
137  return true;
138 }
139 
140 QtLockedFile::~QtLockedFile()
141 {
142  if (isOpen())
143  unlock();
144 }