Permalink
Browse files

Use fstat() instead of getsize() in FilesystemReader.size.

  • Loading branch information...
1 parent f169188 commit b13e7d6a7cc87b9435fc138c04c76aad76fc3718 @allenap committed Jul 5, 2012
Showing with 17 additions and 1 deletion.
  1. +5 −1 tftp/backend.py
  2. +12 −0 tftp/test/test_backend.py
View
@@ -1,6 +1,7 @@
'''
@author: shylent
'''
+from os import fstat
from tftp.errors import Unsupported, FileExists, AccessViolation, FileNotFound
from twisted.python.filepath import FilePath, InsecurePath
import shutil
@@ -141,7 +142,10 @@ def size(self):
@see: L{IReader.size}
"""
- return self.file_path.getsize()
+ if self.file_obj.closed:
+ return None
+ else:
+ return fstat(self.file_obj.fileno()).st_size
def read(self, size):
"""
@@ -77,6 +77,18 @@ def test_size(self):
r = FilesystemReader(self.temp_dir.child('foo'))
self.assertEqual(len(self.test_data), r.size)
+ def test_size_when_reader_finished(self):
+ r = FilesystemReader(self.temp_dir.child('foo'))
+ r.finish()
+ self.assertIsNone(r.size)
+
+ def test_size_when_file_removed(self):
+ # FilesystemReader.size uses fstat() to discover the file's size, so
+ # the absence of the file does not matter.
+ r = FilesystemReader(self.temp_dir.child('foo'))
+ self.existing_file_name.remove()
+ self.assertEqual(len(self.test_data), r.size)
+
def test_cancel(self):
r = FilesystemReader(self.temp_dir.child('foo'))
r.read(3)

0 comments on commit b13e7d6

Please sign in to comment.