void RemoteHandler:: getFileList(){ string f_list; DIR* dirp = opendir(curr_dir.c_str()); struct dirent *dp; while ((dp = readdir(dirp)) != NULL){ struct stat buf; lstat(dp->d_name, &buf); string fname(dp->d_name); if (fname != ".") add_row(fname, &buf, f_list); } f_list += "\n"; //cout << f_list; int cmd_fd = c_node.command_fd; IOStream* ioStream; try{ ioStream->writen(cmd_fd, f_list.c_str(), f_list.length()); } catch(IOStream::IOException){ cerr << "IOException! Get File List Error!\n" << flush; return; } return; }
void RemoteHandler:: getFile(char* filename){ string fullpath; int pos = curr_dir.find_last_of("/"); if(pos == curr_dir.length()-1) fullpath = curr_dir + string(filename); else fullpath = curr_dir + "/" + string(filename); int cmd_fd = c_node.command_fd; int data_fd = c_node.data_fd; IOStream* ioStream; FILE* fileFD = fopen(fullpath.c_str(), "r"); long fsize = fileSize(fileFD); char reply[128]; sprintf(reply, "size|%ld\n", fsize); try{ ioStream->writen(cmd_fd, reply, strlen(reply)); long toSend = fsize; long send_buf; long buf_size = 1024; char buf[buf_size]; send_buf = min(toSend, buf_size); while((fread(buf, 1, send_buf, fileFD) >0) && (toSend >0)){ ioStream->writen(data_fd, buf, send_buf); toSend = toSend-send_buf; send_buf = min(toSend, buf_size); } fclose(fileFD); cout << "[File Sent!]\n" << endl << flush; return; } catch(IOStream::IOException){ cerr << "IOException! in get file, write to GUI Error!\n" << flush; return; } }