计算Go中的文件的硬链接 [英] Counting hard links to a file in Go

查看:227
本文介绍了计算Go中的文件的硬链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 FileInfo手册页,当<$ c

 类型FileInfo接口{
Name()字符串//文件的基本名称
Size()int64 //常规文件的字节长度;系统依赖于他人
Mode()FileMode //文件模式位
ModTime()time.Time //修改时间
IsDir()bool // Mode()的缩写IsDir )
Sys()接口{} //基础数据源(可返回nil)
}

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

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

解决方案

例如,在Linux上,

  package main 

import(
fmt
os
系统调用


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


According to the man page for FileInfo, the following information is available when stat()ing a file in Go:

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)
}

How can I retrieve the number of hard links to a specific file in Go?

UNIX (<sys/stat.h>) defines st_nlink ("reference count of hard links") as a return value from a stat() system call.

解决方案

For example, on 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)
}

Output:

1

这篇关于计算Go中的文件的硬链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