当文件被其他人截断为零时,read() 或 write() 的文件偏移量如何变化
how file offset of read() or write() change when file is truncated to zero by other people
文件偏移量是自动改为0,还是保持不变。
如果文件偏移量保持不变,truncate()
后read()
或write()
时会发生什么。
当您寻求 post 文件末尾时,同样的事情 - 写入扩展它并且读取失败。
由于操作系统和文件系统是世界上最不一致的软件,没有任何答案可以让您尝试一下。
how file offset of read() or write() change when file is truncated
打开的文件描述符的文件偏移量保持不变[1].
what happen when read() or write() after truncate().
read()
:
- 如果偏移量在文件范围内,将读取有效数据。
- 如果偏移量在文件长度之后但在 truncate[1].
范围内,将读取等于 0 的字节
- 如果偏移量超过文件末尾,return 将是 0(即没有读取字节)[3].
write()
:
- 将数据写入指定偏移量的文件[4].
- 如果写入超过文件末尾,将使用填充零调整文件大小[2].
[1] 来自 posix truncate:
If the file previously was larger than length, the extra data is discarded. If the file was previously shorter than length, its size is increased, and the extended area appears as if it were zero-filled.
The truncate() function shall not modify the file offset for any open file descriptions associated with the file.
[2] 来自 posix lseek:
The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.
[3] 来自 posix read:
No data transfer shall occur past the current end-of-file. If the starting position is at or after the end-of-file, 0 shall be returned.
[4] 从 posix write:
After a write() to a regular file has successfully returned:
- Any successful read() from each byte position: in the file that was modified by that write shall return the data specified by the write() for that position until such byte positions are again modified.
文件偏移量是自动改为0,还是保持不变。
如果文件偏移量保持不变,truncate()
后read()
或write()
时会发生什么。
当您寻求 post 文件末尾时,同样的事情 - 写入扩展它并且读取失败。 由于操作系统和文件系统是世界上最不一致的软件,没有任何答案可以让您尝试一下。
how file offset of read() or write() change when file is truncated
打开的文件描述符的文件偏移量保持不变[1].
what happen when read() or write() after truncate().
read()
:
- 如果偏移量在文件范围内,将读取有效数据。
- 如果偏移量在文件长度之后但在 truncate[1]. 范围内,将读取等于 0 的字节
- 如果偏移量超过文件末尾,return 将是 0(即没有读取字节)[3].
write()
:
- 将数据写入指定偏移量的文件[4].
- 如果写入超过文件末尾,将使用填充零调整文件大小[2].
[1] 来自 posix truncate:
If the file previously was larger than length, the extra data is discarded. If the file was previously shorter than length, its size is increased, and the extended area appears as if it were zero-filled.
The truncate() function shall not modify the file offset for any open file descriptions associated with the file.
[2] 来自 posix lseek:
The lseek() function shall allow the file offset to be set beyond the end of the existing data in the file. If data is later written at this point, subsequent reads of data in the gap shall return bytes with the value 0 until data is actually written into the gap.
[3] 来自 posix read:
No data transfer shall occur past the current end-of-file. If the starting position is at or after the end-of-file, 0 shall be returned.
[4] 从 posix write:
After a write() to a regular file has successfully returned:
- Any successful read() from each byte position: in the file that was modified by that write shall return the data specified by the write() for that position until such byte positions are again modified.