Skip to content

Commit 01e0647

Browse files
fix(FileSystemApi): read file with offset and length
1 parent 46a52a3 commit 01e0647

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/CoreApi/FileSystemApi.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,17 @@ internal FileSystemApi(IpfsClient ipfs)
182182

183183
public Task<Stream> ReadFileAsync(string path, long offset, long length = 0, CancellationToken cancel = default(CancellationToken))
184184
{
185-
// TODO: length is not yet supported by daemons
186-
return ipfs.DownloadAsync("cat", cancel, path, $"offset={offset}");
185+
// https://github.com/ipfs/go-ipfs/issues/5380
186+
if (offset > int.MaxValue)
187+
throw new NotSupportedException("Only int offsets are currently supported.");
188+
if (length > int.MaxValue)
189+
throw new NotSupportedException("Only int lengths are currently supported.");
190+
191+
if (length == 0)
192+
length = int.MaxValue; // go-ipfs only accepts int lengths
193+
return ipfs.DownloadAsync("cat", cancel, path,
194+
$"offset={offset}",
195+
$"length={length}");
187196
}
188197

189198
/// <summary>

test/CoreApi/FileSystemApiTest.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ public void Read_With_Offset()
5858
using (var outdata = ipfs.FileSystem.ReadFileAsync(node.Id, offset: 1).Result)
5959
{
6060
Assert.AreEqual(20, outdata.ReadByte());
61+
Assert.AreEqual(30, outdata.ReadByte());
62+
Assert.AreEqual(-1, outdata.ReadByte());
63+
}
64+
}
65+
66+
[TestMethod]
67+
public void Read_With_Offset_Length_1()
68+
{
69+
var ipfs = TestFixture.Ipfs;
70+
var indata = new MemoryStream(new byte[] { 10, 20, 30 });
71+
var node = ipfs.FileSystem.AddAsync(indata).Result;
72+
using (var outdata = ipfs.FileSystem.ReadFileAsync(node.Id, offset: 1, count: 1).Result)
73+
{
74+
Assert.AreEqual(20, outdata.ReadByte());
75+
Assert.AreEqual(-1, outdata.ReadByte());
76+
}
77+
}
78+
79+
[TestMethod]
80+
public void Read_With_Offset_Length_2()
81+
{
82+
var ipfs = TestFixture.Ipfs;
83+
var indata = new MemoryStream(new byte[] { 10, 20, 30 });
84+
var node = ipfs.FileSystem.AddAsync(indata).Result;
85+
using (var outdata = ipfs.FileSystem.ReadFileAsync(node.Id, offset: 1, count: 2).Result)
86+
{
87+
Assert.AreEqual(20, outdata.ReadByte());
88+
Assert.AreEqual(30, outdata.ReadByte());
89+
Assert.AreEqual(-1, outdata.ReadByte());
6190
}
6291
}
6392

0 commit comments

Comments
 (0)