Example #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);
                }
            }
        }
        void internal_watcher_Created(object sender, FileSystemEventArgs e)
        {
            int             new_index = -1;
            WIN32_FIND_DATA new_data  = new WIN32_FIND_DATA();

            //new file or folder
            if (WinAPiFSwrapper.GetFileInfo(e.FullPath, ref new_data))
            {
                internal_list.Add(new_data, null);
                new_index = internal_list.IndexOfKey(new_data);
                //we updates all items from new_index to end
                for (int i = new_index; i < internal_list.Count; i++)
                {
                    OnItemUpdate(i);
                }
                if ((new_data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    cache_directory_count++;
                }
                else
                {
                    cache_files_count++;
                    cache_size += new_data.FileSize;
                }
                OnAfterSummaryUpdate();
            }
        }
        void internal_watcher_Renamed(object sender, RenamedEventArgs e)
        {
            int change_index = -1;
            int new_index    = -1;

            int[]           affected_interval = new int[2];
            WIN32_FIND_DATA new_data          = new WIN32_FIND_DATA();

            if (WinAPiFSwrapper.GetFileInfo(e.Name, ref new_data))
            {
                change_index = internal_find_name(e.OldName);
                if (change_index == -1)
                {
                    return;
                }
                internal_list.RemoveAt(change_index);
                internal_list.Add(new_data, null);
                new_index = internal_list.IndexOfKey(new_data);
                if (new_index == change_index)
                {
                    OnItemUpdate(new_index);
                }
                else
                {
                    affected_interval[0] = Math.Min(change_index, new_index);
                    affected_interval[1] = Math.Max(change_index, new_index);
                    for (int i = affected_interval[0]; i <= affected_interval[1]; i++)
                    {
                        OnItemUpdate(i);
                    }
                }
            }
        }
Example #4
0
        protected override void internal_command_proc()
        {
            QueryPanelInfoEventArgs e = new QueryPanelInfoEventArgs();

            OnQueryCurrentPanel(e);

            if (!(e.ItemCollection is DirectoryList))
            {
                return;
            }

            try
            {
                DirectoryList   dl   = (DirectoryList)e.ItemCollection;
                VolumeSpaceInfo info = WinAPiFSwrapper.GetVolumeSpaceInfo(dl.DirectoryPath);

                VolumeSpaceInfoDialog dialog = new VolumeSpaceInfoDialog();
                dialog.textBoxTotalSize.Text      = IOhelper.SizeToString(info.TotalNumberOfBytes);
                dialog.textBoxTotalAvailable.Text = IOhelper.SizeToString(info.FreeBytesAvailable);
                dialog.Text = dl.DirectoryPath;
                dialog.ShowDialog();
            }
            catch (Exception ex)
            {
                Messages.ShowException(ex);
            }
        }
Example #5
0
        private void create_fs_directory(QueryPanelInfoEventArgs e_current, QueryPanelInfoEventArgs e_other)
        {
            var           dl_current = (DirectoryList)e_current.ItemCollection;
            DirectoryList dl_other   = null;

            if (e_other.ItemCollection is DirectoryList)
            {
                dl_other = (DirectoryList)e_other.ItemCollection;
            }

            //prepare dialog
            var dialog = new CreateDirectoryDialog();

            dialog.Text = CommandMenu.Text;
            dialog.labelParentDir.Text              = dl_current.DirectoryPath + Path.DirectorySeparatorChar;
            dialog.checkBoxUseTemplate.Checked      = false;
            dialog.textBoxTemplateDirectory.Enabled = false;
            if ((dl_other == null) || (e_other.FocusedIndex <= 0))
            {
                dialog.checkBoxUseTemplate.Checked      = false;
                dialog.textBoxTemplateDirectory.Enabled = false;
            }
            else
            {
                dialog.textBoxTemplateDirectory.Text = Path.Combine(dl_other.DirectoryPath, dl_other.GetItemDisplayNameLong(e_other.FocusedIndex));
            }

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

            if (dialog.textBoxDirectoryName.Text == string.Empty)
            {
                Messages.ShowMessage(Options.GetLiteral(Options.LANG_DIRECTORY_EXISTS));
                return;
            }

            var new_directory_name = Path.Combine(dl_current.DirectoryPath, dialog.textBoxDirectoryName.Text);
            var template_dir       = string.Empty;

            if (dialog.checkBoxUseTemplate.Checked)
            {
                template_dir = dialog.textBoxTemplateDirectory.Text;
            }

            try
            {
                WinAPiFSwrapper.CreateDirectoryTree(new_directory_name, template_dir);
            }
            catch (Exception ex)
            {
                Messages.ShowException
                    (ex,
                    string.Format
                        (Options.GetLiteral(Options.LANG_CANNOT_CREATE_DIRECTORY_0),
                        new_directory_name));
            }
        }
Example #6
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++;
                }
            }
        }
Example #7
0
        private void FillStandardPage(string file_name)
        {
            IntPtr file_handle  = IntPtr.Zero;
            string nt_file_name = IOhelper.GetUnicodePath(file_name);

            try
            {
                textBoxName.Text = string.Format
                                       ("{0}",
                                       Path.GetFileName(file_name));

                // find data --------------------------------------
                WIN32_FIND_DATA f_data = new WIN32_FIND_DATA();
                WinAPiFSwrapper.GetFileInfo(file_name, ref f_data);
                textBoxAttributes.Text = f_data.dwFileAttributes.ToString();
                textBoxAltname.Text    = f_data.cAlternateFileName;
                textBoxReaprseTag.Text = f_data.ReparseTag.ToString();
                //--------------------------------------------------

                fileSystemSecurityViewer1.FillContains(file_name);

                try
                {
                    fill_basic_info();
                    //fill_device();
                    fill_standard_info();
                    //fill_volume_attr();
                    //fill_volume_info();
                    fill_links(f_data.ReparseTag);
                    fill_dir_size();
                }
                catch (Exception ex)
                {
                    Messages.ShowException(ex);
                }

                try
                {
                    streamViewer1.FillContents(file_name);
                }
                catch (Exception)
                {
                }
            }
            catch (Exception ex)
            {
                Messages.ShowException(ex);
            }
            finally
            {
                if ((file_handle != IntPtr.Zero) && (file_handle.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE))
                {
                    WinApiFS.CloseHandle(file_handle);
                }
            }
        }
Example #8
0
        protected override void internal_refill()
        {
            internal_list.Clear();

            string[] drive_letters = WinAPiFSwrapper.GetLogicalDrives();
            foreach (string one_letter in drive_letters)
            {
                VolumeInfo vi = new VolumeInfo(one_letter, true);
                internal_list.Add(vi, null);
            }
        }
Example #9
0
        //VERY buggy
        //private void open_edit(string file_name)
        //{
        //    this.file_name = file_name;

        //    EditStream fs = new EditStream(file_name);
        //    Encoding enc = ((Encoding_DisplayInfo)toolStripComboBoxEncoding.SelectedItem).EncodingInfo.GetEncoding();
        //    if (text_provider != null)
        //    {
        //        text_provider.Dispose();
        //    }
        //    text_provider = TextProviderFactory.CreateProvider(fs, enc);
        //    TextViewControl.TextProvider = text_provider;
        //    Text = string.Format(Options.GetLiteral(Options.LANG_FILE_EDIT) + " [{0}]", file_name);

        //    //saveAsToolStripMenuItem.Enabled = true;
        //    //saveToolStripMenuItem.Enabled = true;
        //}

        private void open_view(string file_name, Encoding encoding)
        {
            this.file_name = file_name;

            var fs = WinAPiFSwrapper.CreateStreamEx
                         (file_name,
                         FileAccess.Read,
                         FileShare.Read,
                         FileMode.Open,
                         CreateFileOptions.RANDOM_ACCESS);

            text_provider = TextProviderFactory.CreateProvider(fs, encoding);
            TextViewControl.TextProvider = text_provider;
            Text = string.Format(Options.GetLiteral(Options.LANG_FILE_VIEW) + " [{0}]", file_name);
        }
Example #10
0
        void internal_watcher_Changed(object sender, FileSystemEventArgs e)
        {
            //find affected DATA item
            int change_index = -1;
            int new_index    = -1;

            int[]           affected_interval = new int[2];
            WIN32_FIND_DATA new_data          = new WIN32_FIND_DATA();
            WIN32_FIND_DATA old_data          = new WIN32_FIND_DATA();


            if (WinAPiFSwrapper.GetFileInfo(e.FullPath, ref new_data))
            {
                change_index = internal_find_name(e.Name);
                if (change_index == -1)
                {
                    //not found - does not occur, but...
                    return;
                }
                old_data = internal_list.Keys[change_index];
                internal_list.RemoveAt(change_index);
                internal_list.Add(new_data, null);
                new_index = internal_list.IndexOfKey(new_data);
                if (new_index == change_index)
                {
                    //updated item in same order - update only one item
                    OnItemUpdate(new_index);
                }
                else
                {
                    //updated item change its index
                    //in this case affected items is within new_index and change_index inclusive
                    affected_interval[0] = Math.Min(change_index, new_index);
                    affected_interval[1] = Math.Max(change_index, new_index);
                    for (int i = affected_interval[0]; i <= affected_interval[1]; i++)
                    {
                        OnItemUpdate(i);
                    }
                }
                //summary update
                cache_size = cache_size - old_data.FileSize + new_data.FileSize;
                OnAfterSummaryUpdate();
            }
        }
Example #11
0
        private void copy_selected_to_file()
        {
            if (listViewResult.SelectedItems.Count == 0)
            {
                return;
            }

            FileStreamInfo sel_info = ((InternalListViewItem)listViewResult.SelectedItems[0]).InternalData;

            string source = internal_file_name + sel_info.Name;

            saveFileDialog1.AddExtension       = false;
            saveFileDialog1.AutoUpgradeEnabled = true;
            saveFileDialog1.CheckFileExists    = false;
            saveFileDialog1.CheckFileExists    = false;
            saveFileDialog1.OverwritePrompt    = true;
            saveFileDialog1.ValidateNames      = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string destinsation = saveFileDialog1.FileName;

                this.UseWaitCursor = true;

                try
                {
                    WinAPiFSwrapper.CopyFile
                        (source,
                        destinsation,
                        true,
                        null,
                        IntPtr.Zero,
                        131072);
                }
                catch (Exception ex)
                {
                    UseWaitCursor = false;
                    Messages.ShowException(ex);
                }

                UseWaitCursor = false;
            }
        }
Example #12
0
        /// <summary>
        /// create new empty dir, copy security attr if needed
        /// </summary>
        /// <param name="source_dir">may be empty string</param>
        /// <param name="destination_dir"></param>
        private void create_empty_dir(string source_dir, string destination_dir)
        {
            var dest_dir_data = new WIN32_FIND_DATA();

            if (WinAPiFSwrapper.GetFileInfo(destination_dir, ref dest_dir_data))
            {
                //already exists
                return;
            }
            else
            {
                if ((!string.IsNullOrEmpty(source_dir)) && ((options & CopyEngineOptions.CopySecurityAttributes) == CopyEngineOptions.CopySecurityAttributes))
                {
                    Directory.CreateDirectory(destination_dir, Directory.GetAccessControl(source_dir));
                }
                else
                {
                    Directory.CreateDirectory(destination_dir);
                }
            }
        }
Example #13
0
        //private void fill_volume_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);

        //        FILE_FS_VOLUME_INFORMATION vol_info = ntApiFSwrapper.GetFileVolumeInfo(h_file);
        //        textBoxVolumeCreationTime.Text = string.Format
        //            (Options.GetLiteral(Options.LANG_DATE_TIME_LONG_FORMAT),
        //            vol_info.VolumeCreationDateTime.ToShortDateString(),
        //            vol_info.VolumeCreationDateTime.ToLongTimeString(),
        //            vol_info.VolumeCreationDateTime.Millisecond);
        //        textBoxVolumeLabel.Text = vol_info.VolumeLabel;
        //        textBoxVolumeSerialNumber.Text = vol_info.VolumeSerialNumber.ToString();
        //    }
        //    catch (Exception ex)
        //    {
        //        textBoxVolumeLabel.Text = ex.Message;
        //    }
        //    finally
        //    {
        //        if ((h_file.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE) && (h_file != IntPtr.Zero))
        //        {
        //            WinApiFS.CloseHandle(h_file);
        //        }
        //    }
        //}

        ////private void fill_volume_attr()
        ////{
        ////    IntPtr h_file = IntPtr.Zero;


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

        ////        FILE_FS_ATTRIBUTE_INFORMATION vol_attr = ntApiFSwrapper.GetFileVolumeAttributeInfo(h_file, IntPtr.Zero, 0);
        ////        textBoxVolumeFScaps.Text = vol_attr.VolumeAttributes.ToString();
        ////        textBoxVolumeFStype.Text = vol_attr.FileSystemName;
        ////        textBoxVolumeMaxNameLen.Text = vol_attr.MaximumComponentNameLength.ToString();

        ////        if ((vol_attr.VolumeAttributes & VolumeCaps.ReadOnly) != VolumeCaps.ReadOnly)
        ////        {
        ////            try
        ////            {
        ////                FILE_FS_FULLSIZE_INFORMATION vol_fs = ntApiFSwrapper.GetFileVolumeFullsizeInfo(h_file);
        ////                textBoxVolumeAvailableUnits.Text = string.Format
        ////                    ("{0:N0}",
        ////                    vol_fs.CallerAvailableAllocationUnits);
        ////                textBoxVolumeAvailableBytes.Text = string.Format
        ////                    ("{0:N0}",
        ////                    vol_fs.UserAvailableAllocationBytes);
        ////                textBoxVolumeBytesPerSector.Text = string.Format
        ////                    ("{0:N0}",
        ////                    vol_fs.BytesPerSector);
        ////                textBoxVolumeBytesPerSector.Text = string.Format
        ////                    ("{0:N0}",
        ////                    vol_fs.BytesPerSector);
        ////                textBoxVolumeBytesPerUnit.Text = string.Format
        ////                   ("{0:N0}",
        ////                   vol_fs.BytesPerSector * vol_fs.SectorsPerAllocationUnit);
        ////            }
        ////            catch (Exception ex)
        ////            {
        ////                textBoxVolumeAvailableUnits.Text = ex.Message;
        ////            }
        ////        }

        ////        if ((vol_attr.VolumeAttributes & VolumeCaps.Quotas) == VolumeCaps.Quotas)
        ////        {
        ////            try
        ////            {
        ////                FILE_FS_CONTROL_INFORMATION vol_ctrl = ntApiFSwrapper.GetFileVolumeControlInfo(h_file);
        ////                textBoxVolumeControlFlags.Text = vol_ctrl.FileSystemControlFlags.ToString();
        ////                textBoxVolumeQuotaLimit.Text = string.Format
        ////                    ("{0:N0}",
        ////                    vol_ctrl.DefaultQuotaLimit);
        ////                textBoxVolumeQuoteThreshold.Text = string.Format
        ////                    ("{0:N0}",
        ////                    vol_ctrl.DefaultQuotaThreshold);
        ////            }
        ////            catch (Exception ex)
        ////            {
        ////                textBoxVolumeControlFlags.Text = ex.Message;
        ////            }
        ////        }
        ////    }//try
        ////    catch (Exception ex)
        ////    {
        ////        textBoxVolumeFScaps.Text = ex.Message;
        ////    }
        ////    finally
        ////    {
        ////        if ((h_file.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE) && (h_file != IntPtr.Zero))
        ////        {
        ////            WinApiFS.CloseHandle(h_file);
        ////        }
        ////    }
        ////}

        //private void fill_device()
        //{
        //    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);

        //        FILE_FS_DEVICE_INFORMATION vol_dev = ntApiFSwrapper.GetFileVolumeDeviceInfo(h_file);
        //        textBoxVolumeDeviceCharateristics.Text = vol_dev.Characteristics.ToString();
        //        textBoxVolumeDeviceType.Text = vol_dev.DeviceType.ToString();
        //    }
        //    catch (Exception ex)
        //    {
        //        textBoxVolumeDeviceCharateristics.Text = ex.Message;
        //    }
        //    finally
        //    {
        //        if ((h_file.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE) && (h_file != IntPtr.Zero))
        //        {
        //            WinApiFS.CloseHandle(h_file);
        //        }
        //    }
        //}

        private void fill_links(IO_REPARSE_TAG tag)
        {
            try
            {
                switch (tag)
                {
                case IO_REPARSE_TAG.MOUNT_POINT:
                    REPARSE_DATA_BUFFER_MOUNTPOINT mp_buf = WinAPiFSwrapper.GetMountpointInfo(FileName);
                    textBoxSoftlinks.Text = mp_buf.SubstituteName;
                    break;

                case IO_REPARSE_TAG.SYMLINK:
                    REPARSE_DATA_BUFFER_SYMLINK sl_buf = WinAPiFSwrapper.GetSymboliclinkInfo(FileName);
                    textBoxSoftlinks.Text = sl_buf.SubstituteName;
                    break;
                }
            }
            catch (Exception ex)
            {
                textBoxSoftlinks.Text = ex.Message;
            }
        }
