Пример #1
0
        public bool GetEntryInfo(string path_name, ref FtpEntryInfo info_to_fill, bool force_update)
        {
            string directory = FtpPath.GetDirectory(path_name);
            string file      = FtpPath.GetFile(path_name);
            bool   ret       = false;

            //get parent directory contents
            List <FtpEntryInfo> dir_list = new List <FtpEntryInfo>();

            try
            {
                dir_list = GetDirectoryDetailsList(FtpPath.AppendEndingSeparator(directory), force_update);
            }
            catch { } //supress all errors

            //find entry
            foreach (FtpEntryInfo info in dir_list)
            {
                if (info.EntryName == file)
                {
                    info_to_fill = info.Clone();
                    ret          = true;
                }
            }

            return(ret);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
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;
            }
        }
Пример #5
0
 private void cache_remove(string directory)
 {
     directory = FtpPath.AppendEndingSeparator(directory);
     lock (cache_lock)
     {
         if (internal_cache.ContainsKey(directory))
         {
             internal_cache.Remove(directory);
         }
     }
 }
Пример #6
0
        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());
                }
            }
        }
Пример #7
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();
        }
Пример #8
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));
            }
        }
Пример #9
0
        public FtpStatusCode AppendFile(string path_name, Stream data_to_append, FtpTransferProgress callback, object state)
        {
            FtpWebRequest  req  = null;
            FtpWebResponse resp = null;

            try
            {
                req        = create_request(path_name);
                req.Method = WebRequestMethods.Ftp.AppendFile;
                Stream req_stream = req.GetRequestStream();

                //allocate buffer
                byte[] buffer       = new byte[BUFFER_SIZE];
                int    bytes_readed = 0;

                //callback variables
                long bytes_transferred = 0L;
                bool abort             = false;

                do
                {
                    //call callback
                    bytes_transferred += bytes_readed;
                    if (callback != null)
                    {
                        callback(bytes_transferred, ref abort, state);
                    }
                    if (abort)
                    {
                        break;
                    }
                    bytes_readed = data_to_append.Read(buffer, 0, BUFFER_SIZE);
                    req_stream.Write(buffer, 0, bytes_readed);
                } while (bytes_readed != 0);

                //get response
                req_stream.Close();
                resp = (FtpWebResponse)req.GetResponse();
                cache_remove(FtpPath.GetDirectory(path_name));
                return(resp.StatusCode);
            }
            finally
            {
                if (resp != null)
                {
                    resp.Close();
                }
            }
        }
Пример #10
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);
                }
            }
        }
Пример #11
0
        public void CreateDirectoryTree(string path_name)
        {
            //check if already exists
            FtpEntryInfo current_info = new FtpEntryInfo();

            if (GetEntryInfo(path_name, ref current_info, false))
            {
                //already exists, return
                return;
            }

            //check if parent exist
            string parent_dir = FtpPath.GetDirectory(path_name);

            if (parent_dir == path_name)
            {
                //that is path_name is root path
                return;
            }

            FtpEntryInfo parent_info = new FtpEntryInfo();

            if (GetEntryInfo(parent_dir, ref parent_info, false))
            {
                //parent exists
                if (!parent_info.Directory)
                {
                    //but it is file - abnormal
                    throw new ApplicationException
                              (string.Format
                                  ("Failed create directory '{0}'. Parent entry is file.",
                                  path_name));
                }
                //try create directory path_name
                CreateDirectory(path_name);
            }
            else
            {
                //parent not exists
                CreateDirectoryTree(parent_dir);
                CreateDirectory(path_name);
            }
        }
Пример #12
0
        public FtpStatusCode DeleteDirectory(string path_name)
        {
            FtpWebRequest  req  = null;
            FtpWebResponse resp = null;

            try
            {
                req        = create_request(path_name);
                req.Method = WebRequestMethods.Ftp.RemoveDirectory;
                resp       = (FtpWebResponse)req.GetResponse();
                cache_remove(FtpPath.GetDirectory(path_name));
                return(resp.StatusCode);
            }
            finally
            {
                if (resp != null)
                {
                    resp.Close();
                }
            }
        }
