예제 #1
0
    private async ValueTask <long> CreateDirectory(IDbTransaction transaction, string path, FileChangeEventBuilder eventBuilder)
    {
        var  pathPart    = PathLib.Split(path);
        long?directoryId = null;
        int  i;

        for (i = pathPart.Length; i >= 0; i--)
        {
            var currentPath = "/" + string.Join('/', pathPart.Take(i).ToArray());
            var directory   = await _databaseTable.SelectByPathAsync(transaction, currentPath);

            if (directory == null)
            {
                continue;
            }

            if (directory.IsDirectory)
            {
                directoryId = directory.Id;
                break;
            }

            await Delete(transaction, directory, eventBuilder);
        }

        for (i++; i <= pathPart.Length; i++)
        {
            var currentPath = "/" + string.Join('/', pathPart.Take(i).ToArray());
            directoryId = await _databaseTable.InsertAsync(transaction, currentPath, directoryId, true, null, null);
        }

        return(directoryId !.Value);
    }
예제 #2
0
    public static async ValueTask <FileHandle> Mkdirp(this IFileService fs, Url url)
    {
        var pathParts = PathLib.Split(url.Path);

        var currentUrl = url with {
            Path = "/"
        };
        var currentFileHandle = await fs.CreateFileHandle(currentUrl);

        foreach (var part in pathParts)
        {
            currentUrl = currentUrl with {
                Path = currentUrl.Path == "/" ? currentUrl.Path + part : currentUrl.Path + '/' + part
            };

            try
            {
                currentFileHandle = await fs.CreateDirectory(currentFileHandle, part);
            }
            catch (FileExistsException)
            {
                currentFileHandle = await fs.CreateFileHandle(currentUrl);

                if (!(await fs.Stat(currentFileHandle)).Type.HasFlag(FileType.Directory))
                {
                    throw new FileNotADirectoryException(currentUrl);
                }
            }
        }

        return(currentFileHandle);
    }