]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_p2p/CacheManager.py
Move the DHT stuff out of the main program and into the new DHTManager module.
[quix0rs-apt-p2p.git] / apt_p2p / CacheManager.py
index eda247d61335a39318521b1838b554c97e1a6010..b991093f61c3266c50f27cb93188854abb7960bf 100644 (file)
@@ -245,7 +245,7 @@ class CacheManager:
         self.db.removeUntrackedFiles(self.all_dirs)
         
     #{ Scanning directories
-    def scanDirectories(self):
+    def scanDirectories(self, result = None):
         """Scan the cache directories, hashing new and rehashing changed files."""
         assert not self.scanning, "a directory scan is already under way"
         self.scanning = self.all_dirs[:]
@@ -281,14 +281,12 @@ class CacheManager:
 
         # If it's not a file ignore it
         if not file.isfile():
-            log.msg('entering directory: %s' % file.path)
             reactor.callLater(0, self._scanDirectories, None, walker)
             return
 
         # If it's already properly in the DB, ignore it
         db_status = self.db.isUnchanged(file)
         if db_status:
-            log.msg('file is unchanged: %s' % file.path)
             reactor.callLater(0, self._scanDirectories, None, walker)
             return
         
@@ -307,7 +305,6 @@ class CacheManager:
         hash = HashObject()
         df = hash.hashInThread(file)
         df.addBoth(self._doneHashing, file, walker)
-        df.addErrback(log.err)
     
     def _doneHashing(self, result, file, walker):
         """If successful, add the hashed file to the DB and inform the main program."""
@@ -320,7 +317,7 @@ class CacheManager:
                 url = 'http:/' + file.path[len(self.cache_dir.path):]
                 
             # Store the hashed file in the database
-            new_hash = self.db.storeFile(file, result.digest(),
+            new_hash = self.db.storeFile(file, result.digest(), True,
                                          ''.join(result.pieceDigests()))
             
             # Tell the main program to handle the new cache file
@@ -404,27 +401,31 @@ class CacheManager:
         @param decFile: the file where the decompressed download was written to
             (optional, defaults to the file not having been compressed)
         """
-        if modtime:
-            os.utime(destFile.path, (modtime, modtime))
-            if decFile:
-                os.utime(decFile.path, (modtime, modtime))
-        
         result = hash.verify()
         if result or result is None:
+            if modtime:
+                os.utime(destFile.path, (modtime, modtime))
+            
             if result:
                 log.msg('Hashes match: %s' % url)
+                dht = True
             else:
                 log.msg('Hashed file to %s: %s' % (hash.hexdigest(), url))
+                dht = False
                 
-            new_hash = self.db.storeFile(destFile, hash.digest(),
+            new_hash = self.db.storeFile(destFile, hash.digest(), dht,
                                          ''.join(hash.pieceDigests()))
-            log.msg('now avaliable: %s' % (url))
 
             if self.manager:
                 self.manager.new_cached_file(destFile, hash, new_hash, url)
-                if decFile:
-                    ext_len = len(destFile.path) - len(decFile.path)
-                    self.manager.new_cached_file(decFile, None, False, url[:-ext_len])
+
+            if decFile:
+                # Hash the decompressed file and add it to the DB
+                decHash = HashObject()
+                ext_len = len(destFile.path) - len(decFile.path)
+                df = decHash.hashInThread(decFile)
+                df.addCallback(self._save_complete, url[:-ext_len], decFile, modtime)
+                df.addErrback(self._save_error, url[:-ext_len], decFile)
         else:
             log.msg("Hashes don't match %s != %s: %s" % (hash.hexexpected(), hash.hexdigest(), url))
             destFile.remove()