-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtask_impl.cpp
357 lines (295 loc) · 8.37 KB
/
task_impl.cpp
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
#include "stdafx.h"
#include "upd_task.h"
#include "FileOperations.h"
#include <direct.h>
#include "process.h"
//for batch task
CString parseParams(LPCSTR fn, LPCSTR params);
BOOL copy_file(LPCSTR src, LPCSTR dst_fldr, LPCSTR dst_fn);
BOOL rename_file(LPCSTR src, LPCSTR dst);
BOOL check_RO(LPCSTR src);
BOOL file_exist(LPCSTR src);
BOOL mk_bk_rename(LPCSTR src);
void copy_folder(LPCSTR src, LPCSTR dst);
void checkRightFolderName(LPSTR fn);
CFileOperation fo; // create object
BOOL CTask::exec()
{
if( !is_enabled() ) return TRUE;
sort_sub_tasks();
CTaskArray::iterator sub_task_it = m_sub_tasks.begin();
for(;sub_task_it !=m_sub_tasks.end();++sub_task_it)
if (FALSE==(*sub_task_it)->run()) return FALSE;
return TRUE;
}
BOOL CTaskCopyFiles::run()
{
if( !is_enabled() ) return TRUE;
if(FALSE==CTask::exec()) return FALSE;
CFileNamesArray::iterator it = m_file_names.begin();
string_path file_name;
string16 drive;
string_path dir;
string16 ext;
for(;it!=m_file_names.end();){
_splitpath((*it).c_str(),drive,dir,file_name,ext);
// strcpy(new_path,target_folder());
Msg("[%s]:Copying %s to %s ", name(), (*it).c_str(), target_folder() );
string_path dst_file_name;
strconcat(dst_file_name, target_folder(), "\\", file_name, ext );
BOOL res = copy_file( (*it).c_str(), target_folder(), dst_file_name );
if(res==FALSE){
Msg("-------------------ERROR-----------------------------------" );
Msg("unable to copy file %s to %s",(*it).c_str(), target_folder() );
Msg("the error is %s",fo.m_sError.GetBuffer());
Msg("-----------------------------------------------------------" );
};
++it;
}
return TRUE;
}
BOOL CTaskCopyFolder::run ()
{
if( !is_enabled() ) return TRUE;
if(FALSE==CTask::exec()) return FALSE;
Msg("[%s]:Copying folder %s to %s ",name(),*m_source_folder, *m_target_folder);
check_RO(*m_target_folder);
copy_folder(*m_source_folder, *m_target_folder);
return TRUE;
}
BOOL CTaskExecute::run ()
{
// ïðîâåðèòü : cmd.exe attrib.... copy,,, sds
if( !is_enabled() ) return TRUE;
if(FALSE==CTask::exec()) return FALSE;
string_path cur_dir;
if( xr_strlen(m_working_folder) ){
GetCurrentDirectoryA(_MAX_PATH,cur_dir);
SetCurrentDirectoryA(*m_working_folder);
};
Msg("[%s]:Running %s %s", name(), *m_app_name, (xr_strlen(m_params))?" ":*m_params);
spawnl(_P_WAIT, *m_app_name, (xr_strlen(m_params))?*m_params:" ");
Msg("[%s]:Done.",name());
if( xr_strlen(m_working_folder) )
SetCurrentDirectoryA(cur_dir);
return TRUE;
}
BOOL CTaskBatchExecute::run ()
{
if( !is_enabled() ) return TRUE;
if(FALSE==CTask::exec()) return FALSE;
// åñëè íàäî óñòàíîâèòü ðàá êàòàëîã
string_path cur_dir;
if( xr_strlen(m_working_folder) ){
GetCurrentDirectoryA(_MAX_PATH,cur_dir);
SetCurrentDirectoryA(*m_working_folder);
};
CFileNamesArray::iterator it = m_file_names.begin();
CString params;
if( 0==strstr(*m_params,"$") ){
Msg("[%s]:Executing %s", name(), params.GetBuffer() );
system(*m_params);
Msg("[%s]:Done.",name());
}else{
for(;it!=m_file_names.end();++it){
params = parseParams((*it).c_str(), *m_params);
Msg("[%s]:Executing %s", name(), params.GetBuffer() );
system(params.GetBuffer());
Msg("[%s]:Done.",name());
}
}
if( xr_strlen(m_working_folder) )
SetCurrentDirectoryA(cur_dir);
return TRUE;
}
CString parseParams(LPCSTR fn, LPCSTR params)
{
CString res;
// $dir$ -directory
// $file$ -file name
// $ext$ -file extention
// $full_file_name$ -given full file name with path
string16 drive;
string_path dir;
string_path file_name;
string16 ext;
_splitpath(fn,drive,dir,file_name,ext);
const char* c = params;
while(c){
if(c[0]=='$'){//alias begin
if(c==strstr(c,"$dir$")){
res.Append(dir);
c+=xr_strlen("$dir$");
}else
if(c==strstr(c,"$file$")){
res.Append(file_name);
c+=xr_strlen("$file$");
}else
if(c==strstr(c,"$ext$")){
res.Append(ext);
c+=xr_strlen("$ext$");
}else
if(c==strstr(c,"$full_file_name$")){
res.Append(fn);
c+=xr_strlen("$full_file_name$");
}
}else{
res.AppendChar(c[0]);
if(*(c+1))c+=1;
else
break;
}
}
return res;
}
BOOL copy_file(LPCSTR src, LPCSTR dst_fldr, LPCSTR dst_fn)
{
_VerifyPath(dst_fn);
fo.SetOverwriteMode(true); // reset OverwriteMode flag (optional)
fo.SetAskIfReadOnly(true);
BOOL res;
res = check_RO(dst_fn);
if(FALSE==res)
return FALSE;
Msg("Copying...");
if ( fo.Copy(src, dst_fldr) ){
Msg("Done.");
return TRUE;
}else{
Msg("Warning :%s", fo.m_sError.GetBuffer());
if( (fo.GetErrorCode() == 32) ){
res = mk_bk_rename(dst_fn);
if(FALSE==res)
return FALSE;
return copy_file(src, dst_fldr, dst_fn);
}
return FALSE;
}
}
BOOL mk_bk_rename(LPCSTR src)
{
string_path file_name;
string16 drive;
string_path dir;
string16 ext;
Msg("Creating backup copy for %s", src);
_splitpath(src, drive, dir, file_name, ext);
string16 num;
string16 new_ext;
int start_from=0;
string_path new_file_name;
while(true){
itoa(start_from,num,10);
strconcat(new_ext,"_old",num);
strconcat(new_file_name,drive,dir,"backup\\",file_name,ext,new_ext);
if( !file_exist(new_file_name) ){
_VerifyPath(new_file_name);
if( rename_file(src, new_file_name) )
break;
}
++start_from;
}
return TRUE;
}
BOOL rename_file(LPCSTR src, LPCSTR dst)
{
fo.SetOverwriteMode(true); // reset OverwriteMode flag (optional)
fo.SetAskIfReadOnly(true);
check_RO(dst);
if( fo.Rename(src, dst) ){
Msg("Done.");
return TRUE;
}else{
Msg("-----------------WARNING----------------------------" );
Msg("Cann't rename file %s to %s.",src,dst);
Msg("the error is: %s", fo.m_sError.GetBuffer() );
Msg("----------------------------------------------------" );
return FALSE;
}
return TRUE;
}
BOOL check_RO(LPCSTR src)
{
DWORD dwAttr = GetFileAttributes(src);
//if (dwAttr == -1); //not_found
if ( (dwAttr != -1)&& (dwAttr & FILE_ATTRIBUTE_READONLY) ){
Msg("Warning: File %s has read-only attribute, trying to uncheck RO", src);
dwAttr &=~ FILE_ATTRIBUTE_READONLY;
BOOL res = SetFileAttributes(src, dwAttr);
if(!res){
Msg("Error: Cannot change file read-only attribute");
return FALSE;
}else
Msg("Done.");
}
return TRUE;
}
BOOL file_exist(LPCSTR src)
{
WIN32_FIND_DATA fd;
HANDLE h = ::FindFirstFile(src, &fd);
BOOL res = h!=INVALID_HANDLE_VALUE;
FindClose(h);
return res;
}
void copy_folder(LPCSTR src, LPCSTR dst)
{
WIN32_FIND_DATA fd;
string_path fldr_all_files;
strcpy(fldr_all_files, src);
checkRightFolderName(fldr_all_files);
strconcat(fldr_all_files, fldr_all_files,"*.*");
HANDLE h = ::FindFirstFile(fldr_all_files, &fd);
while( h != INVALID_HANDLE_VALUE ){
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY){
//make new dst...
if( (0==strcmp(fd.cFileName,"."))|| (0==strcmp(fd.cFileName,"..")) ){
::FindNextFile(h, &fd);
continue;
}
string_path new_dst_folder;
strcpy(new_dst_folder,dst);
checkRightFolderName(new_dst_folder);
strconcat(new_dst_folder,new_dst_folder,fd.cFileName);
string_path new_src_folder;
strcpy(new_src_folder,src);
checkRightFolderName(new_src_folder);
strconcat(new_src_folder,new_src_folder,fd.cFileName);
copy_folder(new_src_folder, new_dst_folder);
}else{
string_path new_dist_filename;
string_path new_src_filename;
string16 drive;
string_path dir;
string_path file_name;
string16 ext;
strcpy(new_src_filename,src);
checkRightFolderName(new_src_filename);
strconcat(new_src_filename,new_src_filename,fd.cFileName);
_splitpath(new_src_filename, drive, dir, file_name, ext);
strcpy(new_dist_filename,dst);
checkRightFolderName(new_dist_filename);
strconcat(new_dist_filename,new_dist_filename, file_name, ext );
copy_file(new_src_filename, dst, new_dist_filename);
}
if(!::FindNextFile(h, &fd))
break;
}
FindClose(h);
}
void checkRightFolderName(LPSTR fn)
{
if( (fn+xr_strlen(fn)) != "\\")
strcat(fn,"\\");
}
void _VerifyPath(LPCSTR path)
{
string1024 tmp;
for(int i=0;path[i];i++){
if( path[i]!='\\' || i==0 )
continue;
Memory.mem_copy( tmp, path, i );
tmp[i] = 0;
_mkdir(tmp);
}
}