truncate() 文件实际上会使用磁盘 space 吗?
will truncate() file actually uses disk space?
我正在写一个文件传输程序,我想知道目的地有足够的磁盘space 在开始传输之前。
我使用找到的方法 here 创建了一个“稀疏”(或非稀疏)文件:
func main() {
f, err := os.Create("foo.bar")
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := f.Truncate(1e7); err != nil {
log.Fatal(err)
}
}
我的问题是,“Truncate()”函数会创建一个实际使用那么多磁盘的真实文件 space,还是它只是在“FAT”table 中创建一条记录“声称”该文件使用了那么多 space?
换句话说,如果没有足够的磁盘,Truncate() 会失败吗space?
编辑
我删除了“go”标签,因为它与 golang 无关。并强调,我的目的是创建一个 ACTUALLY 使用 space 的特定大小的文件,这样如果没有足够的磁盘 space,文件创建会失败。
My question is, will the "Truncate()" function create a real file, which actually uses that much disk space, or it just create a record in the "FAT" table to "claim" that the file uses that much space?
这取决于底层文件系统。有些支持稀疏文件,有些则不支持。如果文件系统不支持稀疏文件,则需要实际分配 space。有关文件系统稀疏文件和非稀疏文件的一些信息,请参阅 here。
In another word, will Truncate() fail if there is not enough disk space?
如果没有足够的磁盘 space,截断将失败。但“不够”的确切含义取决于文件系统。如果它支持稀疏文件,它只需要 space 用于实际写入的数据和一些开销。
如果您想确保分配这种大小的文件可能需要的实际磁盘 space,那么您需要实际写入数据,而不仅仅是调用 truncate 或类似的.
我正在写一个文件传输程序,我想知道目的地有足够的磁盘space 在开始传输之前。
我使用找到的方法 here 创建了一个“稀疏”(或非稀疏)文件:
func main() {
f, err := os.Create("foo.bar")
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := f.Truncate(1e7); err != nil {
log.Fatal(err)
}
}
我的问题是,“Truncate()”函数会创建一个实际使用那么多磁盘的真实文件 space,还是它只是在“FAT”table 中创建一条记录“声称”该文件使用了那么多 space?
换句话说,如果没有足够的磁盘,Truncate() 会失败吗space?
编辑
我删除了“go”标签,因为它与 golang 无关。并强调,我的目的是创建一个 ACTUALLY 使用 space 的特定大小的文件,这样如果没有足够的磁盘 space,文件创建会失败。
My question is, will the "Truncate()" function create a real file, which actually uses that much disk space, or it just create a record in the "FAT" table to "claim" that the file uses that much space?
这取决于底层文件系统。有些支持稀疏文件,有些则不支持。如果文件系统不支持稀疏文件,则需要实际分配 space。有关文件系统稀疏文件和非稀疏文件的一些信息,请参阅 here。
In another word, will Truncate() fail if there is not enough disk space?
如果没有足够的磁盘 space,截断将失败。但“不够”的确切含义取决于文件系统。如果它支持稀疏文件,它只需要 space 用于实际写入的数据和一些开销。
如果您想确保分配这种大小的文件可能需要的实际磁盘 space,那么您需要实际写入数据,而不仅仅是调用 truncate 或类似的.