forked from siyuan-note/filelock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelock.go
207 lines (173 loc) · 4.45 KB
/
filelock.go
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
197
198
199
200
201
202
203
204
205
206
207
// FileLock - Read and write files with lock.
// Copyright (c) 2022-present, b3log.org
//
// FileLock is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
//
// See the Mulan PSL v2 for more details.
package filelock
import (
"errors"
"io"
"os"
"strings"
"sync"
"github.com/88250/gulu"
"github.com/siyuan-note/logging"
)
var (
lockMutex = sync.Mutex{}
operatingFiles = map[string]*sync.Mutex{}
)
func Lock(filePath string) {
lock(filePath)
}
func Unlock(filePath string) {
unlock(filePath)
}
func OpenFile(filePath string, flag int, perm os.FileMode) (file *os.File, err error) {
lock(filePath)
file, err = os.OpenFile(filePath, flag, perm)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "open file [%s] failed: %s", filePath, err)
return
}
return
}
func CloseFile(file *os.File) (err error) {
if nil == file {
return
}
defer unlock(file.Name())
err = file.Close()
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "close file [%s] failed: %s", file.Name(), err)
return
}
return
}
func IsExist(filePath string) (ret bool) {
lock(filePath)
defer unlock(filePath)
return gulu.File.IsExist(filePath)
}
func Copy(src, dest string) (err error) {
lock(src)
defer unlock(src)
err = gulu.File.Copy(src, dest)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "copy [src=%s, dest=%s] failed: %s", src, dest, err)
return
}
return
}
func CopyNewtimes(src, dest string) (err error) {
lock(src)
defer unlock(src)
err = gulu.File.CopyNewtimes(src, dest)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "copy [src=%s, dest=%s] failed: %s", src, dest, err)
return
}
return
}
func Rename(p, newP string) (err error) {
if p == newP {
return nil
}
lock(p)
defer unlock(p)
if gulu.File.IsExist(newP) && gulu.File.IsDir(p) && gulu.File.IsDir(newP) {
err = gulu.File.Copy(p, newP)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "copy [p=%s, newP=%s] failed: %s", p, newP, err)
return
}
err = os.RemoveAll(p)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "remove [%s] failed: %s", p, err)
return
}
return
}
err = os.Rename(p, newP)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "rename [p=%s, newP=%s] failed: %s", p, newP, err)
return
}
return
}
func Remove(p string) (err error) {
lock(p)
defer unlock(p)
err = os.RemoveAll(p)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "remove file [%s] failed: %s", p, err)
return
}
return
}
func ReadFile(filePath string) (data []byte, err error) {
lock(filePath)
defer unlock(filePath)
data, err = os.ReadFile(filePath)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "read file [%s] failed: %s", filePath, err)
return
}
return
}
func WriteFile(filePath string, data []byte) (err error) {
lock(filePath)
defer unlock(filePath)
err = gulu.File.WriteFileSafer(filePath, data, 0644)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "write file [%s] failed: %s", filePath, err)
return
}
return
}
func WriteFileByReader(filePath string, reader io.Reader) (err error) {
lock(filePath)
defer unlock(filePath)
err = gulu.File.WriteFileSaferByReader(filePath, reader, 0644)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "write file [%s] failed: %s", filePath, err)
}
return
}
func isDenied(err error) bool {
if nil == err {
return false
}
if errors.Is(err, os.ErrPermission) {
return true
}
errMsg := strings.ToLower(err.Error())
return strings.Contains(errMsg, "access is denied") || strings.Contains(errMsg, "used by another process")
}
func lock(filePath string) {
lockMutex.Lock()
mutex := operatingFiles[filePath]
if nil == mutex {
mutex = &sync.Mutex{}
operatingFiles[filePath] = mutex
}
lockMutex.Unlock()
mutex.Lock()
}
func unlock(filePath string) {
lockMutex.Lock()
mutex := operatingFiles[filePath]
//delete(operatingFiles, filePath) 删了的话并发情况下会导致死锁,得考虑新的回收机制
lockMutex.Unlock()
if nil != mutex {
mutex.Unlock()
}
}