-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveFileEngine.cs
441 lines (399 loc) · 16.4 KB
/
MoveFileEngine.cs
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using netCommander.FileSystemEx;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using System.Security.AccessControl;
namespace netCommander
{
public class MoveFileEngine : IDisposable
{
private CopyFileProgressDialog progress_dialog;
private List<FileInfoEx> initial_source;
private string innitial_destination;
private MoveEngineOptions options;
private CopyProgressRoutine move_progress_delegate_holder;
private Thread internal_thread;
private IntPtr callback_data = IntPtr.Zero;
private MoveFileOptions options_api;
private MethodInvokerUpdateProgress update_progress_delegate_holder;
private string initial_mask = "*";
public MoveFileEngine
(List<FileInfoEx> source,
string destination,
string mask,
MoveEngineOptions options,
CopyFileProgressDialog dialog)
{
initial_mask = mask;
initial_source = source;
innitial_destination = destination;
this.options = options;
progress_dialog = dialog;
internal_thread = new Thread(move_proc);
options_api = MoveFileOptions.CopyAllowed | MoveFileOptions.WriteThrough;
if ((options & MoveEngineOptions.ReplaceExistingFiles) == MoveEngineOptions.ReplaceExistingFiles)
{
options_api = options_api | MoveFileOptions.ReplaceExisting;
}
if (progress_dialog != null)
{
progress_dialog.FormClosing += new FormClosingEventHandler(progress_dialog_FormClosing);
}
update_progress_delegate_holder = new MethodInvokerUpdateProgress(update_progress_unsafe);
move_progress_delegate_holder = new CopyProgressRoutine(move_progress_proc);
}
public void Do()
{
internal_thread.Start();
}
public event EventHandler Done;
private object progress_closing_lock = new object();
private bool progress_closing_unsafe = false;
private bool ProgressCloseBeginSafe
{
get
{
bool ret = false;
lock (progress_closing_lock)
{
ret = progress_closing_unsafe;
}
return ret;
}
set
{
lock (progress_closing_lock)
{
progress_closing_unsafe = value;
}
}
}
void progress_dialog_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
//progress dialog before user or system close
//call from UI thread!
//thread safe set abort_job flag - will be check by file enum functions
AbortJobSafe = true;
//set cancel flag - will be check by CopyFileEx callback
progress_dialog.CancelPressedSafe = true;
//set internal flag - will will be check by CopyFileEx callback
ProgressCloseBeginSafe = true;
}
//callback from CopyFileEx, call from back back thread
private CopyFileExCallbackReturns move_progress_proc(UInt64 TotalFileSize,
UInt64 TotalBytesTransferred,
UInt64 StreamSize,
UInt64 StreamBytesTransferred,
int dwStreamNumber,
CopyFileExState dwCallbackReason,
IntPtr hSourceFile,
IntPtr hDestinationFile,
IntPtr lpData)
{
if (progress_dialog == null)
{
return CopyFileExCallbackReturns.QUIET;
}
if (ProgressCloseBeginSafe)
{
return CopyFileExCallbackReturns.CANCEL;
}
UpdateProgressArgs e = new UpdateProgressArgs();
if (dwCallbackReason == CopyFileExState.STREAM_SWITCH)
{
string src = string.Empty;
string dts = string.Empty;
IOhelper.strings_from_buffer(ref src, ref dts, lpData);
e.SourceFile = src;
e.DestinationFile = dts;
e.Reason = UpdateProgressReason.StreamSwitch;
}
else
{
e.Reason = UpdateProgressReason.ChunkFinish;
}
e.EnableTotalProgress = false;
e.StreamSize = StreamSize;
e.StreamTransferred = StreamBytesTransferred;
update_progress_safe(e);
//need try becouse sync error may occur if progress_dialog closed
try
{
if (progress_dialog.CancelPressedSafe)
{
AbortJobSafe = true;
return CopyFileExCallbackReturns.CANCEL;
}
else
{
return CopyFileExCallbackReturns.CONTINUE;
}
}
catch
{
//and if error occur -> stop copy
AbortJobSafe = true;
return CopyFileExCallbackReturns.CANCEL;
}
}
private void update_progress_safe(UpdateProgressArgs e)
{
if (progress_dialog == null)
{
return;
}
if (ProgressCloseBeginSafe)
{
return;
}
//supress sync errors
try
{
progress_dialog.Invoke(update_progress_delegate_holder, new object[] { e });
}
catch { }
}
private void update_progress_unsafe(UpdateProgressArgs e)
{
//must be invoked on UI thread
switch (e.Reason)
{
case UpdateProgressReason.ChunkFinish:
progress_dialog.SetProgress(e.StreamTransferred, e.StreamSize);
break;
case UpdateProgressReason.JobBegin:
progress_dialog.Text = Options.GetLiteral(Options.LANG_MOVE_RENAME);
progress_dialog.labelSpeed.Text = string.Empty;
progress_dialog.labelStatus.Text = string.Empty;
progress_dialog.labelStatusTotal.Text = string.Empty;
//progress_dialog.progressBarTotal.Enabled = false;
break;
case UpdateProgressReason.JobDone:
progress_dialog.labelStatusTotal.Text = "Done";
break;
case UpdateProgressReason.StreamSwitch:
progress_dialog.labelStatus.Text = string.Format
(Options.GetLiteral(Options.LANG_MOVE_RENAME_0_ARROW_1),
e.SourceFile,
e.DestinationFile);
progress_dialog.SetProgress(e.StreamTransferred, e.StreamSize);
break;
}
}
private object abort_job_lock=new object();
private bool abort_job_unsafe = false;
public bool AbortJobSafe
{
get
{
bool ret = false;
lock (abort_job_lock)
{
ret = abort_job_unsafe;
}
return ret;
}
set
{
lock (abort_job_lock)
{
abort_job_unsafe = value;
}
}
}
private void move_proc()
{
//notify about job begin
UpdateProgressArgs e_begin = new UpdateProgressArgs();
e_begin.EnableTotalProgress = false;
e_begin.Reason = UpdateProgressReason.JobBegin;
update_progress_safe(e_begin);
//check source and destination
//string cur_source = string.Empty;
//WIN32_FIND_DATA src_data = new WIN32_FIND_DATA();
//WIN32_FIND_DATA dst_data = new WIN32_FIND_DATA();
FileInfoEx dest_info = new FileInfoEx();
bool dst_exist = FileInfoEx.TryGet(innitial_destination, ref dest_info);
FileInfoEx cur_source = null;
//bool dst_exist = WinAPiFSwrapper.GetFileInfo(innitial_destination, ref dst_data);
for (int i = 0; i < initial_source.Count; i++)
{
if (AbortJobSafe)
{
break;
}
try
{
cur_source = initial_source[i];
if (!Wildcard.Match(initial_mask, cur_source.FileName))
{
continue;
}
//WinAPiFSwrapper.GetFileInfo(cur_source, ref src_data);
if (cur_source.Directory)
{
//src is directory
if (!dst_exist)
{
//destination not exist - treat as new directory
move_one_item(cur_source, innitial_destination);
dst_exist = FileInfoEx.TryGet(innitial_destination, ref dest_info);
}
else
{
//destination exist
if (dest_info.Directory)
{
//and it is dir -> move cur_source into initial_destination
move_one_item(cur_source, Path.Combine(innitial_destination, cur_source.FileName));
}
else
{
//move dir to existing file -> not normally
AbortJobSafe = !process_errors
(string.Format
(Options.GetLiteral(Options.LANG_WRONG_DESTINATION_SOURCE_0_DIRECTORY_DESTINATION_1_FILE),
cur_source.FileName,
innitial_destination),
null);
continue;
}
}
}
else
{
//source is file
if (!dst_exist)
{
//dest not exists
if (initial_source.Count == 1)
{
//treat dest as file
move_one_item(cur_source, innitial_destination);
}
else
{
//treat dest as dir., move source into new dir
Directory.CreateDirectory(innitial_destination);
dst_exist = FileInfoEx.TryGet(innitial_destination, ref dest_info);
move_one_item(cur_source, Path.Combine(innitial_destination, cur_source.FileName));
}
}
else
{
//dst exists
if (dest_info.Directory)
{
//move file into initial_destination
move_one_item(cur_source, Path.Combine(innitial_destination, cur_source.FileName));
}
else
{
//dst exists and it is existing file
if (initial_source.Count == 1)
{
//move file with new name, overwriting existing
move_one_item(cur_source, innitial_destination);
}
else
{
//not mormal: more then one item into one existing file
AbortJobSafe = !process_errors
(string.Format
(Options.GetLiteral(Options.LANG_WRONG_DESTINATION_CANNOT_MOVE_MANY_ENTRIES_INTO_ONE_FILE_0),
innitial_destination),
null);
continue;
}
}
}//end of dest exists brunch
}//end of src is file
}//end of try
catch (Exception ex)
{
AbortJobSafe = !process_errors
(string.Format
(Options.GetLiteral(Options.LANG_CANNOT_MOVE_0_ARROW_1),
initial_source[i].FullName,
innitial_destination),
ex);
}
}//end of for...
//notify about end of job
UpdateProgressArgs e_end = new UpdateProgressArgs();
e_end.Reason = UpdateProgressReason.JobDone;
update_progress_safe(e_end);
if (Done != null)
{
Done(this, new EventArgs());
}
}//end of proc
/// <summary>
/// work only if source and destination on one volume
/// </summary>
/// <param name="source">can be file or dir</param>
/// <param name="destination">file or dir</param>
private void move_one_item(FileInfoEx source, string destination)
{
//prepare callback buffer
if (callback_data != IntPtr.Zero)
{
Marshal.FreeHGlobal(callback_data);
}
callback_data = IOhelper.strings_to_buffer(source.FullName, destination);
//prepare uni names
string source_uni = IOhelper.GetUnicodePath(source.FullName);
string destination_uni = IOhelper.GetUnicodePath(destination);
//call update dialog before...
UpdateProgressArgs e_begin_move = new UpdateProgressArgs();
e_begin_move.DestinationFile = destination;
e_begin_move.EnableTotalProgress = false;
e_begin_move.Reason = UpdateProgressReason.StreamSwitch;
e_begin_move.SourceFile = source.FullName;
update_progress_safe(e_begin_move);
//and call MoveFileWithProgress
int res = WinApiFS.MoveFileWithProgress
(source_uni,
destination_uni,
move_progress_delegate_holder,
callback_data,
options_api);
if (res == 0)
{
int win_err = Marshal.GetLastWin32Error();
Win32Exception win_ex=new Win32Exception(win_err);
AbortJobSafe = !process_errors
(string.Format
(Options.GetLiteral(Options.LANG_CANNOT_MOVE_0_ARROW_1),
source,
destination),
win_ex);
return;
}
//success move
//notify item done - not needed, source item deleted
}
private bool process_errors(string info, Exception ex)
{
if ((options & MoveEngineOptions.SupressErrors) == MoveEngineOptions.SupressErrors)
{
return true;
}
return Messages.ShowExceptionContinue(ex, info);
}
#region IDisposable Members
public void Dispose()
{
if (callback_data != IntPtr.Zero)
{
Marshal.FreeHGlobal(callback_data);
}
}
#endregion
}
}