This code shows a way to check if two directories are on the same partition in linux with python3. Anybody knows how to do the same in Go?
import stat
import os
def same_partition(dir1: str, dir2: str) -> bool:
stat1 = os.statvfs(dir1)
stat2 = os.statvfs(dir2)
return stat1[stat.ST_DEV] == stat2[stat.ST_DEV]
os.Stat()
returns os.FileInfo
struct and that provides passthrough via Sys()
to the underlying data source - on UNIX systems (so the code is obviously not portable to e.g. Windows) it is outcome of stat
syscall which has device number information. Device number can be obtained from Dev
field of syscall.Stat_t
struct. Here is a quick example of how to get device number from FileInfo
:
// NOTE This is PoC for SO purposes so do error handling, etc.
stat1, _ := os.Stat("/drive1/a.txt")
stat2, _ := os.Stat("/drive2/b.txt")
// returns *syscall.Stat_t
fmt.Println(reflect.TypeOf(statA.Sys()))
fmt.Println(stat1.Sys().(*syscall.Stat_t).Dev)
fmt.Println(stat2.Sys().(*syscall.Stat_t).Dev)