void ftw_fn(const String &path, const Function<void(const String &path, bool isFile)> &callback, int depth, bool dirFirst) {
	auto dp = opendir(path.c_str());
	if (dp == NULL) {
		if (access(path.c_str(), F_OK) != -1) {
			callback(path, true);
		}
	} else {
		if (dirFirst) {
			callback(path, false);
		}
		if (depth < 0 || depth > 0) {
			struct dirent *entry;
			while ((entry = readdir(dp))) {
				if (strcmp(entry->d_name, "..") != 0 && strcmp(entry->d_name, ".") != 0) {
					String newPath = filepath::merge(path, entry->d_name);
					ftw_fn(newPath, callback, depth - 1, dirFirst);
				}
			}
			closedir(dp);
		}
		if (!dirFirst) {
			callback(path, false);
		}
	}
}
Beispiel #2
0
static int do_nftw(const char *path,
                   int (*ftw_fn)(const char*, const struct stat*, int),
                   int (*nftw_fn)(const char*, const struct stat*, int, FTW*),
                   int nfds,
                   int nftw_flags) {
  // TODO: nfds is currently unused.
  if (nfds < 1) {
    errno = EINVAL;
    return -1;
  }

  // Translate to fts_open options.
  int fts_options = FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR;
  if (nftw_fn) {
    fts_options = FTS_COMFOLLOW | ((nftw_flags & FTW_PHYS) ? FTS_PHYSICAL : FTS_LOGICAL);
    if (!(nftw_flags & FTW_CHDIR)) fts_options |= FTS_NOCHDIR;
    if (nftw_flags & FTW_MOUNT) fts_options |= FTS_XDEV;
  }
  bool postorder = (nftw_flags & FTW_DEPTH) != 0;

  // Call fts_open.
  char* const paths[2] = { const_cast<char*>(path), nullptr };
  FTS* fts = fts_open(paths, fts_options, nullptr);
  if (fts == nullptr) {
    return -1;
  }

  // Translate fts_read results into ftw/nftw callbacks.
  int error = 0;
  FTSENT* cur;
  while (error == 0 && (cur = fts_read(fts)) != nullptr) {
    int fn_flag;
    switch (cur->fts_info) {
      case FTS_D:
        // In the postorder case, we'll translate FTS_DP to FTW_DP later.
        // In the can't-access case, we'll translate FTS_DNR to FTW_DNR later.
        if (postorder || access(cur->fts_path, R_OK) == -1) continue;
        fn_flag = FTW_D;
        break;
      case FTS_DNR:
        fn_flag = FTW_DNR;
        break;
      case FTS_DP:
        if (!postorder) continue;
        fn_flag = FTW_DP;
        break;
      case FTS_F:
      case FTS_DEFAULT:
        fn_flag = FTW_F;
        break;
      case FTS_NS:
      case FTS_NSOK:
        fn_flag = FTW_NS;
        break;
      case FTS_SL:
        fn_flag = FTW_SL;
        break;
      case FTS_SLNONE:
        fn_flag = (nftw_fn != nullptr) ? FTW_SLN : FTW_NS;
        break;
      case FTS_DC:
        errno = ELOOP;
        error = -1;
        continue;
      default:
        error = -1;
        continue;
    }

    // Call the appropriate function.
    if (nftw_fn != nullptr) {
      FTW ftw;
      ftw.base = cur->fts_pathlen - cur->fts_namelen;
      ftw.level = cur->fts_level;
      error = nftw_fn(cur->fts_path, cur->fts_statp, fn_flag, &ftw);
    } else {
      error = ftw_fn(cur->fts_path, cur->fts_statp, fn_flag);
    }
  }

  int saved_errno = errno;
  if (fts_close(fts) != 0 && error == 0) {
    error = -1;
  } else {
    errno = saved_errno;
  }
  return error;
}