chanbackup: always close SCB file after reading

In this commit, we fix a bug introduced with the recent bug fix for SCB
state+fail combination. On windwos a rename operation will fail is the
fail one is attempting to rename is still open. Therefore we need to
close the file after we read the contents, to ensure the follow up
rename operations once the channel state changes will succeed. We do
this by using `ioutil.ReadFile`, which will always clsoe the file after
reading.

Fixes #4450.
This commit is contained in:
Olaoluwa Osuntokun 2020-07-08 17:37:53 -07:00
parent 8cb1276dbf
commit c58be5625d
No known key found for this signature in database
GPG Key ID: BC13F65E2DC84465

@ -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
@ -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
}
// 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
}