Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UserLockFile_win.cpp « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0c9bdd224d5a02cf159d20da36f5de8cbcb9db3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2005-2019 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "UserLockFile.h"

UserLockFile::UserLockFile(const QString &lockFilePath)
	: m_handle(0)
	, m_path(lockFilePath) {
}

UserLockFile::~UserLockFile() {
	release();
}

QString UserLockFile::path() const {
	return m_path;
}

bool UserLockFile::acquire() {
	if (m_handle != 0) {
		return false;
	}

	m_handle = CreateFile(
		reinterpret_cast<const wchar_t *>(m_path.utf16()),
		GENERIC_WRITE,
		0,
		NULL,
		CREATE_ALWAYS,
		FILE_ATTRIBUTE_HIDDEN,
		NULL
	);
	if (m_handle == INVALID_HANDLE_VALUE && GetLastError() == ERROR_SHARING_VIOLATION) {
		return false;
	}
	return true;
}

void UserLockFile::release() {
	if (m_handle != 0) {
		CloseHandle(m_handle);
		m_handle = 0;
	}
}