From 14ee763c542b61c5965aac3021fbdd7c12683b5b Mon Sep 17 00:00:00 2001
From: Francois Deppierraz <francois@ctrlaltdel.ch>
Date: Sat, 16 Oct 2010 11:09:22 +0200
Subject: [PATCH] tahoe_cp.py: Don't call urllib.quote with an Unicode
 argument, fix #1224 tahoe_backup.py: Fix another (potential) occurrence of
 calling urllib.quote() with an Unicode parameter

---
 src/allmydata/scripts/tahoe_backup.py |  2 +-
 src/allmydata/scripts/tahoe_cp.py     |  2 +-
 src/allmydata/test/test_cli.py        | 33 +++++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/allmydata/scripts/tahoe_backup.py b/src/allmydata/scripts/tahoe_backup.py
index badc35e3..26279e0c 100644
--- a/src/allmydata/scripts/tahoe_backup.py
+++ b/src/allmydata/scripts/tahoe_backup.py
@@ -50,7 +50,7 @@ def mkdir(contents, options):
 
 def put_child(dirurl, childname, childcap):
     assert dirurl[-1] == "/"
-    url = dirurl + urllib.quote(childname) + "?t=uri"
+    url = dirurl + urllib.quote(unicode_to_url(childname)) + "?t=uri"
     resp = do_http("PUT", url, childcap)
     if resp.status not in (200, 201):
         raise HTTPError("Error during put_child", resp)
diff --git a/src/allmydata/scripts/tahoe_cp.py b/src/allmydata/scripts/tahoe_cp.py
index 4c41db04..9f910042 100644
--- a/src/allmydata/scripts/tahoe_cp.py
+++ b/src/allmydata/scripts/tahoe_cp.py
@@ -51,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):
diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py
index 24531260..d9e34c2a 100644
--- a/src/allmydata/test/test_cli.py
+++ b/src/allmydata/test/test_cli.py
@@ -1487,6 +1487,39 @@ class Cp(GridTestMixin, CLITestMixin, unittest.TestCase):
         d.addCallback(_check)
         return d
 
+    def test_unicode_dirnames(self):
+        self.basedir = "cli/Cp/unicode_dirnames"
+
+        fn1 = os.path.join(unicode(self.basedir), u"\u00C4rtonwall")
+        try:
+            fn1_arg = fn1.encode(get_argv_encoding())
+            artonwall_arg = u"\u00C4rtonwall".encode(get_argv_encoding())
+        except UnicodeEncodeError:
+            raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.")
+
+        self.skip_if_cannot_represent_filename(fn1)
+
+        self.set_up_grid()
+
+        d = self.do_cli("create-alias", "tahoe")
+        d.addCallback(lambda res: self.do_cli("mkdir", "tahoe:test/" + artonwall_arg))
+        d.addCallback(lambda res: self.do_cli("cp", "-r", "tahoe:test", "tahoe:test2"))
+        d.addCallback(lambda res: self.do_cli("ls", "tahoe:test2"))
+        def _check((rc, out, err)):
+            try:
+                unicode_to_output(u"\u00C4rtonwall")
+            except UnicodeEncodeError:
+                self.failUnlessReallyEqual(rc, 1)
+                self.failUnlessReallyEqual(out, "")
+                self.failUnlessIn(quote_output(u"\u00C4rtonwall"), err)
+                self.failUnlessIn("files whose names could not be converted", err)
+            else:
+                self.failUnlessReallyEqual(rc, 0)
+                self.failUnlessReallyEqual(out.decode(get_output_encoding()), u"\u00C4rtonwall\n")
+                self.failUnlessReallyEqual(err, "")
+        d.addCallback(_check)
+
+        return d
 
 class Backup(GridTestMixin, CLITestMixin, StallMixin, unittest.TestCase):
 
-- 
2.45.2