File indexing completed on 2024-04-21 14:55:38

0001 /*
0002    This file is part of the KDE libraries
0003    Copyright (c) 2007 Ralf Habacker <ralf.habacker@freenet.de>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License version 2 as published by the Free Software Foundation.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "klockfile.h"
0021 
0022 #include <windows.h>
0023 
0024 /**
0025  The win32 implementation uses CreateFile() without shared access rights
0026  to detect if the lock file is opened by another process.
0027 */
0028 
0029 class Q_DECL_HIDDEN KLockFile::Private
0030 {
0031 public:
0032     Private(const QString &f, const QString &componentName)
0033         : file(f),
0034           staleTime(0),
0035           isLocked(false),
0036           m_componentName(componentName)
0037     {
0038     }
0039 
0040     QString file;
0041     int staleTime;
0042     bool isLocked;
0043     QString m_componentName;
0044     HANDLE h;
0045 };
0046 
0047 KLockFile::KLockFile(const QString &file, const QString &componentName)
0048     : d(new Private(file, componentName))
0049 {
0050 }
0051 
0052 KLockFile::~KLockFile()
0053 {
0054     unlock();
0055     delete d;
0056 }
0057 
0058 int
0059 KLockFile::staleTime() const
0060 {
0061     return d->staleTime;
0062 }
0063 
0064 void
0065 KLockFile::setStaleTime(int _staleTime)
0066 {
0067     d->staleTime = _staleTime;
0068 }
0069 
0070 KLockFile::LockResult
0071 KLockFile::lock(LockFlags options)
0072 {
0073     if (d->isLocked) {
0074         return LockOK;
0075     }
0076 
0077     LockResult result;
0078 
0079     d->h = CreateFileW(
0080                (WCHAR *)d->file.utf16(),
0081                GENERIC_READ | GENERIC_WRITE,
0082                0,
0083                0,
0084                CREATE_ALWAYS,
0085                FILE_ATTRIBUTE_NORMAL,
0086                0
0087            );
0088     if (!d->h) {
0089         result = LockError;
0090     }
0091 
0092     else if (GetLastError() == NO_ERROR) {
0093 //        qDebug() << "'" << d->file << "' locked";
0094         result = LockOK;
0095     } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
0096         // handle stale lock file
0097         //qDebug() << "stale lock file '" << d->file << "' found, reused file";
0098         // we reuse this file
0099         result = LockOK;
0100     } else if (GetLastError() == ERROR_SHARING_VIOLATION) {
0101         CloseHandle(d->h);
0102         d->h = 0;
0103         //qDebug() << "could not lock file '" << d->file << "' it is locked by another process";
0104         result = LockFail;
0105     } else {
0106         //qDebug() << "could not lock '" << d->file << "' error= " << GetLastError();
0107         result = LockError;
0108     }
0109 
0110     if (result == LockOK) {
0111         d->isLocked = true;
0112     }
0113     return result;
0114 }
0115 
0116 bool
0117 KLockFile::isLocked() const
0118 {
0119     return d->isLocked;
0120 }
0121 
0122 void
0123 KLockFile::unlock()
0124 {
0125     if (d->isLocked) {
0126         //qDebug() << "lock removed for file '" << d->file << "'";
0127         CloseHandle(d->h);
0128         DeleteFileW((WCHAR *)d->file.utf16());
0129         d->h = 0;
0130         d->isLocked = false;
0131     }
0132 }
0133 
0134 bool
0135 KLockFile::getLockInfo(int &pid, QString &hostname, QString &appname)
0136 {
0137     return false;
0138 }