On Thu, 23 Jul 2015 09:47:18 -0700
Ian Lance Taylor <
ia...@golang.org> wrote:
> > Is it possible to check whether one specific file is opened by
> > another process or not in pure go (as fuser) ?
> > On Linux, my goal is to monitor a specific directory with
> > go-fsnotify/fsnotify, and process new files into this directory but
> > only when the copy is done (avoid to process it during they are
> > created/written).
>
> My understanding is that fuser works by looking through the /proc file
> system. A Go program can do the same thing.
>
> Note that any such program will have race conditions. New processes
> can start up, and open files for writing, while you are doing your
> processing.
Jérôme, inotify on Linux supports the type of event meaning
"written then closed" [1], that is, if you monitor a directory for
events of this type on its files, these events will only fire when some
file has been opened for writing (or appending, which doesn't matter)
and then closed.
This is a good approximation of what you're after.
Sure it's inherently racy, but no more than scanning /proc (and is way
simpler).
If `go-fsnotify` has no knobs for this, you could supposedly hack it to
have them or resort to spawning the `inotifywait` program
(`inotify-tools` package in Debian) and reading its stdout.
1.
http://man7.org/linux/man-pages/man7/inotify.7.html
2.
http://linux.die.net/man/1/inotifywait