FTP文件管理项目(本地云)项目日报(八)

2020-08-26 11:28:04 浏览数 (1)

新日程

这两天主板烧了,所以也没啥过多进度,也就把文件服务器模块设计并解决了,项目各个模块处理完之后顺便把流程图画了。 在做文件服务器模块的时候我发现好多功能和数据库服务器相交了,搞得我有点乱,而且协议改来改去,有些文件也是多模块共用,搞得我都晕了,所以我决定接下来重构。

新甘特图已经备好:

上期日报

来我们看看有多少人坚持下来了:

1号

日报7

8号

日报6

稍微有点尴尬。。。 应该是忙着赶进度,我这速度是有点飘,所以上天就让我主板烧了躺床上去歇着了吧。

我的进度

预期计划

文件服务器模块解决。

实际情况

解决了。

FTP服务器流程图
FTP操作图

简单放一些零件基类代码,这个后期也要封装成动态库,不面向用户。

代码语言:javascript复制
//FileBase.h

#pragma once
#include<stdio.h>    // printf   
#include<stdlib.h>   // exit   
#include<string.h>   // bzero   
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<unistd.h>
#include<netinet/in.h> // sockaddr_in   
#include<sys/types.h>  // socket   
#include<sys/socket.h> // socket   

#define SERVER_PORT 20   
#define LENGTH_OF_LISTEN_QUEUE 20   
#define BUFFER_SIZE 1024   
#define FILE_NAME_MAX_SIZE 512

class FileBase
{
public:
	void addFile(char* file_name, int client_socket_fd); 	//上传文件
	int  CreateDir(const char* sPathName);	//新增目录

	void delFile(char* filename);	//删除文件
	void delDir(char* filename);	//删除目录

//	void serchDir();查找目录	这个是属于数据库的事情,应该放在包里直接传过来。然后包做一个对内包,不用跟客户端对接的内部协议
	void serchlist(char* basePath, char* ret);//查找列表
	void serchmode(char* filename);//查找权限

	void modDir(const char* old_path, const char* new_path);	//修改目录
//	void modMode(const char* path, mode_t mode);	//权限留在数据库,我才不管

	void downFile(char* file_name, int new_server_socket_fd);//下载文件

	int connect_to_client();
};

可以看出来其实很多功能当时我是比较纠结放在哪个服务器实现的。

代码语言:javascript复制
//FileBase.cpp

#include "FileBase.h"

void FileBase::downFile(char* file_name,int new_server_socket_fd) {
        // ´ò¿ªÎļþ²¢¶ÁÈ¡ÎļþÊý¾Ý   
        FILE* fp = fopen(file_name, "r");
        if (NULL == fp)
        {
            printf("File:%s Not Foundn", file_name);
        }
        else
        {
            char buffer[BUFFER_SIZE];
            bzero(buffer, BUFFER_SIZE);
            int length = 0;
            // ÿ¶ÁÈ¡Ò»¶ÎÊý¾Ý£¬±ã½«Æä·¢Ë͸ø¿Í»§¶Ë£¬Ñ­»·Ö±µ½Îļþ¶ÁÍêΪֹ   
            while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
            {
                if (send(new_server_socket_fd, buffer, length, 0) < 0)
                {
                    printf("Send File:%s Failed./n", file_name);
                    break;
                }
                bzero(buffer, BUFFER_SIZE);
            }
            // ¹Ø±ÕÎļþ   
            fclose(fp);
            printf("File:%s Transfer Successful!n", file_name);
        }
}	//ÉÏ´«Îļþ


int FileBase::CreateDir(const char* sPathName)
{
    char DirName[256];
    strcpy(DirName, sPathName);
    int i, len = strlen(DirName);
    for (i = 1; i < len; i  )
    {
        if (DirName[i] == '/')
        {
            DirName[i] = 0;
            if (access(DirName, NULL) != 0)
            {
                if (mkdir(DirName, 0755) == -1)
                {
                    printf("mkdir   errorn");
                    return -1;
                }
            }
            DirName[i] = '/';
        }
    }
    return 0;
}

