Skip to content

Commit

Permalink
defer file close
Browse files Browse the repository at this point in the history
  • Loading branch information
kmulvey committed Jun 3, 2023
1 parent a28463a commit f1a1da8
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/app/imagedup/hash/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func NewCache(cacheFileName, promNamespace string, numFiles int) (*Cache, error)
if err != nil {
return nil, fmt.Errorf("HashCache error opening file: %s, err: %w", cacheFileName, err)
}

defer func() {
_ = f.Close() // hard to bubble this error in a defer
// if err != nil {
// return nil, fmt.Errorf("HashCache error closing file: %s, err: %w", cacheFileName, err)
// }
}()

if info, err := f.Stat(); err != nil {
return c, fmt.Errorf("HashCache error stating file: %s, err: %w", cacheFileName, err)
} else if info.Size() == 0 {
Expand All @@ -72,11 +80,6 @@ func NewCache(cacheFileName, promNamespace string, numFiles int) (*Cache, error)
}
}

err = f.Close()
if err != nil {
return nil, fmt.Errorf("HashCache error closing file: %s, err: %w", cacheFileName, err)
}

return c, nil
}

Expand Down

0 comments on commit f1a1da8

Please sign in to comment.