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

Fix symlink behaviour on kqueue #524

Merged
merged 1 commit into from Jul 12, 2023
Merged

Fix symlink behaviour on kqueue #524

merged 1 commit into from Jul 12, 2023

Conversation

arp242
Copy link
Member

@arp242 arp242 commented Oct 15, 2022

There were two problems here:

  • When we see a link inside a directory the resolved version would get added to w.watches etc. but the link won't, resulting in many spurious create events for the link. This fixes Spurious symlink CREATE events on macOS when watching a directory #277; I'm surprised there aren't more reports for this).

  • filepath.EvalSymlinks() will resolve any symlinks in the entire path, rather than just the link itself. This would cause paths such as:

    /path/to/LINK/dir/dir/WATCH-THIS
    

    To have the wrong Event.Name; many of the test cases failed because of this because /tmp is a link to /private/tmp on macOS, but curiously no one reported it AFAIK (I guess many people don't use symlinks all that much).

    Example:

    % mkdir /tmp/xxx
    % touch /tmp/xxx/FILE
    
    % ln -s /tmp/xxx/LINK /tmp/xxx/FILE   # 25 years of Unix experience, and still...
    ln: /tmp/xxx/FILE: File exists
    % ln -s /tmp/xxx/FILE /tmp/xxx/LINK
    

    Before it would do:

    % go run ./cmd/fsnotify watch /tmp/xxx &
    % touch /tmp/xxx/FILE
    03:23:03.2731   1 CHMOD         "/private/tmp/xxx/FILE"
    03:23:03.2744   2 CHMOD         "/tmp/xxx/FILE"
    ^C
    
    % fsnotify watch /tmp/xxx/LINK &
    % touch /tmp/xxx/LINK
    03:26:37.3576   1 CHMOD         "/private/tmp/xxx/FILE"
    

    And now it does:

    % go run ./cmd/fsnotify watch /tmp/xxx &
    03:23:47.8911   1 CHMOD         "/tmp/xxx/FILE"
    ^C
    
    % fsnotify watch /tmp/xxx/LINK &
    % touch /tmp/xxx/LINK
    03:27:38.5227   1 CHMOD         "/tmp/xxx/FILE"
    

@arp242
Copy link
Member Author

arp242 commented Oct 15, 2022

Still got some build failures in CI.

macOS 11 (after restart it did work):

--- FAIL: TestWatchSymlink/277 (1.18s)
    fsnotify_test.go:558: 
        have:
        	CREATE               "/foo"
        	WRITE                ""
        	WRITE                ""
        	WRITE                ""
        	REMOVE               "/foo"
        	CREATE               "/apple"
        	CREATE               "/pear"
        	REMOVE|RENAME        "/apple"
        	REMOVE               "/pear"
        want:
        	CREATE               "/foo"
        	REMOVE               "/foo"
        	CREATE               "/apple"
        	CREATE               "/pear"
        	REMOVE|RENAME        "/apple"
        	REMOVE               "/pear"

FreeBSD:

        	CREATE               "/foo"
        	REMOVE               "/foo"
        	CREATE               "/apple"
        	CREATE               "/pear"
        	REMOVE|RENAME        "/apple"
        	REMOVE               "/pear"
        	REMOVE|WRITE         ""
        	CREATE               "."

        	# After restart
        	CREATE               "/foo"
        	REMOVE               "/foo"
        	CREATE               "/apple"
        	CREATE               "/pear"
        	REMOVE|RENAME        "/apple"
        	REMOVE               "/pear"

NetBSD:

        	CREATE               "/foo"
        	REMOVE               "/foo"
        	CREATE               "/apple"
        	REMOVE|RENAME        "/apple"

        	# After restart
        	CREATE               "/foo"
        	REMOVE               "/foo"
        	CREATE               "/apple"
        	CREATE               "/pear"
        	REMOVE|RENAME        "/apple"
        	REMOVE               "/pear"

There were two problems here:

- When we see a link inside a directory the resolved version would get
  added to w.watches etc. but the link won't, resulting in many spurious
  create events for the link. This fixes #277; I'm surprised there
  aren't more reports for this).

- filepath.EvalSymlinks() will resolve any symlinks in the entire path,
  rather than just the link itself. This would cause paths such as:

      /path/to/LINK/dir/dir/WATCH-THIS

  To have the wrong Event.Name; many of the test cases failed because of
  this because /tmp is a link to /private/tmp on macOS, but curiously no
  one reported it AFAIK (I guess many people don't use symlinks all that
  much).

  Example:

      % mkdir /tmp/xxx
      % touch /tmp/xxx/FILE

      % ln -s /tmp/xxx/LINK /tmp/xxx/FILE   # 25 years of Unix experience, and still...
      ln: /tmp/xxx/FILE: File exists
      % ln -s /tmp/xxx/FILE /tmp/xxx/LINK

  Before it would do:

      % go run ./cmd/fsnotify watch /tmp/xxx &
      % touch /tmp/xxx/FILE
      03:23:03.2731   1 CHMOD         "/private/tmp/xxx/FILE"
      03:23:03.2744   2 CHMOD         "/tmp/xxx/FILE"
      ^C

      % fsnotify watch /tmp/xxx/LINK &
      % touch /tmp/xxx/LINK
      03:26:37.3576   1 CHMOD         "/private/tmp/xxx/FILE"

  And now it does:

      % go run ./cmd/fsnotify watch /tmp/xxx &
      03:23:47.8911   1 CHMOD         "/tmp/xxx/FILE"
      ^C

      % fsnotify watch /tmp/xxx/LINK &
      % touch /tmp/xxx/LINK
      03:27:38.5227   1 CHMOD         "/tmp/xxx/FILE"
@arp242 arp242 merged commit c35de00 into main Jul 12, 2023
17 checks passed
@arp242 arp242 deleted the kq-symlink branch July 12, 2023 18:11
@shogo82148 shogo82148 mentioned this pull request Mar 6, 2024
25 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spurious symlink CREATE events on macOS when watching a directory
1 participant