public Task <Errorable <BlobID> > ResolvePartialID(BlobID.Partial id) { FileInfo[] fis = system.getPathsByPartialID(id); if (fis.Length == 1) { return(Task.FromResult(BlobID.TryParse(id.ToString().Substring(0, 2) + fis[0].Name))); } if (fis.Length == 0) { return(Task.FromResult((Errorable <BlobID>) new BlobIDPartialNoResolutionError(id))); } return(Task.FromResult((Errorable <BlobID>) new BlobIDPartialAmbiguousResolutionError(id, fis.SelectAsArray(f => BlobID.TryParse(id.ToString().Substring(0, 2) + f.Name).Value)))); }
private async Task <Errorable <TreeNode> > getTree(TreeID id) { FileInfo fi = system.getPathByID(id); if (!fi.Exists) { return(new TreeIDRecordDoesNotExistError(id)); } byte[] buf; int nr = 0; using (var fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 16384, true)) { // TODO: implement an async buffered Stream: buf = new byte[16384]; nr = await fs.ReadAsync(buf, 0, 16384).ConfigureAwait(continueOnCapturedContext: false); if (nr >= 16384) { // My, what a large tree you have! throw new NotSupportedException(); } } TreeNode.Builder tb = new TreeNode.Builder(new List <TreeTreeReference>(), new List <TreeBlobReference>()); // Parse the Tree: using (var ms = new MemoryStream(buf, 0, nr, false)) using (var sr = new StreamReader(ms, Encoding.UTF8)) { string line; while ((line = sr.ReadLine()) != null) { if (line.StartsWith("tree ")) { string linked_treeid = line.Substring(5, (TreeID.ByteArrayLength * 2)); string name = line.Substring(6 + (TreeID.ByteArrayLength * 2)); // Attempt to parse the TreeID and verify its existence: Errorable <TreeID> trid = TreeID.TryParse(linked_treeid); if (trid.HasErrors) { return(trid.Errors); } if (!system.getPathByID(trid.Value).Exists) { return(new TreeIDRecordDoesNotExistError(trid.Value)); } tb.Trees.Add(new TreeTreeReference.Builder(name, trid.Value)); } else if (line.StartsWith("blob ")) { string linked_blobid = line.Substring(5, (TreeID.ByteArrayLength * 2)); string name = line.Substring(6 + (TreeID.ByteArrayLength * 2)); // Attempt to parse the BlobID and verify its existence: Errorable <BlobID> blid = BlobID.TryParse(linked_blobid); if (blid.HasErrors) { return(blid.Errors); } if (!system.getPathByID(blid.Value).Exists) { return(new BlobIDRecordDoesNotExistError(blid.Value)); } tb.Blobs.Add(new TreeBlobReference.Builder(name, blid.Value)); } } } // Create the immutable Tree from the Builder: TreeNode tr = tb; // Validate the computed TreeID: if (tr.ID != id) { return(new ComputedTreeIDMismatchError(tr.ID, id)); } return(tr); }