Commit 8c5c22a9 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Hide LoopBackFileSystem type to reduce API clutter.

parent c6e90c78
...@@ -15,26 +15,26 @@ import ( ...@@ -15,26 +15,26 @@ import (
var _ = fmt.Println var _ = fmt.Println
var _ = log.Println var _ = log.Println
// A FUSE filesystem that shunts all request to an underlying file type loopbackFileSystem struct {
// system. Its main purpose is to provide test coverage without
// having to build an actual synthetic filesystem.
type LoopbackFileSystem struct {
Root string Root string
DefaultFileSystem DefaultFileSystem
} }
func NewLoopbackFileSystem(root string) (out *LoopbackFileSystem) { // A FUSE filesystem that shunts all request to an underlying file
out = new(LoopbackFileSystem) // system. Its main purpose is to provide test coverage without
// having to build a synthetic filesystem.
func NewLoopbackFileSystem(root string) (out *loopbackFileSystem) {
out = new(loopbackFileSystem)
out.Root = root out.Root = root
return out return out
} }
func (fs *LoopbackFileSystem) GetPath(relPath string) string { func (fs *loopbackFileSystem) GetPath(relPath string) string {
return filepath.Join(fs.Root, relPath) return filepath.Join(fs.Root, relPath)
} }
func (fs *LoopbackFileSystem) GetAttr(name string, context *fuse.Context) (a *fuse.Attr, code fuse.Status) { func (fs *loopbackFileSystem) GetAttr(name string, context *fuse.Context) (a *fuse.Attr, code fuse.Status) {
fullPath := fs.GetPath(name) fullPath := fs.GetPath(name)
var err error = nil var err error = nil
st := syscall.Stat_t{} st := syscall.Stat_t{}
...@@ -54,9 +54,9 @@ func (fs *LoopbackFileSystem) GetAttr(name string, context *fuse.Context) (a *fu ...@@ -54,9 +54,9 @@ func (fs *LoopbackFileSystem) GetAttr(name string, context *fuse.Context) (a *fu
return a, fuse.OK return a, fuse.OK
} }
var _ = (FileSystem)((*LoopbackFileSystem)(nil)) var _ = (FileSystem)((*loopbackFileSystem)(nil))
func (fs *LoopbackFileSystem) OpenDir(name string, context *fuse.Context) (stream []fuse.DirEntry, status fuse.Status) { func (fs *loopbackFileSystem) OpenDir(name string, context *fuse.Context) (stream []fuse.DirEntry, status fuse.Status) {
// What other ways beyond O_RDONLY are there to open // What other ways beyond O_RDONLY are there to open
// directories? // directories?
f, err := os.Open(fs.GetPath(name)) f, err := os.Open(fs.GetPath(name))
...@@ -92,7 +92,7 @@ func (fs *LoopbackFileSystem) OpenDir(name string, context *fuse.Context) (strea ...@@ -92,7 +92,7 @@ func (fs *LoopbackFileSystem) OpenDir(name string, context *fuse.Context) (strea
return output, fuse.OK return output, fuse.OK
} }
func (fs *LoopbackFileSystem) Open(name string, flags uint32, context *fuse.Context) (fuseFile fuse.File, status fuse.Status) { func (fs *loopbackFileSystem) Open(name string, flags uint32, context *fuse.Context) (fuseFile fuse.File, status fuse.Status) {
f, err := os.OpenFile(fs.GetPath(name), int(flags), 0) f, err := os.OpenFile(fs.GetPath(name), int(flags), 0)
if err != nil { if err != nil {
return nil, fuse.ToStatus(err) return nil, fuse.ToStatus(err)
...@@ -100,20 +100,20 @@ func (fs *LoopbackFileSystem) Open(name string, flags uint32, context *fuse.Cont ...@@ -100,20 +100,20 @@ func (fs *LoopbackFileSystem) Open(name string, flags uint32, context *fuse.Cont
return &fuse.LoopbackFile{File: f}, fuse.OK return &fuse.LoopbackFile{File: f}, fuse.OK
} }
func (fs *LoopbackFileSystem) Chmod(path string, mode uint32, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Chmod(path string, mode uint32, context *fuse.Context) (code fuse.Status) {
err := os.Chmod(fs.GetPath(path), os.FileMode(mode)) err := os.Chmod(fs.GetPath(path), os.FileMode(mode))
return fuse.ToStatus(err) return fuse.ToStatus(err)
} }
func (fs *LoopbackFileSystem) Chown(path string, uid uint32, gid uint32, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Chown(path string, uid uint32, gid uint32, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(os.Chown(fs.GetPath(path), int(uid), int(gid))) return fuse.ToStatus(os.Chown(fs.GetPath(path), int(uid), int(gid)))
} }
func (fs *LoopbackFileSystem) Truncate(path string, offset uint64, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Truncate(path string, offset uint64, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(os.Truncate(fs.GetPath(path), int64(offset))) return fuse.ToStatus(os.Truncate(fs.GetPath(path), int64(offset)))
} }
func (fs *LoopbackFileSystem) Utimens(path string, Atime *time.Time, Mtime *time.Time, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Utimens(path string, Atime *time.Time, Mtime *time.Time, context *fuse.Context) (code fuse.Status) {
var a time.Time var a time.Time
if Atime != nil { if Atime != nil {
a = *Atime a = *Atime
...@@ -125,46 +125,46 @@ func (fs *LoopbackFileSystem) Utimens(path string, Atime *time.Time, Mtime *time ...@@ -125,46 +125,46 @@ func (fs *LoopbackFileSystem) Utimens(path string, Atime *time.Time, Mtime *time
return fuse.ToStatus(os.Chtimes(fs.GetPath(path), a, m)) return fuse.ToStatus(os.Chtimes(fs.GetPath(path), a, m))
} }
func (fs *LoopbackFileSystem) Readlink(name string, context *fuse.Context) (out string, code fuse.Status) { func (fs *loopbackFileSystem) Readlink(name string, context *fuse.Context) (out string, code fuse.Status) {
f, err := os.Readlink(fs.GetPath(name)) f, err := os.Readlink(fs.GetPath(name))
return f, fuse.ToStatus(err) return f, fuse.ToStatus(err)
} }
func (fs *LoopbackFileSystem) Mknod(name string, mode uint32, dev uint32, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Mknod(name string, mode uint32, dev uint32, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(syscall.Mknod(fs.GetPath(name), mode, int(dev))) return fuse.ToStatus(syscall.Mknod(fs.GetPath(name), mode, int(dev)))
} }
func (fs *LoopbackFileSystem) Mkdir(path string, mode uint32, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Mkdir(path string, mode uint32, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(os.Mkdir(fs.GetPath(path), os.FileMode(mode))) return fuse.ToStatus(os.Mkdir(fs.GetPath(path), os.FileMode(mode)))
} }
// Don't use os.Remove, it removes twice (unlink followed by rmdir). // Don't use os.Remove, it removes twice (unlink followed by rmdir).
func (fs *LoopbackFileSystem) Unlink(name string, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Unlink(name string, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(syscall.Unlink(fs.GetPath(name))) return fuse.ToStatus(syscall.Unlink(fs.GetPath(name)))
} }
func (fs *LoopbackFileSystem) Rmdir(name string, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Rmdir(name string, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(syscall.Rmdir(fs.GetPath(name))) return fuse.ToStatus(syscall.Rmdir(fs.GetPath(name)))
} }
func (fs *LoopbackFileSystem) Symlink(pointedTo string, linkName string, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Symlink(pointedTo string, linkName string, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(os.Symlink(pointedTo, fs.GetPath(linkName))) return fuse.ToStatus(os.Symlink(pointedTo, fs.GetPath(linkName)))
} }
func (fs *LoopbackFileSystem) Rename(oldPath string, newPath string, context *fuse.Context) (codee fuse.Status) { func (fs *loopbackFileSystem) Rename(oldPath string, newPath string, context *fuse.Context) (codee fuse.Status) {
err := os.Rename(fs.GetPath(oldPath), fs.GetPath(newPath)) err := os.Rename(fs.GetPath(oldPath), fs.GetPath(newPath))
return fuse.ToStatus(err) return fuse.ToStatus(err)
} }
func (fs *LoopbackFileSystem) Link(orig string, newName string, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Link(orig string, newName string, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(os.Link(fs.GetPath(orig), fs.GetPath(newName))) return fuse.ToStatus(os.Link(fs.GetPath(orig), fs.GetPath(newName)))
} }
func (fs *LoopbackFileSystem) Access(name string, mode uint32, context *fuse.Context) (code fuse.Status) { func (fs *loopbackFileSystem) Access(name string, mode uint32, context *fuse.Context) (code fuse.Status) {
return fuse.ToStatus(syscall.Access(fs.GetPath(name), mode)) return fuse.ToStatus(syscall.Access(fs.GetPath(name), mode))
} }
func (fs *LoopbackFileSystem) Create(path string, flags uint32, mode uint32, context *fuse.Context) (fuseFile fuse.File, code fuse.Status) { func (fs *loopbackFileSystem) Create(path string, flags uint32, mode uint32, context *fuse.Context) (fuseFile fuse.File, code fuse.Status) {
f, err := os.OpenFile(fs.GetPath(path), int(flags)|os.O_CREATE, os.FileMode(mode)) f, err := os.OpenFile(fs.GetPath(path), int(flags)|os.O_CREATE, os.FileMode(mode))
return &fuse.LoopbackFile{File: f}, fuse.ToStatus(err) return &fuse.LoopbackFile{File: f}, fuse.ToStatus(err)
} }
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
"github.com/hanwen/go-fuse/fuse" "github.com/hanwen/go-fuse/fuse"
) )
func (fs *LoopbackFileSystem) StatFs(name string) *fuse.StatfsOut { func (fs *loopbackFileSystem) StatFs(name string) *fuse.StatfsOut {
s := syscall.Statfs_t{} s := syscall.Statfs_t{}
err := syscall.Statfs(fs.GetPath(name), &s) err := syscall.Statfs(fs.GetPath(name), &s)
if err == nil { if err == nil {
...@@ -25,21 +25,21 @@ func (fs *LoopbackFileSystem) StatFs(name string) *fuse.StatfsOut { ...@@ -25,21 +25,21 @@ func (fs *LoopbackFileSystem) StatFs(name string) *fuse.StatfsOut {
return nil return nil
} }
func (fs *LoopbackFileSystem) ListXAttr(name string, context *fuse.Context) ([]string, fuse.Status) { func (fs *loopbackFileSystem) ListXAttr(name string, context *fuse.Context) ([]string, fuse.Status) {
data, errNo := ListXAttr(fs.GetPath(name)) data, errNo := ListXAttr(fs.GetPath(name))
return data, fuse.Status(errNo) return data, fuse.Status(errNo)
} }
func (fs *LoopbackFileSystem) RemoveXAttr(name string, attr string, context *fuse.Context) fuse.Status { func (fs *loopbackFileSystem) RemoveXAttr(name string, attr string, context *fuse.Context) fuse.Status {
return fuse.Status(Removexattr(fs.GetPath(name), attr)) return fuse.Status(Removexattr(fs.GetPath(name), attr))
} }
func (fs *LoopbackFileSystem) String() string { func (fs *loopbackFileSystem) String() string {
return fmt.Sprintf("LoopbackFs(%s)", fs.Root) return fmt.Sprintf("LoopbackFs(%s)", fs.Root)
} }
func (fs *LoopbackFileSystem) GetXAttr(name string, attr string, context *fuse.Context) ([]byte, fuse.Status) { func (fs *loopbackFileSystem) GetXAttr(name string, attr string, context *fuse.Context) ([]byte, fuse.Status) {
data := make([]byte, 1024) data := make([]byte, 1024)
data, errNo := GetXAttr(fs.GetPath(name), attr, data) data, errNo := GetXAttr(fs.GetPath(name), attr, data)
......
...@@ -16,11 +16,11 @@ import ( ...@@ -16,11 +16,11 @@ import (
var _ = log.Println var _ = log.Println
type cacheFs struct { type cacheFs struct {
*pathfs.LoopbackFileSystem pathfs.FileSystem
} }
func (fs *cacheFs) Open(name string, flags uint32, context *fuse.Context) (fuseFile fuse.File, status fuse.Status) { func (fs *cacheFs) Open(name string, flags uint32, context *fuse.Context) (fuseFile fuse.File, status fuse.Status) {
f, c := fs.LoopbackFileSystem.Open(name, flags, context) f, c := fs.FileSystem.Open(name, flags, context)
if !c.Ok() { if !c.Ok() {
return f, c return f, c
} }
...@@ -40,7 +40,7 @@ func setupCacheTest(t *testing.T) (string, *pathfs.PathNodeFs, func()) { ...@@ -40,7 +40,7 @@ func setupCacheTest(t *testing.T) (string, *pathfs.PathNodeFs, func()) {
os.Mkdir(dir+"/orig", 0755) os.Mkdir(dir+"/orig", 0755)
fs := &cacheFs{ fs := &cacheFs{
LoopbackFileSystem: pathfs.NewLoopbackFileSystem(dir + "/orig"), pathfs.NewLoopbackFileSystem(dir + "/orig"),
} }
pfs := pathfs.NewPathNodeFs(fs, nil) pfs := pathfs.NewPathNodeFs(fs, nil)
state, conn, err := fuse.MountNodeFileSystem(dir+"/mnt", pfs, nil) state, conn, err := fuse.MountNodeFileSystem(dir+"/mnt", pfs, nil)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment