Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/ReadFromUri/ReadFromUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public FileAbstraction (string file)

public Stream WriteStream => new FileStream (Name, FileMode.Open);

public bool ReadShareWhenWriting {
get => throw new NotSupportedException ();
set => throw new NotSupportedException ();
}

public void CloseStream (Stream stream)
{
stream.Close ();
Expand Down
40 changes: 40 additions & 0 deletions src/TaglibSharp.Tests/FileFormats/Id3BothFormatTest.cs
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
Expand Down Expand Up @@ -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)) {
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();
}
}
}
5 changes: 5 additions & 0 deletions src/TaglibSharp.Tests/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public MemoryFileAbstraction (int maxSize, byte[] data)

public Stream WriteStream => stream;

public bool ReadShareWhenWriting {
get => throw new NotSupportedException ();
set => throw new NotSupportedException ();
}

public void CloseStream (Stream stream)
{
// This causes a stackoverflow
Expand Down
29 changes: 28 additions & 1 deletion src/TaglibSharp/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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);

/// <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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/TaglibSharp/Tiff/Rw2/IFDReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// USA
//

using System;
using System.IO;
using TagLib.IFD;

Expand Down Expand Up @@ -133,5 +134,10 @@ public void CloseStream (Stream stream)
public Stream WriteStream {
get { return ReadStream; }
}

public bool ReadShareWhenWriting {
get => throw new NotSupportedException ();
set => throw new NotSupportedException ();
}
}
}