Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/radovskyb/watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
radovskyb committed Dec 13, 2018
2 parents 55a7734 + 5f7b4aa commit 3818ec2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
12 changes: 12 additions & 0 deletions ishidden.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions ishidden_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
24 changes: 21 additions & 3 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ func TestRemove(t *testing.T) {
// TODO: Test remove recursive function.

func TestIgnoreHiddenFilesRecursive(t *testing.T) {
// TODO: Write tests for ignore hidden on windows.
if runtime.GOOS == "windows" {
return
}

testDir, teardown := setup(t)
defer teardown()

Expand Down Expand Up @@ -327,6 +332,11 @@ func TestIgnoreHiddenFilesRecursive(t *testing.T) {
}

func TestIgnoreHiddenFiles(t *testing.T) {
// TODO: Write tests for ignore hidden on windows.
if runtime.GOOS == "windows" {
return
}

testDir, teardown := setup(t)
defer teardown()

Expand Down

0 comments on commit 3818ec2

Please sign in to comment.