forked from siyuan-note/filelock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelock.go
150 lines (129 loc) · 3.47 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
// 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"
)
// TODO: 考虑改为每个文件一个锁以提高并发性能
var (
RWLock = sync.Mutex{}
)
func Move(src, dest string) (err error) {
if src == dest {
return nil
}
RWLock.Lock()
defer RWLock.Unlock()
err = os.Rename(src, dest)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "move [src=%s, dest=%s] failed: %s", src, dest, err)
return
}
return
}
func Copy(src, dest string) (err error) {
RWLock.Lock()
defer RWLock.Unlock()
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) {
RWLock.Lock()
defer RWLock.Unlock()
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) {
RWLock.Lock()
defer RWLock.Unlock()
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) {
RWLock.Lock()
defer RWLock.Unlock()
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) {
RWLock.Lock()
defer RWLock.Unlock()
data, err = os.ReadFile(filePath)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "read file [%s] failed: %s", filePath, err)
return
}
return
}
func WriteFileWithoutChangeTime(filePath string, data []byte) (err error) {
RWLock.Lock()
defer RWLock.Unlock()
err = gulu.File.WriteFileSaferWithoutChangeTime(filePath, data, 0644)
if isDenied(err) {
logging.LogFatalf(logging.ExitCodeFileSysErr, "write file [%s] failed: %s", filePath, err)
return
}
return
}
func WriteFile(filePath string, data []byte) (err error) {
RWLock.Lock()
defer RWLock.Unlock()
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) {
RWLock.Lock()
defer RWLock.Unlock()
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")
}