small refactor*
This commit is contained in:
parent
b6b541e050
commit
24a4d30275
232 changed files with 2164 additions and 1906 deletions
server/pkg/go-nfs/example
52
server/pkg/go-nfs/example/helloworld/main.go
Normal file
52
server/pkg/go-nfs/example/helloworld/main.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/memfs"
|
||||
|
||||
nfs "git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs"
|
||||
"git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs/helpers"
|
||||
nfshelper "git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs/helpers"
|
||||
)
|
||||
|
||||
// ROFS is an intercepter for the filesystem indicating it should
|
||||
// be read only. The undelrying billy.Memfs indicates it supports
|
||||
// writing, but does not in implement billy.Change to support
|
||||
// modification of permissions / modTimes, and as such cannot be
|
||||
// used as RW system.
|
||||
type ROFS struct {
|
||||
nfs.Filesystem
|
||||
}
|
||||
|
||||
// Capabilities exports the filesystem as readonly
|
||||
func (ROFS) Capabilities() billy.Capability {
|
||||
return billy.ReadCapability | billy.SeekCapability
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
listener, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to listen: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Server running at %s\n", listener.Addr())
|
||||
|
||||
mem := helpers.WrapBillyFS(memfs.New())
|
||||
f, err := mem.Create(ctx, "hello.txt")
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create file: %v\n", err)
|
||||
return
|
||||
}
|
||||
_, _ = f.Write(ctx, []byte("hello world"))
|
||||
_ = f.Close(ctx)
|
||||
|
||||
handler := nfshelper.NewNullAuthHandler(ROFS{mem})
|
||||
cacheHelper := nfshelper.NewCachingHandler(handler, 1024)
|
||||
fmt.Printf("%v", nfs.Serve(listener, cacheHelper))
|
||||
}
|
38
server/pkg/go-nfs/example/osnfs/changeos.go
Normal file
38
server/pkg/go-nfs/example/osnfs/changeos.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
)
|
||||
|
||||
// NewChangeOSFS wraps billy osfs to add the change interface
|
||||
func NewChangeOSFS(fs billy.Filesystem) billy.Filesystem {
|
||||
return COS{fs}
|
||||
}
|
||||
|
||||
// COS or OSFS + Change wraps a billy.FS to not fail the `Change` interface.
|
||||
type COS struct {
|
||||
billy.Filesystem
|
||||
}
|
||||
|
||||
// Chmod changes mode
|
||||
func (fs COS) Chmod(name string, mode os.FileMode) error {
|
||||
return os.Chmod(fs.Join(fs.Root(), name), mode)
|
||||
}
|
||||
|
||||
// Lchown changes ownership
|
||||
func (fs COS) Lchown(name string, uid, gid int) error {
|
||||
return os.Lchown(fs.Join(fs.Root(), name), uid, gid)
|
||||
}
|
||||
|
||||
// Chown changes ownership
|
||||
func (fs COS) Chown(name string, uid, gid int) error {
|
||||
return os.Chown(fs.Join(fs.Root(), name), uid, gid)
|
||||
}
|
||||
|
||||
// Chtimes changes access time
|
||||
func (fs COS) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
||||
return os.Chtimes(fs.Join(fs.Root(), name), atime, mtime)
|
||||
}
|
28
server/pkg/go-nfs/example/osnfs/changeos_unix.go
Normal file
28
server/pkg/go-nfs/example/osnfs/changeos_unix.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
//go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func (fs COS) Mknod(path string, mode uint32, major uint32, minor uint32) error {
|
||||
dev := unix.Mkdev(major, minor)
|
||||
return unix.Mknod(fs.Join(fs.Root(), path), mode, int(dev))
|
||||
}
|
||||
|
||||
func (fs COS) Mkfifo(path string, mode uint32) error {
|
||||
return unix.Mkfifo(fs.Join(fs.Root(), path), mode)
|
||||
}
|
||||
|
||||
func (fs COS) Link(path string, link string) error {
|
||||
return unix.Link(fs.Join(fs.Root(), path), link)
|
||||
}
|
||||
|
||||
func (fs COS) Socket(path string) error {
|
||||
fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return unix.Bind(fd, &unix.SockaddrUnix{Name: fs.Join(fs.Root(), path)})
|
||||
}
|
36
server/pkg/go-nfs/example/osnfs/main.go
Normal file
36
server/pkg/go-nfs/example/osnfs/main.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
nfs "git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs"
|
||||
"git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs/helpers"
|
||||
nfshelper "git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs/helpers"
|
||||
osfs "github.com/go-git/go-billy/v5/osfs"
|
||||
)
|
||||
|
||||
func main() {
|
||||
port := ""
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Printf("Usage: osnfs </path/to/folder> [port]\n")
|
||||
return
|
||||
} else if len(os.Args) == 3 {
|
||||
port = os.Args[2]
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", ":"+port)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to listen: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("osnfs server running at %s\n", listener.Addr())
|
||||
|
||||
bfs := osfs.New(os.Args[1])
|
||||
bfsPlusChange := helpers.WrapBillyFS(NewChangeOSFS(bfs))
|
||||
|
||||
handler := nfshelper.NewNullAuthHandler(bfsPlusChange)
|
||||
cacheHelper := nfshelper.NewCachingHandler(handler, 1024)
|
||||
fmt.Printf("%v", nfs.Serve(listener, cacheHelper))
|
||||
}
|
37
server/pkg/go-nfs/example/osview/main.go
Normal file
37
server/pkg/go-nfs/example/osview/main.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/willscott/memphis"
|
||||
|
||||
nfs "git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs"
|
||||
"git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs/helpers"
|
||||
nfshelper "git.kmsign.ru/royalcat/tstor/server/pkg/go-nfs/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
port := ""
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Printf("Usage: osview </path/to/folder> [port]\n")
|
||||
return
|
||||
} else if len(os.Args) == 3 {
|
||||
port = os.Args[2]
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", ":"+port)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to listen: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Server running at %s\n", listener.Addr())
|
||||
|
||||
fs := memphis.FromOS(os.Args[1])
|
||||
bfs := helpers.WrapBillyFS(fs.AsBillyFS(0, 0))
|
||||
|
||||
handler := nfshelper.NewNullAuthHandler(bfs)
|
||||
cacheHelper := nfshelper.NewCachingHandler(handler, 1024)
|
||||
fmt.Printf("%v", nfs.Serve(listener, cacheHelper))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue