From 144353bf0622ce0a1e021e6e4d1fe6963122fd17 Mon Sep 17 00:00:00 2001 From: Chris Foster Date: Tue, 26 Apr 2022 20:17:55 +1000 Subject: [PATCH] fixup! Big refactor of newfile() / newdir() + BlobTree API improvements --- src/BlobTree.jl | 14 +++++--------- src/filesystem.jl | 14 +++++++++++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/BlobTree.jl b/src/BlobTree.jl index ffa242d..be28d89 100644 --- a/src/BlobTree.jl +++ b/src/BlobTree.jl @@ -392,12 +392,8 @@ end function _check_new_item(tree, path, overwrite) _check_writeable(tree) - if haskey(tree, path) - if overwrite - delete!(tree, path) - else - error("Overwriting a path $path which already exists requires the keyword `overwrite=true`") - end + if haskey(tree, path) && !overwrite + error("Overwriting a path $path which already exists requires the keyword `overwrite=true`") end end @@ -417,7 +413,7 @@ collection. function newdir(tree::BlobTree, path::RelPath; overwrite=false) _check_new_item(tree, path, overwrite) p = joinpath(tree.path, RelPath(path)) - newdir(tree.root, p) + newdir(tree.root, p; overwrite=overwrite) return BlobTree(tree.root, p) end @@ -445,14 +441,14 @@ end function newfile(tree::BlobTree, path::RelPath; overwrite=false) _check_new_item(tree, path, overwrite) p = joinpath(tree.path, path) - newfile(tree.root, p) + newfile(tree.root, p; overwrite=overwrite) return Blob(tree.root, p) end function newfile(func::Function, tree::BlobTree, path::RelPath; overwrite=false) _check_new_item(tree, path, overwrite) p = joinpath(tree.path, path) - newfile(func, tree.root, p) + newfile(func, tree.root, p; overwrite=overwrite) return Blob(tree.root, p) end diff --git a/src/filesystem.jl b/src/filesystem.jl index 3cf2f85..e62c1fc 100644 --- a/src/filesystem.jl +++ b/src/filesystem.jl @@ -154,8 +154,12 @@ function newdir() return BlobTree(TempFilesystemRoot(path)) end -function newdir(root::AbstractFileSystemRoot, path::RelPath) - mkpath(sys_abspath(root, path)) +function newdir(root::AbstractFileSystemRoot, path::RelPath; overwrite=false) + p = sys_abspath(root, path) + if overwrite + rm(p, force=true, recursive=true) + end + mkpath(p) end @@ -174,12 +178,16 @@ function newfile(func=nothing) return Blob(TempFilesystemRoot(path)) end -function newfile(f::Function, root::AbstractFileSystemRoot, path::RelPath) +function newfile(f::Function, root::AbstractFileSystemRoot, path::RelPath; kws...) p = sys_abspath(root, path) mkpath(dirname(p)) open(f, p, write=true) end +function newfile(root::AbstractFileSystemRoot, path::RelPath; kws...) + newfile(io->nothing, root, path; kws...) +end + #------------------------------------------------------------------------------- # Deprecated newdir() and newfile() variants function newdir(ctx::AbstractFileSystemRoot)