comparison sat/memory/memory.py @ 3300:b56e4c6b13fc

core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
author Goffi <goffi@goffi.org>
date Fri, 19 Jun 2020 14:55:30 +0200
parents 780fb8dd07ef
children 624c60293deb
comparison
equal deleted inserted replaced
3299:83795ff8a633 3300:b56e4c6b13fc
1553 sub_files = yield self.getFiles(client, peer_jid, parent=file_data['id']) 1553 sub_files = yield self.getFiles(client, peer_jid, parent=file_data['id'])
1554 if sub_files and not recursive: 1554 if sub_files and not recursive:
1555 raise exceptions.DataError(_("Can't delete directory, it is not empty")) 1555 raise exceptions.DataError(_("Can't delete directory, it is not empty"))
1556 # we first delete the sub-files 1556 # we first delete the sub-files
1557 for sub_file_data in sub_files: 1557 for sub_file_data in sub_files:
1558 yield self._deleteFile(client, peer_jid, recursive, sub_file_data) 1558 if sub_file_data['type'] == C.FILE_TYPE_DIRECTORY:
1559 sub_file_path = files_path / sub_file_data['name']
1560 else:
1561 sub_file_path = files_path
1562 yield self._deleteFile(
1563 client, peer_jid, recursive, sub_file_path, sub_file_data)
1559 # then the directory itself 1564 # then the directory itself
1560 yield self.storage.fileDelete(file_data['id']) 1565 yield self.storage.fileDelete(file_data['id'])
1561 elif file_data['type'] == C.FILE_TYPE_FILE: 1566 elif file_data['type'] == C.FILE_TYPE_FILE:
1562 log.info(_("deleting file {name} with hash {file_hash}").format( 1567 log.info(_("deleting file {name} with hash {file_hash}").format(
1563 name=file_data['name'], file_hash=file_data['file_hash'])) 1568 name=file_data['name'], file_hash=file_data['file_hash']))
1568 log.debug("there are still references to the file, we keep it") 1573 log.debug("there are still references to the file, we keep it")
1569 else: 1574 else:
1570 file_path = os.path.join(files_path, file_data['file_hash']) 1575 file_path = os.path.join(files_path, file_data['file_hash'])
1571 log.info(_("no reference left to {file_path}, deleting").format( 1576 log.info(_("no reference left to {file_path}, deleting").format(
1572 file_path=file_path)) 1577 file_path=file_path))
1573 os.unlink(file_path) 1578 try:
1579 os.unlink(file_path)
1580 except FileNotFoundError:
1581 log.error(f"file at {file_path!r} doesn't exist but it was referenced in files database")
1574 else: 1582 else:
1575 raise exceptions.InternalError('Unexpected file type: {file_type}' 1583 raise exceptions.InternalError('Unexpected file type: {file_type}'
1576 .format(file_type=file_data['type'])) 1584 .format(file_type=file_data['type']))
1577 1585
1578 @defer.inlineCallbacks 1586 @defer.inlineCallbacks