Example #1
0
    public override async ValueTask <T> ReadFileStream <T>(FileHandle fileHandle, Func <Stream, ValueTask <T> > reader)
    {
        var localFileHandle = new LocalFileHandle(fileHandle);
        var stats           = StatRealPath(GetRealPath(localFileHandle.Path));

        if (!localFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        if (stats.Type.HasFlag(FileType.Directory))
        {
            throw new FileIsADirectoryException(fileHandle);
        }

        var realPath = GetRealPath(localFileHandle.Path);

        await using var stream = File.Open(realPath, FileMode.Open, FileAccess.Read);

        T result;

        try
        {
            result = await reader(stream);
        }
        catch (System.Exception e)
        {
            throw new AggregateException("Exception from reader", e);
        }

        return(result);
    }
Example #2
0
    public override async ValueTask <FileHandle> CreateDirectory(FileHandle parentFileHandle, string name)
    {
        var parentLocalFileHandle = new LocalFileHandle(parentFileHandle);
        var stats = StatRealPath(GetRealPath(parentLocalFileHandle.Path));

        if (!parentLocalFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(parentFileHandle);
        }

        if (!stats.Type.HasFlag(FileType.Directory))
        {
            throw new FileNotADirectoryException(parentFileHandle);
        }

        var targetPath     = PathLib.Join(parentLocalFileHandle.Path, name);
        var targetRealPath = GetRealPath(targetPath);
        var fileType       = GetFileType(targetRealPath);

        if (fileType != null)
        {
            throw new FileExistsException($"The file '{targetPath}' existed.");
        }

        Directory.CreateDirectory(targetRealPath);

        var targetStat       = StatRealPath(targetRealPath);
        var targetFileHandle = new LocalFileHandle(targetPath, targetStat).FileHandle;

        await IndexFile(targetPath, targetFileHandle, targetStat);

        return(targetFileHandle);
    }
Example #3
0
    public override async ValueTask WriteFile(FileHandle fileHandle, ReadOnlyMemory <byte> content)
    {
        var localFileHandle = new LocalFileHandle(fileHandle);
        var filePath        = localFileHandle.Path;
        var stats           = StatRealPath(GetRealPath(filePath));

        if (!localFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        if (stats.Type.HasFlag(FileType.Directory))
        {
            throw new FileIsADirectoryException(fileHandle);
        }

        var realPath = GetRealPath(filePath);

        await using var fileStream = File.OpenWrite(realPath);
        await fileStream.WriteAsync(content);

        await fileStream.FlushAsync();

        var newStats = StatRealPath(GetRealPath(filePath));

        await IndexFile(filePath, fileHandle, newStats);
    }
Example #4
0
    public override ValueTask RemoveProperty(FileHandle fileHandle, string name)
    {
        var localFileHandle = new LocalFileHandle(fileHandle);
        var stats           = StatRealPath(GetRealPath(localFileHandle.Path));

        if (!localFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        return(_hintFileTracker.RemoveProperty(localFileHandle.Path, fileHandle, stats, name));
    }
Example #5
0
    public override async ValueTask <FileStats> Stat(FileHandle fileHandle)
    {
        var localFileHandle = new LocalFileHandle(fileHandle);
        var filePath        = localFileHandle.Path;
        var fileStats       = StatRealPath(GetRealPath(filePath));

        if (!localFileHandle.IsSameFile(fileStats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        await IndexFile(filePath, fileHandle, fileStats);

        return(fileStats);
    }
Example #6
0
    public override async ValueTask SetProperty(
        FileHandle fileHandle,
        string name,
        ReadOnlyMemory <byte> value,
        PropertyFeature feature = PropertyFeature.None)
    {
        var localFileHandle = new LocalFileHandle(fileHandle);
        var stats           = StatRealPath(GetRealPath(localFileHandle.Path));

        if (!localFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        await _hintFileTracker.SetProperty(localFileHandle.Path, fileHandle, stats, name, value, feature);
    }
Example #7
0
    public override async ValueTask Delete(FileHandle fileHandle, FileHandle parentFileHandle, string name, bool recursive)
    {
        var localFileHandle       = new LocalFileHandle(fileHandle);
        var parentLocalFileHandle = new LocalFileHandle(parentFileHandle);

        // check
        if (localFileHandle.Path != PathLib.Join(parentLocalFileHandle.Path, name))
        {
            throw new FileNotFoundException("File handles are inconsistent.");
        }

        var stats       = StatRealPath(GetRealPath(localFileHandle.Path));
        var parentStats = StatRealPath(GetRealPath(parentLocalFileHandle.Path));

        if (!localFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        if (!parentLocalFileHandle.IsSameFile(parentStats))
        {
            throw new FileNotFoundException(parentFileHandle);
        }

        var realPath = GetRealPath(localFileHandle.Path);

        if (stats.Type.HasFlag(FileType.Directory))
        {
            if (recursive)
            {
                Directory.Delete(realPath, true);
            }
            else
            {
                throw new FileIsADirectoryException(fileHandle);
            }
        }
        else
        {
            File.Delete(realPath);
        }

        await IndexDeletedFile(localFileHandle.Path, fileHandle);
    }
Example #8
0
    public override async ValueTask <IEnumerable <Dirent> > ReadDirectory(FileHandle fileHandle)
    {
        var localFileHandle = new LocalFileHandle(fileHandle);
        var realPath        = GetRealPath(localFileHandle.Path);
        var directoryInfo   = new DirectoryInfo(realPath);

        if (!directoryInfo.Exists)
        {
            var fileType = GetFileType(realPath);

            if (fileType != null && !fileType.Value.HasFlag(FileType.Directory))
            {
                throw new FileNotADirectoryException(fileHandle);
            }

            throw new FileNotFoundException(fileHandle);
        }

        var stats = GetFileStatFromFileSystemInfo(directoryInfo);

        if (!localFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        var result = directoryInfo.EnumerateFileSystemInfos().Select(info =>
        {
            var entryLocalFileHandle = new LocalFileHandle(
                PathLib.Join(localFileHandle.Path, info.Name),
                GetFileStatFromFileSystemInfo(info));
            return(new Dirent(
                       info.Name,
                       entryLocalFileHandle.FileHandle,
                       GetFileStatFromFileSystemInfo(info)));
        }).ToImmutableArray();

        await IndexDirectory(localFileHandle.Path, fileHandle, result);

        return(result);
    }
Example #9
0
    public override async ValueTask <ReadOnlyMemory <byte> > ReadFile(FileHandle fileHandle)
    {
        var localFileHandle = new LocalFileHandle(fileHandle);
        var stats           = StatRealPath(GetRealPath(localFileHandle.Path));

        if (!localFileHandle.IsSameFile(stats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        if (stats.Type.HasFlag(FileType.Directory))
        {
            throw new FileIsADirectoryException(fileHandle);
        }

        var realPath = GetRealPath(localFileHandle.Path);

        await IndexFile(localFileHandle.Path, fileHandle, stats);

        var result = await File.ReadAllBytesAsync(realPath);

        return(result);
    }
Example #10
0
    public override async ValueTask <FileHandle> CreateFile(FileHandle parentFileHandle, string name, ReadOnlyMemory <byte> content)
    {
        var parentLocalFileHandle = new LocalFileHandle(parentFileHandle);
        var parentFilePath        = parentLocalFileHandle.Path;
        var filePath    = PathLib.Join(parentFilePath, name);
        var parentStats = StatRealPath(GetRealPath(parentFilePath));

        if (!parentLocalFileHandle.IsSameFile(parentStats))
        {
            throw new FileNotFoundException(parentFileHandle);
        }

        if (!parentStats.Type.HasFlag(FileType.Directory))
        {
            throw new FileNotADirectoryException(parentFileHandle);
        }

        var realPath = GetRealPath(filePath);

        if (GetFileType(realPath) != null)
        {
            throw new FileExistsException($"The file '{filePath}' existed.");
        }

        await using var fileStream = File.OpenWrite(realPath);
        await fileStream.WriteAsync(content);

        await fileStream.FlushAsync();

        var newStats      = StatRealPath(realPath);
        var newFileHandle = new LocalFileHandle(filePath, newStats).FileHandle;

        await IndexFile(parentFilePath, newFileHandle, newStats);

        return(newFileHandle);
    }
Example #11
0
    public override async ValueTask <FileHandle> Rename(
        FileHandle fileHandle,
        FileHandle oldParentFileHandle,
        string oldName,
        FileHandle newParentFileHandle,
        string newName)
    {
        var localFileHandle          = new LocalFileHandle(fileHandle);
        var oldParentLocalFileHandle = new LocalFileHandle(oldParentFileHandle);
        var newParentLocalFileHandle = new LocalFileHandle(newParentFileHandle);

        // check
        if (localFileHandle.Path != PathLib.Join(oldParentLocalFileHandle.Path, oldName))
        {
            throw new FileNotFoundException("File handles are inconsistent.");
        }

        var oldRealPath    = GetRealPath(localFileHandle.Path);
        var oldFileStats   = StatRealPath(oldRealPath);
        var oldParentStats = StatRealPath(GetRealPath(oldParentLocalFileHandle.Path));
        var newParentStats = StatRealPath(GetRealPath(newParentLocalFileHandle.Path));

        if (!localFileHandle.IsSameFile(oldFileStats))
        {
            throw new FileNotFoundException(fileHandle);
        }

        if (!oldParentLocalFileHandle.IsSameFile(oldParentStats))
        {
            throw new FileNotFoundException(oldParentFileHandle);
        }

        if (!newParentLocalFileHandle.IsSameFile(newParentStats))
        {
            throw new FileNotFoundException(newParentFileHandle);
        }

        if (!newParentStats.Type.HasFlag(FileType.Directory))
        {
            throw new FileNotADirectoryException(newParentFileHandle);
        }

        var newPath     = PathLib.Join(newParentLocalFileHandle.Path, newName);
        var newRealPath = GetRealPath(newPath);

        if (GetFileType(newRealPath) != null)
        {
            throw new FileExistsException($"The file '{newPath}' existed.");
        }

        if (oldFileStats.Type.HasFlag(FileType.Directory))
        {
            Directory.Move(oldRealPath, newRealPath);
        }
        else
        {
            File.Move(oldRealPath, newRealPath, false);
        }

        var newFileStats = StatRealPath(newRealPath);

        await IndexDeletedFile(localFileHandle.Path, fileHandle);

        var newFileHandle = new LocalFileHandle(newPath, newFileStats).FileHandle;

        await IndexFile(newPath, new LocalFileHandle(newPath, newFileStats).FileHandle, newFileStats);

        return(newFileHandle);
    }