def mkstemp(self): """Contextmanager to make a temporary file.""" # Creating intermidiate directories and corresponding metadata. # For optimization, check if the subdirectory already exists, # if exists, then it means that it also has its metadata. # Not checking for container, since the container should already # exist for the call to come here. if not os_path.exists(self.datadir): path = self._container_path subdir_list = self._obj_path.split(os.path.sep) for i in range(len(subdir_list)): path = os.path.join(path, subdir_list[i]); if not os_path.exists(path): self._create_dir_object(path) tmpfile = '.' + self._obj + '.' + md5(self._obj + \ str(random.random())).hexdigest() self.tmppath = os.path.join(self.datadir, tmpfile) fd = do_open(self.tmppath, os.O_RDWR | os.O_CREAT | os.O_EXCL) try: yield fd finally: try: do_close(fd) except OSError: pass tmppath, self.tmppath = self.tmppath, None try: do_unlink(tmppath) except OSError as err: if err.errno != errno.ENOENT: raise
def _create_dir_object(self, dir_path): #TODO: if object already exists??? if os.path.exists(dir_path) and not os.path.isdir(dir_path): self.logger.error("Deleting file %s", dir_path) do_unlink(dir_path) #If dir aleady exist just override metadata. mkdirs(dir_path) do_chown(dir_path, self.uid, self.gid) create_object_metadata(dir_path)
def create_dir_object(self, dir_path): if os.path.exists(dir_path) and not os.path.isdir(dir_path): self.logger.error("Deleting file %s", dir_path) do_unlink(dir_path) mkdirs(dir_path) do_chown(dir_path, self.uid, self.gid) return True
def unlink(self): """ Remove the file. """ #Marker dir. if self.is_dir: rmdirs(os.path.join(self.datadir, self.obj)) if not os.path.isdir(os.path.join(self.datadir, self.obj)): self.metadata = {} self.data_file = None else: logging.error('Unable to delete dir %s' % os.path.join(self.datadir, self.obj)) return for fname in do_listdir(self.datadir): if fname == self.obj: try: do_unlink(os.path.join(self.datadir, fname)) except OSError, err: if err.errno != errno.ENOENT: raise
def unlinkold(self, timestamp): """ Remove any older versions of the object file. Any file that has an older timestamp than timestamp will be deleted. :param timestamp: timestamp to compare with each file """ if not self.metadata or self.metadata['X-Timestamp'] >= timestamp: return assert self.data_file, \ "Have metadata, %r, but no data_file" % self.metadata if self._is_dir: # Marker directory object if not rmdirs(self.data_file): logging.error('Unable to delete dir object: %s', self.data_file) return else: # File object do_unlink(self.data_file) self.metadata = {} self.data_file = None
def meta_del(self): if os.path.exists(self.metafile): do_unlink(self.metafile)