KDECore
klockfile_win.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 2007 Ralf Habacker <ralf.habacker@freenet.de> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "klockfile.h" 00021 #include "kcomponentdata.h" 00022 #include "kdebug.h" 00023 00024 #include <windows.h> 00025 00031 class KLockFile::Private 00032 { 00033 public: 00034 Private(const KComponentData &c) 00035 : componentData(c) 00036 { 00037 } 00038 00039 static int debugArea() { static int s_area = KDebug::registerArea("kdecore (KLockFile)"); return s_area; } 00040 00041 QString file; 00042 bool isLocked; 00043 int staleTime; 00044 KComponentData componentData; 00045 HANDLE h; 00046 }; 00047 00048 KLockFile::KLockFile(const QString &file, const KComponentData &componentData) 00049 : d(new Private(componentData)) 00050 { 00051 d->file = file; 00052 d->isLocked = false; 00053 d->staleTime = 0; 00054 } 00055 00056 KLockFile::~KLockFile() 00057 { 00058 unlock(); 00059 delete d; 00060 } 00061 00062 int 00063 KLockFile::staleTime() const 00064 { 00065 return d->staleTime; 00066 } 00067 00068 00069 void 00070 KLockFile::setStaleTime(int _staleTime) 00071 { 00072 d->staleTime = _staleTime; 00073 } 00074 00075 KLockFile::LockResult 00076 KLockFile::lock(LockFlags options) 00077 { 00078 if (d->isLocked) 00079 return LockOK; 00080 00081 LockResult result; 00082 00083 d->h = CreateFileW( 00084 (WCHAR *)d->file.utf16(), 00085 GENERIC_READ | GENERIC_WRITE, 00086 0, 00087 0, 00088 CREATE_ALWAYS, 00089 FILE_ATTRIBUTE_NORMAL, 00090 0 00091 ); 00092 if (!d->h) 00093 result = LockError; 00094 00095 else if (GetLastError() == NO_ERROR) 00096 { 00097 // kDebug(d->debugArea()) << "'" << d->file << "' locked"; 00098 result = LockOK; 00099 } 00100 else if (GetLastError() == ERROR_ALREADY_EXISTS) 00101 { 00102 // handle stale lock file 00103 //kDebug(d->debugArea()) << "stale lock file '" << d->file << "' found, reused file"; 00104 // we reuse this file 00105 result = LockOK; 00106 } 00107 else if (GetLastError() == ERROR_SHARING_VIOLATION) 00108 { 00109 CloseHandle(d->h); 00110 d->h = 0; 00111 //kDebug(d->debugArea()) << "could not lock file '" << d->file << "' it is locked by another process"; 00112 result = LockFail; 00113 } 00114 else { 00115 //kDebug(d->debugArea()) << "could not lock '" << d->file << "' error= " << GetLastError(); 00116 result = LockError; 00117 } 00118 00119 if (result == LockOK) 00120 d->isLocked = true; 00121 return result; 00122 } 00123 00124 bool 00125 KLockFile::isLocked() const 00126 { 00127 return d->isLocked; 00128 } 00129 00130 void 00131 KLockFile::unlock() 00132 { 00133 if (d->isLocked) 00134 { 00135 //kDebug(d->debugArea()) << "lock removed for file '" << d->file << "'"; 00136 CloseHandle(d->h); 00137 DeleteFileW((WCHAR *)d->file.utf16()); 00138 d->h = 0; 00139 d->isLocked = false; 00140 } 00141 } 00142 00143 bool 00144 KLockFile::getLockInfo(int &pid, QString &hostname, QString &appname) 00145 { 00146 return false; 00147 }
KDE 4.6 API Reference