Example #1
0
        private void prepare_extract_list()
        {
            extract_list = new List <ZipEntry>();
            foreach (var initial_src in initial_sources)
            {
                var current_source = FtpPath.Combine(initial_zip_dir, initial_src);
                if (current_source.StartsWith("/"))
                {
                    current_source = current_source.Substring(1);
                }

                var current_entry = zip_file.GetEntry(current_source);
                if ((current_entry != null) && (current_entry.IsFile))
                {
                    if (!extract_list.Contains(current_entry))
                    {
                        extract_list.Add(current_entry);
                    }
                }
                else
                {
                    fill_extract_list(current_source);
                }
            }
            foreach (var entry in extract_list)
            {
                total_bytes += entry.Size;
            }
        }
Example #2
0
        private long calc_dir_size(string dir_name)
        {
            var ret = 0L;

            //get file info list from server
            var dir_list = new List <FtpEntryInfo>();

            try
            {
                dir_list = connection.GetDirectoryDetailsList(dir_name, false);
            }
            catch (Exception ex)
            {
                AbortSafe = !process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_READ_DIRECTORY_CONTENTS_0),
                                    dir_name),
                                ex);
            }
            if (!AbortSafe)
            {
                foreach (var info in dir_list)
                {
                    if (info.Directory)
                    {
                        ret += calc_dir_size(FtpPath.AppendEndingSeparator(FtpPath.Combine(info.DirectoryPath, info.EntryName)));
                    }
                    else
                    {
                        ret += info.Size;
                    }
                }
            }
            return(ret);
        }
Example #3
0
        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);
        }
Example #4
0
        protected override void internal_command_proc()
        {
            QueryPanelInfoEventArgs e_current = new QueryPanelInfoEventArgs();

            OnQueryCurrentPanel(e_current);
            FtpDirectoryList ftp_list = (FtpDirectoryList)e_current.ItemCollection;

            string current_dir = ftp_list.DirectoryPath;

            //show dialog
            CreateDirectoryDialog dialog = new CreateDirectoryDialog();

            dialog.Text = Options.GetLiteral(Options.LANG_DIRECTORY_CREATE);
            dialog.labelParentDir.Text              = current_dir;
            dialog.checkBoxUseTemplate.Enabled      = false;
            dialog.textBoxTemplateDirectory.Enabled = false;

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            string new_dir = FtpPath.Combine(current_dir, dialog.textBoxDirectoryName.Text);

            //create dir tree
            if (ftp_list.MainWindow != null)
            {
                ftp_list.MainWindow.NotifyLongOperation
                    (string.Format
                        (Options.GetLiteral(Options.LANG_DIRECTORY_CREATE_0),
                        new_dir),
                    true);
            }

            try
            {
                ftp_list.Connection.CreateDirectoryTree(new_dir);
            }
            catch (Exception ex)
            {
                Messages.ShowException(ex);
            }

            if (ftp_list.MainWindow != null)
            {
                ftp_list.MainWindow.NotifyLongOperation
                    (string.Empty,
                    false);
            }

            ftp_list.Refill();
        }
Example #5
0
        public override string GetCommandlineTextLong(int index)
        {
            var ret  = string.Empty;
            var info = this[index];

            if (info.EntryName == "..")
            {
                return(ret);
            }
            else
            {
                return(FtpPath.Combine(info.DirectoryPath, info.EntryName));
            }
        }
Example #6
0
        private void download_directory(FtpEntryInfo source_dir_info, string dest_dir)
        {
            //we need list of dir contents
            var contents  = new List <FtpEntryInfo>();
            var dest_file = string.Empty;

            try
            {
                contents = connection.GetDirectoryDetailsList
                               (FtpPath.AppendEndingSeparator
                                   (FtpPath.Combine(source_dir_info.DirectoryPath, source_dir_info.EntryName)),
                               true);
            }
            catch (Exception ex)
            {
                AbortSafe = !process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_READ_DIRECTORY_CONTENTS_0),
                                    FtpPath.Combine(source_dir_info.DirectoryPath, source_dir_info.EntryName)),
                                ex);
            }

            //if success
            foreach (var info in contents)
            {
                if (AbortSafe)
                {
                    break;
                }

                dest_file = Path.Combine(dest_dir, info.EntryName);
                if (info.Directory)
                {
                    //directory - call recursively
                    download_directory(info, dest_file);
                }
                else
                {
                    //that is file
                    download_one_file
                        (info,
                        dest_file);
                }
            }
        }
