def unlink(self): """Mark the node as Dead.""" # we don't modify the 'path' when unlinking the file, to preserve # its location when unlinked if self.is_dir and self.live_children.exists(): raise NotEmpty("Can't unlink a non empty directory.") if self.parent == ROOT_PARENT: raise NoPermission("Can't unlink special files.") pre_kill.send(sender=self.__class__, instance=self) self.status = STATUS_DEAD self.update_generation(save=False) self.save(update_fields=['status', 'generation']) if self.is_file: content_changed.send(sender=self.__class__, instance=self, content_added=False, new_size=0 - getattr(self.content_blob, 'size', 0), enforce_quota=False) if self.parent != ROOT_PARENT: self.parent.when_last_modified = now() self.parent.save(update_fields=['when_last_modified']) post_kill.send(sender=self.__class__, instance=self)
def undelete(self, new_parent=None): """Undelete file or directory. If a new_parent is passed in, the file's parent node and path will be updated as well. """ # no need to do anything with a live node if self.status == STATUS_LIVE: return if new_parent and not new_parent.is_dir: raise NotADirectory("Must reparent to a Directory on Undelete.") parent = new_parent or self.parent self.name = parent.get_unique_childname(self.name) if self.is_file: content_changed.send(sender=self.__class__, instance=self, content_added=False, new_size=getattr(self.content_blob, 'size', 0), enforce_quota=False) self.parent = parent self.volume = parent.volume self.path = parent.full_path self.status = STATUS_LIVE self.update_generation(save=False) # update the parent if self.parent.status == STATUS_DEAD: # if the parent directory dead, we need to check to see if there # is a live directory with the same path to put it in. path, name = os.path.split(self.path) try: # if we have a suitable parent, update the parent self.parent = StorageObject.objects.get( volume__id=self.volume.id, path=path, name=name, status=STATUS_LIVE) except StorageObject.DoesNotExist: # if we can't find a suitable parent, we need to restore the # old one. self.parent.undelete() else: self.parent.when_last_modified = now() self.parent.save(update_fields=['when_last_modified']) else: # if the parent was live, we just need to update the timestamp self.parent.when_last_modified = now() self.parent.save(update_fields=['when_last_modified']) self.save(update_fields=[ 'name', 'parent', 'volume', 'path', 'status', 'generation' ])
def kill(self): """Mark this UDF as Dead.""" pre_kill.send(sender=self.__class__, instance=self) size_to_remove = self.volume_size() content_changed.send( sender=self.__class__, instance=self, content_added=False, new_size=0 - size_to_remove, enforce_quota=False) self.increment_generation(save=False) self.status = STATUS_DEAD self.save(update_fields=['generation', 'status']) post_kill.send(sender=self.__class__, instance=self)
def set_content(self, new_content, enforce_quota=True): """Set the ContentBlob and updates owner's used_storage_bytes.""" if self.is_dir: raise DirectoriesHaveNoContent("Directory has no content.") curr_size = getattr(self.content_blob, 'size', 0) self.content_blob = new_content self.update_generation(save=False) new_size = new_content.size - curr_size content_changed.send( sender=self.__class__, instance=self, content_added=True, new_size=new_size, enforce_quota=enforce_quota) self.save(update_fields=['content_blob', 'generation'])
def kill(self): """Mark this UDF as Dead.""" pre_kill.send(sender=self.__class__, instance=self) size_to_remove = self.volume_size() content_changed.send(sender=self.__class__, instance=self, content_added=False, new_size=0 - size_to_remove, enforce_quota=False) self.increment_generation(save=False) self.status = STATUS_DEAD self.save(update_fields=['generation', 'status']) post_kill.send(sender=self.__class__, instance=self)
def set_content(self, new_content, enforce_quota=True): """Set the ContentBlob and updates owner's used_storage_bytes.""" if self.is_dir: raise DirectoriesHaveNoContent("Directory has no content.") curr_size = getattr(self.content_blob, 'size', 0) self.content_blob = new_content self.update_generation(save=False) new_size = new_content.size - curr_size content_changed.send(sender=self.__class__, instance=self, content_added=True, new_size=new_size, enforce_quota=enforce_quota) self.save(update_fields=['content_blob', 'generation'])
def undelete(self, new_parent=None): """Undelete file or directory. If a new_parent is passed in, the file's parent node and path will be updated as well. """ # no need to do anything with a live node if self.status == STATUS_LIVE: return if new_parent and not new_parent.is_dir: raise NotADirectory("Must reparent to a Directory on Undelete.") parent = new_parent or self.parent self.name = parent.get_unique_childname(self.name) if self.is_file: content_changed.send( sender=self.__class__, instance=self, content_added=False, new_size=getattr(self.content_blob, 'size', 0), enforce_quota=False) self.parent = parent self.volume = parent.volume self.path = parent.full_path self.status = STATUS_LIVE self.update_generation(save=False) # update the parent if self.parent.status == STATUS_DEAD: # if the parent directory dead, we need to check to see if there # is a live directory with the same path to put it in. path, name = os.path.split(self.path) try: # if we have a suitable parent, update the parent self.parent = StorageObject.objects.get( volume__id=self.volume.id, path=path, name=name, status=STATUS_LIVE) except StorageObject.DoesNotExist: # if we can't find a suitable parent, we need to restore the # old one. self.parent.undelete() else: self.parent.when_last_modified = now() self.parent.save(update_fields=['when_last_modified']) else: # if the parent was live, we just need to update the timestamp self.parent.when_last_modified = now() self.parent.save(update_fields=['when_last_modified']) self.save(update_fields=[ 'name', 'parent', 'volume', 'path', 'status', 'generation'])
def unlink_tree(self): """Unlink and entire directory and it's subdirectories""" if not self.is_dir: raise NotADirectory("%s is not a directory." % self.id) if self.parent is None: raise NoPermission("Can't unlink volumes root nodes.") if self.status == STATUS_DEAD: return # First update the generation so that we can use it in the new TXLog # entries. self.update_generation(save=False) right_now = now() descendants = [] if self.live_children.exists(): size_to_remove = self.tree_size content_changed.send(sender=self.__class__, instance=self, content_added=False, new_size=0 - size_to_remove, enforce_quota=False) descendants = list( # make a copy before killing self.descendants.select_related('volume', 'volume__owner')) self.descendants.update(status=STATUS_DEAD, when_last_modified=right_now) pre_unlink_tree.send(sender=self.__class__, instance=self, descendants=descendants) self.status = STATUS_DEAD self.save(update_fields=['status', 'generation']) if self.parent != ROOT_PARENT: self.parent.save() post_unlink_tree.send(sender=self.__class__, instance=self, descendants=descendants)
def unlink_tree(self): """Unlink and entire directory and it's subdirectories""" if not self.is_dir: raise NotADirectory("%s is not a directory." % self.id) if self.parent is None: raise NoPermission("Can't unlink volumes root nodes.") if self.status == STATUS_DEAD: return # First update the generation so that we can use it in the new TXLog # entries. self.update_generation(save=False) right_now = now() descendants = [] if self.live_children.exists(): size_to_remove = self.tree_size content_changed.send( sender=self.__class__, instance=self, content_added=False, new_size=0 - size_to_remove, enforce_quota=False) descendants = list(self.descendants) # make a copy before killing self.descendants.update( status=STATUS_DEAD, when_last_modified=right_now) pre_unlink_tree.send( sender=self.__class__, instance=self, descendants=descendants) self.status = STATUS_DEAD self.save(update_fields=['status', 'generation']) if self.parent != ROOT_PARENT: self.parent.save() post_unlink_tree.send( sender=self.__class__, instance=self, descendants=descendants)
def unlink(self): """Mark the node as Dead.""" # we don't modify the 'path' when unlinking the file, to preserve # its location when unlinked if self.is_dir and self.live_children.exists(): raise NotEmpty("Can't unlink a non empty directory.") if self.parent == ROOT_PARENT: raise NoPermission("Can't unlink special files.") pre_kill.send(sender=self.__class__, instance=self) self.status = STATUS_DEAD self.update_generation(save=False) self.save(update_fields=['status', 'generation']) if self.is_file: content_changed.send( sender=self.__class__, instance=self, content_added=False, new_size=0 - getattr(self.content_blob, 'size', 0), enforce_quota=False) if self.parent != ROOT_PARENT: self.parent.when_last_modified = now() self.parent.save(update_fields=['when_last_modified']) post_kill.send(sender=self.__class__, instance=self)