Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.unix.questions Subject: Re: Which file system? Message-ID: <7741@jpl-devvax.JPL.NASA.GOV> Date: 11 Apr 90 20:17:08 GMT References: <1990Apr11.091700.1253@cs.eur.nl> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 41 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 () { 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