Example #14
0
        protected override void internal_refill()
        {
            //get list
            List <FileStreamInfo> stream_list = new List <FileStreamInfo>();

            try
            {
                stream_list = WinAPiFSwrapper.GetFileStreamInfo_fromBackup(file_name_full, read_sec_streams);
            }
            catch (Exception ex)
            {
                Messages.ShowException(ex);
            }
            internal_list.Clear();
            total_size = 0UL;
            internal_list.Add(internal_parent_info, null);
            foreach (FileStreamInfo info in stream_list)
            {
                internal_list.Add(info, null);
                total_size += info.Size;
            }
        }
Example #15
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);
                }
            }
        }
Example #16
0
        private long calc_source_size()
        {
            ulong ret     = 0;
            var   f_count = 0;
            var   d_count = 0;
            var   data    = new WIN32_FIND_DATA();

            for (var i = 0; i < initial_source.Length; i++)
            {
                if (WinAPiFSwrapper.GetFileInfo(initial_source[i], ref data))
                {
                    if ((data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        ret += WinAPiFSwrapper.GetDirectoryStat
                                   (initial_source[i], true, false, true, ref f_count, ref d_count, null);
                    }
                    else
                    {
                        ret += data.FileSize;
                    }
                }
            }
            return((long)ret);
        }
Example #17
0
        private void fill_listview(string file_name)
        {
            List <FileStreamInfo> stream_list = new List <FileStreamInfo>();

            try
            {
                stream_list = WinAPiFSwrapper.GetFileStreamInfo_fromBackup(file_name, false);
            }
            catch (Exception ex)
            {
                Messages.ShowException(ex);
            }

            foreach (FileStreamInfo data in stream_list)
            {
                InternalListViewItem new_item = new InternalListViewItem(data);
                listViewResult.Items.Add(new_item);
            }

            if (listViewResult.Items.Count > 0)
            {
                listViewResult.Items[0].Selected = true;
            }
        }
Example #18
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.ItemCollection is DirectoryList))
            {
                return;
            }

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

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

            var dl_current = (DirectoryList)e_current.ItemCollection;
            var dl_other   = (DirectoryList)e_other.ItemCollection;
            var group_mode = e_current.SelectedIndices.Length > 1;
            var link_type  = Options.LinkType;

            var sels = new List <FileInfoEx>();

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

            //show dialog
            var dialog = new CreateLinkDialog();

            dialog.Text                 = Options.GetLiteral(Options.LANG_LINK_CREATE);
            dialog.LinkType             = link_type;
            dialog.textBoxLinkname.Text = string.Empty;
            if (group_mode)
            {
                dialog.textBoxLinkname.ReadOnly = true;
                dialog.textBoxLinktarget.Text   = string.Format
                                                      ("{0} " + Options.GetLiteral(Options.LANG_ENTRIES),
                                                      sels.Count);
            }
            else
            {
                dialog.textBoxLinktarget.Text = sels[0].FileName;
            }

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

            link_type        = dialog.LinkType;
            Options.LinkType = link_type;

            foreach (var entry in sels)
            {
                var link_handle = IntPtr.Zero;
                //combine link name
                var link_name = string.Empty;
                if (dialog.textBoxLinkname.Text == string.Empty)
                {
                    //use target file name
                    link_name = Path.Combine(dl_other.DirectoryPath, entry.FileName);
                }
                else
                {
                    link_name = Path.Combine(dl_other.DirectoryPath, dialog.textBoxLinkname.Text);
                }

                try
                {
                    if ((entry.Directory) && (link_type == NTFSlinkType.Hard))
                    {
                        Messages.ShowMessage
                            (string.Format
                                (Options.GetLiteral(Options.LANG_CANNOT_CREATE_HARD_LINK_DIR_0),
                                entry.FullName));
                        continue;
                    }

                    if (link_type == NTFSlinkType.Hard)
                    {
                        var res = WinApiFS.CreateHardLink
                                      (link_name,
                                      entry.FullName,
                                      IntPtr.Zero);
                        if (res == 0)
                        {
                            Messages.ShowException
                                (new Win32Exception(Marshal.GetLastWin32Error()),
                                string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_CREATE_LINK_0_ARROW_1),
                                    entry.FullName,
                                    link_name));
                        }
                        else
                        {
                            OnItemProcessDone(new ItemEventArs(entry.FileName));
                        }
                        continue;
                    }

                    if (link_type == NTFSlinkType.Symbolic)
                    {
                        try
                        {
                            WinAPiFSwrapper.CreateSymbolicLink(link_name, entry.FullName, entry.Directory);
                            OnItemProcessDone(new ItemEventArs(entry.FileName));
                        }
                        catch (Exception)
                        {
                            Messages.ShowException
                                (new Win32Exception(Marshal.GetLastWin32Error()),
                                string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_CREATE_LINK_0_ARROW_1),
                                    entry.FullName,
                                    link_name));
                        }
                        continue;
                    }

                    //link type is junction

                    var link_data  = new WIN32_FIND_DATA();
                    var link_exist = WinAPiFSwrapper.GetFileInfo(link_name, ref link_data);

                    if (link_exist)
                    {
                        Messages.ShowMessage(string.Format(Options.GetLiteral(Options.LANG_DESTINATION_0_EXIST_OVERWRITING_PROHIBITED), link_name));
                        continue;
                    }

                    //jumction target must be directory, create directory
                    Directory.CreateDirectory(link_name);
                    //and retrieve handle
                    link_handle = WinApiFS.CreateFile_intptr
                                      (link_name,
                                      Win32FileAccess.WRITE_ATTRIBUTES | Win32FileAccess.WRITE_EA,
                                      FileShare.ReadWrite,
                                      IntPtr.Zero,
                                      FileMode.Open,
                                      CreateFileOptions.OPEN_REPARSE_POINT | CreateFileOptions.BACKUP_SEMANTICS,
                                      IntPtr.Zero);
                    if (link_handle.ToInt64() == WinApiFS.INVALID_HANDLE_VALUE)
                    {
                        var win_ex = new Win32Exception(Marshal.GetLastWin32Error());
                        Messages.ShowException
                            (win_ex,
                            string.Format
                                (Options.GetLiteral(Options.LANG_CANNOT_CREATE_LINK_0_ARROW_1),
                                link_name,
                                entry.FullName));
                        if (Directory.Exists(link_name))
                        {
                            Directory.Delete(link_name);
                        }
                        continue;
                    }
                    //now handle to link open
                    WinAPiFSwrapper.SetMountpoint(link_handle, entry.FullName, entry.FullName);

                    OnItemProcessDone(new ItemEventArs(entry.FileName));
                }//end try
                catch (Exception ex)
                {
                    Messages.ShowException
                        (ex,
                        string.Format
                            (Options.GetLiteral(Options.LANG_CANNOT_CREATE_LINK_0_ARROW_1),
                            entry.FullName,
                            link_name));
                }
                finally
                {
                    //close handle
                    if ((link_handle.ToInt64() != WinApiFS.INVALID_HANDLE_VALUE) && (link_handle != IntPtr.Zero))
                    {
                        WinApiFS.CloseHandle(link_handle);
                    }
                }
            }//end foreach
        }
