DefaultAliasMarker, TahoeError
from allmydata.scripts.common_http import do_http, HTTPError
from allmydata import uri
-from allmydata.util.stringutils import unicode_to_url, listdir_unicode, open_unicode, \
- abspath_expanduser_unicode, quote_output, to_str
+from allmydata.util import fileutil
+from allmydata.util.fileutil import abspath_expanduser_unicode
+from allmydata.util.encodingutil import unicode_to_url, listdir_unicode, quote_output, to_str
from allmydata.util.assertutil import precondition
-def _put_local_file(pathname, inf):
- # TODO: create temporary file and move into place?
- # TODO: move this to fileutil.
- outf = open_unicode(pathname, "wb")
- try:
- while True:
- data = inf.read(32768)
- if not data:
- break
- outf.write(data)
- finally:
- outf.close()
-
-
class MissingSourceError(TahoeError):
def __init__(self, name):
TahoeError.__init__(self, "No such file or directory %s" % quote_output(name))
def make_tahoe_subdirectory(nodeurl, parent_writecap, name):
url = nodeurl + "/".join(["uri",
urllib.quote(parent_writecap),
- urllib.quote(name),
+ urllib.quote(unicode_to_url(name)),
]) + "?t=mkdir"
resp = do_http("POST", url)
if resp.status in (200, 201):
return True
def open(self, caps_only):
- return open_unicode(self.pathname, "rb")
+ return open(os.path.expanduser(self.pathname), "rb")
class LocalFileTarget:
self.pathname = pathname
def put_file(self, inf):
- _put_local_file(self.pathname, inf)
+ fileutil.put_file(self.pathname, inf)
class LocalMissingTarget:
self.pathname = pathname
def put_file(self, inf):
- _put_local_file(self.pathname, inf)
+ fileutil.put_file(self.pathname, inf)
class LocalDirectorySource:
self.children = {}
children = listdir_unicode(self.pathname)
for i,n in enumerate(children):
- self.progressfunc("examining %d of %d" % (i, len(children)))
+ self.progressfunc("examining %d of %d" % (i+1, len(children)))
pn = os.path.join(self.pathname, n)
if os.path.isdir(pn):
child = LocalDirectorySource(self.progressfunc, pn)
self.children = {}
children = listdir_unicode(self.pathname)
for i,n in enumerate(children):
- self.progressfunc("examining %d of %d" % (i, len(children)))
+ self.progressfunc("examining %d of %d" % (i+1, len(children)))
n = unicode(n)
pn = os.path.join(self.pathname, n)
if os.path.isdir(pn):
def put_file(self, name, inf):
precondition(isinstance(name, unicode), name)
pathname = os.path.join(self.pathname, name)
- _put_local_file(pathname, inf)
+ fileutil.put_file(pathname, inf)
def set_children(self):
pass
return
self.children = {}
for i,(name, data) in enumerate(self.children_d.items()):
- self.progressfunc("examining %d of %d" % (i, len(self.children_d)))
+ self.progressfunc("examining %d of %d" % (i+1, len(self.children_d)))
if data[0] == "filenode":
mutable = data[1].get("mutable", False)
writecap = to_str(data[1].get("rw_uri"))
return
self.children = {}
for i,(name, data) in enumerate(self.children_d.items()):
- self.progressfunc("examining %d of %d" % (i, len(self.children_d)))
+ self.progressfunc("examining %d of %d" % (i+1, len(self.children_d)))
if data[0] == "filenode":
mutable = data[1].get("mutable", False)
writecap = to_str(data[1].get("rw_uri"))
url = self.nodeurl + "uri"
if not hasattr(inf, "seek"):
inf = inf.read()
- filecap = PUT(url, inf)
- # TODO: this always creates immutable files. We might want an option
- # to always create mutable files, or to copy mutable files into new
- # mutable files.
- self.new_children[name] = filecap
+
+ if self.children is None:
+ self.populate(False)
+
+ # Check to see if we already have a mutable file by this name.
+ # If so, overwrite that file in place.
+ if name in self.children and self.children[name].mutable:
+ self.children[name].put_file(inf)
+ else:
+ filecap = PUT(url, inf)
+ # TODO: this always creates immutable files. We might want an option
+ # to always create mutable files, or to copy mutable files into new
+ # mutable files.
+ self.new_children[name] = filecap
def put_uri(self, name, filecap):
self.new_children[name] = filecap
return self.copy_file(source, target)
if isinstance(target, (LocalDirectoryTarget, TahoeDirectoryTarget)):
- # We're copying to an existing directory -- make sure that we
+ # We're copying to an existing directory -- make sure that we
# have target names for everything
for (name, source) in sources:
if name is None and isinstance(source, TahoeFileSource):