Repository navigation
Expand file tree
/
Copy pathwritefs.go
More file actions
216 lines (180 loc) · 5.82 KB
/
Copy pathwritefs.go
File metadata and controls
216 lines (180 loc) · 5.82 KB
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
"bazil.org/fuse"
"bazil.org/fuse/fs"
_ "bazil.org/fuse/fs/fstestutil"
"golang.org/x/net/context"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s MOUNTPOINT\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 2 {
usage()
os.Exit(2)
}
original := flag.Arg(0) //original is the directory to be mirrored
mountpoint := flag.Arg(1)
c, err := fuse.Mount(
mountpoint,
fuse.FSName("completeFS"),
fuse.Subtype("writeFS"),
)
if err != nil {
log.Fatal(err)
}
defer c.Close()
myfs := FSroot{original}
err = fs.Serve(c, myfs)
if err != nil {
log.Fatal(err)
}
// check if the mount process has an error to report
<-c.Ready
if err := c.MountError; err != nil {
log.Fatal(err)
}
}
//FSroot implements the root of the filesystem--------------------------------------------------------
type FSroot struct {
path string
}
func (root FSroot) Root() (fs.Node, error) {
return Dir{root.path}, nil
}
//end of root struct
//Dir implements a directory---------------------------------------------------------------------------
type Dir struct {
path string
}
func (dir Dir) Attr(ctx context.Context, a *fuse.Attr) error {
file_stat := syscall.Stat_t{} //Stat_t struct holds info of a file(dir is also a file in some sense)
syscall.Stat(dir.path, &file_stat) //Stat() function fills the attributes of the file specified by "path"
a.Inode = uint64(file_stat.Ino)
a.Mode = os.ModeDir | os.FileMode(file_stat.Mode)
a.Size = uint64(file_stat.Size)
return nil
}
func (dir Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
opendir, err := os.Open(dir.path) //return type: *File
if err != nil {
log.Fatal(err)
}
fileInfoList, err := opendir.Readdir(-1) //return type []fileInfo
for _, file := range fileInfoList {
if file.Name() == name {
if file.IsDir() {
return Dir{dir.path + "/" + file.Name()}, nil
} else {
return File{dir.path + "/" + file.Name()}, nil
}
}
}
return nil, fuse.ENOENT
}
func (dir Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
fileList := []fuse.Dirent{}
opendir, err := os.Open(dir.path) //return os.file type object
if err != nil {
log.Fatal(err)
}
fileInfoList, err := opendir.Readdir(-1) //Readdir has return type FileInfo(necessary to get name of files)
for _, file := range fileInfoList {
fileStat := syscall.Stat_t{}
syscall.Stat(dir.path+"/"+file.Name(), &fileStat) //Necessary to get #inode
if file.IsDir() {
fileList = append(fileList, fuse.Dirent{Inode: fileStat.Ino, Name: file.Name(), Type: fuse.DT_Dir})
} else {
fileList = append(fileList, fuse.Dirent{Inode: fileStat.Ino, Name: file.Name(), Type: fuse.DT_File})
}
}
return fileList, nil
}
func (dir Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
err := os.Mkdir(dir.path+"/"+req.Name, req.Mode)
newdir := Dir{path: dir.path + "/" + req.Name}
return newdir, err
}
//end of Dir struct-------------------------------------------------------------------------------
//File implements a file--------------------------------------------------------------------------
type File struct {
path string
}
//A Handle is the interface required of an opened file or directory.
type FileHandle struct {
path string
handle *os.File //os.File represents an open file descriptor
}
func (file File) Attr(ctx context.Context, a *fuse.Attr) error {
file_info := syscall.Stat_t{} //Stat_t struct holds info of a file
syscall.Stat(file.path, &file_info) //Stat() function fills the attributes of the file specified by "path"
a.Inode = uint64(file_info.Ino)
a.Mode = os.FileMode(file_info.Mode)
a.Size = uint64(file_info.Size)
return nil
}
func (f File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
if req.Valid.Size() {
os.Truncate(f.path, int64(req.Size))
}
if req.Valid.Atime() || req.Valid.Mtime() {
os.Chtimes(f.path, req.Atime, req.Mtime)
}
if req.Valid.Gid() || req.Valid.Uid() {
os.Chown(f.path, int(req.Uid), int(req.Gid))
}
if req.Valid.Mode() {
os.Chmod(f.path, req.Mode)
}
return nil
}
/*func (file File) ReadAll(ctx context.Context) ([]byte, error) {
content, err := ioutil.ReadFile(file.path)
if err != nil {
log.Fatal(err)
}
return content, nil
}*/
func (fh FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
txt, err := ioutil.ReadFile(fh.path)
resp.Data = txt
return err
}
func (dir Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
openFile, err := os.Create(dir.path + "/" + req.Name)
file := File{path: dir.path + "/" + req.Name}
handle := FileHandle{path: dir.path + "/" + req.Name, handle: openFile}
return file, handle, err
}
func (file File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
fileinfo, err := os.Lstat(file.path)
openFile, err := os.OpenFile(file.path, int(req.Flags), fileinfo.Mode()) //return type *os.File
handle := FileHandle{path: file.path, handle: openFile}
resp.Handle = fuse.HandleID(req.Header.Node)
resp.Flags = fuse.OpenResponseFlags(req.Flags)
return handle, err
}
func (fh FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
n, err := fh.handle.Write(req.Data) //os.Write is method on type os.File
resp.Size = int(n)
return err
}
func (fh FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
err := fh.handle.Close()
return err
}
func (dir Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
err := os.Remove(dir.path + "/" + req.Name)
return err
}
//end of File struct-------------------------------------------------------------------------------