Compare commits

...

4 Commits

Author SHA1 Message Date
453530270@qq.com f29a8c5520 完善进度条显示 2 years ago
453530270@qq.com d059c9e207 增加进度条 2 years ago
453530270@qq.com 9166a7a159 调整代码格式 2 years ago
453530270@qq.com 7cb80ac286 更新文档 2 years ago
  1. 23
      README.md
  2. 4
      client.c
  3. 4
      ftree.h
  4. 32
      server.c

23
README.md

@ -1,2 +1,21 @@
# File Synchronizer
Written in C, File Synchronizer is a program that copies files and/or directories from a host computer to a remote server. If the directories already exist on the server the program updates the files if any changes have been made. To check if any changes have been made the program compares the file sizes, permission, and their hash values. The hash value for a file is generated using a XOR hashing algorithm. The information is transferred byte by byte through a TCP socket.
# 文件同步工具
rc_server 为接收端,rc_client 发送端
举个例子:
接收端A服务器(IP:192.168.1.20),文件接收的存放位置 /root/01 , 使用的端口 57653,记得防火墙开启端口。
1、在A服务器上执行如下命令
```bash
./rc_sersver /root/01
```
2、发送端
将/www/00 下的文件同步到 A服务器上,执行命令
```bash
./rc_client /www/00/ 192.168.1.20
```
3、同步的文件继续保持原有属性,如只读只写等

4
client.c

@ -132,6 +132,7 @@ int rcopy_client(char *src, char *host, unsigned short port, char *r_src, int ty
exit(1);
}
// 发送文件
if (r.type == TRANSFILE) {
FILE *out;
if ((out = fopen(src, "rb")) != NULL) {
@ -147,6 +148,7 @@ int rcopy_client(char *src, char *host, unsigned short port, char *r_src, int ty
}
}
// 读取socket数据
int status, len;
len = read(sockfd, &status, sizeof(int));
if (len != sizeof(int)) {
@ -154,6 +156,7 @@ int rcopy_client(char *src, char *host, unsigned short port, char *r_src, int ty
exit(1);
} else {
if (status == SENDFILE) {
// 获取pid
pid_t pid = fork();
if (pid == 0) {
close(sockfd); // child will initate a new connection.
@ -207,6 +210,7 @@ int rcopy_client(char *src, char *host, unsigned short port, char *r_src, int ty
while (fcalls > 0) {
wait(&status);
if (WIFEXITED(status)) {
// 从子进程的终止状态中提取出退出状态码
pid = WEXITSTATUS(status);
if (!final) {
final = pid;

4
ftree.h

@ -28,6 +28,10 @@
#define PORT 30100
#endif
// 进度条
#define PROCBAR_LEN 101
#define PROC_STYLE '#'
struct request {
int type; // Request type is REGFILE, REGDIR, TRANSFILE
char path[MAXPATH];

32
server.c

@ -26,6 +26,8 @@ struct request *createrequest(void);
void complete_path(const char *child, char *s);
int handleclient(struct client *p);
int checkfile(struct client *p);
// 百分比进度条
void procbar(double total,double current);
int bindandlisten(unsigned short port);
@ -109,6 +111,7 @@ void rcopy_server(unsigned short port, char *dest) {
/*
* Based on the state of the client complete task.
*/
int handleclient(struct client *p) {
@ -156,6 +159,7 @@ int handleclient(struct client *p) {
exit(-1);
}
// printf("size %d read in %d bytes\n", p->info->size, len);
procbar(p->info->size,len);
p->state = AWAITING_HASH;
return 1;
}
@ -211,6 +215,7 @@ int handleclient(struct client *p) {
status = ERROR;
}
} while((len == 1) && ++s < p->info->size);
//close file handle
fclose(in);
if (len != 1) {
perror("server: reading file");
@ -378,3 +383,30 @@ int checkfile(struct client *p) {
}
}
}
/**
*
*/
void procbar(double total,double current){
char bar[PROCBAR_LEN];
memset(bar,'\0',sizeof(bar));
// label
const char *label= "|/_\\";
//
int cnt=0;
int len= strlen(label);
// 占比
double rate = (100*current)/total;
//循环次数,必须为整数
int loop_count = (int) rate;
//loop print
while(cnt<=loop_count){
printf("[%-100s][%.11f%%][%c]\r",bar,rate,label[cnt%len]);
// 直接输出不,非缓冲区
fflush(stdout);
bar[cnt++] = PROC_STYLE;
usleep(2000);
}
// 输出换行,反正被覆盖
printf("\n");
}
Loading…
Cancel
Save