public VfsPathParserContext(PathParser parser, string[] pathElements, VfsEntity root) { this.Parser = parser; foreach (string element in pathElements) { this.RemainingPathElements.Enqueue(element); } this.SpecifiedPath = this.Parser.BuildAbsolutePathStringFromElements(pathElements); Exception = new VfsException(SpecifiedPath, "Unknown path parser error."); AddToEntityStack(root); }
public void AddToEntityStack(VfsEntity entity) { entity.AddHandleRef(); EntityStack.Add(entity); NormalizedPathStack.Add(entity.Name); }
public virtual async Task ParseAsync(VfsPathParserContext ctx, CancellationToken cancel = default) { if (ctx.RemainingPathElements.TryPeek(out string?nextName) == false) { // This is the final entity ctx.Exception = null; return; } nextName._MarkNotNull(); try { ResultOrExeption <ValueHolder <VfsEntity> > result = await this.OpenEntityAsync(nextName, cancel); if (result.IsError && result.Exception is VfsNotFoundException) { ctx.Exception = new VfsNotFoundException(ctx.SpecifiedPath, $"The object \"{nextName}\" is not found on the directory \"{this.FileSystem.PathParser.BuildAbsolutePathStringFromElements(ctx.NormalizedPathStack)}\"."); return; } using (var nextEntityHolder = result.Value) { ctx.RemainingPathElements.Dequeue(); VfsEntity nextEntity = nextEntityHolder.Value; if (nextEntity is VfsDirectory nextDirectory) { ctx.AddToEntityStack(nextDirectory); await nextDirectory.ParseAsync(ctx, cancel); } else if (nextEntity is VfsFile nextFile) { if (ctx.RemainingPathElements.Count >= 1) { throw new VfsNotFoundException(ctx.SpecifiedPath, $"Invalid directory name. The file \"{nextName}\" is found on the directory \"{this.FileSystem.PathParser.BuildAbsolutePathStringFromElements(ctx.NormalizedPathStack)}\"."); } ctx.AddToEntityStack(nextFile); ctx.Exception = null; return; } else { throw new VfsException(ctx.SpecifiedPath, $"The object \"{nextName}\" is unknown type \"{nextEntity.GetType().ToString()}\"."); } } } catch (VfsNotFoundException) { ctx.Exception = new VfsNotFoundException(ctx.SpecifiedPath, $"The object \"{nextName}\" is not found on the directory \"{this.FileSystem.PathParser.BuildAbsolutePathStringFromElements(ctx.NormalizedPathStack)}\"."); return; } }