From 2e30138ac2537b762a03765944b136422b5b299b Mon Sep 17 00:00:00 2001 From: Chris Darroch Date: Sun, 14 Jan 2024 19:54:48 -0800 Subject: [PATCH] tools/util_windows.go: always close cloned files In commit e861b79a49b02996d22191ebb142feb8768358de of #5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, the Windows variant of our tools.CloneFileByPath() function opens both the source and destination file paths provided for a "clone" operation but does not close the resultant file descriptors, so we now add deferred calls to ensure we always close both files. (This minor issue dates from the introduction in commit e55bc4c5d41c227515348ef126743806d7e6a0c2 of the support for the "file clone" operation on ReFS in PR #3790.) --- tools/util_windows.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/util_windows.go b/tools/util_windows.go index 55916f83..86727bd4 100644 --- a/tools/util_windows.go +++ b/tools/util_windows.go @@ -43,8 +43,8 @@ func CheckCloneFileSupported(dir string) (supported bool, err error) { return false, err } defer func() { - _ = src.Close() - _ = os.Remove(src.Name()) + src.Close() + os.Remove(src.Name()) }() // Make src file not empty. @@ -59,8 +59,8 @@ func CheckCloneFileSupported(dir string) (supported bool, err error) { return false, err } defer func() { - _ = dst.Close() - _ = os.Remove(dst.Name()) + dst.Close() + os.Remove(dst.Name()) }() return CloneFile(dst, src) @@ -71,11 +71,13 @@ func CloneFileByPath(dst, src string) (success bool, err error) { if err != nil { return } + defer dstFile.Close() srcFile, err := os.Open(src) if err != nil { return } + defer srcFile.Close() return CloneFile(dstFile, srcFile) }