Unfortunately I'm being forced to work with a piece of software that handles automatically mounting and unmounting a network volume very poorly; it has an annoying tendency to leave a directory where the mount point was, but can't cope properly with that being the case when mounting the volume again later (it's supposed to tidy up but often doesn't).
Naturally I'm on at the developers to get that fixed, but in the meantime I need to do something to tidy up the mount point(s) myself.
So essentially what I need to do is remove the directory, but only if it isn't currently a mount point (as I don't want to delete the volume's contents by accident).
Now, I can get the device ID of the directory and compare it to the device ID of root easily enough, but there's a possibility of a race-condition if I use such a comparison, i.e- if the volume is mounted between checking device IDs and calling
rm -r /mnt/point.
Are there any alternatives? I was intrigued by the possibility of using the
-xdev option, but I'm not sure how I would actually provide a point of comparison, as
find /mnt/point -xdev won't work as the target and its contents are the same device.
rmdir on the assumption that the leftover folder will always be empty seems unreliable, as on some systems a mount point may have a file inside; macOS for example leaves an
.autodiskmounted file inside. While I could create a list of such cases and handle them, it'd be nice (and hopefully useful to others) to have a more general purpose solution for future reference, if such a thing is possible.