From 588fe42085f13bd18a54b1906cceea6847b5e2c2 Mon Sep 17 00:00:00 2001 From: Shawn Hurley Date: Thu, 11 Jan 2024 15:06:30 -0500 Subject: [PATCH] when handeling files during copy ignore hidden/dot files Signed-off-by: Shawn Hurley --- cmd/analyze.go | 7 ++++ cmd/internal/hiddenfile/hidden_unix.go | 14 ++++++++ cmd/internal/hiddenfile/hidden_windows.go | 40 +++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 cmd/internal/hiddenfile/hidden_unix.go create mode 100644 cmd/internal/hiddenfile/hidden_windows.go diff --git a/cmd/analyze.go b/cmd/analyze.go index 83eaea5..f9fab64 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -17,6 +17,7 @@ import ( "strings" "github.com/go-logr/logr" + "github.com/konveyor-ecosystem/kantra/cmd/internal/hiddenfile" "github.com/konveyor/analyzer-lsp/engine" outputv1 "github.com/konveyor/analyzer-lsp/output/v1/konveyor" "github.com/konveyor/analyzer-lsp/provider" @@ -542,6 +543,12 @@ func (a *analyzeCommand) getRulesVolumes() (map[string]string, error) { // This will create the new dir a.handleDir(path, tempDir, r) } else { + // If we are unable to get the file attributes, probably safe to assume this is not a + // valid rule or ruleset and lets skip it for now. + if isHidden, err := hiddenfile.IsHidden(d.Name()); isHidden || err != nil { + a.log.V(5).Info("skipping hidden file", "path", path, "error", err) + return nil + } relpath, err := filepath.Rel(r, path) if err != nil { return err diff --git a/cmd/internal/hiddenfile/hidden_unix.go b/cmd/internal/hiddenfile/hidden_unix.go new file mode 100644 index 0000000..d268c10 --- /dev/null +++ b/cmd/internal/hiddenfile/hidden_unix.go @@ -0,0 +1,14 @@ +//go:build !windows && !windows +// +build !windows,!windows + +package hiddenfile + +const dotCharacter = 46 + +func IsHidden(name string) (bool, error) { + if name[0] == dotCharacter { + return true, nil + } + + return false, nil +} diff --git a/cmd/internal/hiddenfile/hidden_windows.go b/cmd/internal/hiddenfile/hidden_windows.go new file mode 100644 index 0000000..06dbf5b --- /dev/null +++ b/cmd/internal/hiddenfile/hidden_windows.go @@ -0,0 +1,40 @@ +//go:build windows +// +build windows + +package hiddenfile + +import ( + "path/filepath" + "syscall" +) + +const dotCharacter = 46 + +// isHidden checks if a file is hidden on Windows. +func IsHidden(name string) (bool, error) { + // dotfiles also count as hidden (if you want) + if name[0] == dotCharacter { + return true, nil + } + + absPath, err := filepath.Abs(name) + if err != nil { + return false, err + } + + // Appending `\\?\` to the absolute path helps with + // preventing 'Path Not Specified Error' when accessing + // long paths and filenames + // https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd + pointer, err := syscall.UTF16PtrFromString(`\\?\` + absPath) + if err != nil { + return false, err + } + + attributes, err := syscall.GetFileAttributes(pointer) + if err != nil { + return false, err + } + + return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil +}