バイナリ“テール”ファイル
-
04-07-2019 - |
質問
このサイトのほとんどの人はテールに精通していると思いますが、そうでない場合は「フォロー」を提供しますテキストがファイルの末尾に追加されると、これらの文字が端末にダンプされます。
私が探しているのは(そして必要に応じて自分で書くために)バイナリファイルで動作するテールのバージョンです。基本的に、別のネットワークリンクからダウンしたファイルを細流化したいワイヤレスリンクがあります。テールソースコードを見直すと、書き直すのはそれほど難しくありませんが、私はむしろ車輪を再発明したくないです!これは厳密には「テール」ではありません。ファイル全体をコピーしたいのですが、新しいバイトが追加されると監視し、それらをストリーミングします。
アイデア?
解決
bintail アプリケーションもあり、前述のスクリプトよりも堅牢であるように見えます。
bintail パッケージには、単一のアプリケーション bintail が含まれています。プログラムはディスクから通常のファイルを読み取り、テキストファイルに対して tail (1)が行うのと同様に、出力をバイト単位で変換せずに標準出力にパイプします。これは「テーリング」に便利です。 WAVファイルなどのバイナリファイルは、リアルタイムで書き込まれます。このアプリは開発中ですが、すでに私のために設計されたものを実行しています。
他のヒント
hexdumpにパイプする:
tail -f somefile | hexdump -C
Windows用のこの急いでコーディングされたPythonスクリプトは、役に立つかもしれません:
# bintail.py -- reads a binary file, writes initial contents to stdout,
# and writes new data to stdout as it is appended to the file.
import time
import sys
import os
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Time to sleep between file polling (seconds)
sleep_int = 1
def main():
# File is the first argument given to the script (bintail.py file)
binfile = sys.argv[1]
# Get the initial size of file
fsize = os.stat(binfile).st_size
# Read entire binary file
h_file = open(binfile, 'rb')
h_bytes = h_file.read(128)
while h_bytes:
sys.stdout.write(h_bytes)
h_bytes = h_file.read(128)
h_file.close()
# Loop forever, checking for new content and writing new content to stdout
while 1:
current_fsize = os.stat(binfile).st_size
if current_fsize > fsize:
h_file = open(binfile, 'rb')
h_file.seek(fsize)
h_bytes = h_file.read(128)
while h_bytes:
sys.stdout.write(h_bytes)
h_bytes = h_file.read(128)
h_file.close()
fsize = current_fsize
time.sleep(sleep_int)
if __name__ == '__main__':
if len(sys.argv) == 2:
main()
else:
sys.stdout.write("No file specified.")
somefileを少なく
次に shift F
厳密には、これを行うプログラムを作成する必要があります。 tail
はバイナリファイルで動作するように指定されていないためです。新しい「トリクル」を受け取りたい場合は、おそらく回避したいバッファリングの問題もあります。データをできるだけ早く。
Linux coreutils tail(1)は、バイナリファイルで正常に動作します。ほとんどのアプリケーションでは、出力がデータ構造の途中のランダムな場所で開始されないように、行方向を避ける必要があります。ファイルの先頭から単純に開始することでそれを行うことができます。これはまさにあなたが求めていたものです:
tail -c +1 -f somefile
正常に動作します。
これは末尾ではなく、ファイルを徐々にコピーしています。 rsyncを見てください。
ライブストリームでも機能するため、これを使用します。
cat ./some_file_or_dev | hexdump -C
キープレス(およびリリース)をダンプするサンプル:
[user@localhost input]$ sudo cat /dev/input/event2 | hexdump -C
00000000 81 32 b1 5a 00 00 00 00 e2 13 02 00 00 00 00 00 |.2.Z............|
00000010 04 00 04 00 36 00 00 00 81 32 b1 5a 00 00 00 00 |....6....2.Z....|
00000020 e2 13 02 00 00 00 00 00 01 00 36 00 01 00 00 00 |..........6.....|
00000030 81 32 b1 5a 00 00 00 00 e2 13 02 00 00 00 00 00 |.2.Z............|
00000040 00 00 00 00 00 00 00 00 81 32 b1 5a 00 00 00 00 |.........2.Z....|
00000050 a3 af 02 00 00 00 00 00 04 00 04 00 36 00 00 00 |............6...|
00000060 81 32 b1 5a 00 00 00 00 a3 af 02 00 00 00 00 00 |.2.Z............|
^C