]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/scripts/tahoe_cp.py
fixed wrong index in tahoe cp --verbose, as explained in #1805
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_cp.py
index 59c35445d1be806d0621e5f4dcdd8a9da8e46dad..f2f8c92243d4d7758abfb56aa08caef3efd18738 100644 (file)
@@ -8,25 +8,12 @@ from allmydata.scripts.common import get_alias, escape_path, \
                                      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))
@@ -64,7 +51,7 @@ def mkdir(targeturl):
 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):
@@ -81,7 +68,7 @@ class LocalFileSource:
         return True
 
     def open(self, caps_only):
-        return open_unicode(self.pathname, "rb")
+        return open(os.path.expanduser(self.pathname), "rb")
 
 
 class LocalFileTarget:
@@ -90,7 +77,7 @@ class LocalFileTarget:
         self.pathname = pathname
 
     def put_file(self, inf):
-        _put_local_file(self.pathname, inf)
+        fileutil.put_file(self.pathname, inf)
 
 
 class LocalMissingTarget:
@@ -99,7 +86,7 @@ class LocalMissingTarget:
         self.pathname = pathname
 
     def put_file(self, inf):
-        _put_local_file(self.pathname, inf)
+        fileutil.put_file(self.pathname, inf)
 
 
 class LocalDirectorySource:
@@ -116,7 +103,7 @@ 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)
@@ -144,7 +131,7 @@ class LocalDirectoryTarget:
         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):
@@ -168,7 +155,7 @@ class LocalDirectoryTarget:
     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
@@ -252,7 +239,7 @@ class TahoeDirectorySource:
             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"))
@@ -346,7 +333,7 @@ class TahoeDirectoryTarget:
             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"))
@@ -399,11 +386,20 @@ class TahoeDirectoryTarget:
         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
@@ -506,7 +502,7 @@ class Copier:
             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):