寻求与截断以增加(预分配)文件大小
Seek vs Truncate to grow (prealocate) file size
我想为文件预分配 space。
您如何增加文件大小?
(*os.File).Seek(size,os.SEEK_SET)
or
(*os.File).Truncate(size)
两者有什么区别?
Seek function can change the current position in the open file. Under the hood, this function simply calls the lseek system call. This allows the file offset to be set beyond the end of the file, without ever changing the file size. If you try to set an offset larger than the file size, you will get the sparse file - 一个在偏移量 space 上有空洞(值为零的连续字节范围)的文件。稀疏文件通过只存储空洞的元数据而不是使用真正的磁盘块来有效地利用存储,因此文件的物理大小不会改变:
Truncate function changes the size of the file using the truncate系统调用。此函数会更改文件的大小,但与 Seek
不同的是,它不会更改 I/O 偏移量。文件的扩展部分读取为空字节 (‘\0’),这将改变物理大小。
因此,回答您的问题,正确的选择是使用 Truncate
函数。
Truncate
func Truncate(name string, size int64) error
Truncate changes the size of the named file. If the file is a symbolic link, it changes the size of the link's target. If there is an error, it will be of type *PathError
func (*File) Seek
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.
If f is a directory, the behavior of Seek varies by operating system; you can seek to the beginning of the directory on Unix-like operating systems, but not on Windows.
因此,使用 truncate 可以将文件大小更改为您设置的大小,使用 seek 可以 read 文件直到某个偏移量。
我想为文件预分配 space。 您如何增加文件大小?
(*os.File).Seek(size,os.SEEK_SET)
or
(*os.File).Truncate(size)
两者有什么区别?
Seek function can change the current position in the open file. Under the hood, this function simply calls the lseek system call. This allows the file offset to be set beyond the end of the file, without ever changing the file size. If you try to set an offset larger than the file size, you will get the sparse file - 一个在偏移量 space 上有空洞(值为零的连续字节范围)的文件。稀疏文件通过只存储空洞的元数据而不是使用真正的磁盘块来有效地利用存储,因此文件的物理大小不会改变:
Truncate function changes the size of the file using the truncate系统调用。此函数会更改文件的大小,但与 Seek
不同的是,它不会更改 I/O 偏移量。文件的扩展部分读取为空字节 (‘\0’),这将改变物理大小。
因此,回答您的问题,正确的选择是使用 Truncate
函数。
Truncate
func Truncate(name string, size int64) error
Truncate changes the size of the named file. If the file is a symbolic link, it changes the size of the link's target. If there is an error, it will be of type *PathError
func (*File) Seek
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.
If f is a directory, the behavior of Seek varies by operating system; you can seek to the beginning of the directory on Unix-like operating systems, but not on Windows.
因此,使用 truncate 可以将文件大小更改为您设置的大小,使用 seek 可以 read 文件直到某个偏移量。