[Zope-Checkins] CVS: Zope3/lib/python/Zope/Server/VFS - IReadFileSystem.py:1.1.2.4 IWriteFileSystem.py:1.1.2.4 OSFileSystem.py:1.1.2.12

Shane Hathaway shane@cvs.zope.org
Mon, 8 Apr 2002 16:21:38 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Server/VFS
In directory cvs.zope.org:/tmp/cvs-serv12552

Modified Files:
      Tag: Zope3-Server-Branch
	IReadFileSystem.py IWriteFileSystem.py OSFileSystem.py 
Log Message:
Removed the open() method and replaced it with readfile(), writefile(), and
check_writable().  These are more transaction-friendly.


=== Zope3/lib/python/Zope/Server/VFS/IReadFileSystem.py 1.1.2.3 => 1.1.2.4 ===
         """
 
-    def open(path, mode):
-        """Return an open file object.
+    def readfile(path, mode, outstream, start=0, end=-1):
+        """Outputs the file at path to a stream.
         """
-        return file
 
     def stat(path):
         """Return the equivalent of os.stat() on the given path:


=== Zope3/lib/python/Zope/Server/VFS/IWriteFileSystem.py 1.1.2.3 => 1.1.2.4 ===
         """
 
-    def unlink(path):
-        """Remove file. Same as remove.
+    def writefile(path, mode, instream, start=0):
+        """Write data to a file.
         """
 
-    def write(fd, data):
-        """Write the data to a file descriptor.
+    def check_writable(path, mode):
+        """Ensures a path is writable.  Throws an IOError if not."""
 
-           Returns the number of bytes written.
-        """
-
-        return int


=== Zope3/lib/python/Zope/Server/VFS/OSFileSystem.py 1.1.2.11 => 1.1.2.12 ===
     path_module = os.path
 
+    copy_bytes = 65536
+
 
     def __init__ (self, root):
         self.root = root
@@ -87,10 +89,22 @@
         return unix_longify(path, stat_info)
 
 
-    def open(self, path, mode):
+    def readfile(self, path, mode, outstream, start=0, end=-1):
         'See Zope.Server.VFS.IReadFileSystem.IReadFileSystem'
         p = self.translate(path)
-        return open(p, mode)
+        instream = open(p, mode)
+        if start:
+            instream.seek(start)
+        pos = start
+        while end < 0 or pos < end:
+            toread = self.copy_bytes
+            if end >= 0:
+                toread = min(toread, end - pos)
+            data = instream.read(toread)
+            if not data:
+                break
+            pos += len(data)
+            outstream.write(data)
 
 
     def stat(self, path):
@@ -161,15 +175,30 @@
         return os.symlink(src, dst)
 
 
-    def unlink(self, path):
+    def writefile(self, path, mode, instream, start=0):
         'See Zope.Server.VFS.IWriteFileSystem.IWriteFileSystem'
         p = self.translate(path)
-        return os.unlink(p)
-
+        outstream = open(p, mode)
+        if start:
+            outstream.seek(start)
+        while 1:
+            data = instream.read(self.copy_bytes)
+            if not data:
+                break
+            outstream.write(data)
 
-    def write(self, fd, data):
+    def check_writable(self, path, mode):
         'See Zope.Server.VFS.IWriteFileSystem.IWriteFileSystem'
-        return os.write(fd, data)
+        p = self.translate(path)
+        if os.path.exists(p):
+            remove = 0
+        else:
+            remove = 1
+        f = open(p, 'a')  # append mode
+        f.close()
+        if remove:
+            os.remove(p)
+        
 
     #
     ############################################################