加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
command.c 2.81 KB
一键复制 编辑 原始数据 按行查看 历史
SpaceX_zhao 提交于 2023-05-18 22:23 . alopex modify
#include "head.h"
// 接受并执行客户端传来的命令
int execCommand(int netfd, struct userProcess_s *user, MYSQL* mysql){
//char *command[] = {"cd", "ls", "pwd", "mkdir", "rmdir", "puts", "gets", "remove", "end"};
train_t train;
bzero(&train, sizeof(train));
// 接收命令的类型
int commandID;
recvn(netfd, &commandID, sizeof(int));
printf("commandID=%d\n",commandID);
// 接收参数的长度
recvn(netfd, &train.length, sizeof(train.length));
// 接收参数内容
recvn(netfd, train.data, train.length);
int ret;
// 判断命令类型
switch (commandID){
case 0: // 执行cd
LOGRECORD(INFO, "cd");
ret = changeusrPath(mysql, train.data, user);
send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
break;
case 1: // 执行ls
LOGRECORD(INFO, "ls");
lsCurrDir(netfd, mysql, user);
break;
case 2: // 执行pwd
LOGRECORD(INFO, "pwd");
printfCurrPath(netfd, mysql, user);
break;
case 3: // 执行mkdir
LOGRECORD(INFO, "mkdir");
ret = mkDir(train.data, mysql, user);
send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
break;
case 4: // 执行rmdir
LOGRECORD(INFO, "rmdir");
ret = rmDir(train.data, mysql, user);
send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
break;
case 5: // 客户端执行了puts上传,服务端接收
LOGRECORD(INFO, "puts");
unsigned char MD5[16];
bzero(MD5,sizeof(MD5));
// 接收 md5 并校验
ret = recvandcheck(netfd, mysql, MD5);
if(ret == 0){
// 文件表存在该md5,则秒传
printf("秒传\n");
// 接收文件名
train_t filename;
bzero(&filename,sizeof(filename));
recvn(netfd, &filename.length ,sizeof(int));
recvn(netfd, filename.data, train.length);
ret = insertSql(filename.data, mysql, user, MD5);
LOGRECORD(INFO,"insertSql in execCommand !");
}else{
// 文件表不存在该md5,则按类型接收
LOGRECORD(INFO,"file not exist");
ret = recvFile(netfd, MD5, mysql, user);
}
send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
break;
case 6: // 客户端执行了gets下载,服务端发送
LOGRECORD(INFO, "gets");
ret = sendFile(netfd, train.data, mysql, user);
send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
break;
case 7: // 执行remove
LOGRECORD(INFO, "remove");
//rmFile(train.data, user);
ret = deleteServerFile(train.data, mysql, user);
send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
break;
case 8:
close(netfd);
return -1;
default:
break;
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化