Exemple #1
0
        private void fill_basic_info()
        {
            IntPtr h_file = IntPtr.Zero;

            try
            {
                h_file = WinAPiFSwrapper.CreateFileHandle
                             (FileName,
                             Win32FileAccess.READ_ATTRIBUTES | Win32FileAccess.READ_EA,
                             FileShare.ReadWrite | FileShare.Delete,
                             FileMode.Open,
                             is_directory ? CreateFileOptions.BACKUP_SEMANTICS : CreateFileOptions.None);

                NT_FILE_BASIC_INFO b_info = ntApiFSwrapper.GetFileInfo_basic(h_file);


                textBoxAccess.Text = string.Format
                                         (Options.GetLiteral(Options.LANG_DATE_TIME_LONG_FORMAT),
                                         b_info.LastAccessTime.ToShortDateString(),
                                         b_info.LastAccessTime.ToLongTimeString());

                textBoxAccessFT.Text = string.Format
                                           ("{0:N0}", b_info.LastAccessFileTime);

                textBoxChange.Text = string.Format
                                         (Options.GetLiteral(Options.LANG_DATE_TIME_LONG_FORMAT),
                                         b_info.ChangeTime.ToShortDateString(),
                                         b_info.ChangeTime.ToLongTimeString());

                textBoxChangeFT.Text = string.Format
                                           ("{0:N0}", b_info.ChangeFileTime);

                textBoxCreation.Text = string.Format
                                           (Options.GetLiteral(Options.LANG_DATE_TIME_LONG_FORMAT),
                                           b_info.CreationTime.ToShortDateString(),
                                           b_info.CreationTime.ToLongTimeString());

                textBoxCreationFT.Text = string.Format
                                             ("{0:N0}", b_info.CreationFileTime);

                textBoxWrite.Text = string.Format
                                        (Options.GetLiteral(Options.LANG_DATE_TIME_LONG_FORMAT),
                                        b_info.LastWriteTime.ToShortDateString(),
                                        b_info.LastWriteTime.ToLongTimeString());

                textBoxWriteFT.Text = string.Format
                                          ("{0:N0}", b_info.LastWriteFileTime);
            }
            catch (Exception ex)
            {
                textBoxAccess.Text = ex.Message;
            }
            finally
            {
                if ((h_file.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE) && (h_file != IntPtr.Zero))
                {
                    WinApiFS.CloseHandle(h_file);
                }
            }
        }
Exemple #2
0
        private void calc_size_count(FileInfoEx dir_info, ref long size, ref long allocation, ref long file_count, ref long dir_count)
        {
            FileInfoExEnumerable dir_enum = new FileInfoExEnumerable
                                                (Path.Combine(dir_info.FullName, "*"),
                                                true,
                                                true,
                                                true,
                                                true);

            foreach (FileInfoEx info in dir_enum)
            {
                IntPtr h_file = IntPtr.Zero;
                try
                {
                    h_file = WinAPiFSwrapper.CreateFileHandle
                                 (info.FullName,
                                 Win32FileAccess.READ_ATTRIBUTES | Win32FileAccess.READ_EA,
                                 FileShare.ReadWrite | FileShare.Delete,
                                 FileMode.Open,
                                 info.Directory ? CreateFileOptions.BACKUP_SEMANTICS : CreateFileOptions.None);

                    NT_FILE_STANDARD_INFORMATION s_info = ntApiFSwrapper.GetFileInfo_standard(h_file);
                    size       += s_info.EndOfFile;
                    allocation += s_info.AllocationSize;
                    //if (s_info.AllocationSize < s_info.EndOfFile)
                    //{
                    //    /*DEBUG*/
                    //    long delta = s_info.EndOfFile - s_info.AllocationSize;
                    //}
                }
                catch (Exception)
                {
                    //Messages.ShowException
                    //    (ex,
                    //    string.Format
                    //    (Options.GetLiteral(Options.LANG_CANNOT_READ_ATTRIBUTES_0),
                    //    info.FullName));
                    break;
                }
                finally
                {
                    if ((h_file.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE) && (h_file != IntPtr.Zero))
                    {
                        WinApiFS.CloseHandle(h_file);
                    }
                }
                if (info.Directory)
                {
                    calc_size_count(info, ref size, ref allocation, ref file_count, ref dir_count);
                    dir_count++;
                }
                else
                {
                    file_count++;
                }
            }
        }
Exemple #3
0
        private void fill_standard_info()
        {
            IntPtr h_file = IntPtr.Zero;

            try
            {
                h_file = WinAPiFSwrapper.CreateFileHandle
                             (FileName,
                             Win32FileAccess.READ_ATTRIBUTES | Win32FileAccess.READ_EA,
                             FileShare.ReadWrite | FileShare.Delete,
                             FileMode.Open,
                             is_directory ? CreateFileOptions.BACKUP_SEMANTICS : CreateFileOptions.None);

                NT_FILE_STANDARD_INFORMATION s_info = ntApiFSwrapper.GetFileInfo_standard(h_file);



                textBoxSize.Text = string.Format
                                       (Options.GetLiteral(Options.LANG_0_BYTES),
                                       s_info.EndOfFile);
                textBoxAllocationSize.Text = string.Format
                                                 (Options.GetLiteral(Options.LANG_0_BYTES),
                                                 s_info.AllocationSize);
                textBoxHardLinks.Text = s_info.NumberOfLinks.ToString();
            }
            catch (Exception ex)
            {
                textBoxSize.Text = ex.Message;
            }
            finally
            {
                if ((h_file.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE) && (h_file != IntPtr.Zero))
                {
                    WinApiFS.CloseHandle(h_file);
                }
            }
        }
Exemple #4
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 #5
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();
                }
            }
        }