Skip to content

Commit

Permalink
Fix windows fsys path formatting (#110)
Browse files Browse the repository at this point in the history
* Fix windows fsys path formatting

Signed-off-by: Kimmo Lehto <[email protected]>

* Always ps.DoubleQuote

Signed-off-by: Kimmo Lehto <[email protected]>

* Adapt test for doublequote

Signed-off-by: Kimmo Lehto <[email protected]>

* Add test for smb-style paths

Signed-off-by: Kimmo Lehto <[email protected]>

* Complicate the formatPath

Signed-off-by: Kimmo Lehto <[email protected]>

* Lint

Signed-off-by: Kimmo Lehto <[email protected]>

* Just remove the filepath.FromSlash

Signed-off-by: Kimmo Lehto <[email protected]>

* Restore doublequoting

Signed-off-by: Kimmo Lehto <[email protected]>

---------

Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke authored Aug 7, 2023
1 parent 6c181d0 commit b29f216
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
5 changes: 5 additions & 0 deletions pkg/powershell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func SingleQuote(v string) string {

// DoubleQuote escapes a string in a way that can be used as a windows file path
func DoubleQuote(v string) string {
if v[0] == '"' && v[len(v)-1] == '"' {
// already quoted
return v
}

var buf strings.Builder
_, _ = buf.WriteRune('"')
for _, rune := range v {
Expand Down
24 changes: 11 additions & 13 deletions pkg/rigfs/winfsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -379,7 +378,7 @@ func (fsys *WinFsys) OpenFile(name string, mode FileMode, _ FileMode) (File, err
}

log.Debugf("opening remote file %s (mode %s)", name, modeStr)
_, err := fsys.rcp.command(fmt.Sprintf("o %s %s", modeStr, filepath.FromSlash(name)))
_, err := fsys.rcp.command(fmt.Sprintf("o %s %s", modeStr, name))
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
}
Expand All @@ -388,7 +387,7 @@ func (fsys *WinFsys) OpenFile(name string, mode FileMode, _ FileMode) (File, err

// Stat returns fs.FileInfo for the remote file.
func (fsys *WinFsys) Stat(name string) (fs.FileInfo, error) {
resp, err := fsys.rcp.command(fmt.Sprintf("stat %s", filepath.FromSlash(name)))
resp, err := fsys.rcp.command(fmt.Sprintf("stat %s", name))
if err != nil {
return nil, &fs.PathError{Op: "stat", Path: name, Err: fmt.Errorf("%w: stat %s: %w", ErrRcpCommandFailed, name, err)}
}
Expand All @@ -400,7 +399,7 @@ func (fsys *WinFsys) Stat(name string) (fs.FileInfo, error) {

// Sha256 returns the SHA256 hash of the remote file.
func (fsys *WinFsys) Sha256(name string) (string, error) {
resp, err := fsys.rcp.command(fmt.Sprintf("sum %s", filepath.FromSlash(name)))
resp, err := fsys.rcp.command(fmt.Sprintf("sum %s", name))
if err != nil {
return "", &fs.PathError{Op: "sum", Path: name, Err: fmt.Errorf("%w: sha256sum: %w", ErrRcpCommandFailed, err)}
}
Expand All @@ -412,8 +411,7 @@ func (fsys *WinFsys) Sha256(name string) (string, error) {

// ReadDir reads the directory named by dirname and returns a list of directory entries.
func (fsys *WinFsys) ReadDir(name string) ([]fs.DirEntry, error) {
name = strings.ReplaceAll(name, "/", "\\")
resp, err := fsys.rcp.command(fmt.Sprintf("dir %s", filepath.FromSlash(name)))
resp, err := fsys.rcp.command(fmt.Sprintf("dir %s", ps.DoubleQuote(name)))
if err != nil {
return nil, &fs.PathError{Op: "readdir", Path: name, Err: fmt.Errorf("%w: readdir: %w: %w", ErrRcpCommandFailed, err, fs.ErrNotExist)}
}
Expand All @@ -433,7 +431,7 @@ func (fsys *WinFsys) Remove(name string) error {
return fsys.removeDir(name)
}

if err := fsys.conn.Exec(fmt.Sprintf("del %s", ps.DoubleQuote(filepath.FromSlash(name)))); err != nil {
if err := fsys.conn.Exec(fmt.Sprintf("del %s", ps.DoubleQuote(name))); err != nil {
return fmt.Errorf("%w: remove %s: %w", ErrCommandFailed, name, err)
}
return nil
Expand All @@ -445,30 +443,30 @@ func (fsys *WinFsys) RemoveAll(name string) error {
return fsys.removeDirAll(name)
}

if err := fsys.conn.Exec(fmt.Sprintf("del %s", ps.DoubleQuote(filepath.FromSlash(name)))); err != nil {
if err := fsys.conn.Exec(fmt.Sprintf("del %s", ps.DoubleQuote(name))); err != nil {
return fmt.Errorf("%w: remove all %s: %w", ErrCommandFailed, name, err)
}
return nil
}

func (fsys *WinFsys) removeDir(name string) error {
if err := fsys.conn.Exec(fmt.Sprintf("rmdir /q %s", ps.DoubleQuote(filepath.FromSlash(name)))); err != nil {
if err := fsys.conn.Exec(fmt.Sprintf("rmdir /q %s", ps.DoubleQuote(name))); err != nil {
return fmt.Errorf("%w: rmdir %s: %w", ErrCommandFailed, name, err)
}
return nil
}

func (fsys *WinFsys) removeDirAll(name string) error {
if err := fsys.conn.Exec(fmt.Sprintf("rmdir /s /q %s", ps.DoubleQuote(filepath.FromSlash(name)))); err != nil {
if err := fsys.conn.Exec(fmt.Sprintf("rmdir /s /q %s", ps.DoubleQuote(name))); err != nil {
return fmt.Errorf("%w: rmdir %s: %w", ErrCommandFailed, name, err)
}
return nil
}

// MkDirAll creates a directory named path, along with any necessary parents. The permission bits perm are ignored on Windows.
func (fsys *WinFsys) MkDirAll(path string, _ FileMode) error {
if err := fsys.conn.Exec(fmt.Sprintf("mkdir -p %s", ps.DoubleQuote(filepath.FromSlash(path)))); err != nil {
return fmt.Errorf("%w: mkdir %s: %w", ErrCommandFailed, path, err)
func (fsys *WinFsys) MkDirAll(name string, _ FileMode) error {
if err := fsys.conn.Exec(fmt.Sprintf("mkdir -p %s", ps.DoubleQuote(name))); err != nil {
return fmt.Errorf("%w: mkdir %s: %w", ErrCommandFailed, name, err)
}

return nil
Expand Down

0 comments on commit b29f216

Please sign in to comment.