[comp.unix.questions] Which file system?

reino@cs.eur.nl (Reino de Boer) (04/11/90)

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.

Thanks, Reino
-- 
Reino R. A. de Boer     "We want to build the right product right, right?"
Erasmus University Rotterdam ( Informatica )
e-mail: reino@cs.eur.nl

jeff@quark.WV.TEK.COM (Jeff Beadles) (04/12/90)

reino@cs.eur.nl (Reino de Boer) writes:
>Is there a way (in C, perl, etc.) to check which filesystem a file 
>belongs to ?

Well, I can't say that it will work everywhere, but...

% df /usr/jeff/.cshrc

Filesystem    kbytes    used   avail capacity  Mounted on
/dev/ds00a    228422  156238   49341    76%    /

And...

% df /usr2/src/jeff/src

Filesystem    kbytes    used   avail capacity  Mounted on
/dev/ds06a    274074  220359   26307    89%    /usr2


If you really enjoy pain (:-)  You can use "stat" and look at st_dev.

	-Jeff

-- 
Jeff Beadles				jeff@quark.WV.TEK.COM 
Utek Engineering, Tektronix Inc.	+1 503 685 2568
			"Credo quia absurdum"

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (04/12/90)

In article <1990Apr11.091700.1253@cs.eur.nl> reino@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@jpl-devvax.jpl.nasa.gov

net@tub.UUCP (Oliver Laumann) (04/12/90)

In article <1990Apr11.091700.1253@cs.eur.nl> reino@cs.eur.nl (Reino de Boer) writes:
> Is there a way (in C, perl, etc.) to check which filesystem a file 
> belongs to ?

Under Berkeley-UNIX and SunOS:  df filename
Under System V and SunOS:       devnm filename

Regards,
--
Oliver Laumann     net@TUB.BITNET     net@tub.cs.tu-berlin.de     net@tub.UUCP