Example #19
0
        protected override void internal_command_proc()
        {
            QueryPanelInfoEventArgs e = new QueryPanelInfoEventArgs();

            OnQueryCurrentPanel(e);

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

            bool group_mode = false;

            int[] sel_indices = e.SelectedIndices;
            //we not need cache selection
            //becouse e.ItemsCollection must not change (DirectoryList not sort on attributes)
            group_mode = (sel_indices.Length > 1);
            DirectoryList   dl     = (DirectoryList)e.ItemCollection;
            WIN32_FIND_DATA f_data = new WIN32_FIND_DATA();

            string target_file = string.Empty;

            if (!group_mode)
            {
                if (sel_indices.Length == 0)
                {
                    target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(e.FocusedIndex));
                }
                else
                {
                    target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(sel_indices[0]));
                }
                if (target_file.EndsWith(".."))
                {
                    target_file = dl.DirectoryPath;
                }
                if (!WinAPiFSwrapper.GetFileInfo(target_file, ref f_data))
                {
                    return;
                }
            }



            FileAttributesEditDialog dialog = new FileAttributesEditDialog();

            if (!group_mode)
            {
                dialog.buttonClear.Enabled = false;
                set_attributes_to_dialog(dialog, f_data.dwFileAttributes);
                dialog.Text = String.Format
                                  (Options.GetLiteral(Options.LANG_FILE_ATTRIBUTES) + " [{0}]", target_file);
            }
            else
            {
                dialog.buttonClear.Enabled = true;
                dialog.buttonOK.Text       = Options.GetLiteral(Options.LANG_ADD);
                dialog.Text = string.Format
                                  (Options.GetLiteral(Options.LANG_FILE_ATTRIBUTES) + " [{0} " + Options.GetLiteral(Options.LANG_ENTRIES) + "]", sel_indices.Length);
            }
            set_readonly_attributes(dialog);

            DialogResult d_res = dialog.ShowDialog();

            switch (d_res)
            {
            case DialogResult.OK:
                //group mode = add attributes
                //single mode = set attributes
                if (!group_mode)
                {
                    try
                    {
                        set_attributes_file(target_file, get_attributes_from_dialog(dialog));
                    }
                    catch (Exception ex)
                    {
                        Messages.ShowException(ex);
                    }
                }
                else
                {
                    //group mode
                    FileAttributes add_fa = get_attributes_from_dialog(dialog);
                    foreach (int one_index in sel_indices)
                    {
                        target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(one_index));
                        try
                        {
                            add_attributes_file(target_file, add_fa);
                            OnItemProcessDone(new ItemEventArs(one_index));
                        }
                        catch (Exception ex)
                        {
                            Messages.ShowException(ex);
                        }
                    }
                }
                break;

            case DialogResult.Yes:
                //group mode only = clear attributes
                FileAttributes clear_fa = get_attributes_from_dialog(dialog);
                foreach (int one_index in sel_indices)
                {
                    target_file = Path.Combine(dl.DirectoryPath, dl.GetItemDisplayNameLong(one_index));
                    try
                    {
                        clear_attributes_file(target_file, clear_fa);
                        OnItemProcessDone(new ItemEventArs(one_index));
                    }
                    catch (Exception ex)
                    {
                        Messages.ShowException(ex);
                    }
                }
                break;
            }
        }
