void server::pwd() { char path[BUFFSIZE] = {0}; if(NULL == m_guider.pwd(path, BUFFSIZE)) { ;// Error . strcpy(path, "unknow path"); } // Add the pack type. m_buff.push_front(PT_DATA); // Add the path size. char tmp[2] = {0}; parse::parse_str(tmp, 2, strlen(path)); m_buff.insert(m_buff.begin() + 1, tmp, tmp + 2); // Add the path. add_buff(path, strlen(path)); m_analyser.load_data(m_buff); // Add the checkcode. m_buff.push_back(m_analyser.check()); if(-1 == send_data() ) { ;// Error. } }
int get_next_line(int const fd, char **line) { static char *rest_line; char *buff; int nread; int next_start; int ret_man; if (rest_line) if ((ret_man = manage_rest_line(&rest_line, line))) return (ret_man); buff = (char *)malloc(sizeof(char) * (BUFF + 1)); if (!(*line)) { *line = (char *)malloc(sizeof(char)); (*line)[0] = 0; } next_start = 0; while ((nread = read(fd, buff, BUFF)) && nread != -1 && !next_start) { buff[nread] = 0; if ((next_start = add_buff(line, buff)) == -1) { free (buff); return (-1); } } if (next_start > 0) return (fill_rest_line(&rest_line, buff, next_start) == -1) ? -1 : 1; return (0); }
int client::mkdir() { int res = 0; if(m_cmd.size() < 2) { cout << "missing operand" << endl; return res; } char dir_name[BUFFSIZE] = {0}; strcpy(dir_name, m_cmd[1]); if( m_guider.is_at_local() ) { int old_mode = umask(0); if(-1 == (res = ::mkdir(dir_name, 0775)) ) { cerr << "error mkdir in " << __FILE__ << " : " << __LINE__ << endl; } umask(old_mode); } else { m_buff.clear(); // Add the pack type. m_buff.push_front(PT_MKDIR); // Add the new dir's name add_buff(dir_name, strlen(dir_name)); // Add the checkcode. m_analyser.load_data(m_buff); m_buff.push_back(m_analyser.check()); if(-1 != (res = send_data()) ) { char pack_type = acceptable(); if( is_pack_type(PT_DOERR, pack_type) ) { cout << "error mkdir" << endl; } else if( is_pack_type(PT_EENT, pack_type) ) { cout << "directory's name is exist" << endl; } } } return res; }
int client::download() { int res = 0; char file_name[BUFFSIZE] = {0}; if(m_cmd.size() < 2) { do { cout << "file name: "; cout.flush(); cin.getline(file_name, BUFFSIZE); }while(file_name[0] == '\0'); } else { strncpy(file_name, m_cmd[1], BUFFSIZE); } m_buff.clear(); // Add the pack type. m_buff.push_back(PT_DWREQ); // Add the filename. add_buff(file_name, strlen(file_name)); // Add checkcode. m_analyser.load_data(m_buff); m_buff.push_back(m_analyser.check()); if(-1 == send_data()) { ;// Send error. } char pack_type = acceptable(); if( is_pack_type(PT_OK, pack_type) ) { if(-1 == recv_file_info() ) { return -1;// Recv error. } res = recv_file(); } else if( is_pack_type(PT_FNOEXIST, pack_type) ) { cout << "file not exist" << endl;; } return res; }
int client::rm() { if(m_cmd.size() < 2) { cout << "missing oprand." << endl; return 0; } char file_dir[BUFFSIZE] = {0}; strcpy(file_dir, m_cmd[1]); if( m_guider.is_at_local() ) { return _delete(file_dir); } else { m_buff.clear(); // Add the pack type. m_buff.push_front(PT_RM); // Add the oprand. add_buff(file_dir, strlen(file_dir) ); // Add the checkcode. m_analyser.load_data(m_buff); m_buff.push_back(m_analyser.check()); if( -1 == send_data() ) { return -1;// Send error. } char pack_type = acceptable(); if( is_pack_type(PT_NOENT, pack_type) ) { cout << "file or directory is not exist" << endl; } else if( is_pack_type(PT_DOERR, pack_type) ) { cout << "remove file or directory error" << endl; } } return 0; }
FILE *_gcov_fopen(char *name, char *mode) { if(!gcov_enable) return; assert(!gcov_opened); /* write information to buffer */ add_int(GCOVOP_OPEN); add_int(strlen(name)+1); add_buff(name, strlen(name)+1); gcov_opened = 1; /* return dummy FILE *. */ return &gcov_file; }
size_t _gcov_fwrite(void *ptr, size_t itemsize, size_t nitems, FILE *stream) { int size = itemsize * nitems; if(!gcov_enable) return; /* only have one file open at a time to ensure writes go * to the right place. */ assert(gcov_opened); assert(stream == &gcov_file); /* write information to buffer */ add_int(GCOVOP_WRITE); add_int(size); add_buff(ptr, size); return nitems; }
int client::cd() { const char * path = ( (m_cmd.size() > 1) ? m_cmd[1] : "~"); if( m_guider.is_at_local() ) { return m_guider.cd( path ); } m_buff.clear(); // Add the pack type. m_buff.push_front(PT_CD); // Add the path into m_buff. add_buff(path, strlen(path)); // Add the checkcode. m_analyser.load_data(m_buff); m_buff.push_back(m_analyser.check() ); if(-1 == send_data() ) { return -1;// Send pack error. } if( acceptable(PT_NOENT) ) { cout << "' " << path << " '" << "is not exist or not a directory" << endl; } return 0; }
/* easy wrapper for add_buff */ static void add_int(int value) { add_buff((void *) &value, sizeof(int)); }
int client::ls() { const char *tmp[BUFFSIZE] = {0}; if( m_guider.is_at_local() ) { // For local 'ls', it can show more // than one directory. // for(int i = 0; i < m_cmd.size() && i < 10; ++i) { tmp[i] = m_cmd[i]; } // Call the global function ls to show the // information of the specified path in local. if(-1 == ::ls(tmp) ) { return -1; } } else { // For remote 'ls', it can show just // only one directory. // m_buff.clear(); // Add the pack type. m_buff.push_front(PT_LS); // If it has a parameter. if(m_cmd.size() > 1) { // Add the dir-name add_buff(m_cmd[1], strlen(m_cmd[1])); } else { // Show current directory. m_buff.push_back('.'); } // Add the checkcode into the tail of buff. m_analyser.load_data(m_buff); m_buff.push_back(m_analyser.check()); if( -1 == send_data() ) { return -1; } if(-1 == recv_data() ) { return -1; } if( is_pack_type(PT_DATA, m_analyser.get_pack_type()) ) { // Delete the pack type. m_buff.pop_front(); // Print the data. char buff[BUFFSIZE] = {0}; get_buff(buff, BUFFSIZE); cout << buff << endl; } else { ; // Uninterested package. } } return 0; }