将数据添加到文件 [英] Prepending Data to a File

查看:149
本文介绍了将数据添加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何操作系统中,我都没有办法知道程序如何有效地将数据前置到文件中。
然而,这似乎并不困难 - 文件系统可以添加另一个扩展到文件描述符,如果需要的话。

所以问题是,为什么不要操作系统实现这个(而不是微不足道的)操作?我不认为这很容易,因为你建议。确实,文件系统可以分配一个新的块,将前置数据存储在其中,将文件指针改为指向该块,然后链接该块中的其余文件。就像添加一个节点到链表的前面,对吗?

但是,当前缀数据不能填充指定的时候块。我不认为许多文件系统会有链接部分块的机制,但即使这样做会导致巨大的低效率。你最终会得到一个由大部分空白块组成的文件,你必须读写整个文件才能对其进行碎片整理。不妨先做前期的读写操作。


There's no way in any operating system I'm aware of for a program to prepend data to a file efficiently. And yet, this doesn't seem difficult -- the file system can add another extent to a file descriptor if needed.

So the question is, why don't operating systems implement this (rather trivial) operation?

解决方案

I don't think it's as easy as you suggest. It's true that the file-system could allocate a new block, store the prepended data in it, change the file pointer to point to that block and then chain the rest of the file from that block. Just like adding a node to the front of a linked list, right?

But what happens when (as is probably the case) the prepended data doesn't fill the assigned block. I don't imagine that many filesystems would have a machanism for chaining partial blocks, but even if they do it would result in huge inefficiencies. You'd end up with a file consisting of mostly empty blocks, and you have to have to read and write the entire file to defragment it. Might as well just do the read-and-write operation up front when you're prepending in the first place.

这篇关于将数据添加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