From 3b6d3e94204577f28cf6a74c679b2bcfe38b5dd6 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 13 Oct 2023 16:42:45 -0400 Subject: [PATCH] fix: better handling of windows paths (#646) 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 --- pkg/disk/disk_windows.go | 4 ++++ pkg/path/finch.go | 15 --------------- pkg/path/finch_unix.go | 17 +++++++++++++++++ pkg/path/finch_windows.go | 24 ++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 pkg/path/finch_unix.go create mode 100644 pkg/path/finch_windows.go diff --git a/pkg/disk/disk_windows.go b/pkg/disk/disk_windows.go index 56cb18c80..f8e82dc0f 100644 --- a/pkg/disk/disk_windows.go +++ b/pkg/disk/disk_windows.go @@ -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 { diff --git a/pkg/path/finch.go b/pkg/path/finch.go index e84d0ab7a..32a9877dd 100644 --- a/pkg/path/finch.go +++ b/pkg/path/finch.go @@ -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") diff --git a/pkg/path/finch_unix.go b/pkg/path/finch_unix.go new file mode 100644 index 000000000..164018a54 --- /dev/null +++ b/pkg/path/finch_unix.go @@ -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 +} diff --git a/pkg/path/finch_windows.go b/pkg/path/finch_windows.go new file mode 100644 index 000000000..d83285b0f --- /dev/null +++ b/pkg/path/finch_windows.go @@ -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 +}