-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix file locking in attachments (#1377)
- Loading branch information
1 parent
83f2de3
commit 416e6b5
Showing
3 changed files
with
55 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Sentry.Testing; | ||
|
||
namespace Sentry.Tests; | ||
|
||
public class FileAttachmentContentTests | ||
{ | ||
[Fact] | ||
public void DoesNotLock() | ||
{ | ||
// Arrange | ||
using var tempDir = new TempDirectory(); | ||
var filePath = Path.Combine(tempDir.Path, "MyFile.txt"); | ||
File.WriteAllText(filePath, "Hello world!"); | ||
|
||
var attachment = new FileAttachmentContent(filePath); | ||
// Act | ||
using (var stream = attachment.GetStream()) | ||
{ | ||
Assert.False(IsFileLocked(filePath)); | ||
} | ||
} | ||
|
||
private static bool IsFileLocked(string file) | ||
{ | ||
try | ||
{ | ||
using FileStream stream = new(file, FileMode.Open, FileAccess.ReadWrite); | ||
stream.Close(); | ||
} | ||
catch (IOException) | ||
{ | ||
//the file is unavailable because it is: | ||
//still being written to | ||
//or being processed by another thread | ||
//or does not exist (has already been processed) | ||
return true; | ||
} | ||
|
||
//file is not locked | ||
return false; | ||
} | ||
} |