go - 来自 os.Stat/Lstat 的目录大小

标签 go operating-system

假设我在目录上执行 os.Stat():

func main() {
    fi, _ := os.Stat("/tmp")
    println(fi.Size())
}

// 548
// Program exited.

https://play.golang.org/p/NIzGMHRYfi

[FileInfo].Size() 值究竟代表什么?这不是文件大小,所以我猜是文件数量之类的东西? inode ?我在任何地方都找不到明确的答案,所以也许有人可以启发我?

最佳答案

FileInfo 提及

// length in bytes for regular files; system-dependent for others

所以这真的取决于执行环境。
例如参见“Where does ext4 store directory sizes?

在该示例中,目录统计大小返回 4096 字节。
这是目录本身的实际大小,而不是它包含的内容。

The stat command provides no facility for querying anything other then the size of a filesystem object (directory or file).

stat simply doesn't have a way to return multiple sizes—so it can only return the size of the directory itself, not of its contents.
And also "directory size including contents" becomes less clear when you have hardlinked files.

关于go - 来自 os.Stat/Lstat 的目录大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593335/

相关文章:

go - 0777权限写入文件失败

go - 设置 Go 环境以创建自定义 Terraform 提供程序

golang中的json解码用于复杂的json数据

go - 从结构内部的指向 float32 的指针获取值?

memory - 字的大小和寻址

go - 这个 Golang 结构中的第二对大括号是什么?

process - 在单个 CPU 上运行多个进程

windows - 如何检查哪个操作系统?

linux - 软件中断例程和用户态函数的区别

c - error bad execution(操作系统类(class))需要解释