如何检查2个目录是否在同一个分区上?
How to check if 2 directories are on the same partition?
此代码展示了一种检查两个目录是否位于 linux 和 python3 中的同一分区的方法。有人知道如何在 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
结构。这是一个如何从 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)
此代码展示了一种检查两个目录是否位于 linux 和 python3 中的同一分区的方法。有人知道如何在 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
结构。这是一个如何从 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)