/
mounted.go
168 lines (155 loc) · 3.54 KB
/
mounted.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
package tarwfs
import (
"io"
"os"
"github.com/hanwen/go-fuse/fuse"
"path"
"sync"
"syscall"
"archive/tar"
"time"
)
type TarwFS struct {
fuse.DefaultFileSystem
files map[string]*os.FileInfo
isDir map[string]bool
lock *sync.Mutex
w *tar.Writer
}
func New(w io.Writer) (wfs *TarwFS) {
wfs = &TarwFS{
files: map[string]*os.FileInfo{
"": &os.FileInfo{Mode: 0755 | syscall.S_IFDIR},
},
isDir: map[string]bool{"": true},
lock: &sync.Mutex{},
w: tar.NewWriter(w),
}
return
}
func (self *TarwFS) GetAttr(name string) (fi *os.FileInfo, eno fuse.Status) {
//log.Printf("GetAttr:start:%s", name)
self.lock.Lock()
fi, exists := self.files[name]
self.lock.Unlock()
if exists {
eno = fuse.OK
} else {
eno = fuse.ENOENT
}
//log.Printf("GetAttr:finish:%s:%v\t%+v", name, eno, fi)
return
}
func (self *TarwFS) OpenDir(name string) (c chan fuse.DirEntry, eno fuse.Status) {
//log.Printf("GetAttr:start:%s", name)
self.lock.Lock()
is_dir, exists := self.isDir[name]
self.lock.Unlock()
if exists && is_dir {
c = make(chan fuse.DirEntry, 16)
eno = fuse.OK
go func() {
self.lock.Lock()
// send ourselves first.
dent := fuse.DirEntry{Name: ".", Mode: self.files[name].Mode}
//log.Printf("Opendir(%s) -> %s {%+v}", name, ".", dent)
self.lock.Unlock()
c <- dent
self.lock.Lock()
for k, v := range self.files {
self.lock.Unlock()
k_d, k_n := path.Split(k)
if path.Join(name, k_n) == path.Join(k_d, k_n) && path.Join(name, k_n) != name {
dent := fuse.DirEntry{Name: k_n, Mode: v.Mode}
//log.Printf("Opendir(%s) -> %s|%s", name, k_d, k_n)
c <- dent
} else {
//log.Printf("%s is not the parent of %s|%s", name, k_d, k_n)
}
self.lock.Lock()
}
self.lock.Unlock()
close(c)
}()
} else {
if exists {
eno = fuse.ENOTDIR
} else {
eno = fuse.ENOENT
}
}
return
}
func (self *TarwFS) Mkdir(name string, mode uint32) (eno fuse.Status) {
//log.Printf("Open:Mkdir:%s:%x", name, mode)
self.lock.Lock()
_, exists := self.isDir[name]
if exists {
self.lock.Unlock()
eno = fuse.EPERM
} else {
self.files[name] = &os.FileInfo{
Mode: mode | syscall.S_IFDIR,
}
self.isDir[name] = true
self.lock.Unlock()
self.lock.Lock()
now := time.Seconds()
err := self.w.WriteHeader(&tar.Header{
Typeflag: tar.TypeDir,
Name: name,
Mode: int64(mode),
Size: 0,
Ctime: now,
Mtime: now,
Atime: now,
})
self.lock.Unlock()
if err == nil {
eno = fuse.OK
} else {
eno = fuse.EIO
}
}
//log.Printf("Open:Mkdir:%s:%x:%x:done\t%s", name, mode, eno)
return
}
func (self *TarwFS) Create(name string, flags, mode uint32) (file fuse.File, eno fuse.Status) {
//log.Printf("Create:%s", name)
self.lock.Lock()
_, exists := self.isDir[name]
if !exists {
self.files[name] = &os.FileInfo{Mode: mode | syscall.S_IFREG}
self.isDir[name] = false
self.lock.Unlock()
start := time.Seconds()
file = newFile(func(r io.Reader, rlen int64) (err os.Error) {
self.lock.Lock()
err = self.w.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: name,
Mode: int64(mode),
Size: rlen,
Ctime: start,
Mtime: time.Seconds(),
Atime: time.Seconds(),
})
if err == nil {
_, err = io.Copyn(self.w, r, rlen)
}
self.lock.Unlock()
return
})
eno = fuse.OK
} else {
self.lock.Unlock()
eno = fuse.EINVAL
}
return
}
func (self *TarwFS) Unmount() {
// In case someone else is finishing up still, wait for them to unmount the lock.
self.lock.Lock()
self.w.Close()
self.lock.Unlock()
}