summaryrefslogtreecommitdiffstats
path: root/src/locking.c
blob: 77c61f86c7ab68c51572768c0ccc1b9f76701c6e (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * locking.c
 * locking extras
 *
 * Copyright (c) 2012 Nikias Bassen. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifdef WIN32
#include <windows.h>
#else
#include <errno.h>
#endif

#include "locking.h"
#include "common.h"

int lock_file(const char* filename, lock_info_t* lockinfo)
{
	if (!lockinfo) {
		return -1;
	}
#ifdef WIN32
	lockinfo->fp = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (lockinfo->fp == INVALID_HANDLE_VALUE) {
		debug("ERROR: could not open or create lockfile '%s'\n", filename);
		return -1;
	}

	lockinfo->ldata.Offset = 0;
	lockinfo->ldata.OffsetHigh = 0;

	if (!LockFileEx(lockinfo->fp, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &lockinfo->ldata)) {
		debug("ERROR: can't lock file, error %d\n", GetLastError());
		CloseHandle(lockinfo->fp);
		lockinfo->fp = INVALID_HANDLE_VALUE;
		return -1;
	}
#else
	lockinfo->fp = fopen(filename, "a+");

	if (!lockinfo->fp) {
		debug("ERROR: could not open or create lockfile '%s'\n", filename);
		return -1;
	}

	lockinfo->ldata.l_type = F_WRLCK;
	lockinfo->ldata.l_whence = SEEK_SET;
	lockinfo->ldata.l_start = 0;
	lockinfo->ldata.l_len = 0;

	if (fcntl(fileno(lockinfo->fp), F_SETLKW, &lockinfo->ldata) < 0) {
		debug("ERROR: can't lock file, error %d\n", errno);
		fclose(lockinfo->fp);
		lockinfo->fp = NULL;
		return -1;
	}
#endif
	return 0;
}

int unlock_file(lock_info_t* lockinfo)
{
	if (!lockinfo) {
		return -1;
	}
#ifdef WIN32
	if (lockinfo->fp == INVALID_HANDLE_VALUE) {
		return -1;
	}

	lockinfo->ldata.Offset = 0;
	lockinfo->ldata.OffsetHigh = 0;

	if (!UnlockFileEx(lockinfo->fp, 0, 1, 0, &lockinfo->ldata)) {
		debug("ERROR: can't unlock file, error %d\n", GetLastError());
		CloseHandle(lockinfo->fp);
		lockinfo->fp = INVALID_HANDLE_VALUE;
		return -1;
	}
	CloseHandle(lockinfo->fp);
	lockinfo->fp = INVALID_HANDLE_VALUE;
#else
	if (!lockinfo->fp) {
		return -1;
	}

	lockinfo->ldata.l_type = F_UNLCK;
	lockinfo->ldata.l_whence = SEEK_SET;
	lockinfo->ldata.l_start = 0;
	lockinfo->ldata.l_len = 0;

	if (fcntl(fileno(lockinfo->fp), F_SETLK, &lockinfo->ldata) < 0) {
		debug("ERROR: can't unlock file, error %d\n", errno);
		fclose(lockinfo->fp);
		lockinfo->fp = NULL;
		return -1;
	}
	fclose(lockinfo->fp);
	lockinfo->fp = NULL;
#endif
	return 0;
}