- def storeFile(self, path, hash, urlpath, refreshed):
- """Store or update a file in the database."""
- path = os.path.abspath(path)
- stat = os.stat(path)
+ def _removeChanged(self, file, row):
+ """If the file has changed or is missing, remove it from the DB.
+
+ @type file: L{twisted.python.filepath.FilePath}
+ @param file: the file to check
+ @type row: C{dictionary}-like object
+ @param row: contains the expected 'size' and 'mtime' of the file
+ @rtype: C{boolean}
+ @return: True if the file is unchanged, False if it is changed,
+ and None if it is missing
+ """
+ res = None
+ if row:
+ file.restat(False)
+ if file.exists():
+ # Compare the current with the expected file properties
+ res = (row['size'] == file.getsize() and row['mtime'] == file.getmtime())
+ if not res:
+ # Remove the file from the database
+ c = self.conn.cursor()
+ c.execute("DELETE FROM files WHERE path = ?", (file.path, ))
+ self.conn.commit()
+ c.close()
+ return res
+
+ def storeFile(self, file, hash, pieces = ''):
+ """Store or update a file in the database.
+
+ @type file: L{twisted.python.filepath.FilePath}
+ @param file: the file to check
+ @type hash: C{string}
+ @param hash: the hash of the file
+ @type pieces: C{string}
+ @param pieces: the concatenated list of the hashes of the pieces of
+ the file (optional, defaults to the empty string)
+ @return: True if the hash was not in the database before
+ (so it needs to be added to the DHT)
+ """
+ # Hash the pieces to get the piecehash
+ piecehash = ''
+ if pieces:
+ s = sha.new().update(pieces)
+ piecehash = sha.digest()
+
+ # Check the database for the hash