Skip to content

Commit bd4e3ed

Browse files
committed
review adjustments 3
1 parent 2a3c7ed commit bd4e3ed

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

server.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type Server struct {
8080
openFilesLock sync.RWMutex
8181
handleCount int
8282
workDir string
83+
winRoot bool
8384
}
8485

8586
func (svr *Server) nextHandle(f file) string {
@@ -162,6 +163,14 @@ func ReadOnly() ServerOption {
162163
}
163164
}
164165

166+
// WinRoot configures a Server to serve a virtual '/' for windows that lists all drives
167+
func WinRoot() ServerOption {
168+
return func(s *Server) error {
169+
s.winRoot = true
170+
return nil
171+
}
172+
}
173+
165174
// WithAllocator enable the allocator.
166175
// After processing a packet we keep in memory the allocated slices
167176
// and we reuse them for new packets.
@@ -508,7 +517,7 @@ func (p *sshFxpOpenPacket) respond(svr *Server) responsePacket {
508517
osFlags |= os.O_EXCL
509518
}
510519

511-
f, err := openfile(svr.toLocalPath(p.Path), osFlags, 0o644)
520+
f, err := svr.openfile(svr.toLocalPath(p.Path), osFlags, 0o644)
512521
if err != nil {
513522
return statusFromError(p.ID, err)
514523
}

server_posix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ import (
88
"os"
99
)
1010

11-
func openfile(path string, flag int, mode fs.FileMode) (file, error) {
11+
func (s *Server) openfile(path string, flag int, mode fs.FileMode) (file, error) {
1212
return os.OpenFile(path, flag, mode)
1313
}

server_windows.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ func (s *Server) toLocalPath(p string) string {
1818

1919
lp := filepath.FromSlash(p)
2020

21-
if path.IsAbs(p) {
21+
if path.IsAbs(p) { // starts with '/'
22+
if len(p) == 1 {
23+
return `\\.\` // for openfile
24+
}
25+
2226
tmp := lp
2327
for len(tmp) > 0 && tmp[0] == '\\' {
2428
tmp = tmp[1:]
@@ -39,6 +43,11 @@ func (s *Server) toLocalPath(p string) string {
3943
// e.g. "/C:" to "C:\\"
4044
return tmp
4145
}
46+
47+
if s.winRoot {
48+
// Make it so that "/Windows" is not found, and "/c:/Windows" has to be used
49+
return `\\.\` + tmp
50+
}
4251
}
4352

4453
return lp
@@ -93,19 +102,17 @@ func newWinRoot() (*winRoot, error) {
93102

94103
func (f *winRoot) Readdir(n int) ([]os.FileInfo, error) {
95104
drives := f.drives
96-
if n > 0 {
97-
if len(drives) > n {
98-
drives = drives[:n]
99-
}
100-
f.drives = f.drives[len(drives):]
101-
if len(drives) == 0 {
102-
return nil, io.EOF
103-
}
105+
if n > 0 && len(drives) > n {
106+
drives = drives[:n]
107+
}
108+
f.drives = f.drives[len(drives):]
109+
if len(drives) == 0 {
110+
return nil, io.EOF
104111
}
105112

106113
var infos []os.FileInfo
107114
for _, drive := range drives {
108-
fi, err := os.Stat(drive)
115+
fi, err := os.Stat(drive + `\`)
109116
if err != nil {
110117
return nil, err
111118
}
@@ -120,8 +127,8 @@ func (f *winRoot) Readdir(n int) ([]os.FileInfo, error) {
120127
return infos, nil
121128
}
122129

123-
func openfile(path string, flag int, mode fs.FileMode) (file, error) {
124-
if path == "/" {
130+
func (s *Server) openfile(path string, flag int, mode fs.FileMode) (file, error) {
131+
if path == `\\.\` && s.winRoot {
125132
return newWinRoot()
126133
}
127134
return os.OpenFile(path, flag, mode)

0 commit comments

Comments
 (0)