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.)
This commit is contained in:
Chris Darroch 2024-01-14 19:54:48 -08:00
parent b4a0927e39
commit 2e30138ac2

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