Skip to content

Commit 61a1ff8

Browse files
committed
Check hidden files correctly for windows.
1 parent d6c8efa commit 61a1ff8

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

ishidden.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build !windows
2+
3+
package watcher
4+
5+
import (
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
func isHiddenFile(path string) (bool, error) {
11+
return strings.HasPrefix(filepath.Base(path), "."), nil
12+
}

ishidden_windows.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// +build windows
2+
3+
package watcher
4+
5+
import (
6+
"syscall"
7+
)
8+
9+
func isHiddenFile(path string) (bool, error) {
10+
pointer, err := syscall.UTF16PtrFromString(path)
11+
if err != nil {
12+
return false, err
13+
}
14+
15+
attributes, err := syscall.GetFileAttributes(pointer)
16+
if err != nil {
17+
return false, err
18+
}
19+
20+
return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0, nil
21+
}

watcher.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ func (w *Watcher) Add(name string) (err error) {
197197
// If name is on the ignored list or if hidden files are
198198
// ignored and name is a hidden file or directory, simply return.
199199
_, ignored := w.ignored[name]
200-
if ignored || (w.ignoreHidden && strings.HasPrefix(name, ".")) {
200+
201+
isHidden, err := isHiddenFile(name)
202+
if err != nil {
203+
return err
204+
}
205+
206+
if ignored || (w.ignoreHidden && isHidden) {
201207
return nil
202208
}
203209

@@ -244,7 +250,13 @@ outer:
244250
for _, fInfo := range fInfoList {
245251
path := filepath.Join(name, fInfo.Name())
246252
_, ignored := w.ignored[path]
247-
if ignored || (w.ignoreHidden && strings.HasPrefix(fInfo.Name(), ".")) {
253+
254+
isHidden, err := isHiddenFile(path)
255+
if err != nil {
256+
return nil, err
257+
}
258+
259+
if ignored || (w.ignoreHidden && isHidden) {
248260
continue
249261
}
250262

@@ -308,7 +320,13 @@ func (w *Watcher) listRecursive(name string) (map[string]os.FileInfo, error) {
308320
// If path is ignored and it's a directory, skip the directory. If it's
309321
// ignored and it's a single file, skip the file.
310322
_, ignored := w.ignored[path]
311-
if ignored || (w.ignoreHidden && strings.HasPrefix(info.Name(), ".")) {
323+
324+
isHidden, err := isHiddenFile(path)
325+
if err != nil {
326+
return err
327+
}
328+
329+
if ignored || (w.ignoreHidden && isHidden) {
312330
if info.IsDir() {
313331
return filepath.SkipDir
314332
}

0 commit comments

Comments
 (0)