Ωme*_*ega 3 compression zip gzip tar bzip2
我不清楚,什么是正确的.tar文件格式,因为我在三个场景中遇到了正确的功能(见下文).
根据.tar我一直在使用的规范,magic字段(ustar)是以空字符结尾的字符串,version字段是八进制数字,没有尾随空值.
但是我查看了.tar我在服务器上找到的几个文件,我发现了不同的字段和字段的实现,magic并且version它们中的三个似乎都正常工作,可能是因为系统忽略了这些字段.
在以下示例>>中查看单词ustar和root之间的不同(3)字节
场景1(20 20 00):
000000F0 00 00 00 00 | 00 00 00 00 | 00 00 00 00 ............
000000FC 00 00 00 00 | 00 75 73 74 | 61 72 20 20 .....ustar
00000108 00 72 6F 6F | 74 00 00 00 | 00 00 00 00 .root.......
00000114 00 00 00 00 | 00 00 00 00 | 00 00 00 00 ............
Run Code Online (Sandbox Code Playgroud)
场景2(00 20 20):
000000F0 00 00 00 00 | 00 00 00 00 | 00 00 00 00 ............
000000FC 00 00 00 00 | 00 75 73 74 | 61 72 00 20 .....ustar.
00000108 20 72 6F 6F | 74 00 00 00 | 00 00 00 00 root.......
00000114 00 00 00 00 | 00 00 00 00 | 00 00 00 00 ............
Run Code Online (Sandbox Code Playgroud)
场景3(00 00 00):
000000F0 00 00 00 00 | 00 00 00 00 | 00 00 00 00 ............
000000FC 00 00 00 00 | 00 75 73 74 | 61 72 00 00 .....ustar..
00000108 00 72 6F 6F | 74 00 00 00 | 00 00 00 00 .root.......
00000114 00 00 00 00 | 00 00 00 00 | 00 00 00 00 ............
Run Code Online (Sandbox Code Playgroud)
哪一种格式正确?
小智 5
在我看来,没有一个例子是正确的,至少不是POSIX格式.
你可以在这里阅读:
/* tar Header Block, from POSIX 1003.1-1990. */
/* POSIX 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 */
};
#define TMAGIC "ustar" /* ustar and a null */
#define TMAGLEN 6
#define TVERSION "00" /* 00 and no null */
#define TVERSLEN 2
Run Code Online (Sandbox Code Playgroud)
您的第一个示例(Scenario 1)的格式似乎与旧的GNU标头格式匹配:
/* OLDGNU_MAGIC uses both magic and version fields, which are contiguous.
Found in an archive, it indicates an old GNU header format, which will be
hopefully become obsolescent. With OLDGNU_MAGIC, uname and gname are
valid, though the header is not truly POSIX conforming */
#define OLDGNU_MAGIC "ustar " /* 7 chars and a null */
Run Code Online (Sandbox Code Playgroud)
在第二个和第三个示例(Scenario 2和Scenario 3)中,该version字段设置为意外值(根据上述文档,正确的值应为00ASCII或0x30 0x30十六进制),因此该字段很可能被忽略.