File indexing completed on 2024-05-19 04:38:46
0001 /**************************************************************************** 0002 ** 0003 ** Copyright (C) 2016 The Qt Company Ltd. 0004 ** Contact: https://www.qt.io/licensing/ 0005 ** 0006 ** This file is part of Qt Creator. 0007 ** 0008 ** Commercial License Usage 0009 ** Licensees holding valid commercial Qt licenses may use this file in 0010 ** accordance with the commercial license agreement provided with the 0011 ** Software or, alternatively, in accordance with the terms contained in 0012 ** a written agreement between you and The Qt Company. For licensing terms 0013 ** and conditions see https://www.qt.io/terms-conditions. For further 0014 ** information use the contact form at https://www.qt.io/contact-us. 0015 ** 0016 ** GNU General Public License Usage 0017 ** Alternatively, this file may be used under the terms of the GNU 0018 ** General Public License version 3 as published by the Free Software 0019 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT 0020 ** included in the packaging of this file. Please review the following 0021 ** information to ensure the GNU General Public License requirements will 0022 ** be met: https://www.gnu.org/licenses/gpl-3.0.html. 0023 ** 0024 ****************************************************************************/ 0025 0026 #include "qtlockedfile.h" 0027 0028 #include <string.h> 0029 #include <errno.h> 0030 #include <unistd.h> 0031 #include <fcntl.h> 0032 0033 namespace SharedTools { 0034 0035 bool QtLockedFile::lock(LockMode mode, bool block) 0036 { 0037 if (!isOpen()) { 0038 qWarning("QtLockedFile::lock(): file is not opened"); 0039 return false; 0040 } 0041 0042 if (mode == NoLock) 0043 return unlock(); 0044 0045 if (mode == m_lock_mode) 0046 return true; 0047 0048 if (m_lock_mode != NoLock) 0049 unlock(); 0050 0051 struct flock fl; 0052 fl.l_whence = SEEK_SET; 0053 fl.l_start = 0; 0054 fl.l_len = 0; 0055 fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK; 0056 int cmd = block ? F_SETLKW : F_SETLK; 0057 int ret = fcntl(handle(), cmd, &fl); 0058 0059 if (ret == -1) { 0060 if (errno != EINTR && errno != EAGAIN) 0061 qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno)); 0062 return false; 0063 } 0064 0065 0066 m_lock_mode = mode; 0067 return true; 0068 } 0069 0070 0071 bool QtLockedFile::unlock() 0072 { 0073 if (!isOpen()) { 0074 qWarning("QtLockedFile::unlock(): file is not opened"); 0075 return false; 0076 } 0077 0078 if (!isLocked()) 0079 return true; 0080 0081 struct flock fl; 0082 fl.l_whence = SEEK_SET; 0083 fl.l_start = 0; 0084 fl.l_len = 0; 0085 fl.l_type = F_UNLCK; 0086 int ret = fcntl(handle(), F_SETLKW, &fl); 0087 0088 if (ret == -1) { 0089 qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno)); 0090 return false; 0091 } 0092 0093 m_lock_mode = NoLock; 0094 remove(); 0095 return true; 0096 } 0097 0098 QtLockedFile::~QtLockedFile() 0099 { 0100 if (isOpen()) 0101 unlock(); 0102 } 0103 0104 } // namespace SharedTools