Skip to content

Commit

Permalink
Merge pull request #145 from MaxMcAdam/issue-143
Browse files Browse the repository at this point in the history
Check errors on file close
  • Loading branch information
LiilyZhang authored Feb 17, 2023
2 parents f282c4a + 0572412 commit 696d994
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions core/dataURI/dataURI.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func AppendData(uri string, dataReader io.Reader, dataLength uint32, offset int6
if err != nil {
return isLastChunk, common.CreateError(err, fmt.Sprintf("Failed to open file %s to append data. Error: ", dataURI.Path))
}
defer file.Close()
defer closeFileLogError(file)
if _, err = file.Seek(offset, io.SeekStart); err != nil {
return isLastChunk, &common.IOError{Message: fmt.Sprintf("Failed to seek to the offset %d of a file. Error: %s", offset, err.Error())}
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func StoreData(uri string, dataReader io.Reader, dataLength uint32) (int64, comm
if err != nil {
return 0, common.CreateError(err, fmt.Sprintf("Failed to open file %s to write data. Error: ", dataURI.Path))
}
defer file.Close()
defer closeFileLogError(file)

if _, err = file.Seek(0, io.SeekStart); err != nil {
return 0, &common.IOError{Message: "Failed to seek to the start of a file. Error: " + err.Error()}
Expand Down Expand Up @@ -123,7 +123,7 @@ func StoreTempData(uri string, dataReader io.Reader, dataLength uint32) (int64,
if err != nil {
return 0, common.CreateError(err, fmt.Sprintf("Failed to open file %s to write data. Error: ", dataURI.Path))
}
defer file.Close()
defer closeFileLogError(file)

if _, err = file.Seek(0, io.SeekStart); err != nil {
return 0, &common.IOError{Message: "Failed to seek to the start of a file. Error: " + err.Error()}
Expand Down Expand Up @@ -204,7 +204,7 @@ func GetDataChunk(uri string, size int, offset int64) ([]byte, bool, int, common
}
return nil, true, 0, common.CreateError(err, fmt.Sprintf("Failed to open file %s to read data. Error: ", dataURI.Path))
}
defer file.Close()
defer closeFileLogError(file)

eof := false
result := make([]byte, size)
Expand Down Expand Up @@ -252,3 +252,15 @@ func DeleteStoredData(uri string, isTempData bool) common.SyncServiceError {
}
return nil
}

// closeFileLogError will close the given file and log any error encountered if logging is enabled
func closeFileLogError(f *os.File) {
if f == nil {
return
}
if err := f.Close(); err != nil {
if trace.IsLogging(logger.TRACE) {
trace.Trace("Error closing file: %v", err.Error())
}
}
}

0 comments on commit 696d994

Please sign in to comment.