コード例 #1
0
ファイル: HintFileTracker.cs プロジェクト: EYHN/Anything
    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);
    }