diff --git a/fileSystem.c b/fileSystem.c new file mode 100644 index 0000000000000000000000000000000000000000..8b18d18d65b4ccb8b42a7333c7abe01d8e81b077 --- /dev/null +++ b/fileSystem.c @@ -0,0 +1,362 @@ +#include "head.h" + +char* merge(char* aPath, char* cPath){ + char* result = (char *)calloc(1024, sizeof(char)); + strcat(cPath, aPath); + strcpy(result, cPath); + return result; +} + +int changeusrPath(MYSQL* mysql, char* usrPath, userProcess_t* puser){ + userProcess_t* puser1 = (userProcess_t *)calloc(1, sizeof(userProcess_t)); + memcpy(puser1, puser, sizeof(userProcess_t)); + + char* dir;//得到每次分割的字符串 + char* saveptr;//用来保全当前位置指针 + const char* delim = "/";//分隔符 + char Path[256]; + strcpy(Path, usrPath); + + char* p = Path;//获取字符串第一个字符 + if(strcmp(p, delim) == 0){ + puser1->curid = puser1->initid;//跳转到该用户的根目录 + } + + dir = strtok_r(Path, delim, &saveptr);//第一次分割 + while(dir != NULL){ + if(strcmp(".", dir) == 0){ + //什么都不做 + } + else if(strcmp("..", dir) == 0){ + //curid向前追溯一个 如果追溯的id为-1,则什么都不做 若不为-1,curid改为追溯的id + char sql[1024]; + sprintf(sql, "select preid from dirsys where id = %d and tomb = 0 and user = '%s' and type = 'd';", puser1->curid, puser1->username); + printf("%s\n", sql); + + int qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + MYSQL_RES* result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + return -1; + } + + if(mysql_num_rows(result) == 0){ + LOGRECORD(INFO, "mysql_num_rows"); + return -1; + } + + MYSQL_ROW row; + if((row = mysql_fetch_row(result)) == NULL){ + LOGRECORD(ERROR, "mysql_fetch_row"); + return -1; + } + + int tmpid = atoi(row[0]); + if(tmpid != -1){ + puser1->curid = tmpid; + } + + mysql_free_result(result); + } + else{ + //根据用户名、dir、curid、文件类型找寻符合条件选项 根据选项的id修改当前id 错误直接返回-1 + char sql[1024]; + sprintf(sql, "select id from dirsys where preid = %d and tomb = 0 and user = '%s' and type = 'd' and name = '%s';", puser1->curid, puser1->username, dir); + printf("%s\n", sql); + + int qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + MYSQL_RES* result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + return -1; + } + + if(mysql_num_rows(result) == 0){ + LOGRECORD(INFO, "mysql_num_rows"); + return -1; + } + + MYSQL_ROW row; + if((row = mysql_fetch_row(result)) == NULL){ + LOGRECORD(ERROR, "mysql_fetch_row"); + return -1; + } + + puser1->curid = atoi(row[0]); + + mysql_free_result(result); + } + dir = strtok_r(NULL, delim, &saveptr); + } + + //运行至此处,则代表路径正确、目录存在 + puser->curid = puser1->curid; + free(puser1); + return 0; +} + +int lsCurrDir(int netfd, MYSQL* mysql, userProcess_t* puser){ + char sql[1024]; + sprintf(sql, "select id, name, type, user from dirsys where preid = %d and tomb = 0 and user = '%s';", puser->curid, puser->username); + printf("%s\n", sql); + + int qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + int status = -1; + send(netfd, &status, sizeof(int), MSG_NOSIGNAL); + return -1; + } + + MYSQL_RES* result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + int status = -1; + send(netfd, &status, sizeof(int), MSG_NOSIGNAL); + return -1; + } + + if(mysql_num_rows(result) == 0){ + //空目录,发空车 + train_t train1; + bzero(&train1, sizeof(train1)); + train1.length = 0; + send(netfd, &train1, sizeof(train1.length), MSG_NOSIGNAL); + return 0; + } + + MYSQL_ROW row; + char output[1024]; + train_t train2; + while((row = mysql_fetch_row(result)) != NULL){ + bzero(output, sizeof(output)); + sprintf(output, "%s %7s %3s %7s\n", row[0], row[1], row[2], row[3]); + + bzero(&train2, sizeof(train2)); + train2.length = strlen(output); + memcpy(train2.data, output, train2.length); + send(netfd, &train2, sizeof(train2.length)+train2.length, MSG_NOSIGNAL);//发送数据给客户端send + } + + mysql_free_result(result); + return 0; +} + + +int printfCurrPath(int netfd, MYSQL* mysql, userProcess_t* puser){ + userProcess_t* puser1 = (userProcess_t *)calloc(1, sizeof(userProcess_t)); + memcpy(puser1, puser, sizeof(userProcess_t)); + + char userPath[1024] = ""; + if(puser1->curid == puser1->initid){ + strcpy(userPath, "/"); + } + while(puser1->curid != puser1->initid){ + char sql[1024]; + sprintf(sql, "select preid, path from dirsys where id = %d and tomb = 0 and type = 'd' and user = '%s';", puser1->curid, puser1->username); + printf("%s\n", sql); + + int qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + MYSQL_RES* result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + return -1; + } + + if(mysql_num_rows(result) == 0){ + LOGRECORD(INFO, "mysql_num_rows"); + return -1; + } + + MYSQL_ROW row; + if((row = mysql_fetch_row(result)) == NULL){ + LOGRECORD(ERROR, "mysql_fetch_row"); + return -1; + } + + puser1->curid = atoi(row[0]); + //反向拼接字符串 + char* curPath = merge(userPath, row[1]); + strcpy(userPath, curPath); + free(curPath); + + mysql_free_result(result); + } + + train_t train; + bzero(&train, sizeof(train)); + train.length = strlen(userPath); + memcpy(train.data, userPath, train.length); + send(netfd, &train, sizeof(train.length)+train.length, MSG_NOSIGNAL);//发送数据给客户端send + free(puser1); + + return 0; +} + +int mkDir(char* dirname, MYSQL* mysql, userProcess_t* puser){ + char sql[1024]; + sprintf(sql, "select id from dirsys where preid = %d and tomb = 0 and user = '%s' and type = 'd' and name = '%s';", puser->curid, puser->username, dirname); + printf("%s\n", sql); + + int qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + MYSQL_RES* result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + return -1; + } + + if(mysql_num_rows(result) != 0){ + mysql_free_result(result); + LOGRECORD(INFO, "mysql_num_rows"); + return -1; + } + + sprintf(sql, "insert into dirsys (name, type, preid, path, user, tomb) values ('%s', 'd', %d, '/%s', '%s', 0);", dirname, puser->curid, dirname, puser->username); + printf("%s\n", sql); + + qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + mysql_free_result(result); + return 0; +} + +int rmDir(char* dirname, MYSQL* mysql, userProcess_t* puser){ + //先查是否有该目录 + char sql[1024]; + sprintf(sql, "select id from dirsys where preid = %d and tomb = 0 and user = '%s' and type = 'd' and name = '%s';", puser->curid, puser->username, dirname); + printf("%s\n", sql); + + int qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + MYSQL_RES* result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + return -1; + } + + if(mysql_num_rows(result) == 0){ + //向用户发送 目录不存在 + LOGRECORD(INFO, "mysql_num_rows"); + return -1; + } + //记录目录id + MYSQL_ROW row; + if((row = mysql_fetch_row(result)) == NULL){ + LOGRECORD(ERROR, "mysql_fetch_row"); + return -1; + } + + int dirid = atoi(row[0]); + mysql_free_result(result); + //再查是否目录为空 + sprintf(sql, "select id from dirsys where preid = %d and tomb = 0 and user = '%s';", dirid, puser->username); + printf("%s\n", sql); + + qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + return -1; + } + + if(mysql_num_rows(result) != 0){ + //向用户发送 目录非空 + LOGRECORD(INFO, "mysql_num_rows"); + return -1; + } + + //目录为空进行删除 + sprintf(sql, "update dirsys set tomb = 1 where preid = %d and tomb = 0 and user = '%s' and type = 'd' and name = '%s';", puser->curid, puser->username, dirname); + printf("%s\n", sql); + + qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + return 0; +} + + +int deleteServerFile(char* filename, MYSQL* mysql, userProcess_t* puser){ + //先查是否有该文件 + char sql[1024]; + sprintf(sql, "select id from dirsys where preid = %d and tomb = 0 and user = '%s' and type = 'f' and name = '%s';", puser->curid, puser->username, filename); + printf("%s\n", sql); + + int qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + MYSQL_RES* result = mysql_store_result(mysql); + if(result == NULL){ + LOGRECORD(ERROR, "mysql_store_result"); + return -1; + } + + if(mysql_num_rows(result) == 0){ + //向用户发送 文件不存在 + LOGRECORD(INFO, "mysql_num_rows"); + return -1; + } + + mysql_free_result(result); + + //删除文件 + sprintf(sql, "update dirsys set tomb = 1 where preid = %d and tomb = 0 and user = '%s' and type = 'f' and name = '%s';", puser->curid, puser->username, filename); + printf("%s\n", sql); + + qret = mysql_query(mysql, sql); + if(qret != 0){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + LOGRECORD(ERROR, "mysql_query"); + return -1; + } + + return 0; +} diff --git a/head.h b/head.h index 2bcd8f654224fca269272493912014b4a891edfa..6e0414392d5a60f07a0457b3e99d7aa7c174df1f 100644 --- a/head.h +++ b/head.h @@ -5,10 +5,15 @@ #include #include +//typedef struct userProcess_s{ +// int initid; +// char name[128]; +// int currid; +//}userProcess_t; typedef struct userProcess_s{ - int initid; - char name[128]; - int currid; + char username[256];//用户名 + int curid;//当前id + int initid;//初始id }userProcess_t; // 小火车文件协议 @@ -45,13 +50,13 @@ enum{ERROR,WARNING,INFO,DEBUG}; }\ }while(0); // 列出当前目录所有文件,发送给客户端 -int ls(int netfd,struct userProcess_s *usr); +int lsCurrDir(int netfd, MYSQL* mysql, userProcess_t* puser); // 找到用户当前路径,并发送给客户端 -int printfCurrPath(int netfd,userProcess_t *usr); +int printfCurrPath(int netfd, MYSQL* mysql, userProcess_t* puser); // 创建一个目录 -int mkDir(const char* dirName, userProcess_t* usr); +int mkDir(char* dirname, MYSQL* mysql, userProcess_t* puser); // 删除一个目录 -int rmDir(const char* dirName, struct userProcess_s *usr); +int rmDir(char* dirname, MYSQL* mysql, userProcess_t* puser); // 删除文件 int rmFile(const char* fileName, struct userProcess_s *usr); // 上传文件 @@ -61,16 +66,14 @@ int recvFile(int sockfd,MYSQL* usrsql,userProcess_t* usr); // 执行命令 int execCommand(int netfd, struct userProcess_s *user, MYSQL* mysql); // 删除服务器文件 -int deleteServerFile(const char* fileName); -// 目录是否存在 -int isDir(char* path); -// 文件是否存在 -int isFile(char* path); +int deleteServerFile(char* filename, MYSQL* mysql, userProcess_t* puser); // 答应用户的当前路径 -char* printPath(userProcess_t* puser); +//char* printPath(userProcess_t* puser); // 修改用户的服务端路径 -int changeusrPath(char* usrPath, userProcess_t* puser); +int changeusrPath(MYSQL* mysql, char* usrPth, userProcess_t* puser); // 接受数据 int recvn(int sockfd, void *buf, int length); +// 拼接路径 +char* merge(char* aPath, char* cPath); #endif