Example #20
0
        /// <summary>
        /// show error window if needed and returns do second take or no, if no caller must continue to next source
        /// </summary>
        /// <param name="source"></param>
        /// <param name="src_data"></param>
        /// <param name="destination"></param>
        /// <param name="win_error"></param>
        /// <returns></returns>
        private bool process_CopyFileEx_errors(FileInfoEx source_info, string destination, int win_error, ref CopyFileExOptions temp_options)
        {
            var dst_data = new WIN32_FIND_DATA();

            switch (win_error)
            {
            case WinApiFS.ERROR_PATH_NOT_FOUND:     //ERROR_PATH_NOT_FOUND
                //there is no target directory, create it
                var src_dir_3 = Path.GetDirectoryName(source_info.FullName);
                var dst_dir_3 = Path.GetDirectoryName(destination);
                try
                {
                    create_empty_dir(src_dir_3, dst_dir_3);
                }
                catch (Exception ex_3)
                {
                    if (process_error
                            (string.Format
                                (Options.GetLiteral(Options.LANG_CANNOT_CREATE_DIRECTORY_0),
                                dst_dir_3),
                            ex_3))
                    {
                        return(false);
                    }
                    else
                    {
                        stop();
                        return(false);
                    }
                }
                return(true);

            case WinApiFS.ERROR_FILE_EXISTS:     //ERROR_FILE_EXISTS
                if ((options & CopyEngineOptions.RewriteIfSourceNewer) == CopyEngineOptions.RewriteIfSourceNewer)
                {
                    //overwrite enable if source newer
                    WinAPiFSwrapper.GetFileInfo(destination, ref dst_data);
                    if (source_info.WriteFileTime > dst_data.ftLastWriteTime)
                    {
                        temp_options = ~((~temp_options) | CopyFileExOptions.FAIL_IF_EXISTS);
                        return(clear_dest_attributes(destination, dst_data));
                    }
                    else
                    {
                        if (process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_DESTINATION_0_NEWER_THEN_SOURCE_1_OVERWRITING_PROHIBITED),
                                    destination,
                                    source_info.FullName),
                                null))
                        {
                            return(false);
                        }
                        else
                        {
                            stop();
                            return(false);
                        }
                    }
                }

                //destination exists and overwrite prohibited
                if (process_error
                        (string.Format
                            (Options.GetLiteral(Options.LANG_DESTINATION_0_EXIST_OVERWRITING_PROHIBITED),
                            destination),
                        null))
                {
                    return(false);
                }
                else
                {
                    stop();
                    return(false);
                }

            case 5:     //ERROR_ACCESS_DENIED
                //destination file have readonly or hidden attribute
                //clear attributes
                WinAPiFSwrapper.GetFileInfo(destination, ref dst_data);
                return(clear_dest_attributes(destination, dst_data));
            } //end switch

            //default:
            var ex = new Win32Exception(win_error);

            if (process_error
                    (string.Format
                        (Options.GetLiteral(Options.LANG_CANNOT_COPY_0_ARROW_1),
                        source_info.FullName,
                        destination),
                    ex))
            {
                return(false);
            }
            else
            {
                stop();
                return(false);
            }
        }
