syscall.Stat_t does have the Dev field, which is documented by POSIX:
https://linux.die.net/man/2/stat: "The st_dev field describes the device on
which this file resides. (The major(3) and minor(3) macros may be useful to
decompose the device ID in this field.)"

In Go we don't have the major and minor macros but they are basically just
shufflings of the bits. Unfortunately they are not platform independent,
even between versions of Linux. Luckily that doesn't matter when you're
searching for the device.

If you run os.Stat on each file in /dev, you will find for example "sda3",
which will have a value in syscall.Stat_t.Rdev that will match the Dev
value of any file/directory in a filesystem mounted on that device.

Note that there can possibly be more than one device file that has the same
Rdev value, so you may get a different answer than what "df" does, for
example.

If you have the tool "di <https://linux.die.net/man/1/di>", it can do all
this for you:

% di /boot/grub
Filesystem         Mount               Size     Used    Avail %Used  fs Type
/dev/sda1          /boot             235.3M    68.0M   155.2M   34%  ext2
% di -n -f S /boot/grub
/dev/sda1


On Wed, Jan 24, 2018 at 3:08 AM Ian Lance Taylor <i...@golang.org> wrote:

> On Tue, Jan 23, 2018 at 5:35 PM, Matrix Neo <lanxis2372...@gmail.com>
> wrote:
> > Lan,
> >     Thank you for  your reply. Yes, I'm using GNU/Linux. I lookup the
> > findmnt program your mentioned and find that doesn't meet the need.
> > For example, if /data is mounted on /dev/sda3 and the given dir is /data.
> > It's ok to use findmnt to get /dev/sda3. But if the given dir is
> > /data/test1,
> > the findmnt returns nothing.
>
> Sorry the suggestion didn't help, but in any case this is a GNU/Linux
> question, not a Go question as such.
>
> Ian
>
>
> > 在 2018年1月23日星期二 UTC+8下午10:39:58,Ian Lance Taylor写道:
> >>
> >> On Mon, Jan 22, 2018 at 10:11 PM, Matrix Neo <lanxis...@gmail.com>
> wrote:
> >> >
> >> >      I want to get the mount point or device name (like /dev/sda1) by
> >> > the
> >> > directory name. I try my best only to find syscall.Stat_t struct but
> >> > there
> >> > is no field to
> >> > point the information i need. So is there any way to implement this ?
> >>
> >> You neglected to say what kind of system you are running on.  If you
> >> are using GNU/Linux, then as far as I know this information is not
> >> available using `stat`.  You need to run the `findmnt` program or look
> >> in `/proc/self/mountinfo`.
> >>
> >> Ian
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "golang-nuts" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to golang-nuts+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to