-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
47 lines (40 loc) · 849 Bytes
/
file.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
/*
* * Copyright (c) 2023 guojia99 All rights reserved.
* * Created: 2023/2/26 下午5:22.
* * Author: guojia(https://github.com/guojia99)
*/
package flock
import (
"os"
)
const defaultFileFlag = 0600
type file struct {
fileName string
fh *os.File
}
func (f *file) openFile() (err error) {
if f.fh != nil {
return
}
// todo 不同操作系统可能需要不同的配置,这里先用linux的
f.fh, err = os.OpenFile(f.fileName, os.O_CREATE|os.O_RDWR, os.FileMode(defaultFileFlag))
return
}
func (f *file) reOpenFile() (err error) {
if st, fErr := f.fh.Stat(); fErr == nil {
if st.Mode()&defaultFileFlag != defaultFileFlag {
return nil
}
f.closeFile()
if err = f.openFile(); err != nil {
return err
}
}
return nil
}
func (f *file) closeFile() {
if f.fh != nil {
_ = f.fh.Close()
f.fh = nil
}
}