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 do_unlink(tmppath)
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 check_user_xattr(path): if not os_path.exists(path): return False try: xattr.setxattr(path, 'user.test.key1', 'value1') except IOError as err: raise GlusterFileSystemIOError( err.errno, 'xattr.setxattr("%s", "user.test.key1", "value1")' % (path,)) try: xattr.removexattr(path, 'user.test.key1') except IOError as err: logging.exception("check_user_xattr: remove failed on %s err: %s", path, str(err)) #Remove xattr may fail in case of concurrent remove. return True
def check_user_xattr(path): if not os_path.exists(path): return False try: xattr.setxattr(path, 'user.test.key1', 'value1') except IOError as err: logging.exception("check_user_xattr: set failed on %s err: %s", path, str(err)) raise try: xattr.removexattr(path, 'user.test.key1') except IOError as err: logging.exception("check_user_xattr: remove failed on %s err: %s", path, str(err)) #Remove xattr may fail in case of concurrent remove. return True
def update_put_timestamp(self, timestamp): """ Update the PUT timestamp for the container. If the container does not exist, create it using a PUT timestamp of the given value. If the container does exist, update the PUT timestamp only if it is later than the existing value. """ if not os_path.exists(self.datadir): self.initialize(timestamp) else: if timestamp > self.metadata[X_PUT_TIMESTAMP]: self.metadata[X_PUT_TIMESTAMP] = (timestamp, 0) write_metadata(self.datadir, self.metadata)
def is_deleted(self): # The intention of this method is to check the file system to see if # the directory actually exists. return not os_path.exists(self.datadir)
def _dir_exists_read_metadata(self): self._dir_exists = os_path.exists(self.datadir) if self._dir_exists: self.metadata = _read_metadata(self.datadir) return self._dir_exists
def __init__(self, path, device, partition, account, container, obj, logger, keep_data_fp=False, disk_chunk_size=DEFAULT_DISK_CHUNK_SIZE, uid=DEFAULT_UID, gid=DEFAULT_GID, iter_hook=None): self.disk_chunk_size = disk_chunk_size self.iter_hook = iter_hook # Don't support obj_name ending/begining with '/', like /a, a/, /a/b/, # etc. obj = obj.strip(os.path.sep) if os.path.sep in obj: self._obj_path, self._obj = os.path.split(obj) else: self._obj_path = '' self._obj = obj if self._obj_path: self.name = os.path.join(container, self._obj_path) else: self.name = container # Absolute path for object directory. self.datadir = os.path.join(path, device, self.name) self.device_path = os.path.join(path, device) self._container_path = os.path.join(path, device, container) self._is_dir = False self.tmppath = None self.logger = logger self.metadata = {} self.meta_file = None self.fp = None self.iter_etag = None self.started_at_0 = False self.read_to_eof = False self.quarantined_dir = None self.keep_cache = False self.uid = int(uid) self.gid = int(gid) self.suppress_file_closing = False # Don't store a value for data_file until we know it exists. self.data_file = None data_file = os.path.join(self.datadir, self._obj) if not os_path.exists(data_file): return self.data_file = os.path.join(data_file) self.metadata = read_metadata(data_file) if not self.metadata: create_object_metadata(data_file) self.metadata = read_metadata(data_file) if not validate_object(self.metadata): create_object_metadata(data_file) self.metadata = read_metadata(data_file) self.filter_metadata() if os_path.isdir(data_file): self._is_dir = True else: if keep_data_fp: # The caller has an assumption that the "fp" field of this # object is an file object if keep_data_fp is set. However, # this implementation of the DiskFile object does not need to # open the file for internal operations. So if the caller # requests it, we'll just open the file for them. self.fp = do_open(data_file, 'rb')