Exemplo n.º 1
0
 public InternalListViewItem(NT_FILE_STREAM_INFORMATION data)
 {
     InternalData = data;
     Text         = data.StreamName;
     SubItems.Add(string.Format("{0:N0}", data.StreamSize));
     SubItems.Add(string.Format("{0:N0}", data.StreamAllocationSize));
 }
Exemplo n.º 2
0
        public void FillContents(string file_name)
        {
            //directory or file?

            FileName = file_name;
            var            file_hanle = IntPtr.Zero;
            var            win_err    = 0;
            Win32Exception win_ex     = null;

            try
            {
                if (IOhelper.IsDirectory(file_name))
                {
                    file_hanle = WinApiFS.CreateFile_intptr
                                     (file_name,
                                     Win32FileAccess.GENERIC_READ,
                                     FileShare.ReadWrite,
                                     IntPtr.Zero,
                                     FileMode.Open,
                                     CreateFileOptions.BACKUP_SEMANTICS,
                                     IntPtr.Zero);
                }
                else
                {
                    file_hanle = WinApiFS.CreateFile_intptr
                                     (file_name,
                                     Win32FileAccess.GENERIC_READ,
                                     FileShare.ReadWrite,
                                     IntPtr.Zero,
                                     FileMode.Open,
                                     CreateFileOptions.None,
                                     IntPtr.Zero);
                }

                if (file_hanle.ToInt64() == WinApiFS.INVALID_HANDLE_VALUE)
                {
                    win_err = Marshal.GetLastWin32Error();
                    win_ex  = new Win32Exception(win_err);
                    return;
                }

                FillContents(file_hanle);
            }
            finally
            {
                if ((file_hanle != IntPtr.Zero) && (file_hanle.ToInt64() != WinApiFS.INVALID_HANDLE_VALUE))
                {
                    WinApiFS.CloseHandle(file_hanle);
                }

                if (win_ex != null)
                {
                    var fake_info = new NT_FILE_STREAM_INFORMATION();
                    fake_info.StreamName = win_ex.Message;
                    listViewStreams.Items.Add(new InternalListViewItem(fake_info));
                }
            }
        }
Exemplo n.º 3
0
        private void internal_delete_selected()
        {
            if (listViewStreams.SelectedItems.Count == 0)
            {
                return;
            }

            if (Messages.ShowQuestionYesNo
                    (Options.GetLiteral(Options.LANG_DELETE_PROMPT),
                    Options.GetLiteral(Options.LANG_DELETE)) != DialogResult.Yes)
            {
                return;
            }

            var stream_info = new NT_FILE_STREAM_INFORMATION();

            foreach (InternalListViewItem item in listViewStreams.SelectedItems)
            {
                try
                {
                    stream_info = item.InternalData;
                    if (stream_info.StreamName.StartsWith("::$"))
                    {
                        Messages.ShowMessage(Options.GetLiteral(Options.LANG_CANNOT_DELETE_DEFAULT_DATA_STREAM));
                    }
                    else
                    {
                        var del_name = string.Format("{0}{1}", FileName, stream_info.StreamName);
                        var res      = WinApiFS.DeleteFile(del_name);
                        if (res == 0)
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Messages.ShowException
                        (ex,
                        string.Format
                            (Options.GetLiteral(Options.LANG_CANNOT_DELETE_DEFAULT_DATA_STREAM),
                            stream_info.StreamName));
                }
            }

            //update list
            FillContents(FileName);
        }
Exemplo n.º 4
0
        private void internal_copy_selected_to()
        {
            if (listViewStreams.SelectedItems.Count == 0)
            {
                return;
            }

            var fs_info     = new NT_FILE_STREAM_INFORMATION();
            var destination = string.Empty;

            if (listViewStreams.SelectedItems.Count == 1)
            {
                //destination is file name

                saveFileDialog1.InitialDirectory = Path.GetDirectoryName(FileName);
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    destination = saveFileDialog1.FileName;
                    fs_info     = ((InternalListViewItem)listViewStreams.SelectedItems[0]).InternalData;
                    try
                    {
                        this.UseWaitCursor = true;
                        internal_copy_to_file(fs_info, destination, false);
                        this.UseWaitCursor = false;
                    }
                    catch (Exception ex)
                    {
                        this.UseWaitCursor = false;
                        Messages.ShowException(ex);
                    }
                }

                return;
            }
            //else

            folderBrowserDialog1.SelectedPath = Path.GetDirectoryName(FileName);
            if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            destination = folderBrowserDialog1.SelectedPath;

            foreach (InternalListViewItem item in listViewStreams.SelectedItems)
            {
                UseWaitCursor = true;
                fs_info       = item.InternalData;
                var destination_name = string.Empty;

                try
                {
                    destination_name = fs_info.StreamName;
                    destination_name = destination_name.Replace(':', '_');
                    destination_name = Path.Combine(destination, destination_name);
                    internal_copy_to_file(fs_info, destination_name, true);
                    UseWaitCursor = false;
                }
                catch (Exception ex)
                {
                    UseWaitCursor = false;
                    Messages.ShowException
                        (ex,
                        string.Format
                            (Options.GetLiteral(Options.LANG_CANNOT_COPY_0_ARROW_1),
                            fs_info.StreamName,
                            destination_name));
                }
            }
        }
Exemplo n.º 5
0
        private void internal_copy_to_file(NT_FILE_STREAM_INFORMATION fs_stream, string destination_file, bool fail_if_exists)
        {
            if (copy_buffer.Length == 0)
            {
                copy_buffer = new byte[1024 * 64];
            }


            var source_name = string.Format("{0}{1}", FileName, fs_stream.StreamName);


            var source_handle      = IntPtr.Zero;
            var destination_handle = IntPtr.Zero;

            source_handle = WinApiFS.CreateFile_intptr
                                (source_name,
                                Win32FileAccess.GENERIC_READ,
                                FileShare.Read,
                                IntPtr.Zero,
                                FileMode.Open,
                                CreateFileOptions.None,
                                IntPtr.Zero);
            if (source_handle.ToInt64() == WinApiFS.INVALID_HANDLE_VALUE)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            destination_handle = WinApiFS.CreateFile_intptr
                                     (destination_file,
                                     Win32FileAccess.GENERIC_WRITE,
                                     FileShare.Read,
                                     IntPtr.Zero,
                                     fail_if_exists ? FileMode.CreateNew : FileMode.Create,
                                     CreateFileOptions.None,
                                     IntPtr.Zero);
            if (destination_handle.ToInt64() == WinApiFS.INVALID_HANDLE_VALUE)
            {
                WinApiFS.CloseHandle(source_handle);
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            FileStream source_stream      = null;
            FileStream destination_stream = null;

            try
            {
                source_stream      = new FileStream(source_handle, FileAccess.Read, true);
                destination_stream = new FileStream(destination_handle, FileAccess.Write, true);
                var bytes_readed = 0;

                do
                {
                    bytes_readed = source_stream.Read(copy_buffer, 0, copy_buffer.Length);
                    destination_stream.Write(copy_buffer, 0, bytes_readed);
                } while (bytes_readed != 0);
            }
            finally
            {
                destination_stream.Close();
                source_stream.Close();
            }
        }