@@ -6,13 +6,14 @@ import (
66 "io/ioutil"
77 "os"
88 "path"
9+
10+ "github.com/pkg/errors"
911)
1012
1113// File is the base type of an object that reads from the input
1214// file and writes to a temporary file.
1315// When you call Close(), both files are closed,
14- // the input is moved to the temp directory, and the output
15- // replaces it.
16+ // and the output replaces the input.
1617// If you call Abort(), the output file is deleted and the
1718// input file is unaffected.
1819// The object returned implements io.ReadWriteCloser.
@@ -34,11 +35,6 @@ var _ Aborter = (*File)(nil)
3435
3536// New returns an object that reads from the input
3637// file and writes to a temporary file.
37- // When you call Close(), both files are closed,
38- // the input is moved to the temp directory, and the output
39- // replaces it.
40- // If you call Abort(), the output file is deleted and the
41- // input file is unaffected.
4238// The File object returned implements io.ReadWriteCloser.
4339func New (inputFile string ) (* File , error ) {
4440 input , err := os .Open (inputFile )
@@ -74,16 +70,24 @@ func (f *File) Write(b []byte) (int, error) {
7470// Because people might call it twice (once each for in/out)
7571// we want it to be harmless after the first time.
7672func (f * File ) Close () error {
73+ // close both files
7774 if f .input != nil {
78- _ = f .input .Close ()
75+ if err := f .input .Close (); err != nil {
76+ return errors .Wrap (err , "closing rewriter input" )
77+ }
7978 f .input = nil
8079 }
81- if f .output == nil {
82- _ = f .output .Close ()
80+ if f .output != nil {
81+ if err := f .output .Close (); err != nil {
82+ return errors .Wrap (err , "closing rewriter output" )
83+ }
8384 f .output = nil
8485 }
86+ // and now move the tempfile over the original
8587 if f .tempfile != "" {
86- os .Rename (f .tempfile , f .filename )
88+ if err := os .Rename (f .tempfile , f .filename ); err != nil {
89+ return errors .Wrap (err , "renaming rewriter files" )
90+ }
8791 f .tempfile = ""
8892 }
8993 return nil
0 commit comments