OSDN Git Service

c8173a50a8c921d689d90eabae7eb983e9435bd4
[qt-creator-jp/qt-creator-jp.git] / src / shared / qtlockedfile / qtlockedfile_unix.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qtlockedfile.h"
35
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40
41 namespace SharedTools {
42
43 bool QtLockedFile::lock(LockMode mode, bool block)
44 {
45     if (!isOpen()) {
46         qWarning("QtLockedFile::lock(): file is not opened");
47         return false;
48     }
49
50     if (mode == NoLock)
51         return unlock();
52
53     if (mode == m_lock_mode)
54         return true;
55
56     if (m_lock_mode != NoLock)
57         unlock();
58
59     struct flock fl;
60     fl.l_whence = SEEK_SET;
61     fl.l_start = 0;
62     fl.l_len = 0;
63     fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK;
64     int cmd = block ? F_SETLKW : F_SETLK;
65     int ret = fcntl(handle(), cmd, &fl);
66
67     if (ret == -1) {
68         if (errno != EINTR && errno != EAGAIN)
69             qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
70         return false;
71     }
72
73
74     m_lock_mode = mode;
75     return true;
76 }
77
78
79 bool QtLockedFile::unlock()
80 {
81     if (!isOpen()) {
82         qWarning("QtLockedFile::unlock(): file is not opened");
83         return false;
84     }
85
86     if (!isLocked())
87         return true;
88
89     struct flock fl;
90     fl.l_whence = SEEK_SET;
91     fl.l_start = 0;
92     fl.l_len = 0;
93     fl.l_type = F_UNLCK;
94     int ret = fcntl(handle(), F_SETLKW, &fl);
95
96     if (ret == -1) {
97         qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
98         return false;
99     }
100
101     m_lock_mode = NoLock;
102     return true;
103 }
104
105 QtLockedFile::~QtLockedFile()
106 {
107     if (isOpen())
108         unlock();
109 }
110
111 } // namespace SharedTools