From 4d9af257d3824c7066ddc4ab856e00f656224d32 Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 12 Sep 2022 12:13:04 +0800 Subject: Defer file rename routine only if the file is successfully opened. --- recording/runner.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'recording/runner.go') diff --git a/recording/runner.go b/recording/runner.go index a2f2edc..e3735cb 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -204,6 +204,12 @@ func record( saveDir := task.Download.SaveDirectory filePath := path.Join(saveDir, fileName) + file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + task.logger.Error("Cannot open file for writing: %v", err) + cancelled = true + return + } // rename the extension name to originalExtName when finish writing defer func() { if extName == originalExtName { @@ -218,13 +224,6 @@ func record( } task.logger.Info("Rename file \"%s\" to \"%s\".", from, to) }() - - file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) - if err != nil { - task.logger.Error("Cannot open file for writing: %v", err) - cancelled = true - return - } defer func() { _ = file.Close() }() writeBufferSize := task.Download.DiskWriteBufferBytes -- cgit v1.2.3