-
Notifications
You must be signed in to change notification settings - Fork 0
/
browse.go
107 lines (97 loc) · 3.21 KB
/
browse.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
package main
import (
"context"
"os"
"path"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
type browseDir struct {
hasTags
cache *fileCache
}
func (b browseDir) Attr(ctx context.Context, attr *fuse.Attr) error {
attr.Mode = os.ModeDir | 0755
attr.Size = 4096
attr.Uid = uid
attr.Gid = gid
return nil
}
func addItems(items map[string]item, newItems []item) {
for i := range newItems {
items[newItems[i].Name] = newItems[i]
}
}
func (b browseDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
var result = emptyDir()
positiveTagNames, negativeTagNames := b.getTagsWithNegative()
excludeTagNames := append(positiveTagNames, negativeTagNames...)
items := make(map[string]item)
if b.tags == "" {
var baseItems []item
if err := db.Find(&baseItems, "parent_id = 0 AND type = ?", tag).Error; err != nil {
return nil, err
}
addItems(items, baseItems)
}
for _, activeTagName := range positiveTagNames {
var activeItem item
if !db.First(&activeItem, "name = ?", activeTagName).RecordNotFound() {
var childItems, groupTags, childGroupItems []item
if err := db.Find(&childItems, "parent_id = ? AND name NOT IN (?) AND type = ?", activeItem.ID, excludeTagNames, tag).Error; err != nil {
return nil, err
}
addItems(items, childItems)
if err := db.Model(&activeItem).
Where("name NOT IN (?) AND type = ?", excludeTagNames, grouptag).
Related(&groupTags, "Items").Error; err != nil {
return nil, err
}
childGroupIDs := make([]id, len(groupTags))
for i := range groupTags {
childGroupIDs[i] = groupTags[i].ID
}
db.Find(&childGroupItems, "parent_id IN (?) AND name NOT IN (?)", childGroupIDs, excludeTagNames)
addItems(items, childGroupItems)
}
}
for _, v := range items {
result = append(result, fuse.Dirent{Inode: uint64(v.ID), Name: v.Name, Type: fuse.DT_Dir})
b.cache.put(v.Name, &v)
}
if path.Base(b.tags) != negativeTag {
result = append(result,
fuse.Dirent{Name: contentTag, Type: fuse.DT_Dir},
fuse.Dirent{Name: allTagsTag, Type: fuse.DT_Dir},
fuse.Dirent{Name: negativeTag, Type: fuse.DT_Dir})
}
return result, nil
}
func (b browseDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
switch name {
case contentTag:
return filesDir{hasTags: hasTags{tags: b.tags}, cache: newCache()}, nil
case allTagsTag:
return filesDir{hasTags: hasTags{tags: b.tags}, allTags: true, cache: newCache()}, nil
case negativeTag:
return browseDir{hasTags: hasTags{tags: path.Join(b.tags, negativeTag)}, cache: newCache()}, nil
}
if _, ok := b.cache.get(name); ok {
return browseDir{hasTags: hasTags{tags: path.Join(b.tags, name)}, cache: newCache()}, nil
}
var result item
if !db.First(&result, "name = ?", name).RecordNotFound() {
return browseDir{hasTags: hasTags{tags: path.Join(b.tags, result.Name)}, cache: newCache()}, nil
}
return nil, syscall.ENOENT
}
func (b browseDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
return nil, nil, syscall.EACCES
}
func (b browseDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
return syscall.EPERM
}
func (b browseDir) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fs.Node) error {
return syscall.EPERM
}