diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-08-13 01:12:12 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-08-13 01:12:12 +0200 |
commit | 564aaa3c2e68949c620e6ecaa1bd089180d6970d (patch) | |
tree | a358fe2c52c37ccb0e566babe8c82ed1ac969644 /cmd | |
parent | 0683ded364790dbef17dd7b4ba5ad51408671793 (diff) |
Properly truncate the file
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/mfmt/main.go | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/cmd/mfmt/main.go b/cmd/mfmt/main.go index f2aa24f..57d9e8f 100644 --- a/cmd/mfmt/main.go +++ b/cmd/mfmt/main.go @@ -7,6 +7,7 @@ package main import ( "bufio" + "bytes" "fmt" "io" "os" @@ -36,7 +37,23 @@ func main() { continue } defer f.Close() - mfmt(arg, f, f) + + buf := bytes.NewBuffer(make([]byte, 0, 8192)) + mfmt(arg, f, buf) + + if _, err = f.Seek(0, io.SeekStart); err != nil { + warn(err) + continue + } + + if _, err = f.Write(buf.Bytes()); err != nil { + warn(err) + continue + } + + if err = f.Truncate(int64(buf.Len())); err != nil { + warn(err) + } } os.Exit(rv) } |