Example #7
0
        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();
                }
            }
        }
Example #8
0
        private void delete_recursive(FtpEntryInfo info, mainForm main_window)
        {
            if (abort)
            {
                return;
            }

            if (info.Directory)
            {
                //first we must delete all contents of directory
                //get contents
                //and notify main window
                if (main_window != null)
                {
                    main_window.NotifyLongOperation
                        (string.Format
                            (Options.GetLiteral(Options.LANG_FTP_WAIT_RETRIEVE_DIRECTORY_LIST)),
                        true);
                }
                List <FtpEntryInfo> contents = new List <FtpEntryInfo>();
                try
                {
                    contents =
                        connection.GetDirectoryDetailsList
                            (FtpPath.AppendEndingSeparator(FtpPath.Combine(info.DirectoryPath, info.EntryName)), true);
                }
                catch (Exception ex)
                {
                    abort = !process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_READ_DIRECTORY_CONTENTS_0),
                                    FtpPath.Combine(info.DirectoryPath, info.EntryName)),
                                ex);
                }
                if (abort)
                {
                    return;
                }
                foreach (FtpEntryInfo current_info in contents)
                {
                    delete_recursive(current_info, main_window);
                    if (abort)
                    {
                        return;
                    }
                }
                //and after this we can remove directory
                main_window.NotifyLongOperation
                    (string.Format
                        (Options.GetLiteral(Options.LANG_DELETE_NOW_0),
                        FtpPath.Combine(info.DirectoryPath, info.EntryName)),
                    true);
                try
                {
                    connection.DeleteDirectory(FtpPath.Combine(info.DirectoryPath, info.EntryName));
                }
                catch (Exception ex)
                {
                    abort = !process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_DELETE_0),
                                    FtpPath.Combine(info.DirectoryPath, info.EntryName)),
                                ex);
                }
            }
            else
            {
                //remove one file
                main_window.NotifyLongOperation
                    (string.Format
                        (Options.GetLiteral(Options.LANG_DELETE_NOW_0),
                        FtpPath.Combine(info.DirectoryPath, info.EntryName)),
                    true);
                try
                {
                    connection.DeleteFile(FtpPath.Combine(info.DirectoryPath, info.EntryName));
                }
                catch (Exception ex)
                {
                    abort = !process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_DELETE_0),
                                    FtpPath.Combine(info.DirectoryPath, info.EntryName)),
                                ex);
                }
            }
        }
Example #9
0
        public override void GetChildCollection
            (int index,
            ref FileCollectionBase new_collection,
            ref bool use_new,
            ref string preferred_focused_text)
        {
            string old_dir = internal_directory_path;
            string new_dir = string.Empty;

            if (index == 0)
            {
                //go up
                if (internal_directory_path == string.Empty)
                {
                    //already root, return
                    return;
                }

                //need parent dir
                //
                //   first
                //   first/second
                //   first/second/third...

                preferred_focused_text = FtpPath.GetFile(internal_directory_path);
                new_dir = FtpPath.GetDirectory(internal_directory_path);
                internal_directory_path = new_dir;
                use_new = false;

                try
                {
                    Refill();
                }
                catch (Exception ex)
                {
                    internal_directory_path = old_dir;
                    Messages.ShowException(ex);
                }
                return;
            }
            else
            {
                //go down
                if (internal_directory_path == string.Empty)
                {
                    new_dir = internal_list.Keys[index - 1].EntryName;
                }
                else
                {
                    new_dir = FtpPath.Combine(internal_directory_path, internal_list.Keys[index - 1].EntryName);
                }
                use_new = false;
                internal_directory_path = new_dir;

                try
                {
                    Refill();
                }
                catch (Exception ex)
                {
                    internal_directory_path = old_dir;
                    Messages.ShowException(ex);
                }
                return;
            }
        }
