diff options
Diffstat (limited to 'src/carquinyol/filestore.py')
-rw-r--r-- | src/carquinyol/filestore.py | 48 |
1 files changed, 28 insertions, 20 deletions
diff --git a/src/carquinyol/filestore.py b/src/carquinyol/filestore.py index 592c41a..ef6aba8 100644 --- a/src/carquinyol/filestore.py +++ b/src/carquinyol/filestore.py @@ -32,15 +32,16 @@ class FileStore(object): # TODO: add protection against store and retrieve operations on entries # that are being processed async. - def store(self, uid, file_path, transfer_ownership, completion_cb): + def store(self, object_id, file_path, transfer_ownership, completion_cb): """Store a file for a given entry. """ - dir_path = layoutmanager.get_instance().get_entry_path(uid) + layout_manager = layoutmanager.get_instance() + dir_path = layout_manager.get_entry_path(object_id) if not os.path.exists(dir_path): os.makedirs(dir_path) - destination_path = layoutmanager.get_instance().get_data_path(uid) + destination_path = layout_manager.get_data_path(object_id) if file_path: if not os.path.isfile(file_path): raise ValueError('No file at %r' % file_path) @@ -89,15 +90,19 @@ class FileStore(object): unlink_src) async_copy.start() - def retrieve(self, uid, user_id, extension): + def has_data(self, object_id): + file_path = layoutmanager.get_instance().get_data_path(object_id) + return os.path.exists(file_path) + + def retrieve(self, object_id, user_id, extension): """Place the file associated to a given entry into a directory where the user can read it. The caller is reponsible for deleting this file. """ - file_path = layoutmanager.get_instance().get_data_path(uid) + file_path = layoutmanager.get_instance().get_data_path(object_id) if not os.path.exists(file_path): - logging.debug('Entry %r doesnt have any file', uid) + logging.debug('Entry %r doesnt have any file', object_id) return '' if extension is None: @@ -125,7 +130,7 @@ class FileStore(object): if use_instance_dir: os.umask(old_umask) - fd, destination_path = tempfile.mkstemp(prefix=uid + '_', + fd, destination_path = tempfile.mkstemp(prefix='%s-%s_' % object_id, suffix=extension, dir=destination_dir) os.close(fd) os.unlink(destination_path) @@ -143,21 +148,21 @@ class FileStore(object): return destination_path - def get_file_path(self, uid): - return layoutmanager.get_instance().get_data_path(uid) + def get_file_path(self, object_id): + return layoutmanager.get_instance().get_data_path(object_id) - def delete(self, uid): + def delete(self, object_id): """Remove the file associated to a given entry. """ - file_path = layoutmanager.get_instance().get_data_path(uid) + file_path = layoutmanager.get_instance().get_data_path(object_id) if os.path.exists(file_path): os.remove(file_path) - def hard_link_entry(self, new_uid, existing_uid): - existing_file = layoutmanager.get_instance().get_data_path( - existing_uid) - new_file = layoutmanager.get_instance().get_data_path(new_uid) + def hard_link_entry(self, new_object_id, existing_object_id): + layout_manager = layoutmanager.get_instance() + existing_file = layout_manager.get_data_path(existing_object_id) + new_file = layout_manager.get_data_path(new_object_id) logging.debug('removing %r', new_file) os.remove(new_file) @@ -183,8 +188,10 @@ class AsyncCopy(object): self.size = 0 def _cleanup(self): - os.close(self.src_fp) - os.close(self.dest_fp) + if self.src_fp != -1: + os.close(self.src_fp) + if self.dest_fp != -1: + os.close(self.dest_fp) os.chmod(self.dest, 0400) def _copy_block(self, user_data=None): @@ -208,9 +215,10 @@ class AsyncCopy(object): self._complete(None) return False except Exception, err: - logging.error('AC: Error copying %s -> %s: %r', self.src, self. - dest, err) - self._complete(err) + logging.exception('AC: Error copying %s -> %s', self.src, + self.dest) + self._cleanup() + self.completion(err) return False return True |