3 Copyright (C) 2008 jlh (jlh at gmx dot ch)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2 of the License, version 3 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 The GNU General Public License version 2 is included with the source of
20 this program under the file name COPYING. You can also get a copy on
26 #include <sys/types.h>
34 LockFile::LockFile() :
39 LockFile::~LockFile() {
43 bool LockFile::lock(const QString
&fn
) {
45 fd
= open(QFile::encodeName(fileName
).constData(), O_CREAT
| O_WRONLY
, 0644);
48 debug("ERROR: opening lock file failed");
52 if (flock(fd
, LOCK_EX
| LOCK_NB
) != 0) {
55 debug("ERROR: cannot get lock on lock file");
59 // set the FD_CLOEXEC flag, so that when another process forks off of
60 // us, it won't inherit the file descriptor and the lock. see commit
61 // ce6f838b4587528630784c1ea1ad272b64e78544 to see why we do this
63 int flags
= fcntl(fd
, F_GETFD
, 0);
66 debug("ERROR: failed to fcntl() lock file");
72 flags
= fcntl(fd
, F_SETFD
, flags
);
75 debug("ERROR: failed to set FD_CLOEXEC on lock file");
80 debug("Got lock on lock file");
84 void LockFile::unlock() {
87 QFile::remove(fileName
);
93 bool LockFile::isLocked() const {