-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d545a4
commit f1c0a65
Showing
4 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package nfs | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/go-git/go-billy/v5" | ||
) | ||
|
||
var errReadOnly = errors.New("read-only file system") | ||
|
||
type ReadOnlyFS struct { | ||
fs billy.Filesystem | ||
} | ||
|
||
func NewROFS(fs billy.Filesystem) billy.Filesystem { | ||
return &ReadOnlyFS{fs: fs} | ||
} | ||
|
||
func (ro *ReadOnlyFS) Create(filename string) (billy.File, error) { | ||
return nil, errReadOnly | ||
} | ||
|
||
func (ro *ReadOnlyFS) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) { | ||
// Disallow any write flags | ||
if flag != os.O_RDONLY { | ||
return nil, errReadOnly | ||
} | ||
return ro.fs.OpenFile(filename, flag, perm) | ||
} | ||
|
||
func (ro *ReadOnlyFS) Rename(oldpath, newpath string) error { | ||
return errReadOnly | ||
} | ||
|
||
func (ro *ReadOnlyFS) Remove(filename string) error { | ||
return errReadOnly | ||
} | ||
|
||
// Delegate read-only operations to the underlying fs | ||
|
||
func (ro *ReadOnlyFS) Open(filename string) (billy.File, error) { | ||
return ro.fs.Open(filename) | ||
} | ||
|
||
func (ro *ReadOnlyFS) Stat(filename string) (os.FileInfo, error) { | ||
return ro.fs.Stat(filename) | ||
} | ||
|
||
func (ro *ReadOnlyFS) Join(elem ...string) string { | ||
return ro.fs.Join(elem...) | ||
} | ||
|
||
func (ro *ReadOnlyFS) TempFile(dir, prefix string) (billy.File, error) { | ||
return nil, errReadOnly | ||
} | ||
|
||
func (ro *ReadOnlyFS) ReadDir(path string) ([]os.FileInfo, error) { | ||
return ro.fs.ReadDir(path) | ||
} | ||
|
||
func (ro *ReadOnlyFS) MkdirAll(filename string, perm os.FileMode) error { | ||
return errReadOnly | ||
} | ||
|
||
func (ro *ReadOnlyFS) Lstat(filename string) (os.FileInfo, error) { | ||
return ro.fs.Lstat(filename) | ||
} | ||
|
||
func (ro *ReadOnlyFS) Symlink(target, link string) error { | ||
return errReadOnly | ||
} | ||
|
||
func (ro *ReadOnlyFS) Readlink(link string) (string, error) { | ||
return ro.fs.Readlink(link) | ||
} | ||
|
||
func (ro *ReadOnlyFS) Chroot(path string) (billy.Filesystem, error) { | ||
fs, err := ro.fs.Chroot(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return New(fs), nil | ||
} | ||
|
||
func (ro *ReadOnlyFS) Root() string { | ||
return ro.fs.Root() | ||
} | ||
|
||
func (ro *ReadOnlyFS) Capabilities() billy.Capability { | ||
return billy.DefaultCapabilities &^ billy.WriteCapability | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package nfs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/go-git/go-billy/v5/osfs" | ||
"github.com/sonroyaalmerol/pbs-plus/internal/agent/snapshots" | ||
"github.com/sonroyaalmerol/pbs-plus/internal/utils" | ||
"github.com/willscott/go-nfs" | ||
"github.com/willscott/go-nfs/helpers" | ||
) | ||
|
||
func Serve(ctx context.Context, errChan chan string, address, port string, driveLetter string) { | ||
var listener net.Listener | ||
listening := false | ||
|
||
listen := func() { | ||
var err error | ||
listenAt := fmt.Sprintf("%s:%s", address, port) | ||
listener, err = net.Listen("tcp", listenAt) | ||
if err != nil { | ||
errChan <- fmt.Sprintf("Port is already in use! Failed to listen on %s: %v", listenAt, err) | ||
return | ||
} | ||
listening = true | ||
} | ||
|
||
listen() | ||
|
||
for !listening { | ||
retryWait := utils.WaitChan(time.Second * 5) | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-retryWait: | ||
listen() | ||
} | ||
} | ||
|
||
defer listener.Close() | ||
|
||
snapshot, err := snapshots.Snapshot(driveLetter) | ||
if err != nil { | ||
errChan <- fmt.Sprintf("failed to initialize snapshot: %v", err) | ||
return | ||
} | ||
defer snapshot.Close() | ||
|
||
fs := osfs.New(snapshot.SnapshotPath) | ||
readOnlyFs := NewROFS(fs) | ||
nfsHandler := helpers.NewNullAuthHandler(readOnlyFs) | ||
|
||
go func() { | ||
for { | ||
go func() { | ||
err := nfs.Serve(listener, nfsHandler) | ||
if err != nil { | ||
errChan <- fmt.Sprintf("NFS server error: %v", err) | ||
} | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
listener.Close() | ||
return | ||
case <-errChan: | ||
} | ||
} | ||
}() | ||
} |