文件文件系统 (2021)
The File Filesystem (2021)

原始链接: https://mgree.github.io/ffs/

Unix shell 在处理 JSON、YAML 和 TOML 等半结构化数据时功能有限。 为了解决这个限制,有一个名为 ffs(文件即文件系统)的工具。 它允许您将这些数据结构视为传统文件系统的一部分。 通过挂载 JSON、YAML 或 TOML 文件,用户可以使用他们喜欢的 shell 工具。 ffs 目前支持直接读取和编辑 JSON、YAML 和 TOML 文件。 本文演示了如何通过安装 JSON 文件并对其内容进行修改,同时保持原始文件完整来使用 ffs。 完成编辑后,用户可以卸载文件系统。 有关更多信息,请参阅 ffs 手册页或运行“man ffs”。 要安装 ffs,请查看该项目的网站或自己从源代码构建它。 Ffs 可能具有优势,因为它使用众所周知的 shell 工具,可处理各种格式,并且与其他解决方案相比,学习曲线较低。 但是,它可能不适用于操作系统不支持 Linux 或 Mac 的 FUSE、如果改用 Windows 或仅使用搜索功能而无需修改文件的某些场景。 最后,请记住 ffs 在 GPLv3 许可证下运行。

此人讨论了与 Nim 语言编码和使用 Apple 的 APFS 文件系统相关的各种项目。 他们提到了由于条件恶劣而共享某些项目所面临的挑战,并对 FUSE 等潜在概念表示兴奋,FUSE 是一种用户空间文件系统,只需生成请求的数据,而不需要事先创建所有文件。 他们提出了一些可能性,例如通过文件而不是传统方法公开程序的配置。 在讨论 APFS 限制时,他们讨论了 Apple 不愿发布类似 FUSE 的 API 的潜在原因,重点关注系统稳定性和确保正确的用例等方面。 他们反思了过去的缓存优化经验,并使用 Linux 而不是 MacOS 进行此类探索。 此外,他们还提出了改进建议,例如与替代归档工具集成以及考虑内存文件系统。 整个对话展示了该人对编程的热情、他们对系统优化的想法以及他们对使用不同操作系统的看法。
相关文章

原文

The Unix shell is a powerful tool, and the Unix ecosystem provides an incredible array of tools for working with strings. But the shell really only knows how to work with one data structure: the filesystem. Modern systems use all kinds of semi-structured data, like JSON or YAML. These semi-structured formats are essentially trees, and string processing is a bad match—editing JSON with sed is not a very good idea!

Demo of in-place editing, starting from an empty JSON object to building up a compound one; transcript is below

ffs—short for the file filesystem—lets you mount semi-structured data as a filesystem, letting you work with modern formats using your familiar shell tools.

Currently, ffs supports JSON, YAML, and TOML, with more to come.

You can read more about it in the ffs manpage.

Run ffs [file] to mount file.blah at the mountpoint file. The final, updated version of the file will be outputted on stdout.

$ cat object.json 
{ "name": "Michael Greenberg", "eyes": 2, "fingernails": 10, "human": true }
$ ffs -o object_edited.json object.json &
[1] 60182
$ tree object
object
├── eyes
├── fingernails
├── human
└── name

0 directories, 4 files
$ echo Mikey Indiana >object/name
$ echo 1 >object/nose
$ mkdir object/pockets
$ cd object/pockets/
$ echo keys >pants
$ echo pen >shirt
$ cd ..
$ cd ..
$ umount object
$ 
[1]+  Done                    ffs -o object_edited.json object.json
$ cat object_edited.json 
{"eyes":2,"fingernails":10,"human":true,"name":"Mikey Indiana","nose":1,"pockets":{"pants":"keys","shirt":"pen"}}

Notice a few things: the nose key of the resulting object has a number as its value, not a string; the pockets directory got turned into an object.

You can specify an explicit mountpoint by running ffs -m MOUNT file; you can specify an output file with -o OUTPUT. You can edit a file in place by running ffs -i file (as in the gif above)—when the volume is unmounted, the resulting output will be written back to file. Here’s the transcript:

~/ffs/demo $ echo '{}' >demo.json
~/ffs/demo $ ffs -i demo.json &
[1] 56827
~/ffs/demo $ cd demo
~/ffs/demo/demo $ echo 47 >favorite_number
~/ffs/demo/demo $ mkdir likes
~/ffs/demo/demo $ echo true >likes/dogs
~/ffs/demo/demo $ echo false >likes/cats
~/ffs/demo/demo $ touch mistakes
~/ffs/demo/demo $ echo Michael Greenberg >name
~/ffs/demo/demo $ echo https://mgree.github.io >website
~/ffs/demo/demo $ cd ..
~/ffs/demo $ umount demo
~/ffs/demo $ 
[1]+  Done                    ffs -i demo.json
~/ffs/demo $ cat demo.json 
{"favorite_number":47,"likes":{"cats":false,"dogs":true},"mistakes":null,"name":"Michael Greenberg","website":"https://mgree.github.io"}~/ffs/demo $ 
~/ffs/demo $

On Linux you need FUSE; on macOS, you need macFUSE. You can then download a single executable. These are the latest development builds:

See the release page for particular releases; the current version is 0.1.2. You can also build ffs from source.

Check out the paper “Files-as-Filesystems for POSIX Shell Data Processing” from PLOS 2021. The pre-recorded demo is above; the pre-recorded talk is below.

Tools like jq and gron are meant to help you work with JSON on the command line. They’re great tools!

Why might ffs be the right choice for you?

  • ffs supports multiple formats.

  • ffs lets you edit using familiar shell tools.

  • ffs doesn’t involve learning a new language.

Why might ffs not be the right choice for you?

  • You use Windows. (Sorry. 😥)

  • You can’t use FUSE.

  • You only need to search, not edit.

  • Your files are very large.

ffs is licensed under GPLv3.

联系我们 contact @ memedata.com