forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse_dir.go
156 lines (133 loc) · 3.97 KB
/
fuse_dir.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
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build linux darwin freebsd
package fuse
import (
"os"
"path/filepath"
"sync"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
var (
_ fs.Node = (*SwarmDir)(nil)
_ fs.NodeRequestLookuper = (*SwarmDir)(nil)
_ fs.HandleReadDirAller = (*SwarmDir)(nil)
_ fs.NodeCreater = (*SwarmDir)(nil)
_ fs.NodeRemover = (*SwarmDir)(nil)
_ fs.NodeMkdirer = (*SwarmDir)(nil)
)
type SwarmDir struct {
inode uint64
name string
path string
directories []*SwarmDir
files []*SwarmFile
mountInfo *MountInfo
lock *sync.RWMutex
}
func NewSwarmDir(fullpath string, minfo *MountInfo) *SwarmDir {
newdir := &SwarmDir{
inode: NewInode(),
name: filepath.Base(fullpath),
path: fullpath,
directories: []*SwarmDir{},
files: []*SwarmFile{},
mountInfo: minfo,
lock: &sync.RWMutex{},
}
return newdir
}
func (sd *SwarmDir) Attr(ctx context.Context, a *fuse.Attr) error {
a.Inode = sd.inode
a.Mode = os.ModeDir | 0700
a.Uid = uint32(os.Getuid())
a.Gid = uint32(os.Getegid())
return nil
}
func (sd *SwarmDir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fs.Node, error) {
for _, n := range sd.files {
if n.name == req.Name {
return n, nil
}
}
for _, n := range sd.directories {
if n.name == req.Name {
return n, nil
}
}
return nil, fuse.ENOENT
}
func (sd *SwarmDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
var children []fuse.Dirent
for _, file := range sd.files {
children = append(children, fuse.Dirent{Inode: file.inode, Type: fuse.DT_File, Name: file.name})
}
for _, dir := range sd.directories {
children = append(children, fuse.Dirent{Inode: dir.inode, Type: fuse.DT_Dir, Name: dir.name})
}
return children, nil
}
func (sd *SwarmDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
newFile := NewSwarmFile(sd.path, req.Name, sd.mountInfo)
newFile.fileSize = 0 // 0 means, file is not in swarm yet and it is just created
sd.lock.Lock()
defer sd.lock.Unlock()
sd.files = append(sd.files, newFile)
return newFile, newFile, nil
}
func (sd *SwarmDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
if req.Dir && sd.directories != nil {
newDirs := []*SwarmDir{}
for _, dir := range sd.directories {
if dir.name == req.Name {
removeDirectoryFromSwarm(dir)
} else {
newDirs = append(newDirs, dir)
}
}
if len(sd.directories) > len(newDirs) {
sd.lock.Lock()
defer sd.lock.Unlock()
sd.directories = newDirs
}
return nil
} else if !req.Dir && sd.files != nil {
newFiles := []*SwarmFile{}
for _, f := range sd.files {
if f.name == req.Name {
removeFileFromSwarm(f)
} else {
newFiles = append(newFiles, f)
}
}
if len(sd.files) > len(newFiles) {
sd.lock.Lock()
defer sd.lock.Unlock()
sd.files = newFiles
}
return nil
}
return fuse.ENOENT
}
func (sd *SwarmDir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
newDir := NewSwarmDir(req.Name, sd.mountInfo)
sd.lock.Lock()
defer sd.lock.Unlock()
sd.directories = append(sd.directories, newDir)
return newDir, nil
}