加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix – 计算Go中文件的硬链接

发布时间:2020-12-15 21:54:55 所属栏目:安全 来源:网络整理
导读:根据 man page for FileInfo,在Go中使用stat()文件时可以获得以下信息: 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 mod
根据 man page for FileInfo,在Go中使用stat()文件时可以获得以下信息:

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()系统调用的返回值.

解决方法

例如,在Linux上,

package main

import (
    "fmt"
    "os"
    "syscall"
)

func main() {
    fi,err := os.Stat("filename")
    if err != nil {
        fmt.Println(err)
        return
    }
    nlink := uint64(0)
    if sys := fi.Sys(); sys != nil {
        if stat,ok := sys.(*syscall.Stat_t); ok {
            nlink = uint64(stat.Nlink)
        }
    }
    fmt.Println(nlink)
}

输出:

1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读