Skip to content

Commit

Permalink
[core] Save/Restore in-memory torrents too
Browse files Browse the repository at this point in the history
Persist a good-enough copy of the torrent metadata to
disk.
  • Loading branch information
alanmcgovern committed May 4, 2021
1 parent 9358580 commit 17d6eec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/MonoTorrent.Tests/Client/ClientEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ public async Task SaveRestoreState_NoTorrents ()
Assert.AreEqual (engine.Settings, restoredEngine.Settings);
}

[Test]
public async Task SaveRestoreState_OneInMemoryTorrent ()
{
var pieceLength = Piece.BlockSize * 4;
using var tmpDir = TempDir.Create ();

var torrent = TestRig.CreateMultiFileTorrent (TorrentFile.Create (pieceLength, Piece.BlockSize, Piece.BlockSize * 2, Piece.BlockSize * 3), pieceLength, out BEncoding.BEncodedDictionary metadata);

var engine = new ClientEngine (EngineSettingsBuilder.CreateForTests (cacheDirectory: tmpDir.Path));
var torrentManager = await engine.AddAsync (torrent, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = true }.ToSettings ());
await torrentManager.SetFilePriorityAsync (torrentManager.Files[0], Priority.High);
await torrentManager.MoveFileAsync (torrentManager.Files[1], Path.GetFullPath ("some_fake_path.txt"));

var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ());
Assert.AreEqual (engine.Settings, restoredEngine.Settings);
Assert.AreEqual (engine.Torrents[0].Torrent.Name, restoredEngine.Torrents[0].Torrent.Name);
Assert.AreEqual (engine.Torrents[0].SavePath, restoredEngine.Torrents[0].SavePath);
Assert.AreEqual (engine.Torrents[0].Settings, restoredEngine.Torrents[0].Settings);
Assert.AreEqual (engine.Torrents[0].InfoHash, restoredEngine.Torrents[0].InfoHash);
Assert.AreEqual (engine.Torrents[0].MagnetLink.ToV1String (), restoredEngine.Torrents[0].MagnetLink.ToV1String ());

Assert.AreEqual (engine.Torrents[0].Files.Count, restoredEngine.Torrents[0].Files.Count);
for (int i = 0; i < engine.Torrents.Count; i++) {
Assert.AreEqual (engine.Torrents[0].Files[i].FullPath, restoredEngine.Torrents[0].Files[i].FullPath);
Assert.AreEqual (engine.Torrents[0].Files[i].Priority, restoredEngine.Torrents[0].Files[i].Priority);
}
}

[Test]
public async Task SaveRestoreState_OneMagnetLink ()
{
Expand Down Expand Up @@ -217,6 +245,7 @@ public async Task SaveRestoreState_OneTorrentFile_ContainingDirectory ()

var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ());
Assert.AreEqual (engine.Settings, restoredEngine.Settings);
Assert.AreEqual (engine.Torrents[0].Torrent.Name, restoredEngine.Torrents[0].Torrent.Name);
Assert.AreEqual (engine.Torrents[0].SavePath, restoredEngine.Torrents[0].SavePath);
Assert.AreEqual (engine.Torrents[0].Settings, restoredEngine.Torrents[0].Settings);
Assert.AreEqual (engine.Torrents[0].InfoHash, restoredEngine.Torrents[0].InfoHash);
Expand Down Expand Up @@ -244,6 +273,7 @@ public async Task SaveRestoreState_OneTorrentFile_NoContainingDirectory ()

var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ());
Assert.AreEqual (engine.Settings, restoredEngine.Settings);
Assert.AreEqual (engine.Torrents[0].Torrent.Name, restoredEngine.Torrents[0].Torrent.Name);
Assert.AreEqual (engine.Torrents[0].SavePath, restoredEngine.Torrents[0].SavePath);
Assert.AreEqual (engine.Torrents[0].Settings, restoredEngine.Torrents[0].Settings);
Assert.AreEqual (engine.Torrents[0].InfoHash, restoredEngine.Torrents[0].InfoHash);
Expand Down
21 changes: 18 additions & 3 deletions src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,28 @@ public Task<TorrentManager> AddAsync (Torrent torrent, string saveDirectory)
public async Task<TorrentManager> AddAsync (Torrent torrent, string saveDirectory, TorrentSettings settings)
{
await MainLoop.SwitchThread ();
var metadata = new BEncodedDictionary {

var editor = new TorrentEditor (new BEncodedDictionary {
{ "info", BEncodedValue.Decode (torrent.InfoMetadata) }
};
});
editor.SetCustom ("name", (BEncodedString) torrent.Name);

if (torrent.AnnounceUrls.Count > 0) {
if (torrent.AnnounceUrls.Count == 1 && torrent.AnnounceUrls [0].Count == 1) {
editor.Announce = torrent.AnnounceUrls.Single ().Single ();
} else {
foreach (var tier in torrent.AnnounceUrls) {
var list = new List<string> ();
foreach (var tracker in tier)
list.Add (tracker);
editor.Announces.Add (list);
}
}
}

var metadataCachePath = Settings.GetMetadataPath (torrent.InfoHash);
Directory.CreateDirectory (Path.GetDirectoryName (metadataCachePath));
File.WriteAllBytes (metadataCachePath, metadata.Encode ());
File.WriteAllBytes (metadataCachePath, editor.ToDictionary ().Encode ());

return await AddAsync (null, torrent, saveDirectory, settings);
}
Expand Down

0 comments on commit 17d6eec

Please sign in to comment.