def check_cache_dirs(self): """ @brief Check existence of object cache directories and create if necessary @retval string pathname for temporaries directory @throwIOError if file operations go wrong The storage_root directory has to be created and writeable before starting the server. For the rest of the tree, directories will be created if they do not exist. The temporaries directory will also be be created if it does not exist and its pathname returned as the result of sucessfuloperations. Directories are checked to see they are readable, writeable and searchable if they exist. If an file IO operation fails, the appropriate exception is raised, logged and propagated. """ if not os.path.isdir(self.storage_root): self.logerror("Storage root directory %s does not exist." % self.storage_root) raise IOError("Storage root directory does not exist") # There is only one tree for the content files - metadata is in Redis for tree_name in (self.NDO_DIR,): tree_root = "%s%s" % (self.storage_root, tree_name) if not os.path.isdir(tree_root): self.loginfo("Creating object cache tree directory: %s" % tree_root) try: os.mkdir(tree_root, 0755) except Exception, e: self.logerror("Unable to create tree directory %s : %s." % \ (tree_root, str(e))) raise for auth_name in NIname.get_all_algs(): dir_name = "%s%s" % (tree_root, auth_name) if not os.path.isdir(dir_name): self.loginfo("Creating object cache directory: %s" % dir_name) try: os.mkdir(dir_name, 0755) except Exception, e: self.logerror("Unable to create cache directory %s : %s." % (dir_name, str(e))) raise elif not os.access(dir_name, os.R_OK | os.W_OK | os.X_OK): self.logerror("Existing cache directory %s does not have rwx" "access permissions." % dir_name) raise OSError("Cannot access cache directory")
def cache_list(self, alg_list = None): """ @brief Construct a dictionary listing current cache contents for specified digest algorithms @param alg_list list of strings of digest algorithm names or None (= all) @return dictionary with listing or None Check if alg_list contains valid names - or get all from NIname. Return None if no valid names Read the metadata directory entries for selected algorithm names Build a dictionary with an entry for each selected algorithm name Value for each is an array of objects with two entries: - "dgst": digest (ni format) - "ce": boolean indicating if content file exists Return dictionary constructed if alg_list contains known algorithms Return None if anything goes wrong and log infomational message. Note that we don't use the lock here. At present cache entries are never explicitly deleted so the worst that can happen is that the listing is shy of a (very) few last microsedond entries """ all_algs = NIname.get_all_algs() if alg_list is None: alg_list = all_algs else: for alg in alg_list: if not alg in all_algs: self.loginfo("cache_list: Unknown algorithm name requested %s" % alg) return None rslt = {} for alg in alg_list: mfd = "%s%s%s" % (self.storage_root, self.META_DIR, alg) cfd = "%s%s%s" % (self.storage_root, self.NDO_DIR, alg) entries = [] # All cache entries are required to have metadata file, # and may have content file try: for dgst in os.listdir(mfd): ce = os.path.isfile("%s/%s" % (cfd, dgst)) entries.append( { "dgst": dgst, "ce": ce }) except Exception, e: self.logerror("cache_list: error while listing for alg %s: %s" % (alg, str(e))) return None rslt[alg] = entries