From 2a3c7ed21de514f2c138e2efa95906fac880320e Mon Sep 17 00:00:00 2001 From: powellnorma <101364699+powellnorma@users.noreply.github.com> Date: Tue, 9 Jan 2024 21:53:27 +0100 Subject: [PATCH] review adjustments 2 --- server_windows.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/server_windows.go b/server_windows.go index fad5e188..5aabf05f 100644 --- a/server_windows.go +++ b/server_windows.go @@ -1,5 +1,3 @@ -//go:build go1.18 - package sftp import ( @@ -80,26 +78,33 @@ func (i *driveInfo) Name() string { type winRoot struct { dummyFile - doneDirs int + drives []string } -func (f *winRoot) Readdir(n int) ([]os.FileInfo, error) { +func newWinRoot() (*winRoot, error) { drives, err := getDrives() if err != nil { return nil, err } + return &winRoot{ + drives: drives, + }, nil +} - if f.doneDirs >= len(drives) { - return nil, io.EOF +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 + } } - drives = drives[f.doneDirs:] var infos []os.FileInfo - for i, drive := range drives { - if i >= n { - break - } - + for _, drive := range drives { fi, err := os.Stat(drive) if err != nil { return nil, err @@ -112,13 +117,12 @@ func (f *winRoot) Readdir(n int) ([]os.FileInfo, error) { infos = append(infos, di) } - f.doneDirs += len(infos) return infos, nil } func openfile(path string, flag int, mode fs.FileMode) (file, error) { if path == "/" { - return &winRoot{}, nil + return newWinRoot() } return os.OpenFile(path, flag, mode) }