-
Notifications
You must be signed in to change notification settings - Fork 319
Add ability to enable Read Sharing when Writing to files #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,6 @@ | ||||||
using NUnit.Framework; | ||||||
using System.Threading; | ||||||
using System.Threading.Tasks; | ||||||
using TagLib; | ||||||
|
||||||
namespace TaglibSharp.Tests.FileFormats | ||||||
|
@@ -133,5 +135,43 @@ public void TestCreateId3Tags () | |||||
file = File.Create (tempFile); | ||||||
Assert.AreEqual (TagTypes.Id3v1 | TagTypes.Id3v2, file.TagTypes); | ||||||
} | ||||||
|
||||||
[Test] | ||||||
public async Task TestReadShareWhenWriting () | ||||||
{ | ||||||
// Simulate having another thread open the file and begin playing it (by | ||||||
// keeping it open) until we tell it to stop playback and close the file. | ||||||
SemaphoreSlim playbackStarted = new SemaphoreSlim (0); | ||||||
SemaphoreSlim stopPlayback = new SemaphoreSlim (0); | ||||||
_ = Task.Run (async () => { | ||||||
using (var fileToPlay = System.IO.File.Open (tmp_file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider using
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
playbackStarted.Release(); | ||||||
await stopPlayback.WaitAsync(); | ||||||
} | ||||||
}); | ||||||
|
||||||
// Wait until playback in the other thread has begun. | ||||||
await playbackStarted.WaitAsync(); | ||||||
|
||||||
// Now that the file is in use, ensure writing to it leads to an IOException | ||||||
// due to the AudioFile's write stream not being shared for reading. | ||||||
Assert.Catch (typeof (System.IO.IOException), () => { | ||||||
var file = File.Create (tmp_file); | ||||||
file.RemoveTags (TagTypes.AllTags); | ||||||
file.Save (); | ||||||
}); | ||||||
|
||||||
// Now try writing again, but this time explicitly mark the write | ||||||
// stream as being shared for reading. Ensure no exception occurs. | ||||||
Assert.DoesNotThrow (() => { | ||||||
var file = File.Create (tmp_file); | ||||||
file.FileAbstraction.ReadShareWhenWriting = true; | ||||||
file.RemoveTags (TagTypes.AllTags); | ||||||
file.Save (); | ||||||
}); | ||||||
|
||||||
// Finally, tell the other thread it can now "stop" playback and close the file. | ||||||
stopPlayback.Release(); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,6 +189,12 @@ public enum AccessMode | |
/// </summary> | ||
List<string> corruption_reasons; | ||
|
||
/// <summary> | ||
/// Specifies whether the file should be readable by other | ||
/// threads while being written to by the thread that opened it. | ||
/// </summary> | ||
public bool read_share_when_writing; | ||
|
||
#endregion | ||
|
||
|
||
|
@@ -1621,7 +1627,15 @@ public LocalFileAbstraction (string path) | |
/// </value> | ||
public Stream WriteStream => System.IO.File.Open (Name, | ||
FileMode.Open, | ||
FileAccess.ReadWrite); | ||
FileAccess.ReadWrite, | ||
ReadShareWhenWriting ? FileShare.Read : FileShare.None); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider using FileShare.None explicitly instead of the default parameter to make the intent clearer, or add a comment explaining why FileShare.None is the default behavior. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether the file should | ||
/// be readable by other threads while being written to by | ||
/// the thread that opened it. | ||
/// </summary> | ||
public bool ReadShareWhenWriting { get; set; } | ||
|
||
/// <summary> | ||
/// Closes a stream created by the current instance. | ||
|
@@ -1793,6 +1807,19 @@ public interface IFileAbstraction | |
/// </remarks> | ||
Stream WriteStream { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether the file | ||
/// should be readable by other threads while being | ||
/// written to by the thread that opened it. | ||
/// </summary> | ||
/// <remarks> | ||
/// This property is useful when editing ID3 tags of | ||
/// MP3 files. In particular, setting it to true | ||
/// ensures you can listen to a song in one application | ||
/// while simultaneously editing its ID3 tags in another. | ||
/// </remarks> | ||
bool ReadShareWhenWriting { get; set; } | ||
|
||
/// <summary> | ||
/// Closes a stream originating from the current | ||
/// instance. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The discarded task should be awaited or stored in a variable to handle potential exceptions. Consider using
var backgroundTask = Task.Run(...)
and awaiting it at the end of the test.Copilot uses AI. Check for mistakes.