int Recovery::recover(StoreServer &server) { std::vector<std::string> cmds; std::string filename = CMD_SAVE_PATH_FOLDER; int err = 0; for (std::size_t i = 0; i < server.db().size(); ++i) { filename = CMD_SAVE_PATH_FOLDER; filename = filename + "/db" + utils::to_string(i) + ".kvdb"; cmds.clear(); err = read_file(filename, cmds); if (err < 0) { LOG_DEBUG<<"read_file error,filename:" << filename; continue; } //执行命令 err = execute_cmds(server, i, cmds); if (err < 0) { //命令有一条执行失败,就清空那一整个数据库 LOG_DEBUG<<"cmd syntax error"; server.db().clear(i); continue; } } return 0; }
int evaluate(struct SLList *tokens) { struct SLList cmds; init_list(&cmds); IOModifier prev_mod = NO_MODIFIER; struct ExecutableCmd *exe = (struct ExecutableCmd *)malloc(sizeof(struct ExecutableCmd)); init_executable_cmd(exe); while (tokens->length != 0) { struct Token *tok = pop_front(tokens); // check for output redirection conflicts here. if ( prev_mod == PIPE ) { if (exe->output_redir_to) { // conflict // dump the token and cmd lists. destroy_token(tok); while ( (tok = pop_front(tokens)) ) destroy_token(tok); destroy_exe_cmd(exe); destroy_cmd_list(&cmds); fprintf(stderr, "error: syntax error in output redirection.\n"); return -1; } add_back(&cmds, exe); exe = (struct ExecutableCmd *)malloc(sizeof(struct ExecutableCmd)); init_executable_cmd(exe); prev_mod = NO_MODIFIER; } // use previous token's mod, if mod exists, // and this token's info to populate redirection of exe. if (prev_mod == INPUT_REDIR) { exe->input_redir_from = make_file_path(tok->name); } else if (prev_mod == OUTPUT_REDIR) { exe->output_redir_to = make_file_path(tok->name); } else if (prev_mod == ERR_OUTPUT_REDIR) { exe->err_output_redir_to = make_file_path(tok->name); } if ( prev_mod == NO_MODIFIER ) { if ( (exe->full_path = find_cmd(tok->name)) != NULL ) { exe->args = populate_args(tok); if (tok->mod == NO_MODIFIER) { add_back(&cmds, exe); } } else { fprintf(stderr, "error: not a valid command.\n"); destroy_token(tok); destroy_exe_cmd(exe); destroy_cmd_list(&cmds); return -1; } } else if (tok->mod == NO_MODIFIER) { add_back(&cmds, exe); } prev_mod = tok->mod; destroy_token(tok); } // Execute the commands. int exe_status = execute_cmds(&cmds); // TODO: destroy the cmds list. destroy_cmd_list(&cmds); // return 0 if everything went peachy. return exe_status; }