From 5bd7b99f355f9e8a3b3cbfd654daab6dd9fa432f Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Tue, 26 Sep 2023 12:28:07 +0300 Subject: [PATCH] Improve fileutils error handling (#196) --- utils/fileutils.go | 102 +++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 63 deletions(-) diff --git a/utils/fileutils.go b/utils/fileutils.go index 921cb54a..7bc59b04 100644 --- a/utils/fileutils.go +++ b/utils/fileutils.go @@ -107,50 +107,44 @@ func MoveFile(sourcePath, destPath string) (err error) { var inputFile *os.File inputFile, err = os.Open(sourcePath) if err != nil { - return err + return } defer func() { if inputFileOpen { - e := inputFile.Close() - if err == nil { - err = e - } + err = errors.Join(err, inputFile.Close()) } }() inputFileInfo, err := inputFile.Stat() if err != nil { - return err + return } var outputFile *os.File outputFile, err = os.Create(destPath) if err != nil { - return err + return } defer func() { - e := outputFile.Close() - if err == nil { - err = e - } + err = errors.Join(err, outputFile.Close()) }() _, err = io.Copy(outputFile, inputFile) if err != nil { - return err + return } err = os.Chmod(destPath, inputFileInfo.Mode()) if err != nil { - return err + return } // The copy was successful, so now delete the original file err = inputFile.Close() if err != nil { - return err + return } inputFileOpen = false err = os.Remove(sourcePath) - return err + return } // Return the list of files and directories in the specified path @@ -232,12 +226,10 @@ func DownloadFile(downloadTo string, fromUrl string) (err error) { // Get the data resp, err := http.Get(fromUrl) if err != nil { - return err + return } defer func() { - if deferErr := resp.Body.Close(); err == nil { - err = deferErr - } + err = errors.Join(err, resp.Body.Close()) }() if resp.StatusCode != http.StatusOK { err = fmt.Errorf("download failed. status code: %s", resp.Status) @@ -250,9 +242,7 @@ func DownloadFile(downloadTo string, fromUrl string) (err error) { return } defer func() { - if deferErr := out.Close(); err == nil { - err = deferErr - } + err = errors.Join(err, out.Close()) }() // Write the body to file _, err = io.Copy(out, resp.Body) @@ -311,22 +301,19 @@ func removeDirContents(dirPath string) (err error) { return } defer func() { - e := d.Close() - if err == nil { - err = e - } + err = errors.Join(err, d.Close()) }() names, err := d.Readdirnames(-1) if err != nil { - return err + return } for _, name := range names { err = os.RemoveAll(filepath.Join(dirPath, name)) if err != nil { - return err + return } } - return nil + return } // Old runs/tests may leave junk at temp dir. @@ -535,17 +522,15 @@ func Unmarshal(filePath string, loadTarget interface{}) (err error) { return } defer func() { - closeErr := jsonFile.Close() - if err == nil { - err = closeErr - } + err = errors.Join(err, jsonFile.Close()) }() var byteValue []byte byteValue, err = io.ReadAll(jsonFile) if err != nil { return } - return json.Unmarshal(byteValue, &loadTarget) + err = json.Unmarshal(byteValue, &loadTarget) + return } // strip '\n' or read until EOF, return error if read error @@ -553,26 +538,25 @@ func Unmarshal(filePath string, loadTarget interface{}) (err error) { func ReadNLines(path string, total int) (lines []string, err error) { reader, err := os.Open(path) if err != nil { - return nil, err + return } defer func() { - deferErr := reader.Close() - if err == nil { - err = deferErr - } + err = errors.Join(err, reader.Close()) }() - r := bufio.NewReader(reader) + bufferedReader := bufio.NewReader(reader) for i := 0; i < total; i++ { - line, _, err := r.ReadLine() + var line []byte + line, _, err = bufferedReader.ReadLine() lines = append(lines, string(line)) if err == io.EOF { + err = nil break } if err != nil { - return nil, err + return } } - return lines, nil + return } type FileDetails struct { @@ -585,7 +569,7 @@ func GetFileDetails(filePath string, includeChecksums bool) (details *FileDetail if includeChecksums { details.Checksum, err = calcChecksumDetails(filePath) if err != nil { - return details, err + return } } else { details.Checksum = entities.Checksum{} @@ -593,40 +577,32 @@ func GetFileDetails(filePath string, includeChecksums bool) (details *FileDetail file, err := os.Open(filePath) defer func() { - e := file.Close() - if err == nil { - err = e - } + err = errors.Join(err, file.Close()) }() if err != nil { - return nil, err + return } fileInfo, err := file.Stat() if err != nil { - return nil, err + return } details.Size = fileInfo.Size() - return details, nil + return } func calcChecksumDetails(filePath string) (checksum entities.Checksum, err error) { file, err := os.Open(filePath) - defer func() { - e := file.Close() - if err == nil { - err = e - } - }() if err != nil { - return entities.Checksum{}, err + return } - return calcChecksumDetailsFromReader(file) -} + defer func() { + err = errors.Join(err, file.Close()) + }() -func calcChecksumDetailsFromReader(reader io.Reader) (entities.Checksum, error) { - checksumInfo, err := CalcChecksums(reader) + checksumInfo, err := CalcChecksums(file) if err != nil { return entities.Checksum{}, err } - return entities.Checksum{Md5: checksumInfo[MD5], Sha1: checksumInfo[SHA1], Sha256: checksumInfo[SHA256]}, nil + checksum = entities.Checksum{Md5: checksumInfo[MD5], Sha1: checksumInfo[SHA1], Sha256: checksumInfo[SHA256]} + return }