Size of directory from os.Stat/Lstat

huangapple go评论74阅读模式
英文:

Size of directory from os.Stat/Lstat

问题

假设我对一个目录执行os.Stat()操作:

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

// 548
// Program exited.

[FileInfo].Size()的值到底代表什么?它不是文件大小,所以我猜可能是文件数量?或者是inode数量?我在任何地方都找不到明确的答案,所以也许有人可以给我解答一下。

英文:

Let's say I do an os.Stat() on a directory:

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

// 548
// Program exited.

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

What exactly is the [FileInfo].Size() value meant to represent? It's not the file size, so I'm guessing something like number of files? Inodes? I couldn't find a clear answer anywhere, so maybe someone can enlighten me?

答案1

得分: 2

FileInfo提到

//对于常规文件,长度以字节为单位;对于其他文件,取决于系统

所以它真的取决于执行环境。
例如,请参阅“ext4存储目录大小的位置”。

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

stat命令没有提供查询文件系统对象(目录或文件)以外的任何功能。

stat根本没有返回多个大小的方法,因此它只能返回目录本身的大小,而不是其内容的大小。
而且,当你有硬链接文件时,“包括内容的目录大小”变得不太清楚。

英文:

FileInfo mentions

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

So it really depends on the execution environment.
See for instance "Where does ext4 store directory sizes?"

In that example, a directory stat size returns 4096 bytes.
That's the actual size of the directory itself, not what it contains.

> 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.

huangapple
  • 本文由 发表于 2015年11月8日 19:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/33593335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定