Skip to content

Commit

Permalink
fix(PinApi): recursive removal of pins
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jul 28, 2019
1 parent 02b4c24 commit 7624498
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/CoreApi/PinApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,23 @@ public async Task<IEnumerable<Cid>> RemoveAsync(Cid id, bool recursive = true, C
while (todos.Count > 0)
{
var current = todos.Pop();
// TODO: exists is never set to true!
bool exists = false;
await Store.RemoveAsync(current, cancel).ConfigureAwait(false);
if (exists && recursive)
if (recursive)
{
var links = await ipfs.Object.LinksAsync(current, cancel).ConfigureAwait(false);
foreach (var link in links)
if (null != await ipfs.Block.StatAsync(current, cancel).ConfigureAwait(false))
{
todos.Push(link.Id);
try
{
var links = await ipfs.Object.LinksAsync(current, cancel).ConfigureAwait(false);
foreach (var link in links)
{
todos.Push(link.Id);
}
}
catch (Exception)
{
// ignore if current is not an objcet.
}
}
}
dones.Add(current);
Expand Down
20 changes: 19 additions & 1 deletion test/CoreApi/PinApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Ipfs.CoreApi;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -85,6 +84,25 @@ public async Task Add_Recursive()
var cids = await ipfs.Pin.AddAsync(node.Id, true);
Assert.AreEqual(6, cids.Count());
}

[TestMethod]
public async Task Remove_Recursive()
{
var ipfs = TestFixture.Ipfs;
var options = new AddFileOptions
{
ChunkSize = 3,
Pin = false,
RawLeaves = true,
Wrap = true,
};
var node = await ipfs.FileSystem.AddTextAsync("hello world", options);
var cids = await ipfs.Pin.AddAsync(node.Id, true);
Assert.AreEqual(6, cids.Count());

var removedCids = await ipfs.Pin.RemoveAsync(node.Id, true);
CollectionAssert.AreEqual(cids.ToArray(), removedCids.ToArray());
}
}
}

0 comments on commit 7624498

Please sign in to comment.