Skip to content

Commit c58be56

Browse files
committed
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 lightningnetwork#4450.
1 parent 8cb1276 commit c58be56

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

chanbackup/backupfile.go

+7-27
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ type MultiFile struct {
3939
// fileName is the file name of the main back up file.
4040
fileName string
4141

42-
// mainFile is an open handle to the main back up file.
43-
mainFile *os.File
44-
4542
// tempFileName is the name of the file that we'll use to stage a new
4643
// packed multi-chan backup, and the rename to the main back up file.
4744
tempFileName string
@@ -132,32 +129,15 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
132129
func (b *MultiFile) ExtractMulti(keyChain keychain.KeyRing) (*Multi, error) {
133130
var err error
134131

135-
// If the backup file isn't already set, then we'll attempt to open it
136-
// anew.
137-
if b.mainFile == nil {
138-
// We'll return an error if the main file isn't currently set.
139-
if b.fileName == "" {
140-
return nil, ErrNoBackupFileExists
141-
}
142-
143-
// Otherwise, we'll open the file to prep for reading the
144-
// contents.
145-
b.mainFile, err = os.Open(b.fileName)
146-
if err != nil {
147-
return nil, err
148-
}
149-
}
150-
151-
// Before we start to read the file, we'll ensure that the next read
152-
// call will start from the front of the file.
153-
_, err = b.mainFile.Seek(0, 0)
154-
if err != nil {
155-
return nil, err
132+
// We'll return an error if the main file isn't currently set.
133+
if b.fileName == "" {
134+
return nil, ErrNoBackupFileExists
156135
}
157136

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

0 commit comments

Comments
 (0)