Show HN: rewrite, a rust-powered in-place file rewrite utility
neosmart.net
neosmart.net
However, I was going to write this anyway as it satisfies an itch I've had for a while. I had an implementation of `rewrite` as a bash script previously, but was not satisfied with it because I'm using `rewrite` in critical parts of my workflow and just don't trust bash the same way I do rust (or C/C++ or any other language, for that matter).
If you're also new to there are a few new things I've learned about rust that show up in the commit history (though not as much as I would have liked). In particular, using .expect() and .to_string_lossy() to avoid using .unwrap() directly (except where user input isn't a possible cause of failure). I had some try! code in there that was replaced by the new ? operator (not to be confused with the same operator in other languages like C#, the ? in rust is more of an inline try..catch rather than a null test/coalescing operator) but that code didn't even make the first commit.
> You see, sort is too smart to read the entire file into memory, sort it, then spit it out on stdout, which is how it can manage sorting huge amounts of data without completely trashing your system memory.
This is not why the file is empty. sort doesn't need the original file for very long. Very likely it starts a merge sort by sorting smaller chunks of the input file to temporary files. As soon as all these initial-size chunks have been read, the input file is no longer needed.
The reason for the file being truncated is the semantics of I/O redirection:
anyprogram somefile > somefile
means that the file is truncated by the shell (because of the redirection) before "anyprogram" even has the chance to open that file.I do however routinely run into cases where
some chain | tee inputfile.txt > /dev/null
also results in lost data. I will try to find it.You can provoke it by
echo a > a
(sleep 1; cat a) | tee a
very likely "a" is empty now.