tarファイルには、いくつかのフォーマットがあるのだけど、何らかのコマンドで作成したtarファイルが
どのフォーマットで作られているか判断するやり方が見当たらないのでメモしとく。

なお、メジャーなフォーマットは以下らしい1

  • POSIX.1-1988 ustar format
  • GNU tar format
  • POSIX.1-2001 pax format

利用者から見たフォーマット間の大きな違いは、ustar形式のtarフォーマットの場合、tarでまとめる各ファイルのファイル名が256文字制限だったり、各ファイルのサイズ制限が8GBになってること。残りの2つのフォーマットは制限なし。その代わり、他と比べてustar形式は歴史がある形式なので、対応しているツールも多い。

ちなみに、普通のlinux上のtarプログラム(=GNU tar)がデフォルトで使うtarフォーマットは–helpを付ければわかる。
以下のように、最近のtarプログラムならgnuフォーマットのはず。

# tar --help
[...略]
*This* tar defaults to:
--format=gnu -f- -b20 --quoting-style=escape --rmt-command=/sbin/rmt


・tarファイルのフォーマットの見分け方

hexdumpコマンドでtarファイルを出力し、tarファイル内にアーカイブされた各ファイルのtarヘッダ(とpaxヘッダ)で見分けます。2

hexdump -C で出力された情報のなかで、
それぞれのフォーマットごとに特徴的なヘッダ部分は以下。

  • ustar format
    00000100  ...(省略)...  |.ustar.00root...|
    
  • GNU tar format
    00000100  ...(省略)...  |.ustar  .root...|
    
  • pax format
    00000000  ...(省略)...  |./PaxHeaders.108|
    

以下、各フォーマットの確認例です。

# echo "hello" > foo.txt
# tar --format=gnu -cvf test.gnu.tar foo.txt
# tar --format=ustar -cvf test.ustar.tar foo.txt
# tar --format=pax -cvf test.pax.tar foo.txt

# hexdump -C test.ustar.tar
00000000  66 6f 6f 2e 74 78 74 00  00 00 00 00 00 00 00 00  |foo.txt.........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000060  00 00 00 00 30 30 30 30  36 34 34 00 30 30 30 30  |....0000644.0000|
00000070  30 30 30 00 30 30 30 30  30 30 30 00 30 30 30 30  |000.0000000.0000|
00000080  30 30 30 30 30 30 36 00  31 31 34 30 35 31 37 34  |0000006.11405174|
00000090  33 30 34 00 30 31 32 33  37 33 00 20 30 00 00 00  |304.012373. 0...|
000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000100  00 75 73 74 61 72 00 30  30 72 6f 6f 74 00 00 00  |.ustar.00root...|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000120  00 00 00 00 00 00 00 00  00 72 6f 6f 74 00 00 00  |.........root...|
00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000140  00 00 00 00 00 00 00 00  00 30 30 30 30 30 30 30  |.........0000000|
00000150  00 30 30 30 30 30 30 30  00 00 00 00 00 00 00 00  |.0000000........|
00000160  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200  68 65 6c 6c 6f 0a 00 00  00 00 00 00 00 00 00 00  |hello...........|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002800

# hexdump -C test.gnu.tar
00000000  66 6f 6f 2e 74 78 74 00  00 00 00 00 00 00 00 00  |foo.txt.........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000060  00 00 00 00 30 30 30 30  36 34 34 00 30 30 30 30  |....0000644.0000|
00000070  30 30 30 00 30 30 30 30  30 30 30 00 30 30 30 30  |000.0000000.0000|
00000080  30 30 30 30 30 30 36 00  31 31 34 30 35 31 37 34  |0000006.11405174|
00000090  33 30 34 00 30 31 31 30  37 33 00 20 30 00 00 00  |304.011073. 0...|
000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000100  00 75 73 74 61 72 20 20  00 72 6f 6f 74 00 00 00  |.ustar  .root...|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000120  00 00 00 00 00 00 00 00  00 72 6f 6f 74 00 00 00  |.........root...|
00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200  68 65 6c 6c 6f 0a 00 00  00 00 00 00 00 00 00 00  |hello...........|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002800

# hexdump -C test.pax.tar
00000000  2e 2f 50 61 78 48 65 61  64 65 72 73 2e 31 30 38  |./PaxHeaders.108|
00000010  31 2f 66 6f 6f 2e 74 78  74 00 00 00 00 00 00 00  |1/foo.txt.......|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000060  00 00 00 00 30 30 30 30  36 34 34 00 30 30 30 30  |....0000644.0000|
00000070  30 30 30 00 30 30 30 30  30 30 30 00 30 30 30 30  |000.0000000.0000|
00000080  30 30 30 30 30 35 30 00  31 31 34 30 35 31 37 34  |0000050.11405174|
00000090  34 30 31 00 30 31 33 34  34 31 00 20 78 00 00 00  |401.013441. x...|
000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000100  00 75 73 74 61 72 00 30  30 00 00 00 00 00 00 00  |.ustar.00.......|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000140  00 00 00 00 00 00 00 00  00 30 30 30 30 30 30 30  |.........0000000|
00000150  00 30 30 30 30 30 30 30  00 00 00 00 00 00 00 00  |.0000000........|
00000160  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200  32 30 20 61 74 69 6d 65  3d 31 32 37 36 34 34 32  |20 atime=1276442|
00000210  38 37 30 0a 32 30 20 63  74 69 6d 65 3d 31 32 37  |870.20 ctime=127|
00000220  36 34 34 32 38 32 30 0a  00 00 00 00 00 00 00 00  |6442820.........|
00000230  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400  66 6f 6f 2e 74 78 74 00  00 00 00 00 00 00 00 00  |foo.txt.........|
00000410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000460  00 00 00 00 30 30 30 30  36 34 34 00 30 30 30 30  |....0000644.0000|
00000470  30 30 30 00 30 30 30 30  30 30 30 00 30 30 30 30  |000.0000000.0000|
00000480  30 30 30 30 30 30 36 00  31 31 34 30 35 31 37 34  |0000006.11405174|
00000490  33 30 34 00 30 31 32 33  37 33 00 20 30 00 00 00  |304.012373. 0...|
000004a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000500  00 75 73 74 61 72 00 30  30 72 6f 6f 74 00 00 00  |.ustar.00root...|
00000510  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000520  00 00 00 00 00 00 00 00  00 72 6f 6f 74 00 00 00  |.........root...|
00000530  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000540  00 00 00 00 00 00 00 00  00 30 30 30 30 30 30 30  |.........0000000|
00000550  00 30 30 30 30 30 30 30  00 00 00 00 00 00 00 00  |.0000000........|
00000560  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000600  68 65 6c 6c 6f 0a 00 00  00 00 00 00 00 00 00 00  |hello...........|
00000610  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002800
  1. Pythonのtarfileライブラリのマニュアル参照。 []
  2. ヘッダの概説はFreeBSDのtarマニュアルがわかりやすいです。もっと知りたい場合は各tar形式の仕様書を見た方が良いです。 []