/* ////////////////////////////////////////////////////////////////////////////////////// * main */ tb_int_t tb_demo_platform_directory_main(tb_int_t argc, tb_char_t** argv) { #if 1 // home tb_char_t home[TB_PATH_MAXN] = {0}; if (tb_directory_home(home, sizeof(home))) tb_trace_i("home: %s", home); // current tb_char_t current[TB_PATH_MAXN] = {0}; if (tb_directory_current(current, sizeof(current))) tb_trace_i("current: %s", current); // temporary tb_char_t temporary[TB_PATH_MAXN] = {0}; if (tb_directory_temporary(temporary, sizeof(temporary))) tb_trace_i("temporary: %s", temporary); #elif 0 // current tb_char_t current[TB_PATH_MAXN] = {0}; if (tb_directory_current(current, sizeof(current))) tb_trace_i("current: %s", current); // current tb_directory_walk(argv[1]? argv[1] : current, tb_true, tb_true, tb_directory_walk_func, tb_null); #elif 0 tb_directory_remove(argv[1]); #else // tb_directory_walk(argv[1], tb_true, tb_true, tb_directory_walk_func, tb_null); tb_directory_copy(argv[1], argv[2]); #endif return 0; }
static tb_bool_t tb_directory_walk_copy(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) { // check tb_value_t* tuple = (tb_value_t*)priv; tb_assert_and_check_return_val(path && info && tuple, tb_false); // the dest directory tb_char_t const* dest = tuple[0].cstr; tb_assert_and_check_return_val(dest, tb_false); // the file name tb_size_t size = tuple[1].ul; tb_char_t const* name = path + size; // the dest file path tb_char_t dpath[8192] = {0}; tb_snprintf(dpath, 8192, "%s\\%s", dest, name[0] == '\\'? name + 1 : name); // remove the dest file first tb_file_info_t dinfo = {0}; if (tb_file_info(dpath, &dinfo)) { if (dinfo.type == TB_FILE_TYPE_FILE) tb_file_remove(dpath); if (dinfo.type == TB_FILE_TYPE_DIRECTORY) tb_directory_remove(dpath); } // copy switch (info->type) { case TB_FILE_TYPE_FILE: if (!tb_file_copy(path, dpath)) tuple[2].b = tb_false; break; case TB_FILE_TYPE_DIRECTORY: if (!tb_directory_create(dpath)) tuple[2].b = tb_false; break; default: break; } // continue return tb_true; }