I am writing a function that recursively deletes all files and sub-directories in a directory tree, and this function will be used in multithread environment, so I would prefer opendir
/readdir_r
than nftw
(the code is for Linux/Mac OSX/Solaris, while nftw is not thread-safe on some platform).
Since the function is deleting files, security is a great concern. If there's a link pointing to a sensitive location (e.g., the /usr/lib
system directory), I don't want my function to try to delete the files under that directory. For symbolic/hard link files, lstat
then S_ISLNK
will do the job. However, if there's a mount point, S_ISDIR
just returns true on it.
Maybe setmntent
/getmntent
would help, but my experiment on Linux found it can't handle following situation:
- mount //192.168.0.1/share at ~/work/share (need root privilege)
- mv ~/work /tmp/work (does not need root privilege)
- now
getmntent
still reports ~/work/share as the mount point
What I want is like the FTW_MOUNT
flag to nftw
:
man nftw:
...
FTW_MOUNT
If set, stay within the same file system.
I am not sure if the st_dev
field from struct stat
is good for this, I don't know if the dev numbers are always different beyond a mount point.
with readdir_r
is there a way to figure out mounted points?
Thank you!