#include "head.h"

int execCommand(int netfd, struct userProcess_s *user){
    //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));

    // 接收参数的长度
    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(train.data, user);
        send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
        break;
    case 1: // 执行ls
        LOGRECORD(INFO, "ls");
        ls(netfd, user);
        break;
    case 2: // 执行pwd
        LOGRECORD(INFO, "pwd");
        printfCurrPath(netfd,user);
        break;
    case 3: // 执行mkdir
        LOGRECORD(INFO, "mkdir");
        ret = mkDir(train.data,user);
        send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
        break;
    case 4: // 执行rmdir
        LOGRECORD(INFO, "rmdir");
        ret = rmDir(train.data,user);
        send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
        break;
    case 5: // 客户端执行了puts上传,服务端接收
        LOGRECORD(INFO, "puts");
        ret = recvFile(netfd);
        send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
        break;
    case 6: // 客户端执行了gets下载,服务端发送
        LOGRECORD(INFO, "gets");
        ret = sendFile(netfd, train.data);
        send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
        break;
    case 7: // 执行remove
        LOGRECORD(INFO, "remove");
        deleteServerFile(train.data);
        //rmFile(train.data, user);
        send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
        break;
    case 8:
        close(netfd);
        return -1;
    default:
        break;
    }

    return 0;
}