Which file system?

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Thu Apr 12 06:17:08 AEST 1990


In article <1990Apr11.091700.1253 at cs.eur.nl> reino at cs.eur.nl (Reino de Boer) writes:
: Is there a way (in C, perl, etc.) to check which filesystem a file 
: belongs to ?
: 
: I need the answer to the above question in relation to an automatic
: incremental backup scheme we're trying to build.

Depends on what you mean by "check which filesystem".  Do you mean, find
out the mountpoint of the filesystem?  The device on which it's mounted?
Or do you just want a unique handle for each filesystem?

In either C or Perl, you can find the device/inode of a file by using stat().
The device will be unique for each filesystem.  If you want to translate
that into a mount point or a device, then you have to parse /etc/fstab,
or run a program that does.  (Actually, for local devices you could also
look in /dev for a special file with the right major/minor numbers, but that
won't help you with NFS filesystems.)

If you're going to look up a lot of them in Perl, you'd probably just
scan /etc/fstab once and load the devices/mountpoints into an associative
array:

open(FSTAB, '/etc/fstab') || die "Can't open /etc/fstab: $!\n";
while (<FSTAB>) {
    next if /^#/;
    next if /^$/;
    ($device, $mount) = split;
    ($dev) = stat($mount)
    $device{$dev} = $device;
    $mount{$dev} = $mount;
}
close FSTAB;

then if, later, you say

    ($dev) = stat($somefile);

you can get the device from $device{$dev} or the mount point from $mount{$dev}.

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.questions mailing list