From 69b05d49de08d42f926292b8c99d6040bac42f34 Mon Sep 17 00:00:00 2001
From: Thomas Goyne <plorkyeran@aegisub.org>
Date: Wed, 3 Jul 2013 07:12:06 -0700
Subject: [PATCH] Retry commits of file writes for up to a second to work
 around AV scanning

Poorly-written antivirus software briefly lock newly written files to
scan them for viruses, which makes the rename from the temp file to
actual file fail. Work around this by retrying the rename up to ten
times.

Closes #1618.
---
 aegisub/libaegisub/common/io.cpp | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/aegisub/libaegisub/common/io.cpp b/aegisub/libaegisub/common/io.cpp
index 085eb02b8..a66923501 100644
--- a/aegisub/libaegisub/common/io.cpp
+++ b/aegisub/libaegisub/common/io.cpp
@@ -97,7 +97,22 @@ Save::Save(const std::string& file, bool binary)
 
 Save::~Save() {
 	delete fp;
+#ifndef _WIN32
 	util::Rename(tmp_name, file_name);
+#else
+	for (int i = 0; i < 10; ++i) {
+		try {
+			util::Rename(tmp_name, file_name);
+			return;
+		}
+		catch (agi::FileNotAccessibleError const&) {
+			// Retry up to ten times in case it's just locked by a poorly-written antivirus scanner
+			if (i == 9)
+				throw;
+			Sleep(100);
+		}
+	}
+#endif
 }
 
 std::ofstream& Save::Get() {
-- 
GitLab