Skip to content

Commit

Permalink
Normalize paths on windows
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Mar 19, 2024
1 parent 5e9088e commit f703e49
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
12 changes: 7 additions & 5 deletions homedir/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
)

var errNotImplemented = errors.New("not implemented")

// Expand does ~/ style path expansion for files under current user home. ~user/ style paths are not supported.
func Expand(path string) (string, error) {
if !strings.HasPrefix(path, "~") {
return path, nil
func Expand(dir string) (string, error) {
if !strings.HasPrefix(dir, "~") {
return dir, nil
}

parts := strings.Split(path, string(os.PathSeparator))
parts := strings.Split(dir, string(os.PathSeparator))
if parts[0] != "~" {
return "", fmt.Errorf("%w: ~user/ style paths not supported", errNotImplemented)
}
Expand All @@ -27,8 +28,9 @@ func Expand(path string) (string, error) {
if err != nil {
return "", fmt.Errorf("homedir expand: %w", err)
}
home = strings.ReplaceAll(filepath.Clean(home), "\\", "/")

parts[0] = home

return filepath.Join(parts...), nil
return path.Join(parts...), nil
}
9 changes: 5 additions & 4 deletions protocol/ssh/sshconfig/configvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"os"
"os/user"
"path"
"path/filepath"
"runtime"
"slices"
Expand Down Expand Up @@ -416,17 +417,17 @@ func (v *PathValue) SetString(value string, originType ValueOriginType, origin s
}

if runtime.GOOS == "windows" {
// this is a hack to support the __PROGRAMDATA__ in the ssh default config
// this is a hack to support the __PROGRAMDATA__ in the ssh config dumps on windows.
value = strings.ReplaceAll(value, "__PROGRAMDATA__", os.Getenv("PROGRAMDATA"))
}

value = filepath.Clean(value)
value = strings.ReplaceAll(filepath.Clean(value), "\\", "/")

if !filepath.IsAbs(value) {
if origin != "" {
value = filepath.Join(filepath.Dir(origin), value)
value = filepath.Join(path.Dir(origin), value)
} else {
value = filepath.Join(home(), ".ssh", value)
value = path.Join(home(), ".ssh", value)
}
}

Expand Down
4 changes: 2 additions & 2 deletions protocol/ssh/sshconfig/defaultconfig_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ forwardx11 no
forwardx11timeout 1200
forwardx11trusted no
gatewayports no
globalknownhostsfile __PROGRAMDATA__\ssh/ssh_known_hosts __PROGRAMDATA__\ssh/ssh_known_hosts2
globalknownhostsfile __PROGRAMDATA__/ssh/ssh_known_hosts __PROGRAMDATA__/ssh/ssh_known_hosts2
gssapiauthentication no
gssapidelegatecredentials no
hashknownhosts no
Expand Down Expand Up @@ -88,5 +88,5 @@ updatehostkeys false
userknownhostsfile ~/.ssh/known_hosts ~/.ssh/known_hosts2
verifyhostkeydns false
visualhostkey no
xauthlocation /usr/X11R6/bin/xauth
xauthlocation __PROGRAMDATA__/ssh/bin/xauth
`

0 comments on commit f703e49

Please sign in to comment.