I need to list a directory recursively but I want to skip subdirs that were already mounted. There are 2 cases:
a) a filesystem was mounted twice, like in this example:
- "/dev/sda2" was mounted on "/mnt/mnt_point1"
- "/dev/sda2" was mounted on "/mnt/mnt_point2" I want to list "/mnt" but descend only in "/mnt/mnt_point1"
b) part of the file hierarchy was remounted somewhere else, with "mount --bind":
- "mount --bind /home/user/tmp/test /home/user/tmp/mounted_test"
I want to list "/home/user/tmp" but descend only in "test"
"statfs" and "statvfs" don't offer any information to discern if a dir was mounted twice.
One solution would be to read "/etc/mtab" (as "find" command does it) and perform some checks, but I think that this is pretty expensive (one has to read /etc/mtab every time one encounters a dir; if this file is read only when program starts, a mount could occur in between reads, so that the program will be inaccurate).
Another solution would be to filter kernel events (via libudev or Netlink) and do this reading of /etc/mtab only when a MOUNT event was issued.
Do you have any other suggestions? Thanks in advance.
First of all reading mtab
is not as expensive as you might think, if it doesn't change, linux will probably keep it in memory if you access it often.
But for your purpose, mtab
probably will not contain the information that you are looking for. Parse /proc/mounts
instead.
精彩评论