Example #10
0
        }//end proc

        private void upload_directory_recurse(string source_dir, string destination_dir)
        {
            if (AbortSafe)
            {
                return;
            }

            //get fs entries
            //need try
            WinAPiFSwrapper.WIN32_FIND_DATA_enumerable fs_enum = null;
            try
            {
                fs_enum = new WinAPiFSwrapper.WIN32_FIND_DATA_enumerable
                              (Path.Combine(source_dir, "*"), false);
            }
            catch (Exception ex)
            {
                AbortSafe = !process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_READ_DIRECTORY_CONTENTS_0),
                                    source_dir),
                                ex);
            }

            if (fs_enum == null)
            {
                return;
            }

            var source      = string.Empty;
            var destination = string.Empty;

            foreach (var data in fs_enum)
            {
                if (AbortSafe)
                {
                    return;
                }

                if (data.cFileName == ".")
                {
                    continue;
                }

                if (data.cFileName == "..")
                {
                    continue;
                }

                source      = Path.Combine(source_dir, data.cFileName);
                destination = FtpPath.Combine(destination_dir, data.cFileName);
                if ((data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    upload_directory_recurse(source, destination);
                }
                else
                {
                    //source is file
                    upload_one_file(source, destination);
                }
            }
        }
Example #11
0
        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
Example #12
0
        protected override void internal_command_proc()
        {
            var e_current = new QueryPanelInfoEventArgs();
            var e_other   = new QueryPanelInfoEventArgs();

            OnQueryCurrentPanel(e_current);
            OnQueryOtherPanel(e_other);

            if (e_current.FocusedIndex == -1)
            {
                return;
            }

            if (!(e_other.ItemCollection is DirectoryList))
            {
                Messages.ShowMessage(Options.GetLiteral(Options.LANG_WRONG_DESTINATION));
                return;
            }

            var ftp_list = (FtpDirectoryList)e_current.ItemCollection;
            var dir_list = (DirectoryList)e_other.ItemCollection;

            var sel_source = new List <FtpEntryInfo>();

            if (e_current.SelectedIndices.Length == 0)
            {
                sel_source.Add(ftp_list[e_current.FocusedIndex]);
            }
            else
            {
                for (var i = 0; i < e_current.SelectedIndices.Length; i++)
                {
                    sel_source.Add(ftp_list[e_current.SelectedIndices[i]]);
                }
            }

            var destination = dir_list.DirectoryPath;

            //prepare and show user dialog
            var ftp_trans_opts = Options.FtpDownloadOptions;
            var dialog         = new FtpTransferDialog();

            dialog.FtpTransferOptions      = ftp_trans_opts;
            dialog.textBoxDestination.Text = destination;

            if (sel_source.Count == 1)
            {
                dialog.labelSourceFile.Text =
                    ftp_list.Connection.Options.ServerName + FtpPath.Combine(sel_source[0].DirectoryPath, sel_source[0].EntryName);
            }
            else
            {
                dialog.labelSourceFile.Text =
                    string.Format
                        ("{0} items from {1}",
                        sel_source.Count,
                        ftp_list.Connection.Options.ServerName);
            }

            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            //retrieve user selection
            ftp_trans_opts = dialog.FtpTransferOptions;
            destination    = dialog.textBoxDestination.Text;

            //save user selection
            Options.FtpDownloadOptions = ftp_trans_opts;

            //prepare progress window
            dialog_progress = new CopyFileProgressDialog();
            dialog_progress.labelError.Visible            = false;
            dialog_progress.labelSpeed.Text               = string.Empty;
            dialog_progress.labelStatus.Text              = string.Empty;
            dialog_progress.labelStatusTotal.Text         = string.Empty;
            dialog_progress.checkBoxCloseOnFinish.Checked = Options.CopyCloseProgress;

            dialog_progress.TopLevel = true;
            var x_center = Program.MainWindow.Left + Program.MainWindow.Width / 2;
            var y_center = Program.MainWindow.Top + Program.MainWindow.Height / 2;
            var x_dialog = x_center - dialog_progress.Width / 2;
            var y_dialog = y_center - dialog_progress.Height / 2;

            if (x_dialog < 0)
            {
                x_dialog = 0;
            }
            if (x_dialog < 0)
            {
                y_dialog = 0;
            }
            dialog_progress.Show();
            dialog_progress.Location = new System.Drawing.Point(x_dialog, y_dialog);

            //prepare doanload engine
            var engine = new FtpDownloadEngine
                             (sel_source.ToArray(),
                             destination,
                             ftp_list.Connection,
                             ftp_trans_opts,
                             dialog_progress);

            engine.Done             += new EventHandler(engine_Done);
            engine.DownloadItemDone += new ItemEventHandler(engine_DownloadItemDone);

            engine.Run();
        }