private long calc_source_size() { var ret = 0L; foreach (var info in initial_source) { if (info.Directory) { ret += calc_dir_size(FtpPath.AppendEndingSeparator(FtpPath.Combine(info.DirectoryPath, info.EntryName))); } else { ret += info.Size; } } //notify about source size calculated var e = new UpdateProgressArgs(); e.EnableTotalProgress = opts.ShowTotalProgress; e.Reason = UpdateProgressReason.TotalSizeCalculated; e.TotalSize = (ulong)ret; update_progress_safe(e); return(ret); }
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 void update_progress_safe(UpdateProgressArgs e) { if (progress_dialog == null) { return; } if (ProgressCloseBeginSafe) { return; } if (progress_dialog.CancelPressedSafe) { AbortSafe = true; return; } //supress sync errors try { progress_dialog.Invoke(update_progress_delegate_holder, new object[] { e }); } catch { } }
private void ftp_transfer_proc(long bytes_transferred, ref bool abort_transfer, object state) { var state_info = (TransferStateObject)state; count_bytes_current_transferred = bytes_transferred; try { abort_transfer = progress.CancelPressedSafe; } catch { } var update_args = new UpdateProgressArgs(); update_args.DestinationFile = state_info.DestinationFile; update_args.EnableTotalProgress = opts.ShowTotalProgress; update_args.FilesCopied = count_files_processed; update_args.KBytesPerSec = (ulong)calc_speed(); update_args.Reason = UpdateProgressReason.ChunkFinish; update_args.SourceFile = state_info.SourceFile; update_args.StreamSize = (ulong)state_info.Size; update_args.StreamTransferred = (ulong)bytes_transferred; update_args.TotalSize = (ulong)total_source_size; update_args.TotalTransferred = (ulong)(count_bytes_total_transferred + bytes_transferred); update_progress_safe(update_args); }
protected void update_progress_safe(UpdateProgressArgs e) { if (progress_dialog == null) { return; } if (ProgressCloseBeginSafe) { return; } //supress sync errors try { if (progress_dialog.InvokeRequired) { progress_dialog.Invoke(update_progress_delegate_holder, new object[] { e }); } else { update_progress(e); } } catch { } }
private void internal_run() { start_tick = Environment.TickCount; try { //notify about job begin var e = new UpdateProgressArgs(); e.DestinationFile = string.Empty; e.EnableTotalProgress = false; e.FilesCopied = 0; e.KBytesPerSec = 0; e.Reason = UpdateProgressReason.JobBegin; e.SourceFile = string.Empty; e.StreamSize = 0; e.StreamTransferred = 0; e.TotalSize = 0; e.TotalTransferred = 0; update_progress_safe(e); prepare_extract_list(); foreach (var entry in extract_list) { if (AbortSafe) { break; } var relative_path = entry.Name; if (initial_zip_dir.Length != 0) { relative_path = entry.Name.Substring(initial_zip_dir.Length); relative_path = relative_path.TrimStart(new char[] { '/' }); } var destination_path = Path.Combine(initial_destination, relative_path.Replace('/', '\\')); extract_one_entry(entry, destination_path); if (ExtractItemDone != null) { ExtractItemDone(this, new ItemEventArs(FtpPath.GetFile(entry.Name))); } } } finally { //notify job done 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 var source_uni = IOhelper.GetUnicodePath(source.FullName); var destination_uni = IOhelper.GetUnicodePath(destination); //call update dialog before... var 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 var res = WinApiFS.MoveFileWithProgress (source_uni, destination_uni, move_progress_delegate_holder, callback_data, options_api); if (res == 0) { var win_err = Marshal.GetLastWin32Error(); var 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 void update_progress_safe(UpdateProgressArgs e) { if (progress == null) { return; } if (ProgressCloseBeginSafe) { return; } try { progress.Invoke(update_progress_delegate_holder, new object[] { e }); } catch { } //supress all errors }
private void copy_streams(Stream source_stream, Stream destination_stream, long destination_size) { if (buffer.Length != buffer_size) { buffer = new byte[buffer_size]; } var e = new UpdateProgressArgs(); e.TotalSize = (ulong)total_bytes; e.Reason = UpdateProgressReason.ChunkFinish; e.SourceFile = string.Empty; e.StreamSize = (ulong)destination_size; e.DestinationFile = string.Empty; e.EnableTotalProgress = true; e.FilesCopied = 0; var bytes_readed = 0; long bytes_writed = 0; while ((bytes_readed = source_stream.Read(buffer, 0, buffer_size)) != 0) { if (AbortSafe) { break; } destination_stream.Write(buffer, 0, bytes_readed); bytes_writed = bytes_writed + bytes_readed; total_bytes_transferred = total_bytes_transferred + bytes_readed; //time to notify chunk done e.KBytesPerSec = (ulong)calc_speed(); e.StreamTransferred = (ulong)bytes_writed; e.TotalTransferred = (ulong)total_bytes_transferred; update_progress_safe(e); } }
private void update_progress(UpdateProgressArgs e) { //will be invoke in UI thread - canot use anything else e and progress_dialog members switch (e.Reason) { case UpdateProgressReason.ChunkFinish: progress_dialog.SetProgress(e.TotalTransferred, e.TotalSize); progress_dialog.labelSpeed.Text = string.Format (Options.GetLiteral(Options.LANG_0_KBYTE_SEC), e.KBytesPerSec); progress_dialog.labelStatusTotal.Text = string.Format (Options.GetLiteral(Options.LANG_0_BYTES_TRANSFERRED), e.TotalTransferred); break; case UpdateProgressReason.JobBegin: progress_dialog.Text = Options.GetLiteral(Options.LANG_EXTRACT); break; case UpdateProgressReason.JobDone: progress_dialog.SetProgress(100, 100); progress_dialog.labelSpeed.Text = string.Format (Options.GetLiteral(Options.LANG_0_KBYTE_SEC), e.KBytesPerSec); break; case UpdateProgressReason.StreamSwitch: progress_dialog.labelStatus.Text = string.Format (Options.GetLiteral(Options.LANG_0_ARROW_1_2), e.SourceFile, e.DestinationFile, IOhelper.SizeToString(e.StreamSize)); progress_dialog.labelSpeed.Text = string.Format (Options.GetLiteral(Options.LANG_0_KBYTE_SEC), e.KBytesPerSec); break; } }
private void download_one_file(FtpEntryInfo source_info, string dest_file) { var dest_handle = IntPtr.Zero; FileStream dest_stream = null; var update_args = new UpdateProgressArgs(); count_bytes_current_transferred = 0; if (progress != null) { try { AbortSafe = progress.CancelPressedSafe; } catch { } } if (AbortSafe) { return; } try { //time to notify about begin download file update_args.DestinationFile = dest_file; update_args.EnableTotalProgress = opts.ShowTotalProgress; update_args.FilesCopied = count_files_processed; update_args.KBytesPerSec = (ulong)calc_speed(); update_args.Reason = UpdateProgressReason.StreamSwitch; update_args.SourceFile = FtpPath.Combine(source_info.DirectoryPath, source_info.EntryName); update_args.StreamSize = (ulong)source_info.Size; update_args.StreamTransferred = 0; update_args.TotalSize = (ulong)total_source_size; update_args.TotalTransferred = (ulong)count_bytes_total_transferred; update_progress_safe(update_args); //destination file exist? var dest_data = new WIN32_FIND_DATA(); var dest_exist = WinAPiFSwrapper.GetFileInfo(dest_file, ref dest_data); var source_date = connection.GetStamp(FtpPath.Combine(source_info.DirectoryPath, source_info.EntryName)); if (dest_exist) { switch (opts.Overwrite) { case OverwriteExisting.No: //overwrite prohibited AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_DESTINATION_0_EXIST_OVERWRITING_PROHIBITED), dest_file), null); //and return return; case OverwriteExisting.IfSourceNewer: //check file date //get datetime from server if (source_date <= DateTime.FromFileTime(dest_data.ftLastWriteTime)) { //source older AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_DESTINATION_0_NEWER_THEN_SOURCE_1_OVERWRITING_PROHIBITED), dest_file, source_info.EntryName), null); //return return; } break; } } //now we can overwrite destination if it exists //create destination directory WinAPiFSwrapper.CreateDirectoryTree(Path.GetDirectoryName(dest_file), string.Empty); //open dest file (handle will be close at finally block with stream.close()) dest_handle = WinAPiFSwrapper.CreateFileHandle (dest_file, Win32FileAccess.GENERIC_WRITE, FileShare.Read, FileMode.Create, CreateFileOptions.None); //create destination stream dest_stream = new FileStream (dest_handle, FileAccess.Write, true); //set destinstion length dest_stream.SetLength(source_info.Size); //and call download var transferred = connection.DownloadFile (FtpPath.Combine(source_info.DirectoryPath, source_info.EntryName), dest_stream, transfer_progress_delegate_holder, new TransferStateObject(FtpPath.Combine(source_info.DirectoryPath, source_info.EntryName), dest_file, source_info.Size), opts.BufferSize); dest_stream.Close(); //and set attributes File.SetLastWriteTime(dest_file, source_date); //now time to update count count_files_processed++; count_bytes_total_transferred += count_bytes_current_transferred; } catch (Exception ex) { AbortSafe = !process_error (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DOWNLOAD_0), source_info.EntryName), ex); } finally { if (dest_stream != null) { dest_stream.Close(); } } }
private void extract_one_entry(ZipEntry entry, string destination_file) { try { if (entry.IsDirectory) { extract_directory(entry, destination_file); return; } if (!check_destination(entry, destination_file)) { return; } var e = new UpdateProgressArgs(); e.DestinationFile = destination_file; e.EnableTotalProgress = false; e.FilesCopied = 0; e.KBytesPerSec = (ulong)calc_speed(); e.Reason = UpdateProgressReason.StreamSwitch; e.SourceFile = entry.Name; e.StreamSize = (ulong)entry.Size; e.StreamTransferred = 0; e.TotalSize = 0; e.TotalTransferred = (ulong)total_bytes_transferred; update_progress_safe(e); //open streams Stream in_stream = null; FileStream out_stream = null; try { in_stream = zip_file.GetInputStream(entry); out_stream = new FileStream (destination_file, (opts & ArchiveExtractOptions.NeverOverwite) == ArchiveExtractOptions.NeverOverwite ? FileMode.CreateNew : FileMode.Create, FileAccess.Write, FileShare.Read); copy_streams(in_stream, out_stream, entry.Size); } finally { if (in_stream != null) { in_stream.Close(); } if (out_stream != null) { out_stream.Flush(); out_stream.Close(); } } if ((File.Exists(destination_file)) && ((opts & ArchiveExtractOptions.ExtractAttributes) == ArchiveExtractOptions.ExtractAttributes)) { var fi = new FileInfo(destination_file); try { fi.LastWriteTime = entry.DateTime; } catch (Exception ex) { AbortSafe = !process_error (string.Format ("cannot apply timestamp to '{0}'", destination_file), ex); } if (entry.IsDOSEntry) { try { fi.Attributes = (FileAttributes)entry.ExternalFileAttributes; } catch (Exception ex) { AbortSafe = !process_error (string.Format ("cannot apply attributes to '{0}'", destination_file), ex); } } } } catch (Exception ex) { AbortSafe = !process_error(string.Empty, ex); } }
private void internal_do() { start_tick = Environment.TickCount; var update_args = new UpdateProgressArgs(); update_args.EnableTotalProgress = opts.ShowTotalProgress; update_args.FilesCopied = 0; update_args.KBytesPerSec = 0; update_args.Reason = UpdateProgressReason.JobBegin; update_args.StreamTransferred = 0; update_progress_safe(update_args); if (opts.ShowTotalProgress) { total_source_size = calc_source_size(); } var source_data = new WIN32_FIND_DATA(); var dest_info = new FtpEntryInfo(); //bool dest_exists = connection.GetEntryInfo(initial_destination, ref dest_info, false); for (var i = 0; i < initial_source.Length; i++) { if (AbortSafe) { break; } if (!WinAPiFSwrapper.GetFileInfo(initial_source[i], ref source_data)) { AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_SOURCE_FILE_0_NOT_FOUND), initial_source[i]), null); continue; } var dest_exists = connection.GetEntryInfo(initial_destination, ref dest_info, false); if ((source_data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { if (dest_exists) { if (dest_info.Directory) { upload_directory_recurse (initial_source[i], FtpPath.Combine (initial_destination, source_data.cFileName)); }//end of dest is dir else { AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_SOURCE_0_DIRECTORY_DESTINATION_1_FILE), initial_destination[i], initial_destination), null); continue; } }//end of dest_exists else { if (initial_source.Length == 1) { upload_directory_recurse (initial_source[i], initial_destination); } else { upload_directory_recurse (initial_source[i], FtpPath.Combine(initial_destination, source_data.cFileName)); } } //end of dest not exists } //end source is dir else { //source is file if (dest_exists) { if (dest_info.Directory) { upload_one_file (initial_source[i], FtpPath.Combine(initial_destination, source_data.cFileName)); }//end of dest is dir else { if (initial_source.Length != 1) { //cannot upload many entries into one file AbortSafe = !process_error (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_CANNOT_UPLOAD_MANY_ENTRIES_INTO_ONE_FILE), null); continue; } else { //try overwrite existing upload_one_file(initial_source[i], initial_destination); } } }//end of dest exists else { //dest not exists and source is file if (initial_source.Length == 1) { //assume that dest is new file name upload_one_file(initial_source[i], initial_destination); } else { //assume that dest is new dir upload_one_file (initial_source[i], FtpPath.Combine(initial_destination, source_data.cFileName)); } } }//end of source is file if (UploadItemDone != null) { UploadItemDone(this, new ItemEventArs(source_data.cFileName)); } }//end for count_bytes_current_transferred = 0; //time to notify about job completed update_args.EnableTotalProgress = opts.ShowTotalProgress; update_args.FilesCopied = count_files_processed; update_args.KBytesPerSec = (ulong)calc_speed(); update_args.Reason = UpdateProgressReason.JobDone; update_args.TotalSize = (ulong)total_source_size; update_args.TotalTransferred = (ulong)count_bytes_total_transferred; update_progress_safe(update_args); connection.ClearCache(); connection.NotifyUpdateNeeded(); if (Done != null) { Done(this, new EventArgs()); } }//end proc
private void upload_one_file(string source_file, string destination_path) { if (AbortSafe) { return; } var source_handle = IntPtr.Zero; FileStream source_stream = null; var update_args = new UpdateProgressArgs(); try { //open source file try { source_handle = WinAPiFSwrapper.CreateFileHandle (source_file, Win32FileAccess.GENERIC_READ, FileShare.Read, FileMode.Open, CreateFileOptions.None); source_stream = new FileStream (source_handle, FileAccess.Read, true); //time to notify about begin download file update_args.DestinationFile = destination_path; update_args.EnableTotalProgress = opts.ShowTotalProgress; update_args.FilesCopied = count_files_processed; update_args.KBytesPerSec = (ulong)calc_speed(); update_args.Reason = UpdateProgressReason.StreamSwitch; update_args.SourceFile = source_file; update_args.StreamSize = (ulong)source_stream.Length; update_args.StreamTransferred = 0; update_args.TotalSize = (ulong)total_source_size; update_args.TotalTransferred = (ulong)count_bytes_total_transferred; update_progress_safe(update_args); } catch (Exception ex) { AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_CANNOT_OPEN_SOURCE_FILE_0), source_file), ex); } if (source_stream == null) { return; } //need check target dir exist and create if needed var parent_dir = FtpPath.GetDirectory(destination_path); connection.CreateDirectoryTree(parent_dir); //check destination is exists var test_info = new FtpEntryInfo(); if (connection.GetEntryInfo(destination_path, ref test_info, false)) { //destination already exists switch (opts.Overwrite) { case OverwriteExisting.IfSourceNewer: //get timestamp var dest_date = connection.GetStamp(destination_path); if (dest_date < File.GetLastWriteTime(source_file)) { //delete existing target connection.DeleteFile(destination_path); } else { AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_DESTINATION_0_NEWER_THEN_SOURCE_1_OVERWRITING_PROHIBITED), destination_path, source_file), null); return; } break; case OverwriteExisting.No: AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_DESTINATION_0_EXIST_OVERWRITING_PROHIBITED), destination_path), null); return; case OverwriteExisting.Yes: connection.DeleteFile(destination_path); break; } } //update counter count_bytes_current_transferred = 0; //upload... try { connection.UploadFile (destination_path, source_stream, transfer_progress_delegate_holder, new TransferStateObject(source_file, destination_path, source_stream.Length), opts.BufferSize); } catch (Exception ex) { AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_CANNOT_UPLOAD_0_ARROW_1), source_file, destination_path), ex); } //update counter count_bytes_total_transferred += count_bytes_current_transferred; count_bytes_current_transferred = 0; count_files_processed++; //and notify client if (UploadItemDone != null) { UploadItemDone(this, new ItemEventArs(source_file)); } } catch (Exception ex) { AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_CANNOT_UPLOAD_0_ARROW_1), source_file, destination_path), ex); } finally { if (source_stream != null) { source_stream.Close(); } } }
private void update_progress(UpdateProgressArgs e) { //will be invoke in UI thread - canot use anything else e and progress_dialog members switch (e.Reason) { case UpdateProgressReason.ChunkFinish: //progress_dialog.SetProgressStream(e.StreamTransferred, e.StreamSize); if (label_update_count > label_update_interval) { progress_dialog.labelSpeed.Text = string.Format(update_lit_0, e.KBytesPerSec); } if (e.EnableTotalProgress) { progress_dialog.SetProgress(e.TotalTransferred, e.TotalSize); if (label_update_count > label_update_interval) { progress_dialog.labelStatusTotal.Text = string.Format (update_lit_1, e.TotalTransferred, e.TotalSize, (e.TotalSize == 0) ? 0 : (double)e.TotalTransferred / (double)e.TotalSize); } } else { progress_dialog.SetProgress(e.StreamTransferred, e.StreamSize); if (label_update_count > label_update_interval) { progress_dialog.labelStatusTotal.Text = string.Format(update_lit_2, e.TotalTransferred); } } if (label_update_count > label_update_interval) { label_update_count = 0; } else { label_update_count += (e.StreamTransferred - stream_transferred_pre_chunk); } stream_transferred_pre_chunk = e.StreamTransferred; break; case UpdateProgressReason.JobBegin: label_update_count = 0; progress_dialog.Text = Options.GetLiteral(Options.LANG_COPY); break; case UpdateProgressReason.JobDone: label_update_count = 0; progress_dialog.SetProgress(100, 100); progress_dialog.labelSpeed.Text = string.Format(update_lit_0, e.KBytesPerSec); //if (e.EnableTotalProgress) //{ // progress_dialog.SetProgress(100, 100); //} progress_dialog.labelStatusTotal.Text = string.Format (Options.GetLiteral(Options.LANG_0_BYTES_FROM_1_FILES_TRANSFERRED), e.TotalTransferred, e.FilesCopied); break; case UpdateProgressReason.StreamSwitch: stream_transferred_pre_chunk = 0; label_update_count = 0; progress_dialog.labelStatus.Text = string.Format (update_lit_3, e.SourceFile, e.DestinationFile, IOhelper.SizeToString(e.StreamSize)); progress_dialog.labelSpeed.Text = string.Format(update_lit_0, e.KBytesPerSec); if (e.EnableTotalProgress) { progress_dialog.SetProgress(e.TotalTransferred, e.TotalSize); progress_dialog.labelStatusTotal.Text = string.Format (update_lit_1, e.TotalTransferred, e.TotalSize, (e.TotalSize == 0) ? 0 : (double)e.TotalTransferred / (double)e.TotalSize); } else { progress_dialog.labelStatusTotal.Text = string.Format(update_lit_2, e.TotalTransferred); } break; case UpdateProgressReason.TotalSizeCalculated: if (e.EnableTotalProgress) { progress_dialog.labelStatusTotal.Text = string.Format (update_lit_1, e.TotalTransferred, e.TotalSize, (e.TotalSize == 0) ? 0 : (double)e.TotalTransferred / (double)e.TotalSize); } break; } }
private void internal_do() { //back thread var e_begin = new UpdateProgressArgs(); e_begin.Reason = UpdateProgressReason.JobBegin; update_progress_safe(e_begin); //calc total source size, if needed if ((options & CopyEngineOptions.CalculateTotalSize) == CopyEngineOptions.CalculateTotalSize) { total_source_bytes = calc_total_source_size(); var e_total_calc = new UpdateProgressArgs(); e_total_calc.KBytesPerSec = 0; e_total_calc.DestinationFile = string.Empty; e_total_calc.EnableTotalProgress = true; e_total_calc.FilesCopied = 0; e_total_calc.Reason = UpdateProgressReason.TotalSizeCalculated; e_total_calc.SourceFile = string.Empty; e_total_calc.StreamSize = 0; e_total_calc.StreamTransferred = 0; e_total_calc.TotalSize = total_source_bytes; e_total_calc.TotalTransferred = 0; update_progress_safe(e_total_calc); } //set begin job time start_tick = Environment.TickCount; var dest_info = new FileInfoEx(); var dest_exist = false; //go throw initial_source foreach (var source_info in initial_source) { if (abort_job) { break; } //check destination if (!dest_exist) { dest_exist = FileInfoEx.TryGet(initial_destination, ref dest_info); } if (dest_exist) { //destination exists if (dest_info.Directory) { if (source_info.Directory) { copy_dir(source_info, Path.Combine(initial_destination, source_info.FileName)); } else { copy_one_file(source_info, Path.Combine(initial_destination, source_info.FileName)); } }//end of destination is directory else { if ((initial_source.Count == 1) && (!source_info.Directory)) { copy_one_file(source_info, initial_destination); } else { process_error (string.Format (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_0_IS_EXISTING_FILE), initial_destination), null); stop(); } } //end of destination is file } //end of dest exists else { if (initial_source.Count == 1) { if (source_info.Directory) { //tract initial_destination as new directory: copy_dir(source_info, initial_destination); } else { //or new file copy_one_file(source_info, initial_destination); } }//end of single source else { //initial destination will be directory //and copy INTO it if (source_info.Directory) { copy_dir(source_info, Path.Combine(initial_destination, source_info.FileName)); } else { copy_one_file(source_info, Path.Combine(initial_destination, source_info.FileName)); } } //source not single } //end of dest not exists } //end foreach var e_finish = new UpdateProgressArgs(); e_finish.KBytesPerSec = calc_speed(); e_finish.DestinationFile = string.Empty; e_finish.EnableTotalProgress = ((options & CopyEngineOptions.CalculateTotalSize) == CopyEngineOptions.CalculateTotalSize); e_finish.FilesCopied = total_files_copied; e_finish.Reason = UpdateProgressReason.JobDone; e_finish.SourceFile = string.Empty; e_finish.StreamSize = 0; e_finish.StreamTransferred = 0; e_finish.TotalSize = total_source_bytes; e_finish.TotalTransferred = total_bytes_transferred; update_progress_safe(e_finish); if (Done != null) { Done(this, new EventArgs()); } }
//callback from CopyFileEx, call from back back thread private CopyFileExCallbackReturns copy_progress_proc(UInt64 TotalFileSize, UInt64 TotalBytesTransferred, UInt64 StreamSize, UInt64 StreamBytesTransferred, int dwStreamNumber, CopyFileExState dwCallbackReason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData) { //early exit if progress dialog absent or begin close if (progress_dialog == null) { return(CopyFileExCallbackReturns.QUIET); } if (ProgressCloseBeginSafe) { stop(); return(CopyFileExCallbackReturns.CANCEL); } var e = new UpdateProgressArgs(); current_file_bytes_transferred = TotalBytesTransferred; var speed = calc_speed(); if (dwCallbackReason == CopyFileExState.STREAM_SWITCH) { //new stream begin copy //retrieve file names from lpData var src = string.Empty; var dst = string.Empty; IOhelper.strings_from_buffer(ref src, ref dst, lpData); //set update args e.DestinationFile = dst; e.SourceFile = src; e.Reason = UpdateProgressReason.StreamSwitch; } else { e.Reason = UpdateProgressReason.ChunkFinish; } //set update args e.KBytesPerSec = speed; e.StreamSize = StreamSize; e.StreamTransferred = StreamBytesTransferred; e.TotalTransferred = total_bytes_transferred + current_file_bytes_transferred; e.FilesCopied = total_files_copied; e.EnableTotalProgress = ((options & CopyEngineOptions.CalculateTotalSize) == CopyEngineOptions.CalculateTotalSize); e.TotalSize = total_source_bytes; //check progress dialog if (ProgressCloseBeginSafe) { stop(); return(CopyFileExCallbackReturns.CANCEL); } //and try invoke //sync errors may be if progress dialog close before invoking update_progress_safe(e); if (ProgressCloseBeginSafe) { stop(); return(CopyFileExCallbackReturns.CANCEL); } //need try becouse sync error may occur if progress_dialog closed try { if (progress_dialog.CancelPressedSafe) { stop(); return(CopyFileExCallbackReturns.CANCEL); } else { return(CopyFileExCallbackReturns.CONTINUE); } } catch { //and if error occur -> stop copy stop(); return(CopyFileExCallbackReturns.CANCEL); } }
private void move_proc() { //notify about job begin var 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(); var dest_info = new FileInfoEx(); var dst_exist = FileInfoEx.TryGet(innitial_destination, ref dest_info); FileInfoEx cur_source = null; //bool dst_exist = WinAPiFSwrapper.GetFileInfo(innitial_destination, ref dst_data); for (var 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 var e_end = new UpdateProgressArgs(); e_end.Reason = UpdateProgressReason.JobDone; update_progress_safe(e_end); if (Done != null) { Done(this, new EventArgs()); } } //end of proc
//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); } var e = new UpdateProgressArgs(); if (dwCallbackReason == CopyFileExState.STREAM_SWITCH) { var src = string.Empty; var 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(UpdateProgressArgs e) { //will be invoke in UI thread - canot use anything else e and progress_dialog members switch (e.Reason) { case UpdateProgressReason.ChunkFinish: progress_dialog.SetProgressStream(e.StreamTransferred, e.StreamSize); progress_dialog.labelSpeed.Text = string.Format("{0:N0} Kbyte/sec", e.KBytesPerSec); //progress_dialog.labelStatus.Text = // string.Format // ("'{0}' -> '{1}' ({2})", // e.SourceFile, // e.DestinationFile, // IOhelper.SizeToString(e.StreamSize)); if (e.EnableTotalProgress) { progress_dialog.SetProgressTotal(e.TotalTransferred, e.TotalSize); progress_dialog.labelStatusTotal.Text = string.Format ("{0:N0} bytes from {1:N0} ({2:P0}) transferred", e.TotalTransferred, e.TotalSize, (e.TotalSize == 0) ? 0 : (double)e.TotalTransferred / (double)e.TotalSize); } else { progress_dialog.labelStatusTotal.Text = string.Format("{0:N0} bytes transferred", e.TotalTransferred); } break; case UpdateProgressReason.JobBegin: progress_dialog.Text = "Copy"; start_tick = Environment.TickCount; break; case UpdateProgressReason.JobDone: progress_dialog.SetProgressStream(100, 100); progress_dialog.labelSpeed.Text = string.Format("{0:N0} Kbyte/sec", e.KBytesPerSec); if (e.EnableTotalProgress) { progress_dialog.SetProgressTotal(100, 100); } progress_dialog.labelStatusTotal.Text = string.Format ("{0:N0} bytes from {1:N0} file(s) transferred", e.TotalTransferred, e.FilesCopied); break; case UpdateProgressReason.StreamSwitch: progress_dialog.labelStatus.Text = string.Format ("'{0}' -> '{1}' ({2})", e.SourceFile, e.DestinationFile, IOhelper.SizeToString(e.StreamSize)); //progress_dialog.labelSpeed.Text = string.Format("{0:N0} Kbyte/sec", e.KBytesPerSec); if (e.EnableTotalProgress) { progress_dialog.SetProgressTotal(e.TotalTransferred, e.TotalSize); //progress_dialog.labelStatusTotal.Text = // string.Format // ("{0:N0} bytes from {1:N0} ({2:P0}) transferred", // e.TotalTransferred, // e.TotalSize, // (e.TotalSize == 0) ? 0 : (double)e.TotalTransferred / (double)e.TotalSize); } else { //progress_dialog.labelStatusTotal.Text = //string.Format("{0:N0} bytes transferred", e.TotalTransferred); } break; case UpdateProgressReason.TotalSizeCalculated: if (e.EnableTotalProgress) { progress_dialog.labelStatusTotal.Text = string.Format ("{0:N0} bytes from {1:N0} ({2:P0}) transferred", e.TotalTransferred, e.TotalSize, (e.TotalSize == 0) ? 0 : (double)e.TotalTransferred / (double)e.TotalSize); } break; } }
private void internal_do() { start_tick = Environment.TickCount; var update_args = new UpdateProgressArgs(); update_args.EnableTotalProgress = opts.ShowTotalProgress; update_args.FilesCopied = 0; update_args.KBytesPerSec = 0; update_args.Reason = UpdateProgressReason.JobBegin; update_args.StreamTransferred = 0; update_progress_safe(update_args); if (opts.ShowTotalProgress) { total_source_size = calc_source_size(); } var dest_data = new WIN32_FIND_DATA(); //bool dest_exist = WinAPiFSwrapper.GetFileInfo(initial_destination, ref dest_data); foreach (var info in initial_source) { if (AbortSafe) { break; } var dest_exist = WinAPiFSwrapper.GetFileInfo(initial_destination, ref dest_data); if (info.Directory) { if (dest_exist) { if ((dest_data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { //download source directory into directory=initial_destination/info.ENtryName download_directory(info, Path.Combine(initial_destination, info.EntryName)); } else { //show error AbortSafe = !process_error (string.Format (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_SOURCE_0_DIRECTORY_DESTINATION_1_FILE), info.EntryName, initial_destination), null); continue; } } else { //dest not exist if (initial_source.Length == 1) { //that is download one directory to directory with new name download_directory(info, initial_destination); } else { //download driectory INTO initial_destination download_directory(info, Path.Combine(initial_destination, info.EntryName)); } } } else { //info is file if (dest_exist) { if ((dest_data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) { //dest existing dir //download one file into initial destination download_one_file(info, Path.Combine(initial_destination, info.EntryName)); } else { //dest is file if (initial_source.Length != 1) { //cannot download many entries into one file AbortSafe = !process_error (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_CANNOT_DOWNLOAD_MANY_ENTRIES_INTO_ONE_FILE), null); continue; } else { //overwrite existing download_one_file(info, initial_destination); } } } else { //dest not exist and info is file if (initial_source.Length == 1) { //assume that dest is new file name download_one_file(info, initial_destination); } else { //assume that dest is new dir download_one_file(info, Path.Combine(initial_destination, info.EntryName)); } } } if (DownloadItemDone != null) { DownloadItemDone(this, new ItemEventArs(info.EntryName)); } } count_bytes_current_transferred = 0; //time to notify about job completed update_args.EnableTotalProgress = opts.ShowTotalProgress; update_args.FilesCopied = count_files_processed; update_args.KBytesPerSec = (ulong)calc_speed(); update_args.Reason = UpdateProgressReason.JobDone; update_args.TotalSize = (ulong)total_source_size; update_args.TotalTransferred = (ulong)count_bytes_total_transferred; update_progress_safe(update_args); if (Done != null) { Done(this, new EventArgs()); } }