| 36 | constexpr int FTW_MAX_FD{20}; |
| 37 | |
| 38 | int DoTravelDir(const string &root_src_dir_path, const string &relative_src_dir_path, const string &root_dst_dir_path, |
| 39 | vector<string> *file_paths, vector<string> *sub_dir_paths, const bool recursive, |
| 40 | const function<int (const string &, const string &, const string &)> &file_before_callback = {}, |
| 41 | const function<int (const string &, const string &, const string &)> &file_after_callback = {}, |
| 42 | const function<int (const string &, const string &, const string &)> &sub_dir_before_callback = {}, |
| 43 | const function<int (const string &, const string &, const string &)> &sub_dir_after_callback = {}, |
| 44 | const int level = 0) { |
| 45 | string absolute_src_dir_path{root_src_dir_path}; |
| 46 | if (!relative_src_dir_path.empty()) |
| 47 | absolute_src_dir_path += "/" + relative_src_dir_path; |
| 48 | |
| 49 | DIR *dir{nullptr}; |
| 50 | dirent *dirent{nullptr}; |
| 51 | if (!(dir = opendir(absolute_src_dir_path.c_str()))) { |
| 52 | // TODO: |
| 53 | //QLErr("level %d opendir \"%s\" err %d", level, absolute_src_dir_path.c_str(), errno); |
| 54 | |
| 55 | return errno; |
| 56 | } |
| 57 | |
| 58 | while (dirent = readdir(dir)) { |
| 59 | //QLVerb("level %d sub \"%s\"", level, dirent->d_name); |
| 60 | |
| 61 | const string absolute_sub_path{absolute_src_dir_path + "/" + dirent->d_name}; |
| 62 | if (DT_DIR == dirent->d_type) { |
| 63 | //QLVerb("level %d subdir \"%s\"", level, dirent->d_name); |
| 64 | |
| 65 | // directory |
| 66 | if (0 == strcmp(dirent->d_name, ".") || 0 == strcmp(dirent->d_name, "..")) |
| 67 | continue; |
| 68 | |
| 69 | string relative_sub_dir_path; |
| 70 | if (relative_src_dir_path.empty()) |
| 71 | relative_sub_dir_path = dirent->d_name; |
| 72 | else |
| 73 | relative_sub_dir_path = relative_src_dir_path + "/" + dirent->d_name; |
| 74 | |
| 75 | int ret{0}; |
| 76 | int call_back_ret{0}; |
| 77 | if (sub_dir_before_callback) { |
| 78 | call_back_ret = sub_dir_before_callback(root_src_dir_path, relative_sub_dir_path, root_dst_dir_path); |
| 79 | if (0 != call_back_ret) { |
| 80 | // TODO: |
| 81 | //QLErr("sub_dir_before_callback err %d", call_back_ret); |
| 82 | } |
| 83 | } |
| 84 | if (sub_dir_paths) { |
| 85 | sub_dir_paths->emplace_back(relative_sub_dir_path); |
| 86 | } |
| 87 | if (recursive) { |
| 88 | ret = DoTravelDir(root_src_dir_path, relative_sub_dir_path, root_dst_dir_path, |
| 89 | file_paths, sub_dir_paths, recursive, |
| 90 | file_before_callback, file_after_callback, sub_dir_before_callback, sub_dir_after_callback, |
| 91 | level + 1); |
| 92 | } |
| 93 | if (sub_dir_after_callback) { |
| 94 | call_back_ret = sub_dir_after_callback(root_src_dir_path, relative_sub_dir_path, root_dst_dir_path); |
| 95 | if (0 != call_back_ret) { |
no outgoing calls
no test coverage detected