
Truncate method to allow shrinking files without having to completely rewrite them
Zirunis opened this issue ยท 1 comments
Since cc-tweaked now supports opening files in update mode (r+ and r+b) I have changed my adaptive compression algorithms to use it instead of simple write mode. Essentially I wanted to append content (like a logger for example) but compression and padding required to overwrite the last one (or more) bytes at every append, so append mode didn't suffice. Long story short I'm very happy with the update mode! However today I found out my implementation actually had a very obscure bug. I want to spare you the details (unless you're interested) since I started writing half an essay on it, but essentially my file "shrinks" and I need to overwrite the remainder of the file with padding to make sure I can still correctly interpret the file contents. That made me wonder whether it could be possible to shrink a file without having to open it in read mode, reading everything, closing it, opening it in write mode and pasting the shrinked contents again. I don't think so?
Therefore I'd like to suggest a feature to allow truncating files. I can think of three ways:
- Either have a separate method in fs which simply truncates a given file at a given byte length
- Or allow passing a flag when opening a file in an update mode which truncates the file at the current position when flushing/closing (I'm guessing w and w+ define the final file size in a similar way?)
- Or allow passing a flag when writing to a ReadWriteHandle specifically which truncates the file at the current position (so directly behind the newly written content)
This was just a thought I wanted to share, I have no expectations of any action since this is extremely niche.