Skip to content

Commit

Permalink
review adjustments 3
Browse files Browse the repository at this point in the history
  • Loading branch information
powellnorma committed Jan 10, 2024
1 parent 2a3c7ed commit 3892818
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
11 changes: 10 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Server struct {
openFilesLock sync.RWMutex
handleCount int
workDir string
winRoot bool
}

func (svr *Server) nextHandle(f file) string {
Expand Down Expand Up @@ -162,6 +163,14 @@ func ReadOnly() ServerOption {
}
}

// WinRoot configures a Server to serve a virtual '/' for windows that lists all drives
func WinRoot() ServerOption {
return func(s *Server) error {
s.winRoot = true
return nil
}
}

// WithAllocator enable the allocator.
// After processing a packet we keep in memory the allocated slices
// and we reuse them for new packets.
Expand Down Expand Up @@ -508,7 +517,7 @@ func (p *sshFxpOpenPacket) respond(svr *Server) responsePacket {
osFlags |= os.O_EXCL
}

f, err := openfile(svr.toLocalPath(p.Path), osFlags, 0o644)
f, err := svr.openfile(svr.toLocalPath(p.Path), osFlags, 0o644)
if err != nil {
return statusFromError(p.ID, err)
}
Expand Down
2 changes: 1 addition & 1 deletion server_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
"os"
)

func openfile(path string, flag int, mode fs.FileMode) (file, error) {
func (s *Server) openfile(path string, flag int, mode fs.FileMode) (file, error) {
return os.OpenFile(path, flag, mode)
}
31 changes: 19 additions & 12 deletions server_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func (s *Server) toLocalPath(p string) string {

lp := filepath.FromSlash(p)

if path.IsAbs(p) {
if path.IsAbs(p) { // starts with '/'
if len(p) == 1 && s.winRoot {
return `\\.\` // for openfile
}

tmp := lp
for len(tmp) > 0 && tmp[0] == '\\' {
tmp = tmp[1:]
Expand All @@ -39,6 +43,11 @@ func (s *Server) toLocalPath(p string) string {
// e.g. "/C:" to "C:\\"
return tmp
}

if s.winRoot {
// Make it so that "/Windows" is not found, and "/c:/Windows" has to be used
return `\\.\` + tmp
}
}

return lp
Expand Down Expand Up @@ -93,19 +102,17 @@ func newWinRoot() (*winRoot, error) {

func (f *winRoot) Readdir(n int) ([]os.FileInfo, error) {
drives := f.drives
if n > 0 {
if len(drives) > n {
drives = drives[:n]
}
f.drives = f.drives[len(drives):]
if len(drives) == 0 {
return nil, io.EOF
}
if n > 0 && len(drives) > n {
drives = drives[:n]
}
f.drives = f.drives[len(drives):]
if len(drives) == 0 {
return nil, io.EOF
}

var infos []os.FileInfo
for _, drive := range drives {
fi, err := os.Stat(drive)
fi, err := os.Stat(drive + `\`)
if err != nil {
return nil, err
}
Expand All @@ -120,8 +127,8 @@ func (f *winRoot) Readdir(n int) ([]os.FileInfo, error) {
return infos, nil
}

func openfile(path string, flag int, mode fs.FileMode) (file, error) {
if path == "/" {
func (s *Server) openfile(path string, flag int, mode fs.FileMode) (file, error) {
if path == `\\.\` && s.winRoot {
return newWinRoot()
}
return os.OpenFile(path, flag, mode)
Expand Down

0 comments on commit 3892818

Please sign in to comment.