finger exercises: file registry

In which I try to use to keep track of files in a directory so that I can have an in-memory representation without having to re-scan the directory over and over again.

Gonna start off by just watching the current directory

Running on OS X I get weird results when I touch a file. Intuitively touch should be some kind of modification event if the file does not exist or a creation event if it does but I also get deletion events which I don’t understand. Even after further investigation I don’t get it. Googling seems to indicate I’m not the only one. OS X just has a shitty implementation. I’ll mitigate the nonsense with some extra checks

Some sample output

Looks like it is working as expected. I’m gonna factor it out into a generator function because I think this will come in handy later

Using above method to smooth over weird deletion behavior we now have a simple event dispatcher that doesn’t have to worry about weird OS X edge cases

Now I need some kind of accumulator that keeps track of the latest event along with some kind of metadata other than what the event gave us

I’m gonna move the dispatch logic inside associator and add some basic tracking logic

Some output

This gives me a pretty good sense of the directory watching API and I can now take actions and associate extra information with each file in some kind of accumulator. I’m not shooting for anything durable so crashes and failures are ok for the time being but anything production worthy would need to be more robust and be able to properly recover from weird failures and crashes. The simplest thing to do is probably scan the directory ahead of time during startup and use that as the recovery mechanism

This will work but is again not robust. It is possible while the directory is being scanned files are deleted and added while the event listener is still unattached so I will need to think more about this but this is good enough for now. I have basic template to improve in the future as it becomes necessary.