Repository navigation
Expand file tree
/
Copy pathbackupclient.cpp
More file actions
107 lines (99 loc) · 2.7 KB
/
Copy pathbackupclient.cpp
File metadata and controls
107 lines (99 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include<errno.h>
void sendfromdirectory(char *directory,int sd)
{ // /home/raajpatel/Desktop/Testdit/
char buf[100];
struct dirent *dir;
char path[50]="";
strcat(path,"/home/raajpatel/Masterbackup/");
char tempdir[30],targetdir[30];
strcpy(tempdir,directory); //tempdir -> /home/raajpatel/Desktop/Testdit/
char *str=strtok(tempdir,"/");
char *tok=str;
while(str!=NULL) // /home/raajpatel/Desktop/Testdit/
{
tok=str;
cout << "tok " << tok << endl;
str=strtok(NULL,"/"); // Testdir
}
strcat(path,tok); // /home/raajpatel/Masterbackup/Testdir
mkdir(path,0660);
cout << path << endl;
//chdir(path);
strcat(path,"/"); // /home/raajpatel/Masterbackup/Testdir/
DIR *d=opendir(directory); // /home/raajpatel/Desktop/Testdit/
if (d)
{
char msg[10];
while((dir=readdir(d))!=NULL)
{
cout<<"directort test" << dir->d_name << endl; //file3
if ( (strcmp(dir->d_name,".")==0) || (strcmp(dir->d_name,"..")==0))
{
cout <<". or .." << endl;
}
else
{
char fpath[50]="";
strcat(fpath,path); // /home/raajpatel/Masterbackup/Testdir/
strcat(fpath,dir->d_name); // /home/raajpatel/Masterbackup/Testdir/file3
cout <<"fpath "<<fpath << endl;
int fd = open(fpath,O_RDONLY);
perror("open file error");
cout << fd << endl;
send(sd,"filename",9,0);
recv(sd,msg,sizeof(msg),0);
send(sd,dir->d_name,strlen(dir->d_name),0);
recv(sd,msg,sizeof(msg),0);
int n = read(fd,buf,sizeof(buf));
while(n!=0)
{
cout << buf << endl;
send(sd,buf,n,0);
recv(sd,msg,sizeof(msg),0);
bzero(buf,sizeof(buf));
n = read(fd,buf,sizeof(buf));
buf[n]='\0';
}
send(sd,"end",3,0);
recv(sd,msg,sizeof(msg),0);
close(fd);
}
}
}
}
int main(int argc , char *argv[])
{
int sd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in sockaddr_serv;
sockaddr_serv.sin_family=AF_INET;
sockaddr_serv.sin_port=9888;
sockaddr_serv.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(sd,(struct sockaddr *)&sockaddr_serv,sizeof(sockaddr_serv));
socklen_t len = sizeof(sockaddr_serv);
if (connect(sd,(struct sockaddr *)&sockaddr_serv,len)==-1)
{
perror("connect error");
exit(1);
}
if (strcmp(argv[1],"-f")==0)
{
for(int i=1;i<=argc-2;i++)
{
sendfromdirectory(argv[2],sd);
}
}
close(sd);
}