0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自作debファイルで独自拡張子をfileで認識させる方法

Posted at

はじめに

先日リリースしたpdfveilを使って暗号化したファイル(hoge.veil)をfileコマンド認識させたいと思い、実装したのでその時につまったところなどを書きます。
pdfveilを知らない方はぜひ以下の記事を読んでみてください。

各ファイルに記述する内容

では早速fileコマンドを実装していきましょう。

pdfveil.magic

今回は暗号化されたveilファイルの先頭にVEILという文字列を入れています。
それをプロジェクトのルールに作成したpdfveil.magicで認識させ、表示する文言を設定します。

pdfveil.magic
0 string VEIL PDFVeil encrypted file

簡単に説明します。

文字 意味
0 ファイルの先頭を見ろ
string 文字列を指定
VEIL もしVEILを見つけたら実行せよ
PDFVeil encrypted file 説明文

debian/rules

debパッケージを作成する時に作るビルドのルール等を記述するrulesファイルにも書いていきます。

rules
#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS = -O2 -pipe -fstack-protector-strong -Wformat -Werror=format-security
export DEB_LDFLAGS = -Wl,-z,relro -Wl,-z,now

%:
	dh $@ --with python3 --buildsystem=pybuild

override_dh_install:
	dh_install
	cp ./pdfveil.magic /usr/share/file/magic
	file -C -m /usr/share/file/magic > /usr/share/file/magic.mgc

override_dh_installの部分が重要です。
先ほど作ったpdfveil.magicをfileコマンドが参照する/usr/share/file/magicにコピーします。
その後コンパイルして実行可能ファイルにします。

これで、一応実装は完了しているはずです。
一度fileコマンドで確認してみましょう。

$ file memo.veil 
Warning: using regular magic file `/usr/share/misc/magic'
file: file `/usr/share/misc/magic.mgc' is too small (No such file or directory)
memo.veil: PDFVeil encrypted file

一応指定した説明は表示されていますが、警告gでてしまっていて見づらくなっています。

この警告の原因はおそらくrulesで既存の/usr/share/file内のファイルでディレクトリと競合してしまっているためだと思います。

そこで、rulesを修正します。

rules
#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS = -O2 -pipe -fstack-protector-strong -Wformat -Werror=format-security
export DEB_LDFLAGS = -Wl,-z,relro -Wl,-z,now

%:
	dh $@ --with python3 --buildsystem=pybuild

override_dh_install:
	dh_install
	mkdir -p debian/pdfveil/usr/share/pdfveil
	cp ./pdfveil.magic debian/pdfveil/usr/share/pdfveil/pdfveil_magic
	file -C -m debian/pdfveil/usr/share/pdfveil/pdfveil_magic > debian/pdfveil/usr/share/pdfveil/pdfveil.mgc

/usr/share/pdfveilというディレクトリを新しく作りそこにファイルを置くようにしました。
しかし、これではfileコマンドを実行した際に参照されません。

debian/postint

そこで、postintを新しく作成して記述します。
このファイルはdebianパッケージをインストールした後に、実行されるスクリプトです。
今回は、aliasを使って先ほどのmgcファイルfileで認識させようと思います。

postint
#!/bin/sh
set -e

# pdfveil の magic ファイルがインストールされていることを確認
if [ -f /usr/share/file/magic.mgc ] && [ -f /usr/share/pdfveil/pdfveil_magic ]; then
    # ユーザーの ~/.bashrc に alias を設定する
    echo "alias file='file -m /usr/share/file/magic.mgc -m /usr/share/pdfveil/pdfveil_magic'" >> /home/$SUDO_USER/.bashrc
fi

# 新しい設定を反映させるためにシェルを再読み込み
if [ -f /home/$SUDO_USER/.bashrc ]; then
    . /home/$SUDO_USER/.bashrc
fi

これでfileコマンドを使った時にaliasで呼び出されるようになります。

$ chmod +x debian/postint

忘れずに実行権限を付与しておきましょう。

これで設定は完了です。
ビルドして、fileコマンドを使ってみましょう!

$ file memo.veil
memo.veil: PDFVeil encrypted file

ちゃんと指定した文字列が出力されました!!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?