Skip to content

Commit

Permalink
fix: better handling of windows paths (#646)
Browse files Browse the repository at this point in the history
Issue #, if available:

*Description of changes:*
- Handle edge cases when dealing with Windows paths

*Testing done:*
- Tested on my Windows host


- [x] I've reviewed the guidance in CONTRIBUTING.md


#### License Acceptance

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 license.

Signed-off-by: Justin Alvarez <[email protected]>
  • Loading branch information
pendo324 authored Oct 13, 2023
1 parent bfa3557 commit 3b6d3e9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
4 changes: 4 additions & 0 deletions pkg/disk/disk_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ func (m *userDataDiskManager) EnsureUserDataDisk() error {
if err := m.fs.MkdirAll(disksDir, 0o700); err != nil {
return fmt.Errorf("could not create persistent disk directory: %w", err)
}
} else if err != nil {
return fmt.Errorf("error stating disksDir %q: %w", disksDir, err)
}

if _, err := m.fs.Stat(diskPath); errors.Is(err, fs.ErrNotExist) {
if err := m.createDisk(diskPath); err != nil {
return fmt.Errorf("could not create persistent disk: %w", err)
}
} else if err != nil {
return fmt.Errorf("error stating disksDir %q: %w", diskPath, err)
}

if err := m.attachDisk(diskPath); err != nil {
Expand Down
15 changes: 0 additions & 15 deletions pkg/path/finch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ import (
// Finch provides a set of methods that calculate paths relative to the Finch path.
type Finch string

// FinchRootDir returns the path to the Finch root directory.
// $HOME on UNIX and $LocalAppData on Windows.
func (Finch) FinchRootDir(ffd FinchFinderDeps) (string, error) {
if runtime.GOOS == "windows" {
return ffd.Env("LOCALAPPDATA"), nil
}

home, err := ffd.GetUserHome()
if err != nil {
return "", err
}

return home, nil
}

// FinchDir returns the path to the Finch config directory.
func (Finch) FinchDir(rootDir string) string {
return filepath.Join(rootDir, ".finch")
Expand Down
17 changes: 17 additions & 0 deletions pkg/path/finch_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
// +build !windows

package path

// FinchRootDir returns the path to the Finch root directory, which is $HOME on UNIX.
func (Finch) FinchRootDir(ffd FinchFinderDeps) (string, error) {
home, err := ffd.GetUserHome()
if err != nil {
return "", err
}

return home, nil
}
24 changes: 24 additions & 0 deletions pkg/path/finch_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//go:build windows
// +build windows

package path

import (
"golang.org/x/sys/windows/registry"
)

// FinchRootDir returns the path to the Finch root directory, which is %LOCALAPPDATA% on Windows.
// It also canonicalizes any environment variables that may be unexpanded in the path so that all
// paths based on it can be passed safely to other programs which may execute outside of the user's context.
func (Finch) FinchRootDir(ffd FinchFinderDeps) (string, error) {
appDir := ffd.Env("LOCALAPPDATA")
expandedPath, err := registry.ExpandString(appDir)
if err != nil {
return "", err
}

return expandedPath, nil
}

0 comments on commit 3b6d3e9

Please sign in to comment.