From 2fbd9f15fae4a0d73f2c3d10f839a1e7f5451e36 Mon Sep 17 00:00:00 2001 From: Mohanson Date: Tue, 3 Apr 2018 15:26:47 +0800 Subject: [PATCH 1/3] bug fix: WriteFileAtomic Must close file before rename it. --- common/os.go | 1 + 1 file changed, 1 insertion(+) diff --git a/common/os.go b/common/os.go index f1e07115c..47ae4a1cc 100644 --- a/common/os.go +++ b/common/os.go @@ -148,6 +148,7 @@ func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error { } else if n < len(data) { return io.ErrShortWrite } + f.Close() return os.Rename(f.Name(), filename) } From 29a8cb8d87e0ed2567938aed57bb78cc8cd6fee9 Mon Sep 17 00:00:00 2001 From: Mohanson Date: Tue, 3 Apr 2018 16:51:30 +0800 Subject: [PATCH 2/3] add comments. --- common/os.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common/os.go b/common/os.go index 47ae4a1cc..8a0c14f46 100644 --- a/common/os.go +++ b/common/os.go @@ -148,6 +148,9 @@ func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error { } else if n < len(data) { return io.ErrShortWrite } + // Close the file before renaming it, otherwise it will cause "The process + // cannot access the file because it is being used by another process." on windows or + // cause "cross-link error" on linux when you try to save it to another partition. f.Close() return os.Rename(f.Name(), filename) From b221ca0efa588db6875ee8f35f815319f25a3c96 Mon Sep 17 00:00:00 2001 From: Mohanson Date: Tue, 3 Apr 2018 19:04:09 +0800 Subject: [PATCH 3/3] refine comments --- common/os.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/os.go b/common/os.go index 8a0c14f46..450388564 100644 --- a/common/os.go +++ b/common/os.go @@ -149,8 +149,7 @@ func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error { return io.ErrShortWrite } // Close the file before renaming it, otherwise it will cause "The process - // cannot access the file because it is being used by another process." on windows or - // cause "cross-link error" on linux when you try to save it to another partition. + // cannot access the file because it is being used by another process." on windows. f.Close() return os.Rename(f.Name(), filename)