forked from Tencent/phxsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilelock.cpp
More file actions
198 lines (168 loc) · 4.84 KB
/
filelock.cpp
File metadata and controls
198 lines (168 loc) · 4.84 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Tencent is pleased to support the open source community by making PhxSQL available.
Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the GNU General Public License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/GPL-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#ifndef __USE_FILE_OFFSET64
#define __USE_FILE_OFFSET64
#endif
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include "filelock.h"
namespace phxsql {
FileLock::FileLock() {
m_iFd = -1;
}
bool FileLock::Open(const char* sPath) {
m_iFd = open(sPath, O_RDWR | O_CREAT | O_LARGEFILE, S_IRUSR | S_IWUSR);
if (m_iFd < 0) {
return false;
} else {
return true;
}
}
void FileLock::Close() {
close(m_iFd);
m_iFd = -1;
}
FileLock::~FileLock() {
if (m_iFd > 0)
close(m_iFd);
m_iFd = -1;
}
bool FileLock::Fcntl(int iCmd, int iType, int iOffset, int iLen, int iWhence) {
uint64_t offset = iOffset;
uint64_t len = iLen;
return Fcntl(iCmd, iType, offset, len, iWhence);
}
bool FileLock::Fcntl(int iCmd, int iType, uint64_t iOffset, uint64_t iLen, int iWhence) {
#ifdef __WORDSIZE
if( __WORDSIZE != 64) {
if( iCmd == 7 )
{
iCmd= 14;
}
else if( 6 == iCmd )
{
iCmd = 13;
}
else if( 5 == iCmd )
{
iCmd = 12;
}
}
#else
if (iCmd == 7) {
iCmd = 14;
} else if (6 == iCmd) {
iCmd = 13;
} else if (5 == iCmd) {
iCmd = 12;
}
#endif
struct flock64 stLock;
stLock.l_type = iType;
stLock.l_start = iOffset;
stLock.l_whence = iWhence;
stLock.l_len = iLen;
if (fcntl(m_iFd, iCmd, &stLock) < 0) {
return false;
}
return true;
}
static void sigalrm(int signo) {
// do nothing
}
bool FileLock::FcntlTimeOut(int iCmd, int iType, int iOffset, int iLen, int iWhence, int sec) {
uint64_t offset = iOffset;
uint64_t len = iLen;
return FcntlTimeOut(iCmd, iType, offset, len, iWhence, sec);
}
bool FileLock::FcntlTimeOut(int iCmd, int iType, uint64_t iOffset, uint64_t iLen, int iWhence, int sec) {
struct sigaction act, oact;
int ret;
#ifdef __WORDSIZE
if( __WORDSIZE != 64) {
if( iCmd == 7 )
{
iCmd= 14;
}
else if( 6 == iCmd )
{
iCmd = 13;
}
else if( 5 == iCmd )
{
iCmd = 12;
}
}
#else
if (iCmd == 7) {
iCmd = 14;
} else if (6 == iCmd) {
iCmd = 13;
} else if (5 == iCmd) {
iCmd = 12;
}
#endif
struct flock64 stLock;
stLock.l_type = iType;
stLock.l_start = iOffset;
stLock.l_whence = iWhence;
stLock.l_len = iLen;
if (sec > 0) {
memset(&act, 0, sizeof(struct sigaction));
memset(&oact, 0, sizeof(struct sigaction));
act.sa_handler = sigalrm;
sigemptyset(&act.sa_mask);
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT;
#endif
SIGACT: ret = sigaction(SIGALRM, &act, &oact);
if (ret < 0 && errno == EINTR)
goto SIGACT;
if (ret < 0) // error
return false;
alarm(sec);
}
ret = fcntl(m_iFd, iCmd, &stLock);
if (sec > 0) {
alarm(0);
sigaction(SIGALRM, &oact, &act);
}
if (ret < 0) {
return false;
}
return true;
}
bool FileLock::ReadLock(uint64_t iOffset, uint64_t iLen, int iWhence) {
return Fcntl(F_SETLK, F_RDLCK, iOffset, iLen, iWhence);
}
bool FileLock::WriteLock(uint64_t iOffset, uint64_t iLen, int iWhence) {
return Fcntl(F_SETLK, F_WRLCK, iOffset, iLen, iWhence);
}
bool FileLock::ReadLockW(uint64_t iOffset, uint64_t iLen, int iWhence) {
return Fcntl(F_SETLKW, F_RDLCK, iOffset, iLen, iWhence);
}
bool FileLock::WriteLockW(uint64_t iOffset, uint64_t iLen, int iWhence) {
return Fcntl(F_SETLKW, F_WRLCK, iOffset, iLen, iWhence);
}
bool FileLock::ReadLockTimeOut(uint64_t iOffset, int sec, uint64_t iLen, int iWhence) {
return FcntlTimeOut(F_SETLKW, F_RDLCK, iOffset, iLen, iWhence, sec);
}
bool FileLock::WriteLockTimeOut(uint64_t iOffset, int sec, uint64_t iLen, int iWhence) {
return FcntlTimeOut(F_SETLKW, F_WRLCK, iOffset, iLen, iWhence, sec);
}
bool FileLock::Unlock(uint64_t iOffset, uint64_t iLen, int iWhence) {
return Fcntl(F_SETLK, F_UNLCK, iOffset, iLen, iWhence);
}
}