If the program output is to a file, a file already exists at that location, and the program output is smaller than the existing file contents, then the output file is not shrunk, and the file contains garbage data at the end.
Options:
Open output file as truncate. Simple, but if the input file and output file are the same file, have to read the file before opening the output file
File::set_len. Would have to be done at the end of the program, and need to be a special case when closing a file (whereas all current special casing is when opening the file)(using a deconstructor, maybe?)
Two-stage file saving. Even more steps than set_len - especially if attempting to preserve file permissions, does rust have an atomic "create temporary file" method - but has the best resistance to data loss
If the program output is to a file, a file already exists at that location, and the program output is smaller than the existing file contents, then the output file is not shrunk, and the file contains garbage data at the end.
Options:
File::set_len
. Would have to be done at the end of the program, and need to be a special case when closing a file (whereas all current special casing is when opening the file)(using a deconstructor, maybe?)