From f0e9c872ed407a4b4c0ed6f58ff519e0b07c8f4c Mon Sep 17 00:00:00 2001 From: Marsel Mavletkulov Date: Thu, 25 Jan 2024 13:52:09 -0500 Subject: [PATCH] Update LocalFileWriter.Write to return Close errors --- pkg/geoipupdate/database/local_file_writer.go | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/geoipupdate/database/local_file_writer.go b/pkg/geoipupdate/database/local_file_writer.go index 05f14cbd..f436bf57 100644 --- a/pkg/geoipupdate/database/local_file_writer.go +++ b/pkg/geoipupdate/database/local_file_writer.go @@ -46,7 +46,7 @@ func NewLocalFileWriter( } // Write writes the result struct returned by a Reader to a database file. -func (w *LocalFileWriter) Write(result *ReadResult) error { +func (w *LocalFileWriter) Write(result *ReadResult) (err error) { // exit early if we've got the latest database version. if strings.EqualFold(result.OldHash, result.NewHash) { if w.verbose { @@ -56,8 +56,11 @@ func (w *LocalFileWriter) Write(result *ReadResult) error { } defer func() { - if err := result.reader.Close(); err != nil { - log.Printf("closing reader for %s: %+v", result.EditionID, err) + if closeErr := result.reader.Close(); closeErr != nil { + err = errors.Join( + fmt.Errorf("closing reader for %s: %w", result.EditionID, closeErr), + err, + ) } }() @@ -69,34 +72,37 @@ func (w *LocalFileWriter) Write(result *ReadResult) error { return fmt.Errorf("setting up database writer for %s: %w", result.EditionID, err) } defer func() { - if err := fw.close(); err != nil { - log.Printf("closing file writer: %+v", err) + if closeErr := fw.close(); closeErr != nil { + err = errors.Join( + fmt.Errorf("closing file writer: %w", closeErr), + err, + ) } }() - if err := fw.write(result.reader); err != nil { + if err = fw.write(result.reader); err != nil { return fmt.Errorf("writing to the temp file for %s: %w", result.EditionID, err) } // make sure the hash of the temp file matches the expected hash. - if err := fw.validateHash(result.NewHash); err != nil { + if err = fw.validateHash(result.NewHash); err != nil { return fmt.Errorf("validating hash for %s: %w", result.EditionID, err) } // move the temoporary database file into it's final location and // sync the directory. - if err := fw.syncAndRename(databaseFilePath); err != nil { + if err = fw.syncAndRename(databaseFilePath); err != nil { return fmt.Errorf("renaming temp file: %w", err) } // sync database directory. - if err := syncDir(filepath.Dir(databaseFilePath)); err != nil { + if err = syncDir(filepath.Dir(databaseFilePath)); err != nil { return fmt.Errorf("syncing database directory: %w", err) } // check if we need to set the file's modified at time if w.preserveFileTime { - if err := setModifiedAtTime(databaseFilePath, result.ModifiedAt); err != nil { + if err = setModifiedAtTime(databaseFilePath, result.ModifiedAt); err != nil { return err } }