예제 #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);
                }
            }
        }
예제 #2
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);
                }
            }
        }
예제 #3
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);
                }
            }
        }
예제 #4
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);
                }
            }
        }
예제 #5
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);
                }
            }
        }
예제 #6
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));
                }
            }
        }
예제 #7
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++;
                }
            }
        }
예제 #8
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);
                }
            }
        }
예제 #9
0
 public void Dispose()
 {
     if (need_free_buffer_h)
     {
         if (buffer_h != IntPtr.Zero)
         {
             Marshal.FreeHGlobal(buffer_h);
         }
     }
     if (need_free_file_h)
     {
         if ((file_h != IntPtr.Zero) || (file_h.ToInt32() != WinApiFS.INVALID_HANDLE_VALUE))
         {
             WinApiFS.CloseHandle(file_h);
         }
     }
 }
예제 #10
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);
                }
            }
        }
예제 #11
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();
            }
        }
예제 #12
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
        }