0

I need to combine directory watching with event "hijacking". Basically, I want my Python program to listen to a directory (and its subdirs) for when a user double-clicks (i.e. opens/acesses) the file. At that point, I want my prgm to stop the events propagation and obtain a reference to the file.

In short: for some arbitrary directory, I want all attempts at file access to come through my program first, at which point I can do what I want without interference from Windows. How can this be done?

Background: already delved into pywin32 and it seems that even the underlying windows os filesystem api doesn't have a generic file access notifier. while it claims to be there, testing reveals that for most programs calling "open" on a file, the file access timestamp is not updated unless that file is modified or saved. Really, I want something like inotify, but in all the windows ported versions i've found this "IN_OPEN" notification is stripped. I'm assuming this has to do with the underlying Windows api, but maybe I'm missing something...

Thanks,

EDIT: Oops, didn't realize this might sound spammy... The reason I need to access the file open event is so that a user can open a "stub" file which contains only metadata. This stub is then used to generate a temp file which is what ends up being passed to the appropriate application. On save, the file's contents are pushed to a decentralized, cloud storage backend. At no point does the user really have the ability to own a document on the machine. So, "hijacking" the file open is for (1) constructing a file with actual contents (2) opening that file with the correct application

  • I think thats something the OS developers intentionally left out ... that said Im sure there is _some_ way to do it... – Joran Beasley Dec 05 '13 at 01:10
  • I really can't imagine a single non-malicious use case in this scenario. Really this is just a denial of service attack and it has the same limitations as any other method of its kind. – Voo Dec 05 '13 at 01:13
  • Okay your edited scenario seems much more logical. But your approach seems really unnecessarily complicated and still pretty much undoable. But your original problem could be solved much easier: Why not provide a script that the user can just execute instead of just the metadata? Or have one application that allows to generate complete files from the metadata files? Or if you really don't want the user to do any extra work, you can watch the directory and whenever a file is added convert it right there - those things are all doable without any hacks! – Voo Dec 05 '13 at 01:32
  • Thanks for taking a second look @Voo and for providing some good alternatives. These are certainly more workable approaches, but the client insisted on something integrated with Windows. While dropbox can simply listen to folders and push/pull changes, the process of reconstructing files and making sure the OS never saves them to disk is much more difficult without serious system-level manipulation. Anyway, you've given me some other approaches. Thanks! – zackoverflow Dec 05 '13 at 19:34
  • @zackoverflow The never-saving to disk part is certainly harder to implement, the question is why though - it really doesn't offer any kind of security against even the most naive attackers. But yes in this case you'll need to execute the file (either make it a script itself or associate a program with the file ending as theodox proposes) and then use some IPC mechanism. Theoretically a software-driver could work too, but that's going to be fragile and complicated beyond measure. – Voo Dec 05 '13 at 22:24

2 Answers2

0

You can achieve something similar by implementing your own extension and associating it with an intermediate application (or in this case, a python script). This won't catch all file actions but will let you explicitly handle some by extension.

You can use the assoc command to register your extension with a bat file that runs your python:

https://superuser.com/questions/406985/programatically-associate-file-extensions-with-application-on-windows

Community
  • 1
  • 1
theodox
  • 12,028
  • 3
  • 23
  • 36
0

You should check out Dokan, a usermode filesystem for Windows, very similar to FUSE which exists for *NIX operating systems. Unfortunately, there doesn't seem to be an active Python wrapper.

Dokan FUSE "is a wrapper library that makes Dokan compatible with FUSE API." With that you might be able to use a Python binding like fusepy.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328