/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_int_t xm_os_cpfile(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); // get the source and destination tb_char_t const* src = luaL_checkstring(lua, 1); tb_char_t const* dst = luaL_checkstring(lua, 2); tb_check_return_val(src && dst, 0); // done os.cpfile(src, dst) lua_pushboolean(lua, tb_file_copy(src, dst)); // ok return 1; }
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; }