Given the name of a directory, how do I find the filesystem it is on in C?

For example, the output of the df

sample command

Filesystem    MB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4         512.00    322.96   37%     4842     7% /
/dev/hd2        4096.00    717.96   83%    68173    29% /usr
/dev/hd9var     1024.00    670.96   35%     6385     4% /var
/dev/hd3        5120.00      0.39  100%      158    10% /tmp

      

Now if I specify something like /tmp/dummy.txt

should I get /dev/hd3

or just hd3

.

EDIT : thanks torek for the answer. But research /proc

would get very tedious. Can anyone suggest me some system calls that can do the same internally?

+3


source to share


2 answers


df `pwd`

      

... Super simple, works and also tells you how much space there is ...

[stackuser@rhel62 ~]$ pwd
/home/stackuser
[stackuser@rhel62 ~]$ df `pwd`
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda7            250056240 196130640  41223408  83% /
[stackuser@rhel62 ~]$ cd isos
[stackuser@rhel62 isos]$ pwd
/home/stackuser/isos
[stackuser@rhel62 isos]$ df `pwd`
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda5            103216920  90417960  11750704  89% /mnt/sda5
[stackuser@rhel62 isos]$ df $(pwd)
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda5            103216920  90417960  11750704  89% /mnt/sda5

      



... which is the likely reason for asking for the mount point in the first place.

Please note that this backticks, as an alternative (modern) method, which provides more control over the slash and expansion df $(pwd)

. Symbolic links to the tested and moves right bash

, dash

, busybox

, zsh

. Note that tcsh

it won't $(...)

, so stick with the old backtick style in csh options.

For added fun, there are additional switches pwd

and df

.

+4


source


On Linux, use /proc/<pid>/mounts

to access the list of mount points for a given pid

or /proc/self/mounts

(with a literal word self

) to refer to yourself. ( cat

files /proc/self/mount*

to see how they look.)



Then, for each filesystem, you can make a call statfs()

and compare the f_fsid

field f_fsid

with the result of an earlier one statfs()

on the path in question. After matching, fsid's

you have found the corresponding mounted filesystem and can use other data from /proc/self/mounts

. (See, however, statfs(2)

for restrictions on using anything useful with help f_fsid

.)

+3


source







All Articles