可以有效存储在 tar 存档 header 的大小字段中的最大无符号整数是多少

what is the largest unsigned integer that is valid to store in the size field of a tar archive header

在 POSIX tar 归档格式 here 的 GNU 规范中,header 结构定义为:

struct posix_header
{                              /* byte offset */
  char name[100];               /*   0 */
  char mode[8];                 /* 100 */
  char uid[8];                  /* 108 */
  char gid[8];                  /* 116 */
  char size[12];                /* 124 */
  char mtime[12];               /* 136 */
  char chksum[8];               /* 148 */
  char typeflag;                /* 156 */
  char linkname[100];           /* 157 */
  char magic[6];                /* 257 */
  char version[2];              /* 263 */
  char uname[32];               /* 265 */
  char gname[32];               /* 297 */
  char devmajor[8];             /* 329 */
  char devminor[8];             /* 337 */
  char prefix[155];             /* 345 */
                                /* 500 */
};

header的size字段定义为长度为12的char数组,字段的字节长度看起来是12字节(由字节偏移量注释推断)。这在理论上提供了 space 的 12 个字节(=96 位)来存储无符号整数。但是,我怀疑情况并非如此。

根据 standard documentation

The name, linkname, magic, uname, and gname are null-terminated character strings. All other fields are zero-filled octal numbers in ASCII. For historical reasons, a final NUL or space character should also be used.

因此,11 个字节为您提供 11 个八进制数字(0..777777777778,或 0..0x1FFFFFFFF 范围),您的程序需要将其转换为二进制表示形式您认为合适的方式 - 例如,像这样:

uint64_t size;
sscanf(header->size, "%" SCNo64 "", &size);

Demo.

header 中的每个字段都存储为空终止字符串。在文件大小的情况下,它存储为八进制字符串。

所以您总共有 11 个八进制字符(为空字节留出空间),这意味着文件大小为 33 位,或最多 8GB。