News for hackers. Stuff that matters.
Re:Take care editing bash scripts (2019) by account42 on Thursday May 07, 2020 @07:15AM (#23101721)
The traditional way to truncate an existing file is using O_TRUNC in the open call. From your link it looks like Plan 9 has that flag.

> In addition, there are [...] values that can be ORed with the omode: OTRUNC says to truncate the file to zero length before opening it

Re:Take care editing bash scripts (2019) by jolmg on Thursday May 07, 2020 @11:12AM (#23103883)
That's what I meant by clobbering. The thing is that you can't use that flag to say "shorten it by half" to keep the first half, but you can with truncate(2).

My point was that if you can't shorten it by an arbitrary amount to avoid the redundancy of writing the same beginning of the file, then there's really no advantage to it over writing a new file to replace the old one. In fact, I thought clobbering (i.e. O_TRUNC) was nearly equivalent, only with the additional risk of data loss if there's an error while writing.

Re:Take care editing bash scripts (2019) by account42 on Friday May 08, 2020 @07:17AM (#23113508)
Creating a new file also replaces all file metadata (creation time, permissions, ACLs, links, ...) while O_TRUNC only erases the contents.