]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/scripts/tahoe_cp.py
Avoid double-counting source files in 'tahoe cp --verbose'. fixes #1783
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_cp.py
index 5ef9de1451d33610a93973feecad4ebd815be14d..1ad460d897403bec53ebc147dc1ecc8c95336b05 100644 (file)
@@ -9,6 +9,7 @@ from allmydata.scripts.common import get_alias, escape_path, \
 from allmydata.scripts.common_http import do_http, HTTPError
 from allmydata import uri
 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
 
@@ -50,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):
@@ -102,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)
@@ -130,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):
@@ -238,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"))
@@ -332,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"))
@@ -385,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
@@ -492,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):
@@ -511,7 +521,7 @@ class Copier:
         rootcap, path = get_alias(self.aliases, destination_spec, None)
         if rootcap == DefaultAliasMarker:
             # no alias, so this is a local file
-            pathname = os.path.abspath(os.path.expanduser(path.decode('utf-8')))
+            pathname = abspath_expanduser_unicode(path.decode('utf-8'))
             if not os.path.exists(pathname):
                 t = LocalMissingTarget(pathname)
             elif os.path.isdir(pathname):
@@ -551,7 +561,7 @@ class Copier:
         rootcap, path = get_alias(self.aliases, source_spec, None)
         if rootcap == DefaultAliasMarker:
             # no alias, so this is a local file
-            pathname = os.path.abspath(os.path.expanduser(path.decode('utf-8')))
+            pathname = abspath_expanduser_unicode(path.decode('utf-8'))
             name = os.path.basename(pathname)
             if not os.path.exists(pathname):
                 raise MissingSourceError(source_spec)
@@ -642,7 +652,6 @@ class Copier:
 
         for (name,s) in source_files:
             self.attach_to_target(s, name, target)
-            self.files_to_copy += 1
 
         for source in source_dirs:
             self.assign_targets(source, target)