From c58be5625ddd3296a8c3fd48df87dffad5a2bb42 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 8 Jul 2020 17:37:53 -0700 Subject: [PATCH] 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. --- chanbackup/backupfile.go | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/chanbackup/backupfile.go b/chanbackup/backupfile.go index b3a95f51..d2d6b788 100644 --- a/chanbackup/backupfile.go +++ b/chanbackup/backupfile.go @@ -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 - } + // We'll return an error if the main file isn't currently set. + if b.fileName == "" { + return nil, ErrNoBackupFileExists } - // 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 }