From 07893ed1ffd3cdcc1a8b51d0854599d61b2b5fac Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Sat, 12 Jan 2008 10:06:11 -0800 Subject: [PATCH] Use the new DB in the main code. --- apt_dht/MirrorManager.py | 2 +- apt_dht/apt_dht.py | 18 ++++++------------ apt_dht/db.py | 9 +++++++-- apt_dht_Khashmir/khashmir.py | 2 +- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/apt_dht/MirrorManager.py b/apt_dht/MirrorManager.py index 8bf197f..c41dbe2 100644 --- a/apt_dht/MirrorManager.py +++ b/apt_dht/MirrorManager.py @@ -263,7 +263,7 @@ class MirrorManager: self.updatedFile(url[:-len(ext)], decFile.path) if self.manager: - self.manager.download_complete(hash, url, destFile.path) + self.manager.cached_file(hash, url, destFile.path) else: log.msg("Hashes don't match %s != %s: %s" % (hash.hexexpected(), hash.hexdigest(), url)) diff --git a/apt_dht/apt_dht.py b/apt_dht/apt_dht.py index 2af1df9..0a0059f 100644 --- a/apt_dht/apt_dht.py +++ b/apt_dht/apt_dht.py @@ -12,10 +12,12 @@ from PeerManager import PeerManager from HTTPServer import TopLevel from MirrorManager import MirrorManager from Hash import HashObject +from db import DB class AptDHT: def __init__(self, dht): log.msg('Initializing the main apt_dht application') + self.db = DB(config.get('DEFAULT', 'cache_dir') + '/.apt-dht.db') self.dht = dht self.dht.loadConfig(config, config.get('DEFAULT', 'DHT')) self.dht.join().addCallbacks(self.joinComplete, self.joinError) @@ -188,22 +190,14 @@ class AptDHT: return getDefer return response - def download_complete(self, hash, url, file_path): + def cached_file(self, hash, url, file_path): assert file_path.startswith(config.get('DEFAULT', 'cache_dir')) - directory = file_path[:len(config.get('DEFAULT', 'cache_dir'))] - url_path = file_path[len(config.get('DEFAULT', 'cache_dir')):] - if url_path[0] == '/': - url_path = url_path[1:] - top_directory = url_path.split('/',1)[0] - url_path = url_path[len(top_directory):] - http_dir = os.path.join(directory, top_directory) - new_top = self.http_server.addDirectory(http_dir) - url_path = '/' + new_top + url_path - log.msg('now avaliable at %s: %s' % (url_path, url)) + urlpath, newdir = self.db.storeFile(file_path, hash.digest(), config.get('DEFAULT', 'cache_dir')) + log.msg('now avaliable at %s: %s' % (urlpath, url)) if self.my_addr: site = self.my_addr + ':' + str(config.getint('DEFAULT', 'PORT')) - full_path = urlunparse(('http', site, url_path, None, None, None)) + full_path = urlunparse(('http', site, urlpath, None, None, None)) key = hash.norm(bits = config.getint(config.get('DEFAULT', 'DHT'), 'HASH_LENGTH')) storeDefer = self.dht.storeValue(key, full_path) storeDefer.addCallback(self.store_done, full_path) diff --git a/apt_dht/db.py b/apt_dht/db.py index 7f8c449..9725aa8 100644 --- a/apt_dht/db.py +++ b/apt_dht/db.py @@ -69,7 +69,11 @@ class DB: return res def storeFile(self, path, hash, directory): - """Store or update a file in the database.""" + """Store or update a file in the database. + + @return: the urlpath to access the file, and whether a + new url top-level directory was needed + """ path = os.path.abspath(path) directory = os.path.abspath(directory) assert path.startswith(directory) @@ -81,13 +85,14 @@ class DB: c.execute("UPDATE files SET hash = ?, size = ?, mtime = ?, refreshed = ?", (khash(hash), stat.st_size, stat.st_mtime, datetime.now())) newdir = False + urldir = row['urldir'] else: urldir, newdir = self.findDirectory(directory) c.execute("INSERT OR REPLACE INTO files VALUES(?, ?, ?, ?, ?, ?, ?)", (path, khash(hash), urldir, len(directory), stat.st_size, stat.st_mtime, datetime.now())) self.conn.commit() c.close() - return newdir + return '/~' + str(urldir) + path[len(directory):], newdir def getFile(self, path): """Get a file from the database. diff --git a/apt_dht_Khashmir/khashmir.py b/apt_dht_Khashmir/khashmir.py index 8c75c48..af6f0d8 100644 --- a/apt_dht_Khashmir/khashmir.py +++ b/apt_dht_Khashmir/khashmir.py @@ -30,7 +30,7 @@ class KhashmirBase(protocol.Factory): def setup(self, config, cache_dir): self.config = config self.port = config['PORT'] - self.store = DB(os.path.join(cache_dir, 'khashmir.' + str(self.port) + '.db')) + self.store = DB(os.path.join(cache_dir, '.khashmir.' + str(self.port) + '.db')) self.node = self._loadSelfNode('', self.port) self.table = KTable(self.node, config) #self.app = service.Application("krpc") -- 2.39.5