Repository navigation
Expand file tree
/
Copy pathversionclient.cpp
More file actions
156 lines (146 loc) · 3.78 KB
/
Copy pathversionclient.cpp
File metadata and controls
156 lines (146 loc) · 3.78 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#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)
{
char buf[100];
struct dirent *dir;
char path[50]="";
//strcat(path,"/home/osboxes/masterbackup/");
strcat(path,"/home/raajpatel/Masterbackup/");
char tempdir[30],targetdir[30];
strcpy(tempdir,directory); //directory -> /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);
}
/*
char buf[100];
DIR *d;
struct dirent *dir;
d=opendir(argv[2]);
if (d)
{
/*readdir(d);
cout << dir->d_name << endl;
readdir(d);
cout << dir->d_name << endl;
char msg[10];
while((dir=readdir(d))!=NULL)
{
cout << dir->d_name << endl;
//int n=send(sd,dir->d_name,strlen(dir->d_name),0);
//cout << n << endl;
//recv(sd,msg,sizeof(msg),0);
if ( (strcmp(dir->d_name,".")==0) || (strcmp(dir->d_name,"..")==0))
{ cout <<". or .." << endl;
}
else
{
int fd = open(dir->d_name,O_RDONLY);
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);
}
}
}
*/
}
close(sd);
}