[Zope] LocalFS cross platform bug

Dan L. Pierson dan@sol.control.com
Thu, 15 Jun 2000 15:58:33 -0400 (EDT)


If you upload from a browser on a Windows machine to a server running
on Linux, the id (and thus the file name) of the new file will be the
complete Windows path instead of just the file name.  This happens
because os.basename on Linux doesn't know how to handle Windows
pathname syntax.

Here's a rather crude diff -u patch that works for me:

--- LocalFS.py-orig     Fri Apr 14 00:36:06 2000
+++ LocalFS.py  Thu Jun 15 15:36:19 2000
@@ -625,10 +625,16 @@
                     "to this directory.<p>")
             else: raise
 
+    def _canonicalize_pathname(self, path):
+        if (string.count(path, '\\')):
+            if (len(path) > 1 and path[0] in string.letters and path[1] == ':'):
+                path = path[2:]
+            path = string.translate(path, string.maketrans('\\', '/'))
+
     def manage_upload(self, file, REQUEST=None):
         """Upload a file to the local file system. The 'file' parameter
         is a FileUpload instance representing the uploaded file."""
-        id = os.path.basename(file.filename)
+        id = os.path.basename(self._canonicalize_pathname(file.filename))
         path = self._getpath(id)
         data = file.read(_test_read)
         if (find_binary(data) >= 0):