rjeczalik / notify

File system event notification library on steroids.
MIT License
906 stars 128 forks source link

Event path don't get updated if the parent directory is renamed. #119

Open nohupped opened 7 years ago

nohupped commented 7 years ago

Apologies if I missed a way to get around this, and I am not sure if this is the expected behaviour either, but when there is a watcher created for a parent directory for eg: /tmp/testdirectory, and if I moved the parent directory itself as /tmp/moved, any events inside /tmp/moved will be shown as /tmp/directory/, which is the old path.

Code:

package main

import (
        "fmt"
        "github.com/rjeczalik/notify"
)

func main() {
        c := make(chan notify.EventInfo, 1)
        notify.Watch("/tmp/directory", c, notify.InCloseWrite, notify.Create, notify.InMovedFrom, notify.InMovedTo, notify.InCreate, notify.InDelete, notify.InCloseNowrite, notify.InModify)
        defer notify.Stop(c)
        for {
                switch ei := <-c; ei.Event() {
                default:
                        fmt.Println("Event is", ei.Path(), ei)
                }
        }
}

Output: Console:

mkdir /tmp/directory/newdir

Daemon output:

Event is /tmp/directory notify.InCloseNowrite: "/tmp/directory"
Event is /tmp/directory notify.InCloseNowrite: "/tmp/directory"
Event is /tmp/directory/newdir notify.InCreate: "/tmp/directory/newdir"
Event is /tmp/directory/newdir notify.Create: "/tmp/directory/newdir"

Console:

mv /tmp/directory /tmp/moved
mkdir /tmp/moved/anotherdir

Daemon output:

Event is /tmp/directory/anotherdir notify.InCreate: "/tmp/directory/anotherdir"
Event is /tmp/directory/anotherdir notify.Create: "/tmp/directory/anotherdir"
Event is /tmp/directory notify.InCloseNowrite: "/tmp/directory"
Event is /tmp/directory/anotherdir notify.InCloseNowrite: "/tmp/directory/anotherdir"

(Shown as notify.InCreate: "/tmp/directory/anotherdir instead of notify.InCreate: "/tmp/moved/anotherdir" ) Is there any way when the parent directory is moved, it can reflect in the event's path with the updated directory name of the parent directory?

ppknap commented 7 years ago

Hey @nohupped , this is a bug in notify logic. Since inotify sends only file names, we store full paths in memory. However, when directory is moved, these paths are not updated.

Probably, we need to catch IN_MOVE_SELF events and update in-memory paths afterwards.

Note, that moving watched directories is generally not cross-platform - it is not possible to implement this correctly on Windows.

madding commented 5 years ago

Hi @ppknap! I try to do this task, but it impossible. I add IN_MOVE_SELF to InotifyAddWatch and I caught it, but I can't get new name of folder. 1) IN_MOVE_SELF don't trigger IN_MOVE_TO. 2) We have only file description and when I try run os.Readlink for /proc/self/fd/XXX it returned only anon_inode:inotify

No way to fix it. Good solution here it drop PANIC, but it breaks current workflow.