Prepending Data to a File

后端 未结 2 439
孤街浪徒
孤街浪徒 2020-12-11 18:21

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

2条回答
  •  死守一世寂寞
    2020-12-11 18:57

    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.

提交回复
热议问题