From e6042f7b9084b3a317e3b61be4931c5eeed9fa41 Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Wed, 23 Apr 2008 15:50:14 -0700 Subject: [PATCH] Remove temporary files from the peer's download cache. --- TODO | 1 - apt_p2p/PeerManager.py | 12 +++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index 08125f3..10dae92 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ Some last few things to do before release. - Handle/investigate the HTTP client pipeline errors -- remove files from the peer's download cache - update the modtime of files downloaded from peers - also set the Last-Modified header for the return to Apt diff --git a/apt_p2p/PeerManager.py b/apt_p2p/PeerManager.py index d4c7aac..447e697 100644 --- a/apt_p2p/PeerManager.py +++ b/apt_p2p/PeerManager.py @@ -8,7 +8,7 @@ from binascii import b2a_hex, a2b_hex import sha from twisted.internet import reactor, defer -from twisted.python import log +from twisted.python import log, filepath from twisted.trial import unittest from twisted.web2 import stream from twisted.web2.http import Response, splitHostPort @@ -91,11 +91,13 @@ class GrowingFileStream(stream.FileStream): deferred.callback(b) else: # We're done + self._close() deferred = self.deferred self.deferred = None deferred.callback(None) else: # We're done + self._close() deferred = self.deferred self.deferred = None deferred.callback(None) @@ -112,6 +114,7 @@ class GrowingFileStream(stream.FileStream): # If we don't have any available, we're done or deferred if readSize <= 0: if self.finished: + self._close() return None else: self.deferred = defer.Deferred() @@ -124,6 +127,7 @@ class GrowingFileStream(stream.FileStream): if not bytesRead: # End of file was reached, we're done or deferred if self.finished: + self._close() return None else: self.deferred = defer.Deferred() @@ -132,6 +136,12 @@ class GrowingFileStream(stream.FileStream): self.position += bytesRead return b + def _close(self): + """Close the temporary file and remove it.""" + self.f.close() + filepath.FilePath(self.f.name).remove() + self.f = None + class StreamToFile: """Save a stream to a partial file and hash it. -- 2.39.5