• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • io
klockfile_win.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDE libraries
3 Copyright (c) 2007 Ralf Habacker <ralf.habacker@freenet.de>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "klockfile.h"
21#include "kcomponentdata.h"
22#include "kdebug.h"
23
24#include <windows.h>
25
31class KLockFile::Private
32{
33public:
34 Private(const KComponentData &c)
35 : componentData(c)
36 {
37 }
38
39 static int debugArea() { static int s_area = KDebug::registerArea("kdecore (KLockFile)"); return s_area; }
40
41 QString file;
42 bool isLocked;
43 int staleTime;
44 KComponentData componentData;
45 HANDLE h;
46};
47
48KLockFile::KLockFile(const QString &file, const KComponentData &componentData)
49 : d(new Private(componentData))
50{
51 d->file = file;
52 d->isLocked = false;
53 d->staleTime = 0;
54}
55
56KLockFile::~KLockFile()
57{
58 unlock();
59 delete d;
60}
61
62int
63KLockFile::staleTime() const
64{
65 return d->staleTime;
66}
67
68
69void
70KLockFile::setStaleTime(int _staleTime)
71{
72 d->staleTime = _staleTime;
73}
74
75KLockFile::LockResult
76KLockFile::lock(LockFlags options)
77{
78 if (d->isLocked)
79 return LockOK;
80
81 LockResult result;
82
83 d->h = CreateFileW(
84 (WCHAR *)d->file.utf16(),
85 GENERIC_READ | GENERIC_WRITE,
86 0,
87 0,
88 CREATE_ALWAYS,
89 FILE_ATTRIBUTE_NORMAL,
90 0
91 );
92 if (!d->h)
93 result = LockError;
94
95 else if (GetLastError() == NO_ERROR)
96 {
97// kDebug(d->debugArea()) << "'" << d->file << "' locked";
98 result = LockOK;
99 }
100 else if (GetLastError() == ERROR_ALREADY_EXISTS)
101 {
102 // handle stale lock file
103 //kDebug(d->debugArea()) << "stale lock file '" << d->file << "' found, reused file";
104 // we reuse this file
105 result = LockOK;
106 }
107 else if (GetLastError() == ERROR_SHARING_VIOLATION)
108 {
109 CloseHandle(d->h);
110 d->h = 0;
111 //kDebug(d->debugArea()) << "could not lock file '" << d->file << "' it is locked by another process";
112 result = LockFail;
113 }
114 else {
115 //kDebug(d->debugArea()) << "could not lock '" << d->file << "' error= " << GetLastError();
116 result = LockError;
117 }
118
119 if (result == LockOK)
120 d->isLocked = true;
121 return result;
122}
123
124bool
125KLockFile::isLocked() const
126{
127 return d->isLocked;
128}
129
130void
131KLockFile::unlock()
132{
133 if (d->isLocked)
134 {
135 //kDebug(d->debugArea()) << "lock removed for file '" << d->file << "'";
136 CloseHandle(d->h);
137 DeleteFileW((WCHAR *)d->file.utf16());
138 d->h = 0;
139 d->isLocked = false;
140 }
141}
142
143bool
144KLockFile::getLockInfo(int &pid, QString &hostname, QString &appname)
145{
146 return false;
147}
KComponentData
Per component data.
Definition: kcomponentdata.h:47
KDebug::registerArea
static int registerArea(const QByteArray &areaName, bool enabled=true)
Definition: kdebug.cpp:856
KLockFile::getLockInfo
bool getLockInfo(int &pid, QString &hostname, QString &appname)
Returns the pid, hostname and appname of the process holding the lock after the lock functon has retu...
Definition: klockfile_unix.cpp:502
KLockFile::LockResult
LockResult
Possible return values of the lock function.
Definition: klockfile.h:51
KLockFile::LockFail
@ LockFail
The lock could not be acquired because it is held by another process.
Definition: klockfile.h:60
KLockFile::LockOK
@ LockOK
Lock was acquired successfully.
Definition: klockfile.h:55
KLockFile::LockError
@ LockError
The lock could not be acquired due to an error.
Definition: klockfile.h:65
KLockFile::unlock
void unlock()
Release the lock.
Definition: klockfile_unix.cpp:487
KLockFile::~KLockFile
~KLockFile()
Destroys the object, releasing the lock if held.
Definition: klockfile_unix.cpp:122
KLockFile::setStaleTime
void setStaleTime(int _staleTime)
Set the time in seconds after which a lock is considered stale.
Definition: klockfile_unix.cpp:136
KLockFile::KLockFile
KLockFile(const QString &file, const KComponentData &componentName=KGlobal::mainComponent())
Definition: klockfile_unix.cpp:116
KLockFile::lock
LockResult lock(LockFlags flags=LockFlags())
Attempt to acquire the lock.
Definition: klockfile_unix.cpp:386
KLockFile::isLocked
bool isLocked() const
Returns whether the lock is held or not.
Definition: klockfile_unix.cpp:482
KLockFile::staleTime
int staleTime() const
Return the time in seconds after which a lock is considered stale The default is 30.
Definition: klockfile_unix.cpp:129
QString
pid
static pid_t pid
Definition: k3resolvermanager.cpp:249
kcomponentdata.h
kdebug.h
klockfile.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal