void removePath()

in cachelib/common/Utils.cpp [303:332]


void removePath(const std::string& name) {
  if (!pathExists(name)) {
    return;
  }

  if (isDir(name)) {
    auto dir = opendir(name.c_str());
    if (!dir) {
      throwSystemError(errno, folly::sformat("Err removing path={}", name));
    }
    SCOPE_EXIT { free(dir); };
    struct dirent* entry;
    while ((entry = readdir(dir))) {
      if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
        continue;
      }
      std::string path = name + "/" + std::string(entry->d_name);
      removePath(path);
    }
    auto err = rmdir(name.c_str());
    if (err) {
      throwSystemError(errno, folly::sformat("Err removing path={}", name));
    }
  } else {
    auto err = unlink(name.c_str());
    if (err) {
      throwSystemError(errno, folly::sformat("Err removing path={}", name));
    }
  }
}