def put(self, metadata): """ Finalize writing the file on disk, and renames it from the temp file to the real location. This should be called after the data has been written to the temp file. :param purgelock: bool flag to signal if purge lock desired :param metadata: dictionary of metadata to be written :raises AlreadyExistsAsDir : If there exists a directory of the same name """ assert self._tmppath is not None metadata = _adjust_metadata(self._fd, metadata) df = self._disk_file if dir_is_object(metadata): df._create_dir_object(df._data_file, metadata) return if df._is_dir: # A pre-existing directory already exists on the file # system, perhaps gratuitously created when another # object was created, or created externally to Swift # REST API servicing (UFO use case). raise AlreadyExistsAsDir('DiskFile.put(): file creation failed' ' since the target, %s, already exists' ' as a directory' % df._data_file) self._finalize_put(metadata) # Avoid the unlink() system call as part of the mkstemp context # cleanup self._tmppath = None
def _unlinkold(self): if self._is_dir: # Marker, or object, directory. # # Delete from the filesystem only if it contains no objects. # If it does contain objects, then just remove the object # metadata tag which will make this directory a # fake-filesystem-only directory and will be deleted when the # container or parent directory is deleted. # # FIXME: Ideally we should use an atomic metadata update operation metadata = read_metadata(self._data_file) if dir_is_object(metadata): metadata[X_OBJECT_TYPE] = DIR_NON_OBJECT write_metadata(self._data_file, metadata) rmobjdir(self._data_file) else: # Delete file object do_unlink(self._data_file) # Garbage collection of non-object directories. Now that we # deleted the file, determine if the current directory and any # parent directory may be deleted. dirname = os.path.dirname(self._data_file) while dirname and dirname != self._container_path: # Try to remove any directories that are not objects. if not rmobjdir(dirname): # If a directory with objects has been found, we can stop # garabe collection break else: dirname = os.path.dirname(dirname)