Example #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
Example #22
0
        private bool clear_dest_attributes(string destination_file, WIN32_FIND_DATA dest_data)
        {
            var fa_existing = dest_data.dwFileAttributes;
            var fa_needed   = dest_data.dwFileAttributes;

            if ((fa_existing & FileAttributes.Hidden) == FileAttributes.Hidden)
            {
                if ((options & CopyEngineOptions.AllowClearAttributes) == CopyEngineOptions.AllowClearAttributes)
                {
                    fa_needed = ~((~fa_existing) | FileAttributes.Hidden);
                }
                else
                {
                    if (!process_error
                            (string.Format
                                (Options.GetLiteral(Options.LANG_DESTINATION_0_EXISTS_AND_HIDDEN_CLEARING_ATTRS_PROHIBITED),
                                destination_file),
                            null))
                    {
                        stop();
                    }
                    return(false);
                }
            }
            if ((fa_existing & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                if ((options & CopyEngineOptions.AllowClearAttributes) == CopyEngineOptions.AllowClearAttributes)
                {
                    fa_needed = ~((~fa_existing) | FileAttributes.ReadOnly);
                }
                else
                {
                    if (!process_error
                            (string.Format
                                (Options.GetLiteral(Options.LANG_DESTINATION_0_EXISTS_AND_READONLY_CLEARING_ATTRS_PROHIBITED),
                                destination_file),
                            null))
                    {
                        stop();
                    }
                    return(false);
                }
            }

            if (fa_existing != fa_needed)
            {
                try
                {
                    WinAPiFSwrapper.SetFileAttributes(destination_file, fa_needed);
                }
                catch (Exception ex)
                {
                    if (!process_error
                            (string.Format
                                (Options.GetLiteral(Options.LANG_CANNOT_CLEAR_HIDDEN_READONLY_ATTR_DESTINATION_0),
                                destination_file),
                            ex))
                    {
                        stop();
                    }
                    return(false);
                }
            }

            return(true);
        }
Example #23
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();
                }
            }
        }
Example #24
0
        private void copy_dir(FileInfoEx source_info, string destination_dir)
        {
            if ((source_info.LinkInformation != null) && (!destination_remote) && ((options & CopyEngineOptions.CopySymlinkAsSymlink) == CopyEngineOptions.CopySymlinkAsSymlink))
            {
                try
                {
                    //copy link as link and returns
                    Directory.CreateDirectory
                        (destination_dir);
                    switch (source_info.ReparseTag)
                    {
                    case IO_REPARSE_TAG.MOUNT_POINT:
                        WinAPiFSwrapper.SetMountpoint(destination_dir, source_info.LinkInformation.SubstituteName, source_info.LinkInformation.PrintName);
                        break;

                    case IO_REPARSE_TAG.SYMLINK:
                        //need test
                        //link_target and unparsed prefix! need testing behavior of CreateSymbolicLink
                        WinAPiFSwrapper.CreateSymbolicLink(destination_dir, source_info.LinkInformation.SubstituteName, true);
                        //WinAPiFSwrapper.SetSymbolicLink(destination_dir, source_info.LinkInformation.SubstituteName, source_info.LinkInformation.PrintName, false);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    if (!process_error
                            (string.Format
                                (Options.GetLiteral(Options.LANG_CANNOT_CREATE_SOFT_LINK_0),
                                destination_dir),
                            ex))
                    {
                        stop();
                    }
                }
                OnCopyItemDone(new ItemEventArs(source_info.FileName));
                return;
            }

            if ((options & CopyEngineOptions.CopyFilesRecursive) == CopyEngineOptions.CopyFilesRecursive)
            {
                var dir_enum = new FileInfoExEnumerable
                                   (Path.Combine(source_info.FullName, "*"),
                                   true,
                                   true,
                                   true,
                                   true);
                foreach (var info in dir_enum)
                {
                    if (abort_job)
                    {
                        return;
                    }
                    if (info.Directory)
                    {
                        copy_dir(info, Path.Combine(destination_dir, info.FileName));
                    }
                    else
                    {
                        copy_one_file(info, Path.Combine(destination_dir, info.FileName));
                    }
                }
            }

            if ((options & CopyEngineOptions.CreateEmptyDirectories) == CopyEngineOptions.CreateEmptyDirectories)
            {
                create_empty_dir(source_info.FullName, destination_dir);
                OnCopyItemDone(new ItemEventArs(source_info.FileName));
            }
        }
Example #25
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 dest_data = new WIN32_FIND_DATA();

            //bool dest_exist = WinAPiFSwrapper.GetFileInfo(initial_destination, ref dest_data);

            foreach (var info in initial_source)
            {
                if (AbortSafe)
                {
                    break;
                }

                var dest_exist = WinAPiFSwrapper.GetFileInfo(initial_destination, ref dest_data);
                if (info.Directory)
                {
                    if (dest_exist)
                    {
                        if ((dest_data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
                        {
                            //download source directory into directory=initial_destination/info.ENtryName
                            download_directory(info, Path.Combine(initial_destination, info.EntryName));
                        }
                        else
                        {
                            //show error
                            AbortSafe = !process_error
                                            (string.Format
                                                (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_SOURCE_0_DIRECTORY_DESTINATION_1_FILE),
                                                info.EntryName,
                                                initial_destination),
                                            null);
                            continue;
                        }
                    }
                    else
                    {
                        //dest not exist
                        if (initial_source.Length == 1)
                        {
                            //that is download one directory to directory with new name
                            download_directory(info, initial_destination);
                        }
                        else
                        {
                            //download driectory INTO initial_destination
                            download_directory(info, Path.Combine(initial_destination, info.EntryName));
                        }
                    }
                }
                else
                {
                    //info is file
                    if (dest_exist)
                    {
                        if ((dest_data.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
                        {
                            //dest existing dir
                            //download one file into initial destination
                            download_one_file(info, Path.Combine(initial_destination, info.EntryName));
                        }
                        else
                        {
                            //dest is file
                            if (initial_source.Length != 1)
                            {
                                //cannot download many entries into one file
                                AbortSafe = !process_error
                                                (Options.GetLiteral(Options.LANG_WRONG_DESTINATION_CANNOT_DOWNLOAD_MANY_ENTRIES_INTO_ONE_FILE),
                                                null);
                                continue;
                            }
                            else
                            {
                                //overwrite existing
                                download_one_file(info, initial_destination);
                            }
                        }
                    }
                    else
                    {
                        //dest not exist and info is file
                        if (initial_source.Length == 1)
                        {
                            //assume that dest is new file name
                            download_one_file(info, initial_destination);
                        }
                        else
                        {
                            //assume that dest is new dir
                            download_one_file(info, Path.Combine(initial_destination, info.EntryName));
                        }
                    }
                }

                if (DownloadItemDone != null)
                {
                    DownloadItemDone(this, new ItemEventArs(info.EntryName));
                }
            }

            count_bytes_current_transferred = 0;
            //time to notify about job completed
            update_args.EnableTotalProgress = opts.ShowTotalProgress;
            update_args.FilesCopied         = count_files_processed;
            update_args.KBytesPerSec        = (ulong)calc_speed();
            update_args.Reason           = UpdateProgressReason.JobDone;
            update_args.TotalSize        = (ulong)total_source_size;
            update_args.TotalTransferred = (ulong)count_bytes_total_transferred;
            update_progress_safe(update_args);

            if (Done != null)
            {
                Done(this, new EventArgs());
            }
        }
Example #26
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();
                }
            }
        }