I wonder whether linux provide a low-level support for such a use case: When a monitored file is to be deleted, the 开发者_JAVA技巧monitor application can be notified to do something before this file is deleted from the file system.
Before teh monitored file is deleted from the file system, I hope to get a chance to free its related resouces. And I can only locate the resources as long as the file is still on disk and no matter where it moves.
Many thanks! Amanda
There is no direct API for this, but there are a number of techniques you can use.
First, the inotify API can be used to be notified /after/ deletion. To get at the contents of the file, you could create a hard link to it in another directory - this way, when the file is 'deleted', it remains on disk (at a different path) until you're ready to finally delete it.
Alternately, you could interpose a filter using FUSE. This would let you intercept any filesystem operations you like. However, this comes at a performance hit, as all filesystem operations would be intercepted, not just deletions.
精彩评论