cpioで作成したアーカイブは、どのようなフォーマットになってるのでしょうか。
適当なファイルをアーカイブ化して、できたアーカイブをバイナリダンプで確認してみます。
takk@deb9:~/tmp$ ls takk@deb9:~/tmp$ echo -ne "\x1\x3\x3" > a.bin takk@deb9:~/tmp$ hd a.bin 00000000 01 03 03 |...| 00000003 takk@deb9:~/tmp$ echo a.bin | cpio -o > a.bin.cpio 1 block takk@deb9:~/tmp$ file !$ file a.bin.cpio a.bin.cpio: cpio archive takk@deb9:~/tmp$ hd !$ hd a.bin.cpio 00000000 c7 71 01 08 56 58 a4 81 e8 03 e8 03 01 00 00 00 |.q..VX..........| 00000010 f0 5a 44 3a 06 00 00 00 03 00 61 2e 62 69 6e 00 |.ZD:......a.bin.| 00000020 01 03 03 00 c7 71 00 00 00 00 00 00 00 00 00 00 |.....q..........| 00000030 01 00 00 00 00 00 00 00 0b 00 00 00 00 00 54 52 |..............TR| 00000040 41 49 4c 45 52 21 21 21 00 00 00 00 00 00 00 00 |AILER!!!........| 00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00000200 takk@deb9:~/tmp$
おそらくアーカイブの先頭部分はマジックナンバーなんでしょうけど、c7 71ってよくわからない値ですねえ。
バイナリダンプを眺めていても、分かりそうにないので、ソースで確認することにします。
取得はいつものようにapt-get source。cpioパッケージでよいです。
takk@deb9:~/src$ apt-get source cpio
取得すると自動展開されて以下のようなディレクトリができます。
takk@deb9:~/src$ cd cpio-2.11+dfsg/ takk@deb9:~/src/cpio-2.11+dfsg$ ls ABOUT-NLS INSTALL NEWS aclocal.m4 configure gnu po AUTHORS Make.rules README am configure.ac headers rmt COPYING Makefile.am THANKS build-aux debian lib src ChangeLog Makefile.in TODO config.h.in doc m4 tests takk@deb9:~/src/cpio-2.11+dfsg$ cd src takk@deb9:~/src/cpio-2.11+dfsg/src$
まあソースは、srcの下ですね。
takk@deb9:~/src/cpio-2.11+dfsg/src$ ls Makefile.am cpio.h dstring.h global.c safe-stat.h util.c Makefile.in cpiohdr.h extern.h idcache.c tar.c copyin.c defer.c fatal.c main.c tar.h copyout.c defer.h filemode.c makepath.c tarhdr.h copypass.c dstring.c filetypes.h mt.c userspec.c takk@deb9:~/src/cpio-2.11+dfsg/src$
cpioアーカイブのフォーマットが知りたいので、コピーアウトモードで使用するマジックナンバーで検索してみます。おそらくキーワードはmagic。
takk@deb9:~/src/cpio-2.11+dfsg/src$ grep magic copyout.c write_out_new_ascii_header (const char *magic_string, p = stpcpy (ascii_header, magic_string); to_ascii (p, file_hdr->c_magic, 6, LG_8); short_hdr.c_magic = 070707; file_hdr.c_magic = 070707; takk@deb9:~/src/cpio-2.11+dfsg/src$
マジックナンバーは070707らしいです。ただし、これは0xがついていなくて、0から始まるので、8進数です。もしかしてさきほどのバイナリの先頭の数字は8進数でこの数字なのかもしれません。070707は16進数だと、
takk@deb9:~$ printf "%x\n" 070707 71c7 takk@deb9:~$
やはり71c7はマジックナンバーでした。
では構造体を探します。c_magicというメンバ名を手掛かりにヘッダを検索。
takk@deb9:~/src/cpio-2.11+dfsg/src$ grep c_magic *.h cpio.h: c_magic 6 must be "070707" cpiohdr.h: unsigned short c_magic; cpiohdr.h: char c_magic[6]; cpiohdr.h: char c_magic[6]; /* "070701" for "new" portable format cpiohdr.h: unsigned short c_magic; takk@deb9:~/src/cpio-2.11+dfsg/src$
cpio.hとcpiohdr.hの二つがヒットしました。ただ、cpio.hの方は上を見るからに、コメント行のようです。
cpio.hがcpiohdr.hにインクルードされているのでしょうか。だとしたら、定数の集合ヘッダですね。
検索。
takk@deb9:~/src/cpio-2.11+dfsg/src$ grep 'cpio\.h' * Makefile.am: cpio.h\ Makefile.in: cpio.h\ cpio.h:#endif /* cpio.h */ cpiohdr.h:#include <cpio.h> takk@deb9:~/src/cpio-2.11+dfsg/src$
あたりです。
つまり、cpio.hに構造体はありません。
takk@deb9:~/src/cpio-2.11+dfsg/src$ grep struct cpio.h takk@deb9:~/src/cpio-2.11+dfsg/src$
ないです。
一応cpio.hを確認。
takk@deb9:~/src/cpio-2.11+dfsg/src$ cat -n cpio.h
1 /* Extended cpio format from POSIX.1.
2 Copyright (C) 1992, 2005, 2007, 2010 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301 USA. */
18
19 #ifndef _CPIO_H
20
21 #define _CPIO_H 1
22
23 /* A cpio archive consists of a sequence of files.
24 Each file has a 76 byte header,
25 a variable length, NUL terminated filename,
26 and variable length file data.
27 A header for a filename "TRAILER!!!" indicates the end of the archive. */
28
29 #define CPIO_TRAILER_NAME "TRAILER!!!"
30
31 /* All the fields in the header are ISO 646 (approximately ASCII) strings
32 of octal numbers, left padded, not NUL terminated.
33
34 Field Name Length in Bytes Notes
35 c_magic 6 must be "070707"
36 c_dev 6
37 c_ino 6
38 c_mode 6 see below for value
39 c_uid 6
40 c_gid 6
41 c_nlink 6
42 c_rdev 6 only valid for chr and blk special files
43 c_mtime 11
44 c_namesize 6 count includes terminating NUL in pathname
45 c_filesize 11 must be 0 for FIFOs and directories */
46
47 /* Values for c_mode, OR'd together: */
48
49 #define C_IRUSR 000400
50 #define C_IWUSR 000200
51 #define C_IXUSR 000100
52 #define C_IRGRP 000040
53 #define C_IWGRP 000020
54 #define C_IXGRP 000010
55 #define C_IROTH 000004
56 #define C_IWOTH 000002
57 #define C_IXOTH 000001
58
59 #define C_ISUID 004000
60 #define C_ISGID 002000
61 #define C_ISVTX 001000
62
63 #define C_ISBLK 060000
64 #define C_ISCHR 020000
65 #define C_ISDIR 040000
66 #define C_ISFIFO 010000
67 #define C_ISSOCK 0140000
68 #define C_ISLNK 0120000
69 #define C_ISCTG 0110000
70 #define C_ISREG 0100000
71
72 #endif /* cpio.h */
takk@deb9:~/src/cpio-2.11+dfsg/src$
単純な定数ヘッダでした。
ソースを読むのが楽しいのですが、読まずにgrepだけで当たりをつけるのも楽しいです。
推理小説を読んで、犯人を予想して、答が合ってた時の感覚に似てるかもしれません。


コメント