Skip to content

Commit

Permalink
when handeling files during copy ignore hidden/dot files
Browse files Browse the repository at this point in the history
Signed-off-by: Shawn Hurley <[email protected]>
  • Loading branch information
shawn-hurley committed Jan 11, 2024
1 parent bf78f97 commit 588fe42
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions cmd/internal/hiddenfile/hidden_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions cmd/internal/hiddenfile/hidden_windows.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 588fe42

Please sign in to comment.