From: Daira Hopwood Date: Mon, 25 Aug 2014 17:37:31 +0000 (+0100) Subject: Use "long" paths prefixed with \\?\ on Windows. refs #2235 X-Git-Url: https://git.rkrishnan.org/?a=commitdiff_plain;h=7b8a6cdd4e24e76102d74eeecce66118055de0d0;p=tahoe-lafs%2Ftahoe-lafs.git Use "long" paths prefixed with \\?\ on Windows. refs #2235 Signed-off-by: Daira Hopwood --- diff --git a/src/allmydata/scripts/debug.py b/src/allmydata/scripts/debug.py index 8ef82eca..1bba9d5f 100644 --- a/src/allmydata/scripts/debug.py +++ b/src/allmydata/scripts/debug.py @@ -650,7 +650,7 @@ def find_shares(options): out = options.stdout sharedir = storage_index_to_dir(si_a2b(options.si_s)) for d in options.nodedirs: - d = os.path.join(d, "storage/shares", sharedir) + d = os.path.join(d, "storage", "shares", sharedir) if os.path.exists(d): for shnum in listdir_unicode(d): print >>out, os.path.join(d, shnum) @@ -832,7 +832,7 @@ def catalog_shares(options): err = options.stderr now = time.time() for d in options.nodedirs: - d = os.path.join(d, "storage/shares") + d = os.path.join(d, "storage", "shares") try: abbrevs = listdir_unicode(d) except EnvironmentError: diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py index bc40dfab..3f2da593 100644 --- a/src/allmydata/test/test_cli.py +++ b/src/allmydata/test/test_cli.py @@ -3801,7 +3801,7 @@ class Webopen(GridTestMixin, CLITestMixin, unittest.TestCase): raise return d -class Options(unittest.TestCase): +class Options(ReallyEqualMixin, unittest.TestCase): # this test case only looks at argument-processing and simple stuff. def parse(self, args, stdout=None): @@ -3883,14 +3883,14 @@ class Options(unittest.TestCase): # option after, or a basedir argument after, but none in the wrong # place, and not more than one of the three. o = self.parse(["start"]) - self.failUnlessEqual(o["basedir"], os.path.join(os.path.expanduser("~"), - ".tahoe")) + self.failUnlessReallyEqual(o["basedir"], os.path.join(fileutil.abspath_expanduser_unicode(u"~"), + u".tahoe")) o = self.parse(["start", "here"]) - self.failUnlessEqual(o["basedir"], os.path.abspath("here")) + self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"here")) o = self.parse(["start", "--basedir", "there"]) - self.failUnlessEqual(o["basedir"], os.path.abspath("there")) + self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there")) o = self.parse(["--node-directory", "there", "start"]) - self.failUnlessEqual(o["basedir"], os.path.abspath("there")) + self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there")) o = self.parse(["start", "here", "--nodaemon"]) self.failUnlessEqual(o["basedir"], os.path.abspath("here")) diff --git a/src/allmydata/test/test_client.py b/src/allmydata/test/test_client.py index 531215f6..f5b0526b 100644 --- a/src/allmydata/test/test_client.py +++ b/src/allmydata/test/test_client.py @@ -1,4 +1,4 @@ -import os +import os, sys from twisted.trial import unittest from twisted.application import service @@ -68,10 +68,11 @@ class Basic(testutil.ReallyEqualMixin, unittest.TestCase): fileutil.write(os.path.join(basedir, "debug_discard_storage"), "") e = self.failUnlessRaises(OldConfigError, client.Client, basedir) - self.failUnlessIn(os.path.abspath(os.path.join(basedir, "introducer.furl")), e.args[0]) - self.failUnlessIn(os.path.abspath(os.path.join(basedir, "no_storage")), e.args[0]) - self.failUnlessIn(os.path.abspath(os.path.join(basedir, "readonly_storage")), e.args[0]) - self.failUnlessIn(os.path.abspath(os.path.join(basedir, "debug_discard_storage")), e.args[0]) + abs_basedir = fileutil.abspath_expanduser_unicode(unicode(basedir)).encode(sys.getfilesystemencoding()) + self.failUnlessIn(os.path.join(abs_basedir, "introducer.furl"), e.args[0]) + self.failUnlessIn(os.path.join(abs_basedir, "no_storage"), e.args[0]) + self.failUnlessIn(os.path.join(abs_basedir, "readonly_storage"), e.args[0]) + self.failUnlessIn(os.path.join(abs_basedir, "debug_discard_storage"), e.args[0]) for oldfile in ['introducer.furl', 'no_storage', 'readonly_storage', 'debug_discard_storage']: diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index 20f157b9..8eb7adb8 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -15,6 +15,8 @@ from allmydata.util import limiter, time_format, pollmixin, cachedir from allmydata.util import statistics, dictutil, pipeline from allmydata.util import log as tahoe_log from allmydata.util.spans import Spans, overlap, DataSpans +from allmydata.test.common_util import ReallyEqualMixin + class Base32(unittest.TestCase): def test_b2a_matches_Pythons(self): @@ -370,7 +372,7 @@ class Asserts(unittest.TestCase): m = self.should_assert(f, False, othermsg="message2") self.failUnlessEqual("postcondition: othermsg: 'message2' ", m) -class FileUtil(unittest.TestCase): +class FileUtil(ReallyEqualMixin, unittest.TestCase): def mkdir(self, basedir, path, mode=0777): fn = os.path.join(basedir, path) fileutil.make_dirs(fn, mode) @@ -472,7 +474,16 @@ class FileUtil(unittest.TestCase): abspath_cwd = fileutil.abspath_expanduser_unicode(u".") self.failUnless(isinstance(saved_cwd, unicode), saved_cwd) self.failUnless(isinstance(abspath_cwd, unicode), abspath_cwd) - self.failUnlessEqual(abspath_cwd, saved_cwd) + if sys.platform == "win32": + self.failUnlessReallyEqual(abspath_cwd, fileutil.to_windows_long_path(saved_cwd)) + else: + self.failUnlessReallyEqual(abspath_cwd, saved_cwd) + + self.failUnlessReallyEqual(fileutil.to_windows_long_path(u"\\\\?\\foo"), u"\\\\?\\foo") + self.failUnlessReallyEqual(fileutil.to_windows_long_path(u"\\\\.\\foo"), u"\\\\.\\foo") + self.failUnlessReallyEqual(fileutil.to_windows_long_path(u"\\\\server\\foo"), u"\\\\?\\UNC\\server\\foo") + self.failUnlessReallyEqual(fileutil.to_windows_long_path(u"C:\\foo"), u"\\\\?\\C:\\foo") + self.failUnlessReallyEqual(fileutil.to_windows_long_path(u"C:\\foo/bar"), u"\\\\?\\C:\\foo\\bar") # adapted from @@ -496,6 +507,20 @@ class FileUtil(unittest.TestCase): finally: os.chdir(saved_cwd) + def test_create_long_filename(self): + workdir = u"test_create_long_filename" + fileutil.make_dirs(workdir) + long_filename = fileutil.abspath_expanduser_unicode(os.path.join(workdir, u'x'*255)) + def _cleanup(): + fileutil.remove(long_filename) + self.addCleanup(_cleanup) + + fileutil.write(long_filename, "test") + self.failUnless(os.path.exists(long_filename)) + self.failUnlessEqual(fileutil.read(long_filename), "test") + _cleanup() + self.failIf(os.path.exists(long_filename)) + def test_disk_stats(self): avail = fileutil.get_available_space('.', 2**14) if avail == 0: diff --git a/src/allmydata/util/fileutil.py b/src/allmydata/util/fileutil.py index 9446a3f6..7f4f2975 100644 --- a/src/allmydata/util/fileutil.py +++ b/src/allmydata/util/fileutil.py @@ -305,7 +305,25 @@ def abspath_expanduser_unicode(path): # We won't hit because # there is always at least one Unicode path component. - return os.path.normpath(path) + path = os.path.normpath(path) + + if sys.platform == "win32": + path = to_windows_long_path(path) + + return path + +def to_windows_long_path(path): + # '/' is normally a perfectly valid path component separator in Windows. + # However, when using the "\\?\" syntax it is not recognized, so we + # replace it with '\' here. + path = path.replace(u"/", u"\\") + + if path.startswith(u"\\\\?\\") or path.startswith(u"\\\\.\\"): + return path + elif path.startswith(u"\\\\"): + return u"\\\\?\\UNC\\" + path[2 :] + else: + return u"\\\\?\\" + path have_GetDiskFreeSpaceExW = False