Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshots): Append path separator to Shadow Copy root directory on Windows #3891

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fs/localfs/local_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -52,6 +54,12 @@ func (e *filesystemEntry) fullPath() string {
return e.prefix + e.Name()
}

func (e *filesystemEntry) isWindowsVSSVolume() bool {
return runtime.GOOS == "windows" && //nolint:goconst
e.prefix == `\\?\GLOBALROOT\Device\` &&
strings.HasPrefix(e.Name(), "HarddiskVolumeShadowCopy")
}

func (e *filesystemEntry) Owner() fs.OwnerInfo {
return e.owner
}
Expand Down
8 changes: 7 additions & 1 deletion fs/localfs/local_fs_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@
func (fsd *filesystemDirectory) Iterate(ctx context.Context) (fs.DirectoryIterator, error) {
fullPath := fsd.fullPath()

f, direrr := os.Open(fullPath) //nolint:gosec
// Direct Windows volume paths (e.g. Shadow Copy) require a trailing \ or listing will fail
appendPath := ""
if fsd.isWindowsVSSVolume() {
appendPath = string(os.PathSeparator)
}

f, direrr := os.Open(fullPath + appendPath) //nolint:gosec
if direrr != nil {
return nil, errors.Wrap(direrr, "unable to read directory")
}
Expand Down Expand Up @@ -115,7 +121,7 @@
// cause os.Lstat to fail with "Incorrect function" error unless they
// end with a separator. Retry the operation with the separator added.
var e syscall.Errno
//nolint:goconst

Check failure on line 124 in fs/localfs/local_fs_os.go

View workflow job for this annotation

GitHub Actions / Lint (ubuntu-latest)

directive `//nolint:goconst` is unused for linter "goconst" (nolintlint)

Check failure on line 124 in fs/localfs/local_fs_os.go

View workflow job for this annotation

GitHub Actions / Lint (macos-latest)

directive `//nolint:goconst` is unused for linter "goconst" (nolintlint)
if runtime.GOOS == "windows" &&
!strings.HasSuffix(path, string(filepath.Separator)) &&
errors.As(err, &e) && e == 1 {
Expand Down
23 changes: 23 additions & 0 deletions fs/localfs/local_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,29 @@ func TestDirPrefix(t *testing.T) {
}
}

func TestIsWindowsVSSVolume(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("non-Windows platform")
}

var (
goodPrefix = `\\?\GLOBALROOT\Device\`
goodName = "HarddiskVolumeShadowCopy05"
)

cases := map[*filesystemEntry]bool{
{}: false,
{prefix: goodPrefix, name: goodName}: true,
{prefix: `C:\`, name: goodName}: false,
{prefix: goodPrefix, name: "HarddiskVolume"}: false,
{prefix: goodPrefix + goodName + `\`, name: "subdirectory"}: false,
}

for entry, want := range cases {
require.Equal(t, want, entry.isWindowsVSSVolume(), entry.prefix+entry.name)
}
}

func assertNoError(t *testing.T, err error) {
t.Helper()

Expand Down
Loading