From a94379a38ddaa69d7fcbb16848e45c0815d4cfcd Mon Sep 17 00:00:00 2001 From: wangcheng <1846935001@qq.com> Date: Wed, 17 May 2023 21:07:27 +0800 Subject: [PATCH 1/2] wang add fileSystem.c --- fileSystem.c | 416 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 416 insertions(+) create mode 100644 fileSystem.c diff --git a/fileSystem.c b/fileSystem.c new file mode 100644 index 0000000..d84ba9f --- /dev/null +++ b/fileSystem.c @@ -0,0 +1,416 @@ +#include + +typedef struct userProcess_s{ + char username[256];//用户名 + int curid;//当前id + int initid;//初始id +}userProcess_t; + +int changeusrPath(MYSQL* mysql, char* usrPth, userProcess_t* puser); +int lsCurrDir(int netfd, MYSQL* mysql, userProcess_t* puser); +int printfCurrPath(MYSQL* mysql, userProcess_t* puser); +int mkDir(char* dirname, MYSQL* mysql, userProcess_t* puser); +int rmDir(char* dirname, MYSQL* mysql, userProcess_t* puser); +int deleteServerFile(char* filename, MYSQL* mysql, userProcess_t* puser); +char* merge(char* aPath, char* cPath); + +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(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; +} + + +int main(void){ + MYSQL* mysql = mysql_init(NULL); + MYSQL* ret = mysql_real_connect(mysql, "localhost", "root", "12345678", "test", 0, NULL, 0); + if(ret == NULL){ + fprintf(stderr, "error:%s\n", mysql_error(mysql)); + return -1; + } + + userProcess_t* puser = (userProcess_t *)calloc(1, sizeof(userProcess_t)); + strcpy(puser->username, "wang"); + puser->curid = 1; + puser->initid = 1; + + char usrPath[] = "dir2/dir3"; + + int a = changeusrPath(mysql, usrPath, puser); + printf("a = %d\n", a); + printf("%d\n", puser->curid); + + lsCurrDir(mysql, puser); + + printfCurrPath(mysql, puser); + + char dirname[] = "IKUN"; + mkDir(dirname, mysql, puser); + rmDir(dirname, mysql, puser); + + strcpy(usrPath, ".."); + changeusrPath(mysql, usrPath, puser); + printfCurrPath(mysql, puser); + + char filename[] = "file1"; + deleteServerFile(filename, mysql, puser); + mysql_close(mysql); + + return 0; +} + -- Gitee From f642310e63c810bb03853caddbab8cf0b36c65f0 Mon Sep 17 00:00:00 2001 From: wangcheng <1846935001@qq.com> Date: Wed, 17 May 2023 21:22:03 +0800 Subject: [PATCH 2/2] wang change fileSystem.c head.h --- fileSystem.c | 58 ++-------------------------------------------------- head.h | 31 +++++++++++++++------------- 2 files changed, 19 insertions(+), 70 deletions(-) diff --git a/fileSystem.c b/fileSystem.c index d84ba9f..8b18d18 100644 --- a/fileSystem.c +++ b/fileSystem.c @@ -1,18 +1,4 @@ -#include - -typedef struct userProcess_s{ - char username[256];//用户名 - int curid;//当前id - int initid;//初始id -}userProcess_t; - -int changeusrPath(MYSQL* mysql, char* usrPth, userProcess_t* puser); -int lsCurrDir(int netfd, MYSQL* mysql, userProcess_t* puser); -int printfCurrPath(MYSQL* mysql, userProcess_t* puser); -int mkDir(char* dirname, MYSQL* mysql, userProcess_t* puser); -int rmDir(char* dirname, MYSQL* mysql, userProcess_t* puser); -int deleteServerFile(char* filename, MYSQL* mysql, userProcess_t* puser); -char* merge(char* aPath, char* cPath); +#include "head.h" char* merge(char* aPath, char* cPath){ char* result = (char *)calloc(1024, sizeof(char)); @@ -170,7 +156,7 @@ int lsCurrDir(int netfd, MYSQL* mysql, userProcess_t* puser){ } -int printfCurrPath(MYSQL* mysql, userProcess_t* puser){ +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)); @@ -374,43 +360,3 @@ int deleteServerFile(char* filename, MYSQL* mysql, userProcess_t* puser){ return 0; } - - -int main(void){ - MYSQL* mysql = mysql_init(NULL); - MYSQL* ret = mysql_real_connect(mysql, "localhost", "root", "12345678", "test", 0, NULL, 0); - if(ret == NULL){ - fprintf(stderr, "error:%s\n", mysql_error(mysql)); - return -1; - } - - userProcess_t* puser = (userProcess_t *)calloc(1, sizeof(userProcess_t)); - strcpy(puser->username, "wang"); - puser->curid = 1; - puser->initid = 1; - - char usrPath[] = "dir2/dir3"; - - int a = changeusrPath(mysql, usrPath, puser); - printf("a = %d\n", a); - printf("%d\n", puser->curid); - - lsCurrDir(mysql, puser); - - printfCurrPath(mysql, puser); - - char dirname[] = "IKUN"; - mkDir(dirname, mysql, puser); - rmDir(dirname, mysql, puser); - - strcpy(usrPath, ".."); - changeusrPath(mysql, usrPath, puser); - printfCurrPath(mysql, puser); - - char filename[] = "file1"; - deleteServerFile(filename, mysql, puser); - mysql_close(mysql); - - return 0; -} - diff --git a/head.h b/head.h index 2bcd8f6..6e04143 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 -- Gitee