static retvalue regenerate_filelist(void *data, const char *filekey) { bool reread = ((struct rfd*)data)->reread; size_t l = strlen(filekey); char *debfilename; char *filelist; size_t fls; retvalue r; if (l <= 4 || memcmp(filekey+l-4, ".deb", 4) != 0) return RET_NOTHING; if (!reread && !table_recordexists(rdb_contents, filekey)) return RET_NOTHING; debfilename = files_calcfullfilename(filekey); if (FAILEDTOALLOC(debfilename)) return RET_ERROR_OOM; r = getfilelist(&filelist, &fls, debfilename); free(debfilename); if (RET_IS_OK(r)) { if (verbose > 0) (void)puts(filekey); if (verbose > 6) { const char *p = filelist; while (*p != '\0') { (void)putchar(' '); (void)puts(p); p += strlen(p)+1; } } r = table_adduniqsizedrecord(rdb_contents, filekey, filelist, fls, true, true); free(filelist); } return r; }
static int parse_options(int argc, char *argv[]) { int c; int opt_index = -1; gn_error rc; /* Every option should be in this array. */ static struct option long_options[] = { /* FIXME: these comments are nice, but they would be more useful as docs for the user */ /* Display usage. */ { "help", optional_argument, NULL, OPT_HELP }, /* Display version and build information. */ { "version", no_argument, NULL, OPT_VERSION }, /* Monitor mode */ { "monitor", optional_argument, NULL, OPT_MONITOR }, /* Alternate config file location */ { "config", required_argument, NULL, OPT_CONFIGFILE }, /* Alternate phone section from the config */ { "phone", required_argument, NULL, OPT_CONFIGMODEL }, /* Get Security Code */ { "getsecuritycode", no_argument, NULL, OPT_GETSECURITYCODE }, /* Enter Security Code mode */ { "entersecuritycode", required_argument, NULL, OPT_ENTERSECURITYCODE }, /* Get Security Code status */ { "getsecuritycodestatus", no_argument, NULL, OPT_GETSECURITYCODESTATUS }, /* Change Security Code */ { "changesecuritycode", required_argument, NULL, OPT_CHANGESECURITYCODE }, /* Set date and time */ { "setdatetime", optional_argument, NULL, OPT_SETDATETIME }, /* Get date and time mode */ { "getdatetime", no_argument, NULL, OPT_GETDATETIME }, /* Set alarm */ { "setalarm", optional_argument, NULL, OPT_SETALARM }, /* Get alarm */ { "getalarm", no_argument, NULL, OPT_GETALARM }, /* Voice call mode */ { "dialvoice", required_argument, NULL, OPT_DIALVOICE }, /* Answer the incoming call */ { "answercall", required_argument, NULL, OPT_ANSWERCALL }, /* Hangup call */ { "hangup", required_argument, NULL, OPT_HANGUP }, /* Get ToDo note mode */ { "gettodo", required_argument, NULL, OPT_GETTODO }, /* Write ToDo note mode */ { "writetodo", required_argument, NULL, OPT_WRITETODO }, /* Delete all ToDo notes mode */ { "deletealltodos", no_argument, NULL, OPT_DELETEALLTODOS }, /* Get calendar note mode */ { "getcalendarnote", required_argument, NULL, OPT_GETCALENDARNOTE }, /* Write calendar note mode */ { "writecalendarnote", required_argument, NULL, OPT_WRITECALENDARNOTE }, /* Delete calendar note mode */ { "deletecalendarnote", required_argument, NULL, OPT_DELCALENDARNOTE }, /* Get display status mode */ { "getdisplaystatus", no_argument, NULL, OPT_GETDISPLAYSTATUS }, /* Get memory mode */ { "getphonebook", required_argument, NULL, OPT_GETPHONEBOOK }, /* Write phonebook (memory) mode */ { "writephonebook", optional_argument, NULL, OPT_WRITEPHONEBOOK }, /* Delete phonebook entry from memory mode */ { "deletephonebook", required_argument, NULL, OPT_DELETEPHONEBOOK }, /* Get speed dial mode */ { "getspeeddial", required_argument, NULL, OPT_GETSPEEDDIAL }, /* Set speed dial mode */ { "setspeeddial", required_argument, NULL, OPT_SETSPEEDDIAL }, /* Create SMS folder mode */ { "createsmsfolder", required_argument, NULL, OPT_CREATESMSFOLDER }, /* Delete SMS folder mode */ { "deletesmsfolder", required_argument, NULL, OPT_DELETESMSFOLDER }, /* Show SMS folder names and its attributes */ { "showsmsfolderstatus",no_argument, NULL, OPT_SHOWSMSFOLDERSTATUS }, /* Get SMS message mode */ { "getsms", required_argument, NULL, OPT_GETSMS }, /* Delete SMS message mode */ { "deletesms", required_argument, NULL, OPT_DELETESMS }, /* Send SMS message mode */ { "sendsms", required_argument, NULL, OPT_SENDSMS }, /* Ssve SMS message mode */ { "savesms", optional_argument, NULL, OPT_SAVESMS }, /* Send logo as SMS message mode */ { "sendlogo", required_argument, NULL, OPT_SENDLOGO }, /* Send ringtone as SMS message */ { "sendringtone", required_argument, NULL, OPT_SENDRINGTONE }, /* Get ringtone */ { "getringtone", required_argument, NULL, OPT_GETRINGTONE }, /* Set ringtone */ { "setringtone", required_argument, NULL, OPT_SETRINGTONE }, /* Play ringtone */ { "playringtone", required_argument, NULL, OPT_PLAYRINGTONE }, /* Convert ringtone */ { "ringtoneconvert", required_argument, NULL, OPT_RINGTONECONVERT }, /* Get list of the ringtones */ { "getringtonelist", no_argument, NULL, OPT_GETRINGTONELIST }, /* Delete ringtones */ { "deleteringtone", required_argument, NULL, OPT_DELETERINGTONE }, /* Get SMS center number mode */ { "getsmsc", optional_argument, NULL, OPT_GETSMSC }, /* Set SMS center number mode */ { "setsmsc", no_argument, NULL, OPT_SETSMSC }, /* For development purposes: run in passive monitoring mode */ { "pmon", no_argument, NULL, OPT_PMON }, /* NetMonitor mode */ { "netmonitor", required_argument, NULL, OPT_NETMONITOR }, /* Identify */ { "identify", no_argument, NULL, OPT_IDENTIFY }, /* Send DTMF sequence */ { "senddtmf", required_argument, NULL, OPT_SENDDTMF }, /* Resets the phone */ { "reset", required_argument, NULL, OPT_RESET }, /* Set logo */ { "setlogo", required_argument, NULL, OPT_SETLOGO }, /* Get logo */ { "getlogo", required_argument, NULL, OPT_GETLOGO }, /* View logo */ { "viewlogo", required_argument, NULL, OPT_VIEWLOGO }, /* Show profile */ { "getprofile", optional_argument, NULL, OPT_GETPROFILE }, /* Set profile */ { "setprofile", no_argument, NULL, OPT_SETPROFILE }, /* Get the active profile */ { "getactiveprofile", no_argument, NULL, OPT_GETACTIVEPROFILE }, /* Set the active profile */ { "setactiveprofile", required_argument, NULL, OPT_SETACTIVEPROFILE }, /* Show texts from phone's display */ { "displayoutput", no_argument, NULL, OPT_DISPLAYOUTPUT }, /* Simulate pressing the keys */ { "keysequence", no_argument, NULL, OPT_KEYPRESS }, /* Simulate pressing the keys */ { "enterchar", no_argument, NULL, OPT_ENTERCHAR }, /* Divert calls */ { "divert", no_argument, NULL, OPT_DIVERT }, /* SMS reader */ { "smsreader", no_argument, NULL, OPT_SMSREADER }, /* For development purposes: insert you function calls here */ { "foogle", no_argument, NULL, OPT_FOOGLE }, /* Get WAP bookmark */ { "getwapbookmark", required_argument, NULL, OPT_GETWAPBOOKMARK }, /* Write WAP bookmark */ { "writewapbookmark", required_argument, NULL, OPT_WRITEWAPBOOKMARK }, /* Delete WAP bookmark */ { "deletewapbookmark", required_argument, NULL, OPT_DELETEWAPBOOKMARK }, /* Get WAP setting */ { "getwapsetting", required_argument, NULL, OPT_GETWAPSETTING }, /* Write WAP setting */ { "writewapsetting", no_argument, NULL, OPT_WRITEWAPSETTING }, /* Activate WAP setting */ { "activatewapsetting", required_argument, NULL, OPT_ACTIVATEWAPSETTING }, /* List GSM networks */ { "listnetworks", no_argument, NULL, OPT_LISTNETWORKS }, /* Get network info */ { "getnetworkinfo", no_argument, NULL, OPT_GETNETWORKINFO }, /* Get (sim)lock info */ { "getlocksinfo", no_argument, NULL, OPT_GETLOCKSINFO }, /* Get file list */ { "getfilelist", required_argument, NULL, OPT_GETFILELIST }, /* Get file details by id */ { "getfiledetailsbyid", optional_argument, NULL, OPT_GETFILEDETAILSBYID }, /* Get file id */ { "getfileid", required_argument, NULL, OPT_GETFILEID }, /* Get file */ { "getfile", required_argument, NULL, OPT_GETFILE }, /* Get file by id */ { "getfilebyid", required_argument, NULL, OPT_GETFILEBYID }, /* Get all files */ { "getallfiles", required_argument, NULL, OPT_GETALLFILES }, /* Put a file */ { "putfile", required_argument, NULL, OPT_PUTFILE }, /* Delete a file */ { "deletefile", required_argument, NULL, OPT_DELETEFILE }, /* Delete a file by id */ { "deletefilebyid", required_argument, NULL, OPT_DELETEFILEBYID }, /* Shell like interface */ { "shell", no_argument, NULL, OPT_SHELL }, /* Get MMS message mode */ { "getmms", required_argument, NULL, OPT_GETMMS }, /* Delete MMS message mode */ { "deletemms", required_argument, NULL, OPT_DELETEMMS }, /* Check if the phone responds */ { "ping", no_argument, NULL, OPT_PING }, { 0, 0, 0, 0 }, }; /* Every command which requires arguments should have an appropriate entry in this array. */ static struct gnokii_arg_len gals[] = { { OPT_HELP, 1, 100, 0 }, { OPT_CONFIGFILE, 1, 100, 0 }, { OPT_CONFIGMODEL, 1, 100, 0 }, { OPT_ENTERSECURITYCODE, 1, 100, 0 }, { OPT_CHANGESECURITYCODE,1, 1, 0 }, { OPT_SETDATETIME, 0, 5, 0 }, { OPT_SETALARM, 0, 2, 0 }, { OPT_DIALVOICE, 1, 1, 0 }, { OPT_ANSWERCALL, 1, 1, 0 }, { OPT_HANGUP, 1, 1, 0 }, { OPT_GETTODO, 1, 3, 0 }, { OPT_WRITETODO, 2, 3, 0 }, { OPT_GETCALENDARNOTE, 1, 3, 0 }, { OPT_WRITECALENDARNOTE, 2, 3, 0 }, { OPT_DELCALENDARNOTE, 1, 2, 0 }, { OPT_GETPHONEBOOK, 2, 4, 0 }, { OPT_WRITEPHONEBOOK, 0, 10, 0 }, { OPT_DELETEPHONEBOOK, 2, 3, 0 }, { OPT_GETSPEEDDIAL, 1, 1, 0 }, { OPT_SETSPEEDDIAL, 3, 3, 0 }, { OPT_CREATESMSFOLDER, 1, 1, 0 }, { OPT_DELETESMSFOLDER, 1, 1, 0 }, { OPT_GETSMS, 2, 6, 0 }, { OPT_DELETESMS, 2, 3, 0 }, { OPT_SENDSMS, 1, 10, 0 }, { OPT_SAVESMS, 0, 12, 0 }, { OPT_SENDLOGO, 3, 4, GAL_XOR }, { OPT_SENDRINGTONE, 2, 2, 0 }, { OPT_GETSMSC, 0, 3, 0 }, { OPT_GETWELCOMENOTE, 1, 1, 0 }, { OPT_SETWELCOMENOTE, 1, 1, 0 }, { OPT_NETMONITOR, 1, 1, 0 }, { OPT_SENDDTMF, 1, 1, 0 }, { OPT_SETLOGO, 1, 4, 0 }, { OPT_GETLOGO, 1, 4, 0 }, { OPT_VIEWLOGO, 1, 1, 0 }, { OPT_GETRINGTONE, 1, 3, 0 }, { OPT_SETRINGTONE, 1, 5, 0 }, { OPT_PLAYRINGTONE, 1, 3, 0 }, { OPT_RINGTONECONVERT, 2, 2, 0 }, { OPT_DELETERINGTONE, 1, 2, 0 }, { OPT_RESET, 0, 1, 0 }, { OPT_GETPROFILE, 0, 3, 0 }, { OPT_SETACTIVEPROFILE, 1, 1, 0 }, { OPT_DIVERT, 6, 10, 0 }, { OPT_GETWAPBOOKMARK, 1, 1, 0 }, { OPT_WRITEWAPBOOKMARK, 2, 2, 0 }, { OPT_DELETEWAPBOOKMARK, 1, 1, 0 }, { OPT_GETWAPSETTING, 1, 2, 0 }, { OPT_ACTIVATEWAPSETTING,1, 1, 0 }, { OPT_MONITOR, 0, 1, 0 }, { OPT_GETFILELIST, 1, 1, 0 }, { OPT_GETFILEDETAILSBYID,0, 1, 0 }, { OPT_GETFILEID, 1, 1, 0 }, { OPT_GETFILE, 1, 2, 0 }, { OPT_GETFILEBYID, 1, 2, 0 }, { OPT_GETALLFILES, 1, 1, 0 }, { OPT_PUTFILE, 2, 2, 0 }, { OPT_DELETEFILE, 1, 1, 0 }, { OPT_DELETEFILEBYID, 1, 1, 0 }, { OPT_SHELL, 0, 0, 0 }, { OPT_GETMMS, 2, 6, 0 }, { OPT_DELETEMMS, 2, 3, 0 }, { OPT_FOOGLE, 0, 0, 0 }, { OPT_PING, 0, 0, 0 }, { 0, 0, 0, 0 }, }; /* Handle command line arguments. * -c equals to --config * -p equals to --phone */ c = getopt_long(argc, argv, "c:p:", long_options, &opt_index); switch (c) { /* No argument given - we should display usage. */ case -1: return usage(stderr, -1, NULL); /* First, error conditions */ case '?': case ':': /* --shell command does not set argv[0] */ if (argv[0]) fprintf(stderr, _("Use '%s --help' for usage information.\n"), argv[0]); else fprintf(stderr, _("Use '--help' for usage information.\n")); return 1; /* Then, options with no arguments */ case OPT_VERSION: version(); return 0; /* That's a bit ugly... */ case 'c': c = OPT_CONFIGFILE; opt_index = 0; break; case 'p': c = OPT_CONFIGMODEL; opt_index = 1; break; } /* We have to build an array of the arguments which will be passed to the functions. Please note that every text after the --command will be passed as arguments. A syntax like gnokii --cmd1 args --cmd2 args will not work as expected; instead args --cmd2 args is passed as a parameter. */ if (checkargs(c, gals, argc, long_options[opt_index].has_arg)) { return usage(stderr, -1, NULL); } /* Other options that do not need initialization */ switch (c) { case OPT_CONFIGFILE: if (configfile) return usage(stderr, -1, NULL); configfile = optarg; return parse_options(argc, argv); case OPT_CONFIGMODEL: if (configmodel) return usage(stderr, -1, NULL); configmodel = optarg; return parse_options(argc, argv); case OPT_HELP: return usage(stdout, argc, argv); case OPT_VIEWLOGO: return viewlogo(optarg); case OPT_LISTNETWORKS: list_gsm_networks(); return GN_ERR_NONE; case OPT_RINGTONECONVERT: return ringtoneconvert(argc, argv); } /* Initialise the code for the GSM interface. */ if (c != OPT_FOOGLE && state == NULL && businit()) return -1; switch (c) { /* Monitoring options */ case OPT_MONITOR: rc = monitormode(argc, argv, data, state); break; case OPT_GETDISPLAYSTATUS: rc = getdisplaystatus(data, state); break; case OPT_DISPLAYOUTPUT: rc = displayoutput(data, state); break; case OPT_NETMONITOR: rc = netmonitor(optarg, data, state); break; /* SMS options */ case OPT_SENDSMS: rc = sendsms(argc, argv, data, state); break; case OPT_SAVESMS: rc = savesms(argc, argv, data, state); break; case OPT_GETSMS: rc = getsms(argc, argv, data, state); break; case OPT_DELETESMS: rc = deletesms(argc, argv, data, state); break; case OPT_GETSMSC: rc = getsmsc(argc, argv, data, state); break; case OPT_SETSMSC: rc = setsmsc(data, state); break; case OPT_CREATESMSFOLDER: rc = createsmsfolder(optarg, data, state); break; case OPT_DELETESMSFOLDER: rc = deletesmsfolder(optarg, data, state); break; case OPT_SHOWSMSFOLDERSTATUS: rc = showsmsfolderstatus(data, state); break; case OPT_SMSREADER: rc = smsreader(data, state); break; /* Phonebook options */ case OPT_GETPHONEBOOK: rc = getphonebook(argc, argv, data, state); break; case OPT_WRITEPHONEBOOK: rc = writephonebook(argc, argv, data, state); break; case OPT_DELETEPHONEBOOK: rc = deletephonebook(argc, argv, data, state); break; /* Calendar options */ case OPT_GETCALENDARNOTE: rc = getcalendarnote(argc, argv, data, state); break; case OPT_WRITECALENDARNOTE: rc = writecalendarnote(argc, argv, data, state); break; case OPT_DELCALENDARNOTE: rc = deletecalendarnote(argc, argv, data, state); break; /* ToDo options */ case OPT_GETTODO: rc = gettodo(argc, argv, data, state); break; case OPT_WRITETODO: rc = writetodo(argc, argv, data, state); break; case OPT_DELETEALLTODOS: rc = deletealltodos(data, state); break; /* Dialling and call handling options */ case OPT_GETSPEEDDIAL: rc = getspeeddial(optarg, data, state); break; case OPT_SETSPEEDDIAL: rc = setspeeddial(argv, data, state); break; case OPT_DIALVOICE: rc = dialvoice(optarg, data, state); break; case OPT_SENDDTMF: rc = senddtmf(optarg, data, state); break; case OPT_ANSWERCALL: rc = answercall(optarg, data, state); break; case OPT_HANGUP: rc = hangup(optarg, data, state); break; case OPT_DIVERT: rc = divert(argc, argv, data, state); break; /* Profile options */ case OPT_GETPROFILE: rc = getprofile(argc, argv, data, state); break; case OPT_SETPROFILE: rc = setprofile(data, state); break; case OPT_GETACTIVEPROFILE: rc = getactiveprofile(data, state); break; case OPT_SETACTIVEPROFILE: rc = setactiveprofile(argc, argv, data, state); break; /* Phone settings options */ case OPT_RESET: rc = reset(optarg, data, state); break; case OPT_GETDATETIME: rc = getdatetime(data, state); break; case OPT_SETDATETIME: rc = setdatetime(argc, argv, data, state); break; case OPT_GETALARM: rc = getalarm(data, state); break; case OPT_SETALARM: rc = setalarm(argc, argv, data, state); break; /* WAP options */ case OPT_GETWAPBOOKMARK: rc = getwapbookmark(optarg, data, state); break; case OPT_WRITEWAPBOOKMARK: rc = writewapbookmark(argc, argv, data, state); break; case OPT_DELETEWAPBOOKMARK: rc = deletewapbookmark(optarg, data, state); break; case OPT_GETWAPSETTING: rc = getwapsetting(argc, argv, data, state); break; case OPT_WRITEWAPSETTING: rc = writewapsetting(data, state); break; case OPT_ACTIVATEWAPSETTING: rc = activatewapsetting(optarg, data, state); break; /* Logo options */ case OPT_SENDLOGO: rc = sendlogo(argc, argv, data, state); break; case OPT_SETLOGO: rc = setlogo(argc, argv, data, state); break; case OPT_GETLOGO: rc = getlogo(argc, argv, data, state); break; /* Ringtone options */ case OPT_SENDRINGTONE: rc = sendringtone(argc, argv, data, state); break; case OPT_GETRINGTONE: rc = getringtone(argc, argv, data, state); break; case OPT_SETRINGTONE: rc = setringtone(argc, argv, data, state); break; case OPT_PLAYRINGTONE: rc = playringtone(argc, argv, data, state); break; case OPT_GETRINGTONELIST: rc = getringtonelist(data, state); break; case OPT_DELETERINGTONE: rc = deleteringtone(argc, argv, data, state); break; /* Security options */ case OPT_IDENTIFY: rc = identify(state); break; case OPT_GETLOCKSINFO: rc = getlocksinfo(data, state); break; case OPT_GETSECURITYCODE: rc = getsecuritycode(data, state); break; case OPT_ENTERSECURITYCODE: rc = entersecuritycode(optarg, data, state); if (rc == GN_ERR_NONE && optind < argc) return parse_options(argc, argv); break; case OPT_GETSECURITYCODESTATUS: rc = getsecuritycodestatus(data, state); break; case OPT_CHANGESECURITYCODE: rc = changesecuritycode(optarg, data, state); break; /* File options */ case OPT_GETFILELIST: rc = getfilelist(optarg, data, state); break; case OPT_GETFILEDETAILSBYID: rc = getfiledetailsbyid(argc, argv, data, state); break; case OPT_GETFILEID: rc = getfileid(optarg, data, state); break; case OPT_GETFILE: rc = getfile(argc, argv, data, state); break; case OPT_GETFILEBYID: rc = getfilebyid(argc, argv, data, state); break; case OPT_GETALLFILES: rc = getallfiles(optarg, data, state); break; case OPT_PUTFILE: rc = putfile(argc, argv, data, state); break; case OPT_DELETEFILE: rc = deletefile(optarg, data, state); break; case OPT_DELETEFILEBYID: rc = deletefilebyid(optarg, data, state); break; /* Misc options */ case OPT_PMON: rc = pmon(data, state); break; case OPT_KEYPRESS: rc = presskeysequence(data, state); break; case OPT_ENTERCHAR: rc = enterchar(data, state); break; case OPT_GETNETWORKINFO: rc = getnetworkinfo(data, state); break; case OPT_SHELL: rc = shell(data, state); break; case OPT_PING: rc = ping(data, state); break; #ifndef WIN32 case OPT_FOOGLE: rc = foogle(argc, argv); break; #endif /* MMS options */ case OPT_GETMMS: rc = getmms(argc, argv, data, state); break; case OPT_DELETEMMS: rc = deletemms(argc, argv, data, state); break; default: rc = GN_ERR_FAILED; fprintf(stderr, _("Unknown option: %d\n"), c); break; } return rc; }
/** *函数说明:完成从服务器上传文件的过程 *参数说明:参数1是连接服务器的socket描述符 */ void upload(int st) { //得到本地文件列表和文件数目 struct fileinfo filelist[100]; memset(filelist, 0, sizeof(filelist)); int n = getfilelist(CLIENT_FILES_DIR, filelist); //如果没有文件,那么返回 if (n == 0) { //给服务器发送一个文件的错误信息,告知其停止等待 struct fileinfo file; file.filelen = 0; send(st, &file, sizeof(file), 0); printf("no file to upload\n"); return; } //打印本地文件列表 showfilelist(filelist); //输入一个合法的文件名 char filename[128]; char file_dir[128]; memset(file_dir, 0, sizeof(file_dir)); while (1) { printf("enter a filename to upload:\n"); memset(filename, 0, sizeof(filename)); read(STDIN_FILENO, filename, sizeof(filename)); //如果只是输入了一个回车,那么重新输入 if (strcmp(filename, "\n") == 0) { continue; } //把最后的回车去掉 filename[strlen(filename)-1] = '\0'; //获得文件的完整路径 strcpy(file_dir, CLIENT_FILES_DIR); strcat(file_dir, filename); //通过查看文件信息判断文件是否存在,存在则发送文件 struct stat64 filestat; if (lstat64(file_dir, &filestat) == -1) { //文件不存在,重新输入 continue; } else //如果存在,那么将文件信息发送给服务器 { //从文件名得到文件信息,包含了文件的md5值 struct fileinfo file; int i = 0; for (; i < n; i++) { if (strcmp(filelist[i].filename, filename) == 0) { break; } } file = filelist[i]; //将文件信息发送给服务器 send(st, &file, sizeof(file), 0); //接收服务器端该文件的大小,不存在则为0 long long server_file_size = 0; recv(st, &server_file_size, sizeof(server_file_size), 0); //发送文件,等待服务器反馈,即服务器的MD5校验是否成功,否则重新发送 while (sendfile(file_dir, file.filelen, server_file_size, st) == 0) { int i = 0; recv(st, &i, sizeof(i), 0); if (i == 0) { printf("MD5 check by server success!\n"); break; } else { printf("MD5 check by server faild, send again!\n"); server_file_size = 0; } } break; } } return; }
void *accept_thread(void *accept_sock) { //socket descriptor for each client int acptsock; //Retrieve the socket that passed from pthread_create acptsock= (intptr_t)accept_sock; //Buffer to send&receive data// char buf[256]; //Received bytes int bytes; //Declare the structure of the sending message in order //the filemanager to communicate with the client and vice versa FILEHEADER *msg = (FILEHEADER *)malloc(sizeof(FILEHEADER)); //While the client is connect to the system you have to keep the connection while(1) { //If connection is established then start communicating // //Initialize buffer with zeros // bzero(buf, sizeof(buf)); //Waiting...to receive data from the client// if (recv(acptsock, buf, sizeof(buf), 0) < 0) { perror("Received() failed!"); close(acptsock); pthread_exit((void *) 0); } //Check if direc received a message if(strlen(buf) != 0) { //Show the message that received// printf("----------------------------------\n"); printf("accept_thread received: %s\n", buf); } else if (strlen(buf) == 0) { //printf("Unable to received messsage!\n"); close(acptsock); pthread_exit((void *) 0); } //Decode the message that receive from the client //in order to identify the type of the message// decode(buf,msg); if( strcmp(msg->type , "REQCLIENTID" )== 0) { bzero(buf,sizeof(buf)); //encode the clientID sprintf(buf,"REQCLIENTID,%ld,%ld" , registerClient(msg->username ), msg->MSGID ); if (send(acptsock, buf, sizeof(buf) , 0) < 0 ) { perror("Send:Unable to send clientID"); } //Deallocations free(msg->username); } else if( strcmp(msg->type , "REQCREATE" )== 0 ) { printf("Received Create: %s , owner:%ld\n", msg->filename, msg->owner); //Store fileid unsigned long fileid = lookUpFileID(msg->filename , msg->owner ); if(fileid == -1) { //Store the new file in the metadata //Also , retrieve the fileID for the file fileid = registerFile(msg->filename, msg->owner); } printf("REQCREATE Function return: %ld \n", fileid); bzero(buf, sizeof(buf)); //encode the clientID sprintf(buf, "REQCREATE,%ld,%ld", fileid, msg->MSGID); if (send(acptsock, buf, sizeof(buf), 0) < 0) { perror("Send:Unable to send clientID"); } //Deallocations free(msg->filename); } else if( strcmp(msg->type , "REQFILEID" )== 0 ) { printf("Received REQFILEID: %s , owner:%ld\n" , msg->filename ,msg->owner); //Store the new file in the metadata //Also , retrieve the fileID for the file unsigned long fileid = lookUpFileID(msg->filename , msg->owner ); if(fileid == -1) { //Store the new file in the metadata //Also , retrieve the fileID for the file fileid = registerFile(msg->filename, msg->owner); } printf("REQFILEID Function return: %ld \n" , fileid); bzero(buf,sizeof(buf)); //encode the clientID sprintf(buf,"REQFILEID,%ld,%ld" , fileid , msg->MSGID ); if (send(acptsock, buf, sizeof(buf) , 0) < 0 ) { perror("Send:Unable to send clientID"); } //Deallocations free(msg->filename); } else if( strcmp(msg->type , "REQFILESLIST" )== 0 ) { bzero(buf,sizeof(buf)); GString *list=NULL; list=getfilelist(list); printf("List\n"); if (send(acptsock, list->str, strlen(list->str) , 0) < 0 ) { perror("Send:Unable to send clientID"); } //Deallocate memory g_string_free(list,TRUE); } }//While }