-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilecache.go
91 lines (78 loc) · 2.11 KB
/
filecache.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
package get2ch
import (
"github.com/tanaton/get2ch-go/unlib"
"io/ioutil"
"os"
"path"
"time"
)
const (
BOARD_SETTING = "setting"
tBOARD_LIST_NAME = "ita.data" // 板情報格納ファイル
tBOARD_SUBJECT_NAME = "subject.txt" // スレッド一覧格納ファイル名
tBOARD_SETTING_NAME = "setting.txt" // 板情報格納ファイル名
)
type State struct {
fsize int64
atime int64
mtime int64
}
func (s *State) Size() int64 { return s.fsize }
func (s *State) Amod() int64 { return s.atime }
func (s *State) Mmod() int64 { return s.mtime }
type FileCache struct {
Folder string // dat保管フォルダ名
}
func NewFileCache(root string) *FileCache {
return &FileCache{
Folder: root,
}
}
func (fc *FileCache) Path(s, b, t string) string {
if s == "" && b == "" && t == "" {
return fc.Folder + "/" + tBOARD_LIST_NAME
} else if t == BOARD_SETTING {
return fc.Folder + "/" + b + "/" + tBOARD_SETTING_NAME
} else if t == "" {
return fc.Folder + "/" + b + "/" + tBOARD_SUBJECT_NAME
}
return fc.Folder + "/" + b + "/" + t[0:4] + "/" + t + ".dat"
}
func (fc *FileCache) GetData(s, b, t string) ([]byte, error) {
logfile := fc.Path(s, b, t)
return ioutil.ReadFile(logfile)
}
func (fc *FileCache) SetData(s, b, t string, d []byte) error {
logfile := fc.Path(s, b, t)
os.MkdirAll(path.Dir(logfile), 0666)
return ioutil.WriteFile(logfile, d, 0666)
}
func (fc *FileCache) SetDataAppend(s, b, t string, d []byte) error {
logfile := fc.Path(s, b, t)
fp, err := os.OpenFile(logfile, os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return err
}
_, err = fp.Write(d)
fp.Close()
return err
}
func (fc *FileCache) SetMod(s, b, t string, m, a int64) error {
// atimeとmtimeの順番に注意
return os.Chtimes(fc.Path(s, b, t), time.Unix(a, 0).UTC(), time.Unix(m, 0).UTC())
}
func (fc *FileCache) Exists(s, b, t string) bool {
_, err := os.Stat(fc.Path(s, b, t))
return err == nil
}
func (fc *FileCache) Stat(s, b, t string) (CacheState, error) {
st, err := unlib.Stat(fc.Path(s, b, t))
if err != nil {
return nil, err
}
return &State{
fsize: st.Size,
atime: st.Atime,
mtime: st.Mtime,
}, nil
}