Skip to content
Merged
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
13 changes: 12 additions & 1 deletion CHANGELOG.md

Large diffs are not rendered by default.

33 changes: 28 additions & 5 deletions src/TagLibSharp2/Aac/AacFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,24 @@ internal void MarkPicturesSkipped ()
/// <summary>
/// Gets or sets the APEv2 tag after the stream. Set to null to remove it on save.
/// </summary>
public ApeTag? ApeTag { get; set; }
public ApeTag? ApeTag {
get => _apeTag;
set {
_apeTag = value;
_apeGone = false;
}
}

ApeTag? _apeTag;

// The APE tag the view shows: an emptied tag a save over the source did not write is held for a caller who holds it
// (review 2 B1) and written again once it holds a value, but the file holds no APE tag, so the view goes on as a fresh
// read of that file does, its first write creating an ID3v2 tag.
ApeTag? ViewApe => _apeGone && _apeTag is { StoresNothing: true } ? null : _apeTag;

// Whether the source holds no APE tag for the tag held: set by a save over the source that wrote none, cleared by one
// that wrote it, and by an assignment. A save elsewhere does not change it (contract suite round 3, R1).
bool _apeGone;

/// <summary>
/// Gets or sets the ID3v1 tag in the last 128 bytes. Set to null to remove it on save.
Expand Down Expand Up @@ -134,7 +151,7 @@ internal void MarkPicturesSkipped ()
/// tag. The first write on a tagless file creates an ID3v2 tag, and a write that leaves that tag
/// empty removes it again, so a save writes no empty tag.
/// </remarks>
public Tag? Tag => _disposed ? null : CombinedTag.Over (() => [Id3v2Tag, ApeTag, Id3v1Tag],
public Tag? Tag => _disposed ? null : CombinedTag.Over (() => [Id3v2Tag, ViewApe, Id3v1Tag],
_creation ??= new TagCreation (() => new Id3v2Tag (), tag => Id3v2Tag = (Id3v2Tag?)tag));

/// <inheritdoc />
Expand Down Expand Up @@ -448,12 +465,14 @@ Written Plan (AacTailSource source)

// Lyrics3 belongs in front of ID3v1 and goes when that tag goes.
var keepsLyrics = Id3v1Tag is not null && tail.LyricsStart >= audioEnd && tail.Id3v1Start > tail.LyricsStart;
var apeHeader = ApeTag is not null && ApeTag.HeaderForSave (ApeTag, _readApeTag, _readApeHeader, defaultHeader: true);
// An APE tag that holds nothing is not written (#258).
var ape = ApeTag is { StoresNothing: false } ? ApeTag : null;
var apeHeader = ape is not null && ApeTag.HeaderForSave (ape, _readApeTag, _readApeHeader, defaultHeader: true);
return new Written (
Id3v2Tag?.Render () ?? BinaryData.Empty,
audioStart,
audioEnd - audioStart,
ApeTag?.RenderWithOptions (apeHeader) ?? BinaryData.Empty,
ape?.RenderWithOptions (apeHeader) ?? BinaryData.Empty,
apeHeader,
keepsLyrics ? tail.LyricsStart : 0,
keepsLyrics ? tail.Id3v1Start - tail.LyricsStart : 0,
Expand All @@ -475,7 +494,11 @@ FileWriteResult Adopt (FileWriteResult result, Written written, IFileSystem? fil

_audioStart = written.Id3v2.Length;
Id3v2Size = written.Id3v2.Length;
_readApeTag = ApeTag;
// No empty APE tag was written (#258), so the file holds none: the tag the object holds, kept for a caller who
// holds it (review 2 B1), is then not a tag the file read, and a value set next is written with the layout of a
// tag this file did not read, as a fresh read's view writes it. A save elsewhere does not get here (round 3, R1).
_apeGone = _apeTag is { StoresNothing: true };
_readApeTag = _apeGone ? null : _apeTag;
_readApeHeader = written.ApeHeader;
Source.Rewrote (length);
return result;
Expand Down
99 changes: 92 additions & 7 deletions src/TagLibSharp2/Aiff/AiffFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,21 @@ internal void MarkPicturesSkipped ()
/// </remarks>
public Id3v2Tag? Id3v2Tag {
get => _id3;
set => _id3 = value;
set {
_id3 = value;
_id3Gone = false;
}
}

// The ID3 tag the view shows: an emptied tag a save over the source did not write is held for a caller who holds it
// (review 2 B1) and written again once it holds a value, but the file holds no ID3 chunk, so the view goes on as a
// fresh read of that file does, its first write creating a tag.
internal Id3v2Tag? ViewId3 => _id3Gone && _id3 is { IsEmpty: true } ? null : _id3;

// Whether the source holds no ID3 chunk for the tag held: set by a save over the source that wrote none, cleared by
// one that wrote it, and by an assignment. A save elsewhere leaves it as it was (contract suite round 3).
bool _id3Gone;

/// <summary>
/// Gets the combined metadata view.
/// </summary>
Expand Down Expand Up @@ -724,12 +736,38 @@ static bool HasText (string[]? value)

internal Id3v2Tag EnsureId3 ()
{
Id3Creation.BeforeWrite (() => [Id3v2Tag]);
Id3Creation.BeforeWrite (() => [ViewId3]);
return Id3v2Tag!;
}

/// <summary>The ID3 tag the view creates on a first write, and drops while it stays empty.</summary>
internal TagCreation Id3Creation => _id3Creation ??= new TagCreation (() => new Id3v2Tag (), tag => Id3v2Tag = (Id3v2Tag?)tag);
internal TagCreation Id3Creation => _id3Creation ??= new TagCreation (() => TakeGoneId3 () ?? new Id3v2Tag (), StoreCreatedId3);

// The ID3 tag a view write creates on a file that holds none: the hidden tag the object kept, when there is one, so a
// caller who holds it still holds the file's tag (review 3 B1). A save over the source already made it a new tag
// (ResetToNew), so it is what a fresh read's view creates.
Id3v2Tag? TakeGoneId3 ()
{
if (!_id3Gone || _id3 is not { IsEmpty: true } kept)
return null;
kept.ResetToNew ();
return kept;
}

// A view write that leaves the tag it created empty removes it (TagCreation); the hidden tag the object kept is hidden
// again instead, as the file still holds no ID3 chunk.
void StoreCreatedId3 (Tag? tag)
{
if (tag is null && _id3 is { IsEmpty: true } && _id3Reused) {
_id3Gone = true;
return;
}

_id3Reused = tag is not null && ReferenceEquals (tag, _id3) && _id3Gone;
Id3v2Tag = (Id3v2Tag?)tag;
}

bool _id3Reused;

TagCreation? _id3Creation;

Expand Down Expand Up @@ -983,14 +1021,34 @@ public FileWriteResult SaveToFile (string path, IFileSystem? fileSystem = null)
if (SaveRefusal () is { } refused)
return refused;
if (_skipped.Count == 0)
return SaveLoaded (path, fileSystem);
return SettleEmptyId3 (SaveLoaded (path, fileSystem), path, fileSystem);

return SaveFromSource (path, fileSystem, default);
return SettleEmptyId3 (SaveFromSource (path, fileSystem, default), path, fileSystem);
} catch (Id3v2TagTooLargeException tooLarge) {
return FileWriteResult.Failure (tooLarge.Message);
}
}

// A save writes no empty ID3 chunk. After a successful one over the source the file holds none, and the view goes on
// as a fresh read's (ViewId3); the tag itself stays attached. After a save elsewhere the object still describes its
// source, which holds the chunk (#235, review 1; contract suite round 3).
FileWriteResult SettleEmptyId3 (FileWriteResult result, string path, IFileSystem? fileSystem)
{
if (result.IsSuccess && (_id3 is { IsEmpty: true } || _id3Gone)
&& SavedOverSource.HoldsFile (SourcePath, _sourceFileSystem, path, fileSystem ?? _sourceFileSystem)) {
_id3Gone = _id3 is { IsEmpty: true };
// The tag kept is now the one a view creates on that file, so its next value is written as a fresh read's
// would be (review 3 B1).
if (_id3Gone)
_id3!.ResetToNew ();
}

return result;
}

async Task<FileWriteResult> SettleEmptyId3 (Task<FileWriteResult> save, string path, IFileSystem? fileSystem) =>
SettleEmptyId3 (await save.ConfigureAwait (false), path, fileSystem);

FileWriteResult SaveLoaded (string path, IFileSystem? fileSystem)
{
var fs = fileSystem ?? _sourceFileSystem;
Expand Down Expand Up @@ -1092,10 +1150,10 @@ public Task<FileWriteResult> SaveToFileAsync (
if (SaveRefusal () is { } refused)
return Task.FromResult (refused);
if (_skipped.Count == 0)
return SaveLoadedAsync (path, fileSystem, cancellationToken);
return SettleEmptyId3 (SaveLoadedAsync (path, fileSystem, cancellationToken), path, fileSystem);

// "Was not loaded" and a short source are decided before the token, matching the sync save.
return Task.FromResult (SaveFromSource (path, fileSystem, cancellationToken));
return Task.FromResult (SettleEmptyId3 (SaveFromSource (path, fileSystem, cancellationToken), path, fileSystem));
} catch (Id3v2TagTooLargeException tooLarge) {
return Task.FromResult (FileWriteResult.Failure (tooLarge.Message));
}
Expand Down Expand Up @@ -1321,6 +1379,33 @@ void WritePlanned (Stream output, Stream source, List<AiffChunk> chunks, long ch
}
}

/// <summary>
/// Saves the AIFF file back to its source path.
/// </summary>
/// <remarks>
/// This convenience method saves the file back to the path it was read from, through the file system it was read
/// with unless <paramref name="fileSystem"/> is given, as <see cref="SaveToFile(string, IFileSystem)"/> does.
/// Requires that the file was read using <see cref="ReadFromFile"/> or
/// <see cref="ReadFromFileAsync"/>.
/// </remarks>
/// <param name="fileSystem">Optional file system abstraction.</param>
/// <returns>The write result.</returns>
public FileWriteResult SaveToFile (IFileSystem? fileSystem = null)
{
// A tag a save renders that is too large for its size field fails the save, never throws (#275).
try {
if (PictureSkip.Refuse (this, Id3v2Tag) is { } denied)
return denied;
if (string.IsNullOrEmpty (SourcePath))
return FileWriteResult.Failure ("No source path available. File was not read from disk.");

var fs = fileSystem ?? _sourceFileSystem;
return SaveToFile (SourcePath!, fs);
} catch (Id3v2TagTooLargeException tooLarge) {
return FileWriteResult.Failure (tooLarge.Message);
}
}

/// <summary>
/// Saves the AIFF file back to its source path asynchronously.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/TagLibSharp2/Aiff/AiffTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public override bool RemoveCustomField (string name)
return removed;
}

Id3v2Tag? Id3 => _file.Id3v2Tag;
Id3v2Tag? Id3 => _file.ViewId3;

/// <inheritdoc/>
/// <remarks>
Expand Down
Loading
Loading