1

I am aware that fallocate --keep-size can be used to extend a file without touching its logical size. Is there a way to do the reverse, i.e. shrink a file's logical size without touching its blocks?

1 Answer 1

0

If you don't want to remove blocks but want to change the logical file size, then presumably you want to delete a hole at the end of the file.

truncate / ftruncate can do this, but it is not hole aware, so you would have to separately detect the hole at the end of the file and then use ftruncate to shrink the file.

Please note, ftruncate is only half of a solution, and while most modern unixes support detecting holes in files, there is not a portable way to do this, as they all do it slightly differently.

Also, make sure you understand what fallocate is doing. It fills in holes and allocates data blocks to them. So once you have run fallocate on a file, all of its logical size is represented by physical data blocks, and you can't change its size without releasing some of those datablocks.

6
  • Thanks for the info, but is there an official document stating this behavior? POSIX says for truncate, "If the file previously was larger than this size, the extra data is lost." It didn't say whether that data is physically lost or logically lost.
    – Tabokie
    Aug 4, 2021 at 4:50
  • You have to detect the hole and truncate at the start of the hole at the end of the file, assuming there is one. By definition, holes in files do not use any data blocks.
    – user10489
    Aug 4, 2021 at 5:12
  • Oh, but can I do the same with a normal chunk of file? To be precise, my use case is reusing a WAL file without reallocating blocks later.
    – Tabokie
    Aug 4, 2021 at 5:16
  • That isn't the question you asked, and it is probably not safe to mess with a WAL file at all. Sqlite will automatically remove the WAL file when it is done with it.
    – user10489
    Aug 4, 2021 at 5:20
  • If you are asking if you can remove a datablock from a file without removing data from a file, then your question is nonsense and should be closed.
    – user10489
    Aug 4, 2021 at 5:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .