#include #include #include #include #include #define K_VISION_DIR "/hard/k_vision" #define AI_VISION_DIR "/hard/ai_vision" namespace fs = boost::filesystem; std::vector get_file_names(const std::string& strDir) { std::vector vs; fs::path path(strDir); if (!fs::exists(path)) { return vs; } fs::directory_iterator dir_iter(path); for (auto it:dir_iter) { if (fs::is_regular_file(it) && fs::exists(it)) { vs.push_back(it.path().filename().string()); } } return vs; } std::vector get_file_paths(const std::string& strDir) { std::vector vs; fs::path path(strDir); if (!fs::exists(path)) { return vs; } fs::directory_iterator dir_iter(path); for (auto it:dir_iter) { if (fs::is_regular_file(it) && fs::exists(it)) { vs.push_back(it.path().string()); } } return vs; } std::vector get_search_paths(const std::string& strFileName, int nBeginTime, int nEndTime) { std::vector vs; if (strFileName.empty()) { return vs; } std::string strDate = strFileName.substr(0,13); int date[6] = {0}; sscanf(strDate.c_str(),"%02d%02d%02d_%02d%02d%02d",&date[0],&date[1],&date[2],&date[3],&date[4],&date[5]); struct tm input_time = {0}; input_time.tm_year = date[0] + 2000 - 1900; input_time.tm_mon = date[1] - 1; input_time.tm_mday = date[2]; input_time.tm_hour = date[3]; input_time.tm_min = date[4]; input_time.tm_sec = date[5]; time_t tt[3] = {0}; tt[0] = mktime(&input_time); tt[1] = tt[0] + nBeginTime; tt[2] = tt[0] + nEndTime; tm* tm_begin = localtime(&tt[1]); char szPathBegin[255]; snprintf(szPathBegin, sizeof(szPathBegin),"/ssd/ok/%d%02d%02d/%d%02d%02d_%02d/%d%02d%02d_%02d%02d/", tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, tm_begin->tm_min); tm* tm_end = localtime(&tt[2]); char szPathEnd[255]; snprintf(szPathEnd, sizeof(szPathEnd),"/ssd/ok/%d%02d%02d/%d%02d%02d_%02d/%d%02d%02d_%02d%02d/", tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, tm_end->tm_min); if (std::string(szPathBegin) == std::string(szPathEnd)) { vs.push_back(szPathBegin); } else { vs.push_back(szPathBegin); vs.push_back(szPathEnd); } return vs; } std::vector get_file_timestamp_range(const std::string& strFileName, int nBeginTime, int nEndTime) { std::vector vs; if (strFileName.empty() || nBeginTime > nEndTime) { return vs; } std::string strDate = strFileName.substr(0,13); int date[6] = {0}; sscanf(strDate.c_str(),"%02d%02d%02d_%02d%02d%02d",&date[0],&date[1],&date[2],&date[3],&date[4],&date[5]); struct tm input_time = {0}; input_time.tm_year = date[0] + 2000 - 1900; input_time.tm_mon = date[1] - 1; input_time.tm_mday = date[2]; input_time.tm_hour = date[3]; input_time.tm_min = date[4]; input_time.tm_sec = date[5]; time_t tt[3] = {0}; tt[0] = mktime(&input_time); tt[1] = tt[0] + nBeginTime; tt[2] = tt[0] + nEndTime; tm* tm_begin = localtime(&tt[1]); char szBegin[255]; snprintf(szBegin, sizeof(szBegin),"%d%02d%02d%02d%02d%02d", tm_begin->tm_year + 1900, tm_begin->tm_mon + 1, tm_begin->tm_mday, tm_begin->tm_hour, tm_begin->tm_min, tm_begin->tm_sec); tm* tm_end = localtime(&tt[2]); char szEnd[255]; snprintf(szEnd, sizeof(szEnd),"%d%02d%02d%02d%02d%02d", tm_end->tm_year + 1900, tm_end->tm_mon + 1, tm_end->tm_mday, tm_end->tm_hour, tm_end->tm_min, tm_end->tm_sec); vs.push_back(std::atoll(szBegin)); vs.push_back(std::atoll(szEnd)); return vs; } long long get_file_timestamp(const std::string& strPath) { std::string str; if (strPath.empty()) { return 0; } fs::path path(strPath); str = path.filename().string(); auto strTime = str.substr(0, 15); strTime.erase(8, 1); return std::atoll(strTime.c_str()); } int main(int argc, char* argv[]) { int nParam[2] = {0}; if (argc != 3) { std::cout << "error: input param invalid" << std::endl; std::cout << "usage: ./ajx_find_files 8 12" << std::endl; return -1; } else { nParam[0] = std::atoi(argv[1]); nParam[1] = std::atoi(argv[2]); std::cout << "command-line: " << argv[0] << " " << argv[1] << " " << argv[2] << std::endl; } auto tt = time(nullptr); auto tm = localtime(&tt); char szSaveDir[255]; snprintf(szSaveDir, sizeof (szSaveDir), "%s/%d%02d%02d/", AI_VISION_DIR, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); fs::create_directories(szSaveDir); auto vec_file_name = get_file_names(K_VISION_DIR); if (vec_file_name.empty()) { std::cout << "error: K_VISION_DIR is empty" << std::endl; return -1; } std::set set_matched_path; std::cout << "info: >>> match file begin <<<" << std::endl; int j = 0; for (auto it:vec_file_name) { ++j; auto vec_datetime = get_file_timestamp_range(it, nParam[0], nParam[1]); if (vec_datetime.empty()) { continue; } auto vec_search_path = get_search_paths(it, nParam[0], nParam[1]); for (auto dir:vec_search_path) { auto vec_file_path = get_file_paths(dir); for (auto path:vec_file_path) { auto timestamp = get_file_timestamp(path); if (timestamp >= vec_datetime.front() && timestamp <= vec_datetime.back()) { set_matched_path.insert(path); } } } std::cout << "info: match progress " << j << "/" << vec_file_name.size() << std::endl; } std::cout << "info: >>> match file end(" << set_matched_path.size() << ") <<<" << std::endl; std::cout << "info: >>> copy file begin <<<" << std::endl; int i = 0; for (auto it:set_matched_path) { fs::path path(it); auto strName = path.filename().string(); std::string strPath = std::string(szSaveDir) + strName; boost::system::error_code err_code; fs::copy_file(it, strPath, fs::copy_option::overwrite_if_exists, err_code); if (err_code.value() == 0) { ++i; } else { std::cout << "error: copy file failed - " << it << std::endl; } if (i % 50 == 0) { std::cout << "info: copy progress " << i << "/" << set_matched_path.size() << std::endl; } } std::cout << "info: >>> copy file end(" << i << ") <<<" << std::endl; return 0; }