Skip to content

Commit

Permalink
Improve fileutils error handling (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 authored Sep 26, 2023
1 parent 8fc7f4a commit 5bd7b99
Showing 1 changed file with 39 additions and 63 deletions.
102 changes: 39 additions & 63 deletions utils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -535,44 +522,41 @@ 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
// readNLines reads up to 'total' number of lines separated by \n.
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 {
Expand All @@ -585,48 +569,40 @@ 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{}
}

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
}

0 comments on commit 5bd7b99

Please sign in to comment.