truncate 不支持创建大小大于 2^32-1 的文件

问题描述 投票:0回答:1

显然,我的

truncate
系统上的 gnu
x86_64
不支持创建大小 >= 8EiB(= 2^32-1 字节 = 9223372036854775807 字节)的文件。

/usr/bin/truncate: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=c45930b128536914bf8134c0f11745cbd115d34b, for GNU/Linux 3.2.0, stripped

truncate -s 9223372036854775807 file
truncate -s 8E file
truncate: Invalid number: ‘8E’: Value too large for defined data type

我假设文件大小达到 16EiB = 2^64 字节才有效,或至少 2^64 - 1 字节。

为什么我对截断实现的直觉有缺陷?

x86-64 filesize integer-overflow gnu-coreutils
1个回答
0
投票

gnu truncate 的源代码包含以下定义:

static bool do_ftruncate (int fd, char const *fname, off_t ssize, off_t rel_mode_t rel_mode)

使用

off_t
中的
#include <sys/types.h>
类型作为文件大小。

由于它是使用

gcc
/
glibc
构建的,且
_FILE_OFFSET_BITS
设置为 64,因此
off_t
指的是
off64_t
,根据 The,“能够寻址最大 2^63 字节的文件” GNU C 库参考手册,因为它是有符号整数:

[Data Type]
off_t
This is a signed integer type used to represent file sizes. In the GNU C Library, this type is no narrower than int.
If the source is compiled with _FILE_OFFSET_BITS == 64 this type is transparently replaced by off64_t.

[Data Type]
off64_t
This type is used similar to off_t. The difference is that even on 32 bit machines, where the off_t type would have 32 bits, off64_t has 64 bits and so is able to address files up to 2^63 bytes in length.
When compiling with _FILE_OFFSET_BITS == 64 this type is available under the name off_t.
© www.soinside.com 2019 - 2024. All rights reserved.