I have a program which writes some files to a directory. My problem is that if the directory isn't mounted for some reason, the program writes to the directory anyway, but the files end up in the system's filesystem, not on the normally mounted filesystem. I would like writes to the directory to fail if that directory isn't mounted. How do I do this in Linux?
Make the underlying mount point read-only.
When the additional filesystem is mounted, it's permissions take precedence IIRC.
I stumbled across this problen once also.
In my case the scripts accessing the directory in question as root, so the readonly option would have been no solution.
But there are two other possible workarounds:
1st: Check the with the mount command, if the directory is mounted:
2nd: Create manually a flag file once the FS is mounted and check that in your script. If the flag-file is not there, the FS may not be mounted
The first solution is more reliable if suitable for your system whereas the second one is more portable.