Skip to content

Commit

Permalink
simplify atomic write logic (Debian#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsquire authored and stapelberg committed Jul 18, 2019
1 parent e36cb8d commit 73b1bc5
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions internal/write/atomically.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,24 @@ func Atomically(dest string, compress bool, write func(w io.Writer) error) (err

bufw := bufio.NewWriter(f)

w := io.Writer(bufw)
var gzipw *gzip.Writer
if compress {
// NOTE(stapelberg): gzip’s decompression phase takes the same
// time, regardless of compression level. Hence, we invest the
// maximum CPU time once to achieve the best compression.
gzipw, err = gzip.NewWriterLevel(bufw, gzip.BestCompression)
gzipw, err := gzip.NewWriterLevel(bufw, gzip.BestCompression)
if err != nil {
return err
}
defer gzipw.Close()
w = gzipw
}

if err := write(w); err != nil {
return err
}

if compress {
if err := write(gzipw); err != nil {
return err
}
if err := gzipw.Close(); err != nil {
return err
}
} else {
if err := write(bufw); err != nil {
return err
}
}

if err := bufw.Flush(); err != nil {
Expand Down

0 comments on commit 73b1bc5

Please sign in to comment.