tags:

views:

26

answers:

1

Hello. 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.

+1  A: 

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.

Jens Gustedt
Actually, mtab contains also info about "mount --bind". Example:cat /etc/mtab/dev/sda1 / ext3 rw,noatime,user_xattr,commit=0 0 0/dev/sda2 /mnt/mnt_point1 ext3 /dev/sda2 /mnt/mnt_point2 ext3/home/florin/tmp/to_mount/balance_trees /home/florin/tmp/to_mount/and cat /proc/mounts /dev/root / ext3 /dev/root /home/florin/tmp/to_mount//dev/sda2 /mnt/mnt_point1 ext3 /dev/sda2 /mnt/mnt_point2 ext3Thanks for your suggestion.
baudolino
@baudolino: yes it `may` contain that info, but it mustn't. `mount` can be told to not write an entry into `mtab`. `/proc/mounts` tells you what is real, without cheating. There is a para in the `man` page that explains the difference.
Jens Gustedt
Thanks, I'll keep this in mind
baudolino