Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(store): extract (*Store).saveChunk method #15144

Merged
merged 2 commits into from
Feb 27, 2023

Conversation

mark-rushakoff
Copy link
Member

Description

There was a valid, but ignored, linter warning about a deferred close in a loop. Extract a method so that the defer can exist in a more narrow function scope, instead of each defer accumulating as the channel is iterated.

While here, only call os.MkdirAll once at the start, instead of repeatedly with the same arguments on every chunk encountered. Also distinguish the wrapped error message when closing the chunk file vs. the incoming chunk body.

Author Checklist

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

There was a valid, but ignored, linter warning about a deferred close in
a loop. Extract a method so that the defer can exist in a more narrow
function scope, instead of each defer accumulating as the channel is
iterated.

While here, only call os.MkdirAll once at the start, instead of
repeatedly with the same arguments on every chunk encountered. Also
distinguish the wrapped error message when closing the chunk file vs.
the incoming chunk body.
@mark-rushakoff mark-rushakoff requested a review from a team as a code owner February 23, 2023 21:42
defer chunkBody.Close()

path := s.pathChunk(snapshot.Height, snapshot.Format, index)
chunkFile, err := os.Create(path)

Check failure

Code scanning / gosec

Potential file inclusion via variable

Potential file inclusion via variable
// the whole operation will fail anyway.
if !dirCreated {
dir := s.pathSnapshot(height, format)
if err := os.MkdirAll(dir, 0o755); err != nil {

Check failure

Code scanning / gosec

Expect directory permissions to be 0750 or less

Expect directory permissions to be 0750 or less
// The hash of the chunk is appended to the snapshot's metadata,
// and the overall snapshot hash is updated with the chunk content too.
func (s *Store) saveChunk(chunkBody io.ReadCloser, index uint32, snapshot *types.Snapshot, chunkHasher, snapshotHasher hash.Hash) error {
defer chunkBody.Close()

Check failure

Code scanning / gosec

Deferring unsafe method "Close" on type "io.WriteCloser"

Deferring unsafe method "Close" on type "io.ReadCloser"
if err != nil {
return errors.Wrapf(err, "failed to create snapshot chunk file %q", path)
}
defer chunkFile.Close()

Check failure

Code scanning / gosec

Deferring unsafe method "Close" on type "io.WriteCloser"

Deferring unsafe method "Close" on type "*os.File"
Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

Copy link
Contributor

@likhita-809 likhita-809 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@julienrbrt julienrbrt enabled auto-merge (squash) February 27, 2023 09:56
@julienrbrt julienrbrt merged commit caf9410 into main Feb 27, 2023
@julienrbrt julienrbrt deleted the mr/refactor-store-savechunk branch February 27, 2023 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants