d.addCallback(lambda res: deleter.old_child)
return d
- def create_empty_directory(self, name, overwrite=True):
- """I create and attach an empty directory at the given name. I return
- a Deferred that fires (with the new directory node) when the
- operation finishes."""
+ def create_subdirectory(self, name, initial_children={}, overwrite=True):
assert isinstance(name, unicode)
if self.is_readonly():
return defer.fail(NotMutableError())
d = self._nodemaker.create_new_mutable_directory()
+ if initial_children:
+ d.addCallback(lambda n: n.set_children(initial_children))
def _created(child):
entries = [(name, child, None)]
a = Adder(self, entries, overwrite=overwrite)
d = node.get(path[0])
def _maybe_create(f):
f.trap(NoSuchChildError)
- return node.create_empty_directory(path[0])
+ return node.create_subdirectory(path[0])
d.addErrback(_maybe_create)
d.addCallback(self._get_or_create_directories, path[1:])
return d
d = node.get(path[0])
def _maybe_create(f):
f.trap(NoSuchChildError)
- return node.create_empty_directory(path[0])
+ return node.create_subdirectory(path[0])
d.addErrback(_maybe_create)
d.addCallback(self._get_or_create_directories, path[1:])
return d
string. I raise NoSuchChildError if I do not have a child by that
name."""
- def create_empty_directory(name, overwrite=True):
- """I create and attach an empty directory at the given name. The
- child name must be a unicode string. I return a Deferred that fires
- when the operation finishes."""
+ def create_subdirectory(name, initial_children={}, overwrite=True):
+ """I create and attach a directory at the given name. The new
+ directory can be empty, or it can be populated with children
+ according to 'initial_children', which takes a dictionary in the same
+ format as set_children (i.e. mapping unicode child name to (writecap,
+ readcap, metadata) triples). The child name must be a unicode string.
+ I return a Deferred that fires (with the new directory node) when the
+ operation finishes."""
def move_child_to(current_child_name, new_parent, new_child_name=None,
overwrite=True):
self.rooturi = n.get_uri()
return n.add_file(u"good", upload.Data("small", convergence=""))
d.addCallback(_stash_root_and_create_file)
- d.addCallback(lambda ign:
- self.rootnode.create_empty_directory(u"1share"))
+ d.addCallback(lambda ign: self.rootnode.create_subdirectory(u"1share"))
d.addCallback(lambda n:
self.delete_shares_numbered(n.get_uri(), range(1,10)))
- d.addCallback(lambda ign:
- self.rootnode.create_empty_directory(u"0share"))
+ d.addCallback(lambda ign: self.rootnode.create_subdirectory(u"0share"))
d.addCallback(lambda n:
self.delete_shares_numbered(n.get_uri(), range(0,10)))
d.addCallback(lambda ign:
# now add a subdir, and a file below that, then make the subdir
# unrecoverable
- d.addCallback(lambda ign:
- self.rootnode.create_empty_directory(u"subdir"))
+ d.addCallback(lambda ign: self.rootnode.create_subdirectory(u"subdir"))
d.addCallback(_stash_uri, "subdir")
d.addCallback(lambda fn:
fn.add_file(u"subfile", upload.Data(DATA+"2", "")))
d.addCallback(lambda ignored:
self.nodes["broken"].add_file(u"large1", large1))
d.addCallback(lambda ignored:
- self.nodes["broken"].create_empty_directory(u"subdir-good"))
+ self.nodes["broken"].create_subdirectory(u"subdir-good"))
large2 = upload.Data("Lots of data\n" * 1000 + "large2" + "\n", None)
d.addCallback(lambda subdir: subdir.add_file(u"large2-good", large2))
d.addCallback(lambda ignored:
- self.nodes["broken"].create_empty_directory(u"subdir-unrecoverable"))
+ self.nodes["broken"].create_subdirectory(u"subdir-unrecoverable"))
d.addCallback(self._stash_node, "subdir-unrecoverable")
large3 = upload.Data("Lots of data\n" * 1000 + "large3" + "\n", None)
d.addCallback(lambda subdir: subdir.add_file(u"large3-good", large3))
self.root = n
return n
d.addCallback(_created_root)
- d.addCallback(lambda root: root.create_empty_directory(u"subdir"))
+ d.addCallback(lambda root: root.create_subdirectory(u"subdir"))
def _add_children(subdir_node):
self.subdir_node = subdir_node
kids = {}
d = c.create_dirnode()
def _created_root(rootnode):
self._rootnode = rootnode
- return rootnode.create_empty_directory(u"subdir")
+ return rootnode.create_subdirectory(u"subdir")
d.addCallback(_created_root)
def _created_subdir(subdir):
self._subdir = subdir
self.shouldFail(dirnode.NotMutableError, "set_uri ro", None,
ro_dn.delete, u"child")
self.shouldFail(dirnode.NotMutableError, "set_uri ro", None,
- ro_dn.create_empty_directory, u"newchild")
+ ro_dn.create_subdirectory, u"newchild")
self.shouldFail(dirnode.NotMutableError, "set_metadata_for ro", None,
ro_dn.set_metadata_for, u"child", {})
self.shouldFail(dirnode.NotMutableError, "set_uri ro", None,
# /
# /child = mutable
- d.addCallback(lambda res: n.create_empty_directory(u"subdir"))
+ d.addCallback(lambda res: n.create_subdirectory(u"subdir"))
# /
# /child = mutable
d.addCallback(lambda res:
self.shouldFail(ExistingChildError, "mkdir-no",
"child 'subdir' already exists",
- n.create_empty_directory, u"subdir",
+ n.create_subdirectory, u"subdir",
overwrite=False))
d.addCallback(lambda res: n.list())
d.addCallback(_check_manifest)
def _add_subsubdir(res):
- return self.subdir.create_empty_directory(u"subsubdir")
+ return self.subdir.create_subdirectory(u"subsubdir")
d.addCallback(_add_subsubdir)
# /
# /child = mutable
(metadata['key'] == "value"), metadata))
d.addCallback(lambda res: n.delete(u"newfile-metadata"))
- d.addCallback(lambda res: n.create_empty_directory(u"subdir2"))
+ d.addCallback(lambda res: n.create_subdirectory(u"subdir2"))
def _created2(subdir2):
self.subdir2 = subdir2
# put something in the way, to make sure it gets overwritten
d.addErrback(self.explain_error)
return d
+ def test_create_subdirectory(self):
+ self.basedir = "dirnode/Dirnode/test_create_subdirectory"
+ self.set_up_grid()
+ c = self.g.clients[0]
+
+ d = c.create_dirnode()
+ def _then(n):
+ # /
+ self.rootnode = n
+ fake_file_uri = make_mutable_file_uri()
+ other_file_uri = make_mutable_file_uri()
+ md = {"metakey": "metavalue"}
+ kids = {u"kid1": (fake_file_uri, fake_file_uri),
+ u"kid2": (other_file_uri, other_file_uri, md),
+ }
+ d = n.create_subdirectory(u"subdir", kids)
+ def _check(sub):
+ d = n.get_child_at_path(u"subdir")
+ d.addCallback(lambda sub2: self.failUnlessEqual(sub2.get_uri(),
+ sub.get_uri()))
+ d.addCallback(lambda ign: sub.list())
+ return d
+ d.addCallback(_check)
+ def _check_kids(kids2):
+ self.failUnlessEqual(sorted(kids.keys()), sorted(kids2.keys()))
+ self.failUnlessEqual(kids2[u"kid2"][1]["metakey"], "metavalue")
+ d.addCallback(_check_kids)
+ return d
+ d.addCallback(_then)
+ return d
+
class Packing(unittest.TestCase):
# This is a base32-encoded representation of the directory tree
# root/file1
d.addCallback(lambda res:
root_node.add_file(u'file2', upload.Data("Sekrit Codes", None)))
d.addCallback(lambda res:
- root_node.create_empty_directory(u"dir1"))
+ root_node.create_subdirectory(u"dir1"))
d.addCallback(lambda res: root_node)
return d
d = root_node.set_node(u'file1', filenode)
# We've overwritten file1. Let's try it with a directory
d.addCallback(lambda res:
- root_node.create_empty_directory(u'dir2'))
+ root_node.create_subdirectory(u'dir2'))
d.addCallback(lambda res:
root_node.set_node(u'dir2', filenode))
# We try overwriting a file with a child while also specifying
self._root_directory_uri = new_dirnode.get_uri()
return c0.create_node_from_uri(self._root_directory_uri)
d.addCallback(_made_root)
- d.addCallback(lambda root: root.create_empty_directory(u"subdir1"))
+ d.addCallback(lambda root: root.create_subdirectory(u"subdir1"))
def _made_subdir1(subdir1_node):
self._subdir1_node = subdir1_node
d1 = subdir1_node.add_file(u"mydata567", ut)
def _do_publish2(self, res):
ut = upload.Data(self.data, convergence=None)
- d = self._subdir1_node.create_empty_directory(u"subdir2")
+ d = self._subdir1_node.create_subdirectory(u"subdir2")
d.addCallback(lambda subdir2: subdir2.add_file(u"mydata992", ut))
return d
d.addCallback(self.log, "GOT private directory")
def _got_new_dir(privnode):
rootnode = self.clients[0].create_node_from_uri(self._root_directory_uri)
- d1 = privnode.create_empty_directory(u"personal")
+ d1 = privnode.create_subdirectory(u"personal")
d1.addCallback(self.log, "made P/personal")
d1.addCallback(lambda node: node.add_file(u"sekrit data", ut))
d1.addCallback(self.log, "made P/personal/sekrit data")
d1.addCallback(lambda res: dirnode.list())
d1.addCallback(self.log, "dirnode.list")
- d1.addCallback(lambda res: self.shouldFail2(NotMutableError, "mkdir(nope)", None, dirnode.create_empty_directory, u"nope"))
+ d1.addCallback(lambda res: self.shouldFail2(NotMutableError, "mkdir(nope)", None, dirnode.create_subdirectory, u"nope"))
d1.addCallback(self.log, "doing add_file(ro)")
ut = upload.Data("I will disappear, unrecorded and unobserved. The tragedy of my demise is made more poignant by its silence, but this beauty is not for you to ever know.", convergence="99i-p1x4-xd4-18yc-ywt-87uu-msu-zo -- completely and totally unguessable string (unless you read this)")
# unrecoverable, then see what happens
d.addCallback(lambda ign:
- self.rootnode.create_empty_directory(u"subdir"))
+ self.rootnode.create_subdirectory(u"subdir"))
d.addCallback(_stash_uri, "subdir")
d.addCallback(lambda subdir_node:
subdir_node.add_file(u"grandchild",
if should_create_intermediate_directories(req):
# create intermediate directories
if DEBUG: print " making intermediate directory"
- d = self.node.create_empty_directory(name)
+ d = self.node.create_subdirectory(name)
d.addCallback(make_handler_for,
self.client, self.node, name)
return d
if (method,t) in [ ("POST","mkdir"), ("PUT","mkdir") ]:
if DEBUG: print " making final directory"
# final directory
- d = self.node.create_empty_directory(name)
+ d = self.node.create_subdirectory(name)
d.addCallback(make_handler_for,
self.client, self.node, name)
return d
return defer.succeed(self.node.get_uri()) # TODO: urlencode
name = name.decode("utf-8")
replace = boolean_of_arg(get_arg(req, "replace", "true"))
- d = self.node.create_empty_directory(name, overwrite=replace)
+ d = self.node.create_subdirectory(name, overwrite=replace)
d.addCallback(lambda child: child.get_uri()) # TODO: urlencode
return d
d = node.get(path[0])
def _maybe_create(f):
f.trap(NoSuchChildError)
- return node.create_empty_directory(path[0])
+ return node.create_subdirectory(path[0])
d.addErrback(_maybe_create)
d.addCallback(self._get_or_create_directories, path[1:])
return d