void FileBase::delFile(char* filename) {
    printf("The file to delete:");
    if (remove(filename) == 0)
    {
        printf("Removed %s.", filename);
    }
    else
    {
        perror("remove file");
    }
}

void FileBase::delDir(char* filename) {
    printf("The file to delete:");
    if (remove(filename) == 0)
    {
        printf("Removed %s.", filename);
    }
    else
    {
        perror("remove Dir");
    }
}

void FileBase::serchlist(char* basePath,char* ret){
    DIR* dir;
    struct dirent* ptr;

    if ((dir = opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr = readdir(dir)) != NULL)
    {
        if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)    ///current dir OR parrent dir
            continue;
        else if (ptr->d_type == 8) {

            strcat(ret,"File£º");
            strcat(ret, ptr->d_name);
            strcpy(ret, ">");  
        }    ///file
        else if (ptr->d_type == 10) {

            strcat(ret, "Link File£º");
            strcat(ret, ptr->d_name);
            strcpy(ret, ">");   
        }    ///link file
        else if (ptr->d_type == 4)    ///dir
        {
            strcat(ret, "Dir£º");
            strcat(ret, ptr->d_name);
            strcpy(ret, ">");  
        }
    }
    closedir(dir);
}//²éÕÒÁбí

int FileBase::serchmode(char* filename) {
    char name[BUFSIZ];
    int flag = 0;
    if (access(name, F_OK) == 0) {

        printf("Îļþ´æÔÚn");

        if (access(name,W_OK) == 0) 
            flag  = 4;
        if (access(name, R_OK) == 0) 
            flag  = 2; 
        if (access(name, X_OK) == 0)
            flag  = 1;
    }
    return flag;
}

void FileBase::modDir(const char* old_path, const char* new_path) {
    rename(old_path, new_path);
}	

//void FileBase::modMode(const char* path, mode_t mode) {
//    chmod(path,mode);
//}	

void FileBase::addFile(char* file_name, int client_socket_fd) {
    FILE* fp = fopen(file_name, "w");
    if (NULL == fp)
    {
        printf("File:t%s Can Not Open To Writen", file_name);
        exit(1);
    }

    char buffer[BUFFER_SIZE];
    bzero(buffer, BUFFER_SIZE);
    int length = 0;
    while ((length = recv(client_socket_fd, buffer, BUFFER_SIZE, 0)) > 0)
    {
        if (fwrite(buffer, sizeof(char), length, fp) < length)
        {
            printf("File:t%s Write Failedn", file_name);
            break;
        }
        bzero(buffer, BUFFER_SIZE);
    }

    printf("Receive File:t%s From Server IP Successful!n", file_name);
    close(fp);
}//·¢ËÍÎļþ   

int FileBase::connect_to_client() {
  
        struct sockaddr_in client_addr;
        bzero(&client_addr, sizeof(client_addr));
        client_addr.sin_family = AF_INET;
        client_addr.sin_addr.s_addr = htons(INADDR_ANY);
        client_addr.sin_port = htons(0);

        int client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
        if (client_socket_fd < 0)
        {
            perror("Create Socket Failed:");
            exit(1);
        }
        if (-1 == (bind(client_socket_fd, (struct sockaddr*) & client_addr, sizeof(client_addr))))
        {
            perror("Client Bind Failed:");
            exit(1);
        }

        struct sockaddr_in server_addr;
        bzero(&server_addr, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) == 0)  
        {
            perror("Server IP Address Error:");
            exit(1);
        }
        server_addr.sin_port = htons(SERVER_PORT);
        socklen_t server_addr_length = sizeof(server_addr);

        if (connect(client_socket_fd, (struct sockaddr*) & server_addr, server_addr_length) < 0)
        {
            perror("Can Not Connect To Server IP:");
            exit(0);
        }
        return client_socket_fd;
}

具体调用就那么用咯。

重构·整体流程

图太长了,截不下来,我就左右分了。

下一工作日任务

重构,将可以封装在动态库里的封装在动态库里,将不能封装在动态库里的,改好之后封装到动态库。 那么,第一步,前置服务器的封装,理想情况是只放出场景类来,GoGoGo!!!

0 人点赞