From 61a1ff89688956ab0295bc2302423dd431a4a228 Mon Sep 17 00:00:00 2001 From: Benjamin Radovsky Date: Thu, 13 Dec 2018 11:17:00 +1100 Subject: [PATCH] Check hidden files correctly for windows. --- ishidden.go | 12 ++++++++++++ ishidden_windows.go | 21 +++++++++++++++++++++ watcher.go | 24 +++++++++++++++++++++--- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 ishidden.go create mode 100644 ishidden_windows.go diff --git a/ishidden.go b/ishidden.go new file mode 100644 index 0000000..0f242e8 --- /dev/null +++ b/ishidden.go @@ -0,0 +1,12 @@ +// +build !windows + +package watcher + +import ( + "path/filepath" + "strings" +) + +func isHiddenFile(path string) (bool, error) { + return strings.HasPrefix(filepath.Base(path), "."), nil +} diff --git a/ishidden_windows.go b/ishidden_windows.go new file mode 100644 index 0000000..306c6b7 --- /dev/null +++ b/ishidden_windows.go @@ -0,0 +1,21 @@ +// +build windows + +package watcher + +import ( + "syscall" +) + +func isHiddenFile(path string) (bool, error) { + pointer, err := syscall.UTF16PtrFromString(path) + 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 +} diff --git a/watcher.go b/watcher.go index 07ab8ea..e353296 100644 --- a/watcher.go +++ b/watcher.go @@ -197,7 +197,13 @@ func (w *Watcher) Add(name string) (err error) { // If name is on the ignored list or if hidden files are // ignored and name is a hidden file or directory, simply return. _, ignored := w.ignored[name] - if ignored || (w.ignoreHidden && strings.HasPrefix(name, ".")) { + + isHidden, err := isHiddenFile(name) + if err != nil { + return err + } + + if ignored || (w.ignoreHidden && isHidden) { return nil } @@ -244,7 +250,13 @@ outer: for _, fInfo := range fInfoList { path := filepath.Join(name, fInfo.Name()) _, ignored := w.ignored[path] - if ignored || (w.ignoreHidden && strings.HasPrefix(fInfo.Name(), ".")) { + + isHidden, err := isHiddenFile(path) + if err != nil { + return nil, err + } + + if ignored || (w.ignoreHidden && isHidden) { continue } @@ -308,7 +320,13 @@ func (w *Watcher) listRecursive(name string) (map[string]os.FileInfo, error) { // If path is ignored and it's a directory, skip the directory. If it's // ignored and it's a single file, skip the file. _, ignored := w.ignored[path] - if ignored || (w.ignoreHidden && strings.HasPrefix(info.Name(), ".")) { + + isHidden, err := isHiddenFile(path) + if err != nil { + return err + } + + if ignored || (w.ignoreHidden && isHidden) { if info.IsDir() { return filepath.SkipDir }