Exemple #1
0
        public static FILE_FS_VOLUME_INFORMATION GetFileVolumeInfo(string file_name)
        {
            IntPtr file_handle = IntPtr.Zero;

            try
            {
                file_handle = WinApiFS.CreateFile_intptr
                                  (file_name,
                                  Win32FileAccess.GENERIC_READ,
                                  FileShare.ReadWrite,
                                  IntPtr.Zero,
                                  FileMode.Open,
                                  CreateFileOptions.None,
                                  IntPtr.Zero);
                if (file_handle.ToInt32() == WinApiFS.INVALID_HANDLE_VALUE)
                {
                    int            win_err = Marshal.GetLastWin32Error();
                    Win32Exception win_ex  = new Win32Exception(win_err);
                    throw win_ex;
                }
                return(GetFileVolumeInfo(file_handle));
            }
            finally
            {
                if ((file_handle != IntPtr.Zero) && (file_handle.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE))
                {
                    WinApiFS.CloseHandle(file_handle);
                }
            }
        }
Exemple #2
0
        public static NT_FILE_BASIC_INFO GetFileInfo_basic(string file_name)
        {
            IntPtr file_handle = IntPtr.Zero;

            try
            {
                //open file
                file_handle = WinApiFS.CreateFile_intptr
                                  (file_name,
                                  Win32FileAccess.GENERIC_READ,
                                  FileShare.ReadWrite,
                                  IntPtr.Zero,
                                  FileMode.Open,
                                  CreateFileOptions.BACKUP_SEMANTICS,
                                  IntPtr.Zero);

                if (file_handle.ToInt32() == WinApiFS.INVALID_HANDLE_VALUE)
                {
                    int       win_err = Marshal.GetLastWin32Error();
                    Exception ex      = new Win32Exception(win_err);
                    throw ex;
                }

                NT_FILE_BASIC_INFO ret = GetFileInfo_basic(file_handle);

                return(ret);
            }
            finally
            {
                if ((file_handle != IntPtr.Zero) && (file_handle.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE))
                {
                    WinApiFS.CloseHandle(file_handle);
                }
            }
        }
Exemple #3
0
        public static NT_FILE_STREAM_INFORMATION[] GetFileInfo_stream(string file_name)
        {
            IntPtr file_handle = IntPtr.Zero;

            try
            {
                file_handle = WinApiFS.CreateFile_intptr
                                  (file_name,
                                  Win32FileAccess.GENERIC_READ,
                                  FileShare.ReadWrite,
                                  IntPtr.Zero,
                                  FileMode.Open,
                                  CreateFileOptions.BACKUP_SEMANTICS, //need for retrieve info about directory
                                  IntPtr.Zero);
                if (file_handle.ToInt32() == WinApiFS.INVALID_HANDLE_VALUE)
                {
                    int       win_err = Marshal.GetLastWin32Error();
                    Exception ex      = new Win32Exception(win_err);
                    throw ex;
                }

                return(GetFileInfo_stream(file_handle, IntPtr.Zero, 0));
            }
            finally
            {
                if ((file_handle != IntPtr.Zero) && (file_handle.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE))
                {
                    WinApiFS.CloseHandle(file_handle);
                }
            }
        }
Exemple #4
0
        public static NT_FILE_STANDARD_INFORMATION GetFileInfo_standard(string file_name)
        {
            var file_handle = IntPtr.Zero;

            try
            {
                file_handle = WinApiFS.CreateFile_intptr
                                  (file_name,
                                  Win32FileAccess.GENERIC_READ,
                                  FileShare.ReadWrite,
                                  IntPtr.Zero,
                                  FileMode.Open,
                                  CreateFileOptions.None,
                                  IntPtr.Zero);
                if (file_handle.ToInt64() == WinApiFS.INVALID_HANDLE_VALUE)
                {
                    var       win_err = Marshal.GetLastWin32Error();
                    Exception ex      = new Win32Exception(win_err);
                    throw ex;
                }

                var ret = GetFileInfo_standard(file_handle);
                return(ret);
            }
            finally
            {
                if ((file_handle != IntPtr.Zero) && (file_handle.ToInt64() != WinApiFS.INVALID_HANDLE_VALUE))
                {
                    WinApiFS.CloseHandle(file_handle);
                }
            }
        }
Exemple #5
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));
                }
            }
        }
Exemple #6
0
                public InternalEnumerator(string file_name)
                {
                    file_h = WinApiFS.CreateFile_intptr
                                 (file_name,
                                 Win32FileAccess.GENERIC_READ,
                                 FileShare.ReadWrite,
                                 IntPtr.Zero,
                                 FileMode.Open,
                                 CreateFileOptions.BACKUP_SEMANTICS, //need for retrieve info about directory
                                 IntPtr.Zero);
                    if (file_h.ToInt32() == WinApiFS.INVALID_HANDLE_VALUE)
                    {
                        int       win_err = Marshal.GetLastWin32Error();
                        Exception ex      = new Win32Exception(win_err);
                        throw ex;
                    }
                    need_free_file_h = true;

                    int buffer_size = STREAM_INFO_BUFFER_LEN;

                    need_free_buffer_h = true;
                    buffer_h           = Marshal.AllocHGlobal(buffer_size);

                    Marshal.WriteInt32(buffer_h, 4, 0);

                    IO_STATUS_BLOCK status = new IO_STATUS_BLOCK();

                    uint res = ntApiFS.NtQueryInformationFile
                                   (file_h,
                                   ref status,
                                   buffer_h,
                                   buffer_size,
                                   FILE_INFORMATION_CLASS.FileStreamInformation);

                    //check result
                    NTSTATUS_helper.ThrowOnError(res, status, NTSTATUS_severity.Warning);
                }
Exemple #7
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();
            }
        }
Exemple #8
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
        }