-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle file sharing violation when trying to write a file (#1728) #patch
Related Sentry event ID: 3b8b48f6402d488484a0b3b86a6056bf
- Loading branch information
1 parent
237e8af
commit 0404f04
Showing
16 changed files
with
90 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
namespace ImperatorToCK3.CommonUtils; | ||
|
||
using commonItems; | ||
using System; | ||
using Polly; | ||
using System.IO; | ||
using Exceptions; | ||
using System.Text; | ||
|
||
public static class FileOpeningHelper { | ||
private const string CloseProgramsHint = "You should close all programs that may be using the file."; | ||
|
||
private static bool IsFilesSharingViolation(Exception ex) { | ||
const int sharingViolationHResult = unchecked((int)0x80070020); | ||
return ex.HResult == sharingViolationHResult; | ||
} | ||
|
||
public static StreamWriter OpenWriteWithRetries(string filePath) => OpenWriteWithRetries(filePath, Encoding.UTF8); | ||
|
||
public static StreamWriter OpenWriteWithRetries(string filePath, Encoding encoding) { | ||
const int maxAttempts = 10; | ||
StreamWriter? writer = null; | ||
|
||
int currentAttempt = 0; | ||
|
||
var policy = Policy | ||
.Handle<IOException>(IsFilesSharingViolation) | ||
.WaitAndRetry(maxAttempts, | ||
sleepDurationProvider: _ => TimeSpan.FromSeconds(30), | ||
onRetry: (_, _, _) => { | ||
currentAttempt++; | ||
Logger.Warn($"Attempt {currentAttempt} to open \"{filePath}\" failed. {CloseProgramsHint}"); | ||
}); | ||
|
||
try { | ||
policy.Execute(() => { | ||
writer = new StreamWriter(filePath, append: false, encoding); | ||
}); | ||
} catch (IOException ex) when (IsFilesSharingViolation(ex)) { | ||
Logger.Debug(ex.ToString()); | ||
throw new UserErrorException($"Failed to open \"{filePath}\" for writing. {CloseProgramsHint}"); | ||
} | ||
|
||
if (writer is null) { | ||
throw new UserErrorException($"Failed to open \"{filePath}\" for writing: unknown error."); | ||
} | ||
|
||
return writer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.