From 564aaa3c2e68949c620e6ecaa1bd089180d6970d Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 13 Aug 2024 01:12:12 +0200 Subject: Properly truncate the file --- cmd/mfmt/main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'cmd/mfmt') 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) } -- cgit v1.2.3