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

chanbackup: always close SCB file after reading #4456

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions chanbackup/backupfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ type MultiFile struct {
// fileName is the file name of the main back up file.
fileName string

// mainFile is an open handle to the main back up file.
mainFile *os.File

// tempFileName is the name of the file that we'll use to stage a new
// packed multi-chan backup, and the rename to the main back up file.
tempFileName string
Expand Down Expand Up @@ -132,32 +129,15 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
func (b *MultiFile) ExtractMulti(keyChain keychain.KeyRing) (*Multi, error) {
var err error

// If the backup file isn't already set, then we'll attempt to open it
// anew.
if b.mainFile == nil {
// We'll return an error if the main file isn't currently set.
if b.fileName == "" {
return nil, ErrNoBackupFileExists
}

// Otherwise, we'll open the file to prep for reading the
// contents.
b.mainFile, err = os.Open(b.fileName)
if err != nil {
return nil, err
}
}

// Before we start to read the file, we'll ensure that the next read
// call will start from the front of the file.
_, err = b.mainFile.Seek(0, 0)
if err != nil {
return nil, err
// We'll return an error if the main file isn't currently set.
if b.fileName == "" {
return nil, ErrNoBackupFileExists
}

// With our seek successful, we'll now attempt to read the contents of
// the entire file in one swoop.
multiBytes, err := ioutil.ReadAll(b.mainFile)
// Now that we've confirmed the target file is populated, we'll read
// all the contents of the file. This function ensures that file is
// always closed, even if we can't read the contents.
multiBytes, err := ioutil.ReadFile(b.fileName)
if err != nil {
return nil, err
}
Expand Down