2

In the Unix File system (UFS), the file is represented as an inode structure which has 15 pointers that reference the direct blocks or indirect blocks.

Taking the below images is an example.

Inode Structure

Each block represented as Data on the right hand side contains the actual file data. And the size of this data block usually is 4096 and is decided during the file system creation.

For a huge file of 40 MB it would occupy nearly 1K data blocks. Given this scenario, if we append data to this file I see it would only impact the last block or if there is no space in the last data block it will create new data block.

But if we add some data (some 200 bytes) at the start of file, would it have cascade effect on the below data blocks and results in moving (or pushing) last 200 bytes of its each data block to the next data block?

Similarly when we delete the first 200 bytes from the first data block, will it have cascade effect on the lower data blocks?

Or is there an efficient way that UFS or in general file systems employ to handle such scenarios, may be some buffer space is reserved for each data block?

Thanks in advance.

4

Most filesystems don't support inserting data at the beginning of a file, and Unix doesn't have an API for that. In most operating systems, the only ways to modify a file are to overwrite a segment (e.g. change aaaaaaaaaa to aaabbbaaaa), to append data at the end (e.g. change aaaaaaaaaa to aaaaaaaaaacccc), or to truncate the file (e.g. change aaaaaaaaaa to aaaaa).

If you want to add data at the beginning of a file, create a new file with the additional data, and copy the content of the old file after that.

This is true both for the original Unix and for most if not all modern ones (and more generally for most operating systems).

  • @Giles Thanks for the response. I am not sure I got it Most filesystems don't support inserting data at the beginning of a file. Because let us say we have a filesystem with blocksize of 4 bytes and the file contents are ABCDWXYZ. Since the block size is 4 bytes 1st block will have ABCD and second will have WXYZ. Now if I add 123 to the start of file. It effectively becomes 123ABCDWXYZ and we are able insert data at the beginning of file Isn't it?So now the first block contains 123A, 2nd one BCDW and 3rd one XYZ. I am new to Unix, sorry I it looks too basic a question. – Madhusudana Reddy Sunnapu Feb 23 '16 at 3:14
  • @MadhusudanaReddySunnapu You can't insert data at the beginning of the file. You can only create a new file with blocks 123A, BCDW and XYZ. – Gilles Feb 23 '16 at 10:54
  • so in the above example when we inserted 123 in the existing file, it has resulted in creating 4 new blocks against this file and discarded the earlier 3 blocks? Or did I misinterpret your comment? – Madhusudana Reddy Sunnapu Feb 23 '16 at 11:12
  • @MadhusudanaReddySunnapu No, it doesn't discard any block. You cannot insert data into an existing file. You have to create a new file, and then remove the old file. – Gilles Feb 23 '16 at 12:05
  • Frankly, that is the point that I am not able to understand. You said cannot insert data into an existing file. But then when I added 123 into an existing text file, say myfile.txt, I see (or at least I have been thinking) that I have inserted data into existing file myfile.txt. I doubt I am interpreting your comment wrong, but could you help me understand that? Also, can you point me to any link or material that would help me in this context. – Madhusudana Reddy Sunnapu Feb 23 '16 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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