Hello, everyone,
Looks like my previous patch has a serious error which causes files to never get closed/flushed/whatever. After a couple of days of testing, the attached patch seems to be working OK.

I'm sorry for any inconvenience I might have caused.

Best regards,
Anton

Index: BitTorrent/Storage.py
===================================================================
--- BitTorrent/Storage.py       (revision 228)
+++ BitTorrent/Storage.py       (working copy)
@@ -23,23 +23,26 @@
                 open(file, 'wb').close()
         self.begins = [i[0] for i in self.ranges]
         self.total_length = total
-        self.handles = {}
-        self.whandles = {}
+        self.modes = {}
+        self.wfiles = {}
         self.tops = {}
         for file, length in files:
             if exists(file):
                 l = getsize(file)
                 if l != length:
-                    self.handles[file] = open(file, 'rb+')
-                    self.whandles[file] = 1
+                    self.modes[file] = 'rb+'
+                    self.wfiles[file] = 1
                     if l > length:
-                        self.handles[file].truncate(length)
+                        fh = open(file, self.modes[file])
+                        fh.truncate(length)
+                        fh.close()
                 else:
-                    self.handles[file] = open(file, 'rb')
+                    self.modes[file] = 'rb'
                 self.tops[file] = l
             else:
-                self.handles[file] = open(file, 'wb+')
-                self.whandles[file] = 1
+                self.modes[file] = 'wb+'
+                self.wfiles[file] = 1
+                open(file, self.modes[file]).close()
 
     def was_preallocated(self, pos, length):
         for file, begin, end in self._intervals(pos, length):
@@ -49,11 +52,8 @@
 
     def set_readonly(self):
         # may raise IOError or OSError
-        for file in self.whandles.keys():
-            old = self.handles[file]
-            old.flush()
-            old.close()
-            self.handles[file] = open(file, 'rb')
+        for file in self.wfiles.keys():
+            self.modes[file] = 'rb'
 
     def get_total_length(self):
         return self.total_length
@@ -71,27 +71,26 @@
     def read(self, pos, amount):
         r = []
         for file, pos, end in self._intervals(pos, amount):
-            h = self.handles[file]
+            h = open(file, self.modes[file])
             h.seek(pos)
             r.append(h.read(end - pos))
+            h.close()
         return ''.join(r)
 
     def write(self, pos, s):
         # might raise an IOError
         total = 0
         for file, begin, end in self._intervals(pos, len(s)):
-            if not self.whandles.has_key(file):
-                self.handles[file].close()
-                self.handles[file] = open(file, 'rb+')
-                self.whandles[file] = 1
-            h = self.handles[file]
+            self.modes[file] = 'rb+'
+            self.wfiles[file] = 1
+            h = open(file, self.modes[file])
             h.seek(begin)
             h.write(s[total: total + end - begin])
             total += end - begin
+            h.close()
 
     def close(self):
-        for h in self.handles.values():
-            h.close()
+        pass
 
 def lrange(a, b, c):
     r = []

Reply via email to