在 Go 中计算文件的硬链接

根据FileInfo的手册页,stat()在 Go 中 ing 文件时可以使用以下信息:


type FileInfo interface {

        Name() string       // base name of the file

        Size() int64        // length in bytes for regular files; system-dependent for others

        Mode() FileMode     // file mode bits

        ModTime() time.Time // modification time

        IsDir() bool        // abbreviation for Mode().IsDir()

        Sys() interface{}   // underlying data source (can return nil)

}

如何在 Go 中检索特定文件的硬链接数?


UNIX ( <sys/stat.h>) 将st_nlink(“硬链接的引用计数” )定义为stat()系统调用的返回值。


哆啦的时光机
浏览 197回答 1
1回答

慕丝7291255

例如,在 Linux 上,package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "syscall")func main() {&nbsp; &nbsp; fi, err := os.Stat("filename")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; nlink := uint64(0)&nbsp; &nbsp; if sys := fi.Sys(); sys != nil {&nbsp; &nbsp; &nbsp; &nbsp; if stat, ok := sys.(*syscall.Stat_t); ok {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nlink = uint64(stat.Nlink)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(nlink)}输出:1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go