Пример #13
0
        public FtpStatusCode Rename(string existing_path_name, string new_name)
        {
            FtpWebRequest  req  = null;
            FtpWebResponse resp = null;

            try
            {
                req          = create_request(existing_path_name);
                req.Method   = WebRequestMethods.Ftp.Rename;
                req.RenameTo = new_name;
                resp         = (FtpWebResponse)req.GetResponse();
                cache_remove(FtpPath.GetDirectory(existing_path_name));
                return(resp.StatusCode);
            }
            finally
            {
                if (resp != null)
                {
                    resp.Close();
                }
            }
        }
Пример #14
0
        public FtpStatusCode DeleteFile(string path_name)
        {
            FtpWebRequest  req  = null;
            FtpWebResponse resp = null;
            var            ret  = FtpStatusCode.Undefined;

            try
            {
                req        = create_request(path_name);
                req.Method = WebRequestMethods.Ftp.DeleteFile;
                resp       = (FtpWebResponse)req.GetResponse();
                ret        = resp.StatusCode;
                cache_remove(FtpPath.GetDirectory(path_name));
            }
            finally
            {
                if (resp != null)
                {
                    resp.Close();
                }
            }
            return(ret);
        }
Пример #15
0
 protected override void internal_refill()
 {
     try
     {
         OnLongOperaion(Options.GetLiteral(Options.LANG_FTP_WAIT_RETRIEVE_DIRECTORY_LIST), true);
         var entry_list =
             internal_conection.GetDirectoryDetailsList
                 (FtpPath.AppendEndingSeparator(internal_directory_path),
                 false);
         internal_list.Clear();
         foreach (var info in entry_list)
         {
             internal_list.Add(info, null);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         OnLongOperaion(string.Empty, false);
     }
 }
Пример #16
0
 protected override void internal_refill()
 {
     try
     {
         OnLongOperaion("Wait while retrive file list from server...", true);
         List <FtpEntryInfo> entry_list =
             internal_conection.GetDirectoryDetailsList
                 (FtpPath.AppendEndingSeparator(internal_directory_path),
                 false);
         internal_list.Clear();
         foreach (FtpEntryInfo info in entry_list)
         {
             internal_list.Add(info, null);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         OnLongOperaion("Completed.", false);
     }
 }
Пример #17
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);
                }
            }
        }
Пример #18
0
        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();
                }
            }
        }
Пример #19
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();
                }
            }
        }
Пример #20
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);
                }
            }
        }
Пример #21
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
Пример #22
0
        public override void GetChildCollection
            (int index,
            ref FileCollectionBase new_collection,
            ref bool use_new,
            ref string preferred_focused_text)
        {
            if (LockSafe)
            {
                Messages.ShowMessage("Cannot change directory now.");
                return;
            }

            if (!GetItemIsContainer(index))
            {
                use_new = false;
                return;
            }

            //directory to go to..
            var target_dir   = internal_list.Keys[index];
            var old_virt_dir = string.Empty;

            if (target_dir == "..")
            {
                //up
                if (zip_virtual_dir == string.Empty)
                {
                    //go to directory list
                    try
                    {
                        new_collection         = new DirectoryList(0, false, Path.GetDirectoryName(zip_file_name));
                        preferred_focused_text = Path.GetFileName(zip_file_name);
                        use_new = true;
                        new_collection.MainWindow = (mainForm)Program.MainWindow;
                        new_collection.Refill();
                    }
                    catch (Exception ex)
                    {
                        Messages.ShowException(ex);
                    }
                    return;
                }
                else
                {
                    //up

                    old_virt_dir           = zip_virtual_dir;
                    zip_virtual_dir        = FtpPath.GetDirectory(zip_virtual_dir);
                    preferred_focused_text = old_virt_dir;
                    try
                    {
                        Refill();
                    }
                    catch (Exception ex)
                    {
                        zip_virtual_dir = old_virt_dir;
                        Messages.ShowException(ex);
                    }
                    return;
                }
            }
            else
            {
                //go to down

                old_virt_dir = zip_virtual_dir;

                //down
                zip_virtual_dir = target_dir;

                try
                {
                    Refill();
                }
                catch (Exception ex)
                {
                    zip_virtual_dir = old_virt_dir;
                    Messages.ShowException(ex);
                }
                return;
            }
        }
Пример #23
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;
            }
        }
Пример #24
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();
        }