-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.go
273 lines (238 loc) · 5.44 KB
/
catalog.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"bufio"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/Bnei-Baruch/filer-backend/fileindex"
"github.com/Bnei-Baruch/filer-backend/fileutils"
)
var (
reDisk *regexp.Regexp
reTape *regexp.Regexp
reWIN *regexp.Regexp
storages sync.Map
)
func InitStorages() {
reDisk, _ = regexp.Compile("^[0-9][0-9][0-9]$")
reTape, _ = regexp.Compile("^(ltfs|lto)-[0-9-]*$")
reWIN, _ = regexp.Compile("^[a-z]:$")
}
// list of index files
func GetIndexList(path string) IndexList {
ft, err := fileutils.Collect(path)
if err != nil {
log.Println(err)
}
il := make([]IndexFile, 0, 10)
for _, dir := range ft {
for _, fi := range dir.List {
if fi.Mode().IsRegular() && fi.Name()[0] != '.' {
il = append(il, IndexFile{dir.FullPath(fi), fi.ModTime().Unix(), nil})
}
}
}
return il
}
// Type: IndexList
func (il IndexList) FindPath(path string) *IndexFile {
for _, i := range il {
if i.Path == path {
return &i
}
}
return nil
}
// Type: IndexMain
func NewIndex(path string) *IndexMain {
return &IndexMain{List: make(IndexList, 0), fs: fileindex.NewFastSearch(), Path: path}
}
func (idx *IndexMain) GetFS() (fs *fileindex.FastSearch) {
p := unsafe.Pointer(&idx.fs)
return (*fileindex.FastSearch)(atomic.LoadPointer((*unsafe.Pointer)(p)))
}
func (idx *IndexMain) IsModified() bool {
indexes := GetIndexList(idx.Path)
now := time.Now().Unix()
idx.Lock()
list := idx.List
idx.Unlock()
if len(indexes) != len(list) {
return true
}
for _, idxfile := range indexes {
// ignore files modified within last 5s
if now-idxfile.Mtime < 5 {
continue
}
il := list.FindPath(idxfile.Path)
if il == nil || il.Mtime != idxfile.Mtime {
return true
}
}
return false
}
// Load all indexes recursively. Reload an index if modification time is changed.
func (idx *IndexMain) Load() {
indexes := GetIndexList(idx.Path)
idx.Lock()
curlist := idx.List
idx.Unlock()
list := make(IndexList, 0, 10)
fs := fileindex.NewFastSearch()
for _, idxfile := range indexes {
var fl fileindex.FileList
curidx := curlist.FindPath(idxfile.Path)
if curidx == nil || curidx.Mtime != idxfile.Mtime {
fl = load(idxfile.Path)
log.Printf("Loaded %d records from %s\n", len(fl), idxfile.Path)
} else {
fl = curidx.Files
}
list = append(list, IndexFile{Path: idxfile.Path, Mtime: idxfile.Mtime, Files: fl})
fs.AddList(fl)
}
idx.Lock()
idx.List = list
idx.SetFS(fs)
idx.Unlock()
}
func (idx *IndexMain) SetFS(fs *fileindex.FastSearch) {
p := unsafe.Pointer(&idx.fs)
atomic.StorePointer((*unsafe.Pointer)(p), unsafe.Pointer(fs))
}
// filter unnecessary files
func filter(fr *fileindex.FileRec, storage *fileindex.Storage) bool {
path, name := filepath.Split(fr.Path)
ext := filepath.Ext(name)
if fr.Size == 0 || name == "Thumbs.db" || name == ".DS_Store" || ext == ".lnk" {
return false
}
if storage != nil {
if _, ok := storages.Load(storage.Id); !ok {
storages.Store(storage.Id, storage)
}
fr.Device = storage
return true
}
dirs := strings.Split(path, "/")
if dirs[0] == "" {
dirs = dirs[1:]
}
if len(dirs) < 2 {
log.Println("Wrong path:", fr.Path)
return false
}
id := "unknown"
status := "offline"
switch dirs[0] {
case "mnt":
if reDisk.Match([]byte(dirs[1])) {
id = "disk-" + dirs[1]
status = "nearline"
} else {
id = conf.Location.Hostname + "-" + dirs[1]
status = "online"
}
case "net":
switch dirs[1] {
case "nas":
id = "nas-" + dirs[2]
status = "online"
case "server":
switch dirs[2] {
case "b", "original":
id = "server-d:"
case "r":
id = "server-h:"
case "buffer", "nas":
id = "server-e:"
}
status = "online"
}
case "tape":
if reTape.Match([]byte(dirs[1])) {
id = dirs[1]
}
default:
if reWIN.Match([]byte(dirs[0])) {
id = "server-" + dirs[0]
status = "online"
}
}
if v, ok := storages.Load(id); ok {
fr.Device = v.(*fileindex.Storage)
return true
}
if id == "unknown" {
log.Println("Unknown storage:", fr.Path, fr.Sha1)
}
storage = &fileindex.Storage{
Id: id,
Status: status,
Access: conf.Location.Access,
Country: conf.Location.Country,
Location: conf.Location.Name,
}
storages.Store(id, storage)
fr.Device = storage
return true
}
// import an index from path using filter
func load(path string) fileindex.FileList {
f, err := os.Open(path)
if err != nil {
log.Println(err)
return fileindex.FileList{}
}
defer f.Close()
var storage *fileindex.Storage
pp := strings.Split(path, "/")
switch pp[len(pp)-2] {
case "nl-nforce":
storage = &fileindex.Storage{
Id: "0618278a-0602-4d7a-bb95-b1f176774490",
Status: "online",
Access: "internet",
Country: "nl",
Location: "nforce",
}
case "ca-ovh":
storage = &fileindex.Storage{
Id: "aa886ee5-5d9b-413a-baae-63079c89575d",
Status: "online",
Access: "internet",
Country: "ca",
Location: "ovh",
}
case "ca-uri":
storage = &fileindex.Storage{
Id: "fcae6eb0-6e24-436d-b01f-30ec1a0a4817",
Status: "online",
Access: "local",
Country: "ca",
Location: "uri",
}
case "ru-piter":
storage = &fileindex.Storage{
Id: "b569d59c-8b7f-41c6-b37a-1ceaeccc3a8a",
Status: "online",
Access: "local",
Country: "ru",
Location: "piter",
}
}
fl, err := fileindex.Load(bufio.NewReader(f), func(fr *fileindex.FileRec) bool {
return filter(fr, storage)
})
if err != nil {
log.Println(err)
}
return fl
}