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?

link|improve this question
So, which program is it? – slhck Oct 20 '11 at 22:01
It is a program you can edit? – Luke Oct 20 '11 at 22:04
Not a program I can edit. – Nick Oct 20 '11 at 22:45
Is it a program you could wrap with a shell script? – ott-- Oct 20 '11 at 22:51
feedback

2 Answers

Make the underlying mount point read-only.

When the additional filesystem is mounted, it's permissions take precedence IIRC.

link|improve this answer
feedback

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:

 if mount | grep /that/mountpoint
  then
    ...

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

 mount /that/mountpoint
 touch /that/mountpoint/.is_mounted

 if test -e /that/mountpoint/.is_mounted
  then
    ...

The first solution is more reliable if suitable for your system whereas the second one is more portable.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.