显示HN:TapeHead – 一个用于文件流状态随机访问的CLI工具
Show HN: TapeHead – A CLI tool for stateful random access of file streams

原始链接: https://github.com/emamoah/tapehead

## TapeHead:文件流调试工具 TapeHead 是一款命令行实用工具,通过 REPL(读取-求值-打印循环)提供对文件流的随机访问。它允许用户**查找**、**读取**和**写入**文件中的特定位置,非常适合调试与文件相关的代码,例如设备驱动程序。 用户通过诸如 `seek`(带有绝对位置、相对移动或从末尾查找的选项)、`read`(指定要读取的字节数)和 `write`(带有文本或字节序列)等命令与 TapeHead 交互。提示符会显示当前文件位置 (`pos`) 以及读取/写入的字节数 (`in`, `out`)。 TapeHead 由 Emmanuel Amoah 创建,起因是他需要这样的工具来调试自定义的 "scull" 驱动程序,填补了现有实用工具的空白。它支持具有读写、只读或只写权限的文件。安装通过 `cargo install --git https://github.com/emamoah/tapehead.git` 进行。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Show HN: TapeHead – 一个用于文件流状态随机访问的 CLI 工具 (github.com/emamoah) 3 分,emamoah 发表于 2 小时前 | 隐藏 | 过去 | 收藏 | 讨论 我写这个工具是为了调试驱动程序,因为我找不到一个允许我打开文件、随机查找并读写的工具。 我也觉得它有一天可能会对某人有用。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

A command-line utility for random access of file streams.

File: "test.txt" (67 bytes) [RW]

[pos:0]> seek 10
[pos:10]> read . 5
ello!
[in:5, pos:15]> read 9 5
hello
[in:5, pos:14]> quit
Screen recording demo TapeHead demo

The tool runs a REPL session in which you can run commands to read, write, and seek to any position within the file stream. It's quite useful for tasks like debugging a driver, or anything that has to do with file streams.

cargo install --git https://github.com/emamoah/tapehead.git

I wrote a shabby version of this when I was trying to debug my scull driver, which controls a simple virtual character device. I was amazed I couldn't find a tool that allowed me to statefully seek, read and write to a file, so I just improvised some code. After that, I thought it a good idea to rewrite it properly and publish it, in case someone else one day might need it too.

The initial name I thought to give it was "seeker" (since seeking was the most important aspect) but there was already a crate with that name, so I came up with "TapeHead" which also characterises the tool's behaviour quite well.

TapeHead v0.1.0

Author: Emmanuel Amoah (https://emamoah.com/)

Enter "help" for more information.



File: "test.txt" (67 bytes) [RW]

[pos:0]>

After the prologue is a line with the following details about the opened file:

File: "test.txt" (67 bytes) [RW]
          ^          ^       ^
      filepath   filesize   permissions

The permissions can be one of these three, detected automatically when opening the file:

  • [RW] - Readable & Writable
  • [RO] - Read-only
  • [WO] - Write-only

The prompt contains a combination of the following segments:

  • pos:<number> - Current position of the file pointer, always shown. If the stream is not seekable (e.g., a Unix FIFO), it displays a * instead of a number.
  • in:<number> - Number of bytes read from the file after executing the previous command. Not shown if nothing was read.
  • out:<number> - Number of bytes written to the file after executing the previous command. Not shown if nothing was written.

The following are the supported commands in the REPL, also accessible through the help command.

The following syntaxes are allowed for commands with a seek argument.

  • .

    • Keep file pointer at its current position.
  • number (e.g. 9)

    • Move to the numberth position from the beginning of the file.
  • +number (e.g. +10)

    • Move forward number bytes from the current position. It is possible to seek beyond the end of a file.
  • -number (e.g. -2)

    • Move backwards number bytes from the current position. Seeking to a position before byte 0 is an error.
  • [number]< (e.g. 40< , <)

    • Move to the number'th byte from the end of the file. If number is omitted, move to the end of the file.
  • read . - Read the rest of the file from the current position.
  • write < hello - Seek to the end of the file and write the text "hello".
  • read 0 10 - Read the first 10 bytes of the file.
  • seek 5< - Seek to the 5th-to-last byte of the file.
  • write -5 hello - Move backwards 5 bytes and write "hello".
  • writeb 0 74 61 70 65 68 65 61 64 0a - Write "tapehead" followed by a newline at the beginning of the file.
联系我们 contact @ memedata.com