Example #1
0
void Channel::get_subchannel_names(KVS &kvs, int owner_id, 
				   const std::string &prefix, 
				   std::vector<std::string> &names, 
				   unsigned int nlevels) {
  std::string kvs_prefix = string_printf("%d", owner_id);
  if (prefix != "") kvs_prefix += "." + prefix;
  std::vector<std::string> keys;
  kvs.get_subkeys(kvs_prefix, keys, nlevels, not_all_digits_filter);
  for (unsigned i = 0; i < keys.size(); i++) {
    if (filename_suffix(keys[i]) == "info") {
      std::string name_with_uid = filename_sans_suffix(keys[i]);
      tassert(strchr(name_with_uid.c_str(), '.'));
      names.push_back(1+strchr(name_with_uid.c_str(), '.'));
    }
  }
}
Example #2
0
/// Get subkeys
/// \param key
/// \param nlevels:  1=only return immediate children; 2=children and grandchildren; (unsigned int) -1: all children
/// \return All subkeys, recursively
void FilesystemKVS::get_subkeys(const std::string &key, std::vector<std::string> &keys, 
				unsigned int nlevels, bool (*subdir_filter)(const char *subdirname)) const {
  std::string path = directory_key_to_path(key);
  std::string prefix = (key == "") ? "" : key+".";
  DIR *dir = opendir(path.c_str());
  if (!dir) return;
  while (1) {
    struct dirent *ent = readdir(dir);
    if (!ent) break;
    if (!strcmp(ent->d_name, ".")) continue;
    if (!strcmp(ent->d_name, "..")) continue;
    if (filename_suffix(ent->d_name) == "val") {
      keys.push_back(prefix+filename_sans_suffix(ent->d_name));
    } else if (nlevels > 1 && (!subdir_filter || (*subdir_filter)(ent->d_name))) {
      // If it's a directory, recurse, honoring symlinks
      bool is_directory = false;
      std::string fullpath = path+"/"+ent->d_name;
	
      switch (ent->d_type) {
      case DT_DIR: 
	is_directory = true; 
	break;
      case DT_UNKNOWN:
      case DT_LNK: {
	struct stat statbuf;
	int ret = stat(fullpath.c_str(), &statbuf);
	if (ret != 0) {
	  perror(("stat " + fullpath).c_str());
	} else {
	  is_directory = S_ISDIR(statbuf.st_mode);
	}
      }
      }
      if (is_directory) get_subkeys(prefix+ent->d_name, keys, nlevels-1);
    }
  }
  closedir(dir);
}