2
$\begingroup$

When you edit information in a file, but you add in more information than was already present, how does the file system handle it without an overflow into the next section of the file?

Here's an example. Consider having statements 1-12 in a 512-byte block, and you had statements 13-24 in the next 512-byte block. [Image 1] These blocks are both part of the same file. If you edited the first block, so you add another few New Statements [Image 2] past statement 12, this would overflow into the next block (statements 13-24). How would this condition be handled? Through shifting all the statements ahead of the added statement forward, or putting the new edit in a completely separate new block?

(sorry if the pictures or wording are a bit improper; this is my first time)

Unedited Blocks

Edited Blocks

$\endgroup$
1
  • $\begingroup$ The answer might theoretically depend on the filesystem. In practice, for small files, I imagine you just have to write the entire file again from memory. Large databases are implemented more carefully. $\endgroup$ Apr 2, 2020 at 20:39

2 Answers 2

1
$\begingroup$

Filesystems typically don't handle internal edits to files as you describe them. When you edit a program file, a text editor will pull the whole file into memory, allow you to make modifications to a buffer and then write the whole thing back out to disk. Actual file I/O operations are usually quite limited by the operating system, e.g. overwrite existing file contents, truncate the file to a smaller size discarding what's beyond EOF, and appending to the file.

Text editors manage the business of allowing arbitrary insertion and deletion of lines and blocks of text. These programs use an assortment of data structures, from arrays of lines to gap buffers to more elaborate structures, depending on the editor. An old Blogspot article describes how a few popular text editors work internally; I commend you to it and to the source code of the many open-source text editors available if you wish to learn more.

$\endgroup$
0
$\begingroup$

In principle this might depend on the filesystem, but I'll give the practical answer for every filesystem I've seen:

Filesystems store bytes, not statements.

If you insert some bytes into the middle of a file, it's like overwriting (so some of the data gets shifted forward) -- it doesn't add a new block with just the inserted data, because that would require variable-size blocks. Filesystems typically use fixed-size blocks.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.