2

当方ubuntu14.10を使っています.

自分の環境ではwhatisデータベースは/var/cache/man/index.dbに存在しています(これも正直確証はありません).
直接見てもバイナリのようになっていてよくわからない文字列が並んでいました.

データベースへのアクセスの方法を教えてくださると助かります.

1 件の回答 1

3

ごく大雑把に説明します。

まず、ファイルの素性を調べると、

$ file /var/cache/man/index.db
/var/cache/man/index.db: GNU dbm 1.x or ndbm database, little endian

dbm 形式のファイルであることが分かります。また、man mandb を参照しますと、以下の様に書かれています。

Name                Type          Async   Filename
──────────────────────────────────────────────────────────
Berkeley db         Binary tree   Yes     index.bt
GNU gdbm v >= 1.6   Hashed        Yes     index.db
GNU gdbm v <  1.6   Hashed        No      index.db
UNIX ndbm           Hashed        No      index.(dir|pag)

Hashed となっていますので、index.db の中身を表示してみましょう。以下は perl を使う例です。

#!/usr/bin/perl -w

use GDBM_File;

tie(%dbm, 'GDBM_File', "/var/cache/man/index.db", &GDBM_READER, 0444) || die;

while ( ($key, $val) = each(%dbm) ) {
  chop($key); chop($val);
  print "$key = $val\n";
}

untie(%dbm);

これを show_dbm_hash.pl として保存して実行します。

$ ./show_dbm_hash.pl
vgagl = -       7       7       1335881665      A       -       -       gz      a fast framebuffer-level graphics library based on svgalib
bf_copy = -     1       1       1391041525      A       -       -       gz      shell script to copy a bogofilter working directory
grub-mkstandalone = -   1       1       1400180564      A       -       -       gz      make a memdisk-based GRUB image
                               :
                               :

ls コマンドと URI::Escape(perl module) を例にして、内容の説明をします。

ls = -  1   1   1421207414  A   -   -   gz  list directory contents
uri::escape = URI::Escape   3pm 3   1335817207  A   -   -   gz  Percent...

$val に相当する部分(= の右側)はタブ区切りになっていて、最大で 9 個の文字列が並んでいます。それぞれのフィールド値の意味は以下の通りです(部分的に不明)。

0: レコード名(man のタイトル名)
   レコードキー($key)と同じ場合には - が入ります。
   uri::escape では URI::Escape となっています。
1: man ファイルの拡張子
   ls コマンドの場合、man ファイルは /usr/share/man/man1/ls.1.gz、
   URI::Escape では /usr/share/man/man3/URI::Escape.3pm.gz となっています。
2: man のセクション番号
3: man ファイルの mtime
   date --date '@1421207414' などとすると、日付が分かります。
4: 不明
5: 不明
6: 不明
7: man ファイルが圧縮されている場合、その拡張子
8: whatis コマンドの description 部分
2
  • なるほど,fileコマンドを使えば大体の情報がわかるのですか. 2015年2月1日 6:45
  • 2
    @chikyukotei はい、そうです。実は file コマンドも /usr/share/file/magic.mgc という DB ファイルを参照していたりします(dbmとは異なる独自の形式の様です)。
    – user4986
    2015年2月1日 6:52

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。