Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OSFileWatcher to support symlinks #6172

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions internal/watch/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package watch

import (
"log"
"os"
"path"
"path/filepath"
"strings"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -60,15 +62,31 @@ func (f *OSFileWatcher) watch() error {
}
f.watcher = watcher

realFile, err := filepath.EvalSymlinks(f.file)
if err != nil {
return err
}

dir, file := path.Split(f.file)
go func(file string) {
for {
select {
case event := <-watcher.Events:
if (event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Create == fsnotify.Create) &&
strings.HasSuffix(event.Name, file) {
f.onEvent()
if event.Op&fsnotify.Create == fsnotify.Create ||
event.Op&fsnotify.Write == fsnotify.Write {
if finfo, err := os.Lstat(event.Name); err != nil {
log.Printf("can not lstat file: %v\n", err)
} else if finfo.Mode()&os.ModeSymlink != 0 {
if currentRealFile, err := filepath.EvalSymlinks(f.file); err == nil &&
currentRealFile != realFile {
f.onEvent()
Copy link
Member

@ElvinEfendi ElvinEfendi Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you not have to condition this on strings.HasSuffix(event.Name, file)? Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the FileWatcher watches the entire directory (that's not new behaviour). Using the example in the PR description, the watcher will watch $dir/ca.crt, which is a symlink to $dir/..data/ca.crt, which is a symlink to $dir/..<dated_dir>/ca.crt. The CREATE event in $dir is not for a file called ca.crt (which would have the correct suffix), but for a directory called ..data which symlinks to the dated directory. However, the watcher should detect that the top-level symlink ($dir/ca.crt) now points to a different file, and trigger an event.
tl;dr when any file is created in the directory being watched, this FileWatcher should confirm if the symlink it was told to monitor has been updated somewhere within the directory (maybe deeper than 1 level).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More details: this comment shows the internal order of events.

realFile = currentRealFile
}
continue
}
if strings.HasSuffix(event.Name, file) {
f.onEvent()
}
}
case err := <-watcher.Errors:
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions internal/watch/file_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package watch
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -67,3 +69,73 @@ func TestFileWatcher(t *testing.T) {
t.Fatalf("expected an event shortly after writing a file")
}
}

func TestFileWatcherWithNestedSymlink(t *testing.T) {
target1, err := ioutil.TempFile("", "t1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer target1.Close()
defer os.Remove(target1.Name())
dir := path.Dir(target1.Name())

innerLink := path.Join(dir, "innerLink")
if err = os.Symlink(target1.Name(), innerLink); err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer os.Remove(innerLink)
mainLink := path.Join(dir, "mainLink")
if err = os.Symlink(innerLink, mainLink); err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer os.Remove(mainLink)

targetName, err := filepath.EvalSymlinks(mainLink)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if targetName != target1.Name() {
t.Fatalf("expected symlink to point to %v, not %v", target1.Name(), targetName)
}

count := 0
events := make(chan bool, 10)
fw, err := NewFileWatcher(mainLink, func() {
count++
if count != 1 {
t.Fatalf("expected 1 but returned %v", count)
}
if targetName, err = filepath.EvalSymlinks(mainLink); err != nil {
t.Fatalf("unexpected error: %v", err)
}
events <- true
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer fw.Close()

target2, err := ioutil.TempFile("", "t2")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer target2.Close()
defer os.Remove(target2.Name())

if err = os.Remove(innerLink); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err = os.Symlink(target2.Name(), innerLink); err != nil {
t.Fatalf("unexpected error: %v", err)
}

timeoutChan := prepareTimeout()
select {
case <-events:
case <-timeoutChan:
t.Fatalf("expected an event shortly after creating a file and relinking")
}
if targetName != target2.Name() {
t.Fatalf("expected symlink to point to %v, not %v", target2.Name(), targetName)
}
}