/
file_lock.lua
111 lines (94 loc) · 2.44 KB
/
file_lock.lua
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
---------------------------------------------------------------------------
--
-- Filename: file_lock.lua
--
-- Description: file lock for lua by fcntl
--
-- Version: 1.0
-- Created: 10/19/2018 11:23:53 AM
-- Revision: None
--
-- Author: gerrard (gerrard), <gyc.ssdut@gmail.com>
-- Company:
--
---------------------------------------------------------------------------
local fcntl = require('posix.fcntl')
local stat = require('posix.sys.stat')
local unistd = require('posix.unistd')
local _M = {}
_M.version = '0.0.1'
function _M.new(self, opts)
if not opts.name then
return nil, 'file name required'
end
local fd = fcntl.open(
opts.name,
opts.flags or fcntl.O_CREAT + fcntl.O_WRONLY + fcntl.O_TRUNC,
opts.mode or stat.S_IRUSR + stat.S_IWUSR + stat.S_IRGRP + stat.S_IROTH
)
local lock = {
l_type = fcntl.F_WRLCK,
l_whence = fcntl.SEEK_SET,
l_start = 0,
l_len = 0
}
t = {
fd = fd,
lock = lock
}
return setmetatable(t, {__index = _M})
end
function _M.get_fd(self)
return self.fd
end
function _M.get_lock(self)
return self.lock
end
function _M.set_lock_type(self, l_type)
local lock = self:get_lock()
lock.l_type = l_type
self.lock = lock
end
function _M.fcntl(self, cmd)
local fd = self:get_fd()
local lock = self:get_lock()
return fcntl.fcntl(fd, cmd, lock)
end
function _M.get_lock_type(self)
local lock = self:get_lock()
return lock.l_type
end
function _M.read_lock(self)
self:set_lock_type(fcntl.F_RDLCK)
return self:fcntl(fcntl.F_SETLK)
end
function _M.write_lock(self)
self:set_lock_type(fcntl.F_WRLCK)
return self:fcntl(fcntl.F_SETLK)
end
function _M.unlock(self)
self:set_lock_type(fcntl.F_UNLCK)
return self:fcntl(fcntl.F_SETLK)
end
function _M.try_read_lock(self)
self:set_lock_type(fcntl.F_RDLCK)
local status, errmsg, errno = self:fcntl(fcntl.F_GETLK)
if status and self:get_lock_type() == fcntl.F_UNLCK then
return true
end
return false, errmsg, errno
end
function _M.try_write_lock(self)
self:set_lock_type(fcntl.F_WRLCK)
local status, errmsg, errno = self:fcntl(fcntl.F_GETLK)
if status and self:get_lock_type() ~= fcntl.F_UNLCK then
return true
end
return false, errmsg, errno
end
function _M.delete(self)
self:unlock()
local fd = self:get_fd()
unistd.close(fd)
end
return _M