nsfw: Mkdir -p are not correctly reported on Linux

Description

When creating recursively directories with mkdir -p, the watcher only reports the first directory.

Steps to Reproduce

  1. Install the CLI from #59
  2. Run nsfw /tmp
  3. Execute mkdir -p /tmp/foo/bar/baz

Expected behavior:

The watcher says that directories foo, foo/bar and foo/bar/baz have been created:

Created: /tmp/foo
Created: /tmp/foo/bar
Created: /tmp/foo/bar/baz

Actual behavior:

The watcher only says that the directory foo has been created:

Created: /tmp/foo

Additional Information

As far as I know, inotify is not recursive, and it’s nsfw code that creates sub-watchers for directories inside the watched directory. When inotify sends the event for foo, the directories foo/bar and foo/bar/baz have already been created, so even if the watcher is able to find these directories to tell inotify to watch them, it fails to also send an event for them.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

Well, often, it is not the directories but the files that are interesting. And it can happen with files too. For example:

$ mkdir -p foo/bar
$ echo baz > foo/bar/baz
$ mv foo /path/to/watched/dir/

will report only an event for foo. In that case, you can scan that directory and see the new file, but having a reliable way to do that can be tricky. It’s a race between NSFW adding its inotify on sub-directories and the scan, and some files can be missed by a race condition.

The first example I can think of is if the added directory contains two sub-directories, one with a lot of things inside and the other is just empty. If NSFW starts with the big sub-directory and the user scan starts the empty directory, the inotify watch can be setup after the scan, and during the time, it a file is created in this empty directory, it will be missed.

It’s an edge case, but it looks like these edge cases happen more often that I’d like when I’m working with file system watchers.

So, in my humble opinion, it would be better if NSFW emits the events in this case. But I also understand that you may want to do that as it increases what NSFW does, and it has a cost to maintain this code. Maybe a middleground can be to add documentation for the edge cases like this.

We have been troubled by the same bug. How do you work around in the end?