Exemple #1
0
 public override string GetItemDisplaySummaryInfo(int index)
 {
     if (index == 0)
     {
         return("");
     }
     else
     {
         FtpEntryInfo info = internal_list.Keys[index - 1];
         if (info.Directory)
         {
             return(string.Format
                        ("Directory [{0}] [{1} {2}]",
                        info.Permission,
                        info.Timestamp.ToShortDateString(),
                        info.Timestamp.ToShortTimeString()));
         }
         else
         {
             return(string.Format
                        ("{0} [{1}] [{2} {3}]",
                        IOhelper.SizeToString(info.Size),
                        info.Permission,
                        info.Timestamp.ToShortDateString(),
                        info.Timestamp.ToShortTimeString()));
         }
     }
 }
Exemple #2
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);
        }
Exemple #3
0
        public static FtpEntryInfo FromDetailedListLine(string line, string directory_path)
        {
            int   try_count  = FtpListParseFormats.Length;
            Match reg_result = null;

            for (int i = 0; i < try_count; i++)
            {
                reg_result = Regex.Match(line, FtpListParseFormats[i]);
                if (reg_result.Success)
                {
                    break;
                }
            }

            if (!reg_result.Success)
            {
                throw new ApplicationException(string.Format
                                                   ("Cannot parse line '{0}'.", line));
            }

            FtpEntryInfo ret = new FtpEntryInfo();

            //now success
            //get groups
            string dir_value = reg_result.Groups["dir"].Value;

            ret.Directory = ((dir_value != "") && (dir_value != "-"));

            ret.Permission = reg_result.Groups["permission"].Value;

            string size_value = reg_result.Groups["size"].Value;
            long   size_l     = 0L;

            long.TryParse(size_value, out size_l);
            ret.Size = size_l;

            string   ts_value = reg_result.Groups["timestamp"].Value;
            DateTime ts_dt    = new DateTime();

            DateTime.TryParse(ts_value, out ts_dt);
            ret.Timestamp = ts_dt;

            if (directory_path == FtpPath.PathSeparator)
            {
                ret.DirectoryPath = string.Empty;
            }
            else
            {
                ret.DirectoryPath = directory_path;
            }

            ret.EntryName = reg_result.Groups["name"].Value;

            return(ret);
        }
Exemple #4
0
        public FtpEntryInfo Clone()
        {
            FtpEntryInfo ret = new FtpEntryInfo();

            ret.Directory     = Directory;
            ret.DirectoryPath = DirectoryPath;
            ret.EntryName     = EntryName;
            ret.Permission    = Permission;
            ret.Size          = Size;
            ret.Timestamp     = Timestamp;
            return(ret);
        }
        public override string GetCommandlineTextShort(int index)
        {
            string       ret  = string.Empty;
            FtpEntryInfo info = this[index];

            if (info.EntryName == "..")
            {
                return(ret);
            }
            else
            {
                return(info.EntryName);
            }
        }
        public override string GetCommandlineTextLong(int index)
        {
            string       ret  = string.Empty;
            FtpEntryInfo info = this[index];

            if (info.EntryName == "..")
            {
                return(ret);
            }
            else
            {
                return(FtpPath.Combine(info.DirectoryPath, info.EntryName));
            }
        }
Exemple #7
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);
                }
            }
        }
Exemple #8
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);
            }
        }
Exemple #9
0
        /// <summary>
        /// path_name must be /-ended
        /// </summary>
        /// <param name="path_name"></param>
        /// <returns></returns>
        public List <FtpEntryInfo> GetDirectoryDetailsList(string path_name, bool force_update)
        {
            StreamReader        s_reader = null;
            List <FtpEntryInfo> ret      = new List <FtpEntryInfo>();
            FtpWebRequest       req      = null;
            FtpWebResponse      resp     = null;

            if (!force_update)
            {
                force_update = !cache_retrieve(path_name, ret);
            }

            if (force_update)
            {
                try
                {
                    req        = create_request(path_name);
                    req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                    resp       = (FtpWebResponse)req.GetResponse();

                    s_reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
                    string line = string.Empty;
                    while ((line = s_reader.ReadLine()) != null)
                    {
                        FtpEntryInfo entry = FtpEntryInfo.FromDetailedListLine(line, path_name);
                        ret.Add(entry);
                    }
                    //update cache
                    cache_update(path_name, ret);
                }
                finally
                {
                    if (resp != null)
                    {
                        resp.Close();
                    }
                }
            }

            return(ret);
        }
Exemple #10
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();
                }
            }
        }
Exemple #11
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);
                }
            }
        }
Exemple #12
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();
                }
            }
        }
Exemple #13
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