Example #1
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);
                }
            }
        }
Example #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);
                }
            }
        }
Example #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);
                }
            }
        }
Example #4
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);
                }
            }
        }
Example #5
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);
                }
            }
        }
 public void Dispose()
 {
     if (mount_point_buffer != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(mount_point_buffer);
     }
     WinApiFS.FindVolumeMountPointClose(search_handle);
 }
Example #7
0
 public void Dispose()
 {
     if (volume_guid_buffer != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(volume_guid_buffer);
     }
     WinApiFS.FindVolumeClose(search_handle);
 }
Example #8
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));
                }
            }
        }
Example #9
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 #10
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);
                }
            }
        }
            public bool MoveNext()
            {
                int err_code = 0;
                int res      = 0;

                if (search_handle == IntPtr.Zero)
                {
                    search_handle = WinApiFS.FindFirstVolumeMountPoint(volume_guid, mount_point_buffer, mount_point_buffer_len_tchars);
                    if (search_handle.ToInt32() == WinApiFS.INVALID_HANDLE_VALUE)
                    {
                        err_code = Marshal.GetLastWin32Error();
                        if (err_code == 18)
                        {
                            //no more files
                            return(false);
                        }
                        else if (err_code == 21)
                        {
                            //device not ready
                            return(false);
                        }
                        else
                        {
                            throw new Win32Exception(err_code);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    res = WinApiFS.FindNextVolumeMountPoint(search_handle, mount_point_buffer, mount_point_buffer_len_tchars);
                    if (res == 0)
                    {
                        err_code = Marshal.GetLastWin32Error();
                        if (err_code == 18)
                        {
                            //no more files
                            return(false);
                        }
                        else
                        {
                            throw new Win32Exception(err_code);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
Example #12
0
        public static VolumeSpaceInfo GetInfo(string directory)
        {
            var ret = new VolumeSpaceInfo();

            var res = WinApiFS.GetDiskFreeSpaceEx(directory, ref ret.FreeBytesAvailable, ref ret.TotalNumberOfBytes, ref ret.TotalNumberOfFreeBytes);

            if (res == 0)
            {
                var winErr = Marshal.GetLastWin32Error();
                throw new System.ComponentModel.Win32Exception(winErr);
            }

            return(ret);
        }
Example #13
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);
        }
Example #14
0
            public bool MoveNext()
            {
                var err_code = 0;
                var res      = 0;

                if (search_handle == IntPtr.Zero)
                {
                    search_handle = WinApiFS.FindFirstVolume(volume_guid_buffer, volume_guid_buffer_len_tchars);
                    if (search_handle.ToInt64() == WinApiFS.INVALID_HANDLE_VALUE)
                    {
                        err_code = Marshal.GetLastWin32Error();
                        if (err_code == 18)
                        {
                            //no more files
                            return(false);
                        }
                        else
                        {
                            throw new Win32Exception(err_code);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    res = WinApiFS.FindNextVolume(search_handle, volume_guid_buffer, volume_guid_buffer_len_tchars);
                    if (res == 0)
                    {
                        err_code = Marshal.GetLastWin32Error();
                        if (err_code == 18)
                        {
                            //no more files
                            return(false);
                        }
                        else
                        {
                            throw new Win32Exception(err_code);
                        }
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
Example #15
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);
         }
     }
 }
Example #16
0
        } //end of proc

        /// <summary>
        /// work only if source and destination on one volume
        /// </summary>
        /// <param name="source">can be file or dir</param>
        /// <param name="destination">file or dir</param>
        private void move_one_item(FileInfoEx source, string destination)
        {
            //prepare callback buffer
            if (callback_data != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(callback_data);
            }
            callback_data = IOhelper.strings_to_buffer(source.FullName, destination);

            //prepare uni names
            var source_uni      = IOhelper.GetUnicodePath(source.FullName);
            var destination_uni = IOhelper.GetUnicodePath(destination);

            //call update dialog before...
            var e_begin_move = new UpdateProgressArgs();

            e_begin_move.DestinationFile     = destination;
            e_begin_move.EnableTotalProgress = false;
            e_begin_move.Reason     = UpdateProgressReason.StreamSwitch;
            e_begin_move.SourceFile = source.FullName;
            update_progress_safe(e_begin_move);

            //and call MoveFileWithProgress
            var res = WinApiFS.MoveFileWithProgress
                          (source_uni,
                          destination_uni,
                          move_progress_delegate_holder,
                          callback_data,
                          options_api);

            if (res == 0)
            {
                var win_err = Marshal.GetLastWin32Error();
                var win_ex  = new Win32Exception(win_err);
                AbortJobSafe = !process_errors
                                   (string.Format
                                       (Options.GetLiteral(Options.LANG_CANNOT_MOVE_0_ARROW_1),
                                       source,
                                       destination),
                                   win_ex);
                return;
            }
            //success move
            //notify item done - not needed, source item deleted
        }
Example #17
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 #18
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);
                }
Example #19
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();
            }
        }
Example #20
0
        private void copy_one_file(FileInfoEx source_info, string destination_file)
        {
            //check mask
            if (!Wildcard.Match(initial_mask, source_info.FileName))
            {
                return;
            }

            var success = false;

            //now prepare callback buffer
            if (callback_data != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(callback_data);
            }
            callback_data = IOhelper.strings_to_buffer(source_info.FullName, destination_file);

            //prepare uni names
            var source_uni = IOhelper.GetUnicodePath(source_info.FullName);
            var dest_uni   = IOhelper.GetUnicodePath(destination_file);

            //string source_uni = source_info.FullName;
            //string dest_uni = destination_file;

            //and call CopyFileEx first time
            var res = WinApiFS.CopyFileEx
                          (source_uni,
                          dest_uni,
                          copy_progress_routine_delegate_holder,
                          callback_data,
                          IntPtr.Zero,
                          copyFileEx_options);

            if (res == 0)
            {
                var win_err = Marshal.GetLastWin32Error();
                //process win_err and do second take if needed
                var temp_copy_options = copyFileEx_options;
                if (process_CopyFileEx_errors(source_info, destination_file, win_err, ref temp_copy_options))
                {
                    //second take
                    res = WinApiFS.CopyFileEx
                              (source_uni,
                              dest_uni,
                              copy_progress_routine_delegate_holder,
                              callback_data,
                              IntPtr.Zero,
                              temp_copy_options);
                    if (res == 0)
                    {
                        win_err = Marshal.GetLastWin32Error();
                        var win_ex_2 = new Win32Exception(win_err);
                        if (!process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_COPY_0_ARROW_1),
                                    source_info.FullName,
                                    destination_file),
                                win_ex_2))
                        {
                            stop();
                        }
                    }
                    else
                    {
                        success = true;
                    }
                }
            }//end of res==0 brunch
            else
            {
                //copy success
                success = true;
            }

            if (success)
            {
                //update counts
                total_bytes_transferred       += current_file_bytes_transferred;
                current_file_bytes_transferred = 0;
                total_files_copied++;

                //time to notify current panel -> unset selection
                var e_file_done = new ItemEventArs(source_info.FileName);
                OnCopyItemDone(e_file_done);

                //set sec attributes if needed
                if ((options & CopyEngineOptions.CopySecurityAttributes) == CopyEngineOptions.CopySecurityAttributes)
                {
                    try
                    {
                        File.SetAccessControl(destination_file, File.GetAccessControl(source_info.FullName));
                    }
                    catch (Exception ex)
                    {
                        if (!process_error
                                (string.Format
                                    (Options.GetLiteral(Options.LANG_CANNOT_SET_SEC_ATTRIBUTES_0),
                                    destination_file),
                                ex))
                        {
                            stop();
                        }
                    }
                }
            }
        }
        private void delete_entry(FileInfoEx entry)
        {
            /*
             * We first try to delete directory (if set flag DeleteEmptyDirectories)
             * if it is link, DeleteFile finction deletes link, not target, even if target not empty
             * and return.
             * If flag DeleteEmptyDirectories not setted, we check LinkInformation and
             * if it not null (that is entry is soft link) -> return, else
             * remove its contains.
             */

            int win_err = 0;
            int res     = 0;

            if (abort)
            {
                return;
            }

            if (!check_readonly(entry))
            {
                return;
            }

            if (entry.Directory)
            {
                if (!check_readonly(entry))
                {
                    return;
                }

                //first try to delete dir, assume it is empty
                if ((opts & DeleteFileOptions.DeleteEmptyDirectories) == DeleteFileOptions.DeleteEmptyDirectories)
                {
                    res = WinApiFS.RemoveDirectory(entry.FullName);
                    if (res == 0)
                    {
                        win_err = Marshal.GetLastWin32Error();
                        if (win_err == WinApiFS.ERROR_DIR_NOT_EMPTY)
                        {
                            //directory not empty
                            if ((opts & DeleteFileOptions.RecursiveDeleteFiles) == DeleteFileOptions.RecursiveDeleteFiles)
                            {
                                //recursive delete needed
                                FileInfoExEnumerable dir_enum = new FileInfoExEnumerable
                                                                    (Path.Combine(entry.FullName, "*"), false, false, false, true);
                                try
                                {
                                    foreach (FileInfoEx info in dir_enum)
                                    {
                                        delete_entry(info);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    abort = !process_errors
                                                (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                                ex);
                                }
                            }//end of dir enum
                            //now dir probably empty
                            res = WinApiFS.RemoveDirectory(entry.FullName);
                            if (res == 0)
                            {
                                //not success
                                win_err = Marshal.GetLastWin32Error();
                                if (win_err != WinApiFS.ERROR_DIR_NOT_EMPTY)
                                //supress -> when delete on mask,
                                //dir may be not empty
                                {
                                    abort = !process_errors
                                                (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                                new Win32Exception(win_err));
                                }
                            }
                        }//end of dir not empty
                        else
                        {
                            abort = !process_errors
                                        (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                        new Win32Exception(win_err));
                        }
                    }//end of error process
                }
                else
                {
                    //not to delete empty dirs
                    //recursive delete needed (if not link) but directory not will remove
                    //check reparse tag
                    if ((opts & DeleteFileOptions.RecursiveDeleteFiles) == DeleteFileOptions.RecursiveDeleteFiles)
                    {
                        if (entry.LinkInformation == null)
                        {
                            FileInfoExEnumerable dir_enum = new FileInfoExEnumerable
                                                                (Path.Combine(entry.FullName, "*"), false, false, false, true);
                            try
                            {
                                foreach (FileInfoEx info in dir_enum)
                                {
                                    delete_entry(info);
                                }
                            }
                            catch (Exception ex)
                            {
                                abort = !process_errors
                                            (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                            ex);
                            }
                        }
                    }
                }
            }//end of if(entry.Directory)
            else
            {
                //entry is file

                //check mask
                if (Wildcard.Match(delete_mask, entry.FileName, false))
                {
                    if (check_readonly(entry))
                    {
                        if (main_window != null)
                        {
                            main_window.NotifyLongOperation
                                (string.Format(Options.GetLiteral(Options.LANG_DELETE_NOW_0), entry.FullName),
                                true);
                        }
                        res = WinApiFS.DeleteFile(entry.FullName);
                        if (res == 0)
                        {
                            abort = !process_errors
                                        (string.Format(Options.GetLiteral(Options.LANG_CANNOT_DELETE_0), entry.FullName),
                                        new Win32Exception(Marshal.GetLastWin32Error()));
                        }
                    }
                }
            }
        }
Example #22
0
        private void initInfo()
        {
            if (ready)
            {
                return;
            }

            var root                     = RootPathName;
            var nameBufferSize           = (WinApiFS.MAX_PATH + 1) * Marshal.SystemDefaultCharSize;
            var nameCharsCount           = WinApiFS.MAX_PATH + 1;
            var ptVolumeNameBuffer       = Marshal.AllocHGlobal(nameBufferSize);
            var ptVolumeSerialNumber     = Marshal.AllocHGlobal(4);
            var ptMaximumComponentLength = Marshal.AllocHGlobal(4);
            var ptFileSystemFlags        = Marshal.AllocHGlobal(4);
            var ptFileSystemName         = Marshal.AllocHGlobal(nameBufferSize);

            try
            {
                var res = WinApiFS.GetVolumeInformation
                              (root,
                              ptVolumeNameBuffer,
                              nameCharsCount,
                              ptVolumeSerialNumber,
                              ptMaximumComponentLength,
                              ptFileSystemFlags,
                              ptFileSystemName,
                              nameCharsCount);
                if (res == 0) //not process 'device not ready (21)', 'network drive not found (53) 'path not found (3)'
                {
                    var errCode = Marshal.GetLastWin32Error();
                    if (errCode == 21)
                    {
                        DeviceReady = false;
                        ready       = true;
                    }
                    else if (errCode == 53)
                    {
                        DeviceReady = false;
                        ready       = true;
                    }
                    else if (errCode == 3)
                    {
                        DeviceReady = false;
                        ready       = true;
                    }
                    else
                    {
                        Messages.ShowException
                            (new Win32Exception(errCode),
                            string.Format
                                ("Failed get volume {0} properties.",
                                root));
                        DeviceReady = false;
                        ready       = true;
                    }
                }
                else
                {
                    DeviceReady = true;

                    RootPathName           = root;
                    VolumeName             = Marshal.PtrToStringAuto(ptVolumeNameBuffer);
                    MaximumComponentLength = Marshal.ReadInt32(ptMaximumComponentLength);
                    FileSystemFlags        = (VolumeCaps)Marshal.ReadInt32(ptFileSystemFlags);
                    FileSystemName         = Marshal.PtrToStringAuto(ptFileSystemName);

                    var serialBytes = new byte[4];
                    Marshal.Copy(ptVolumeSerialNumber, serialBytes, 0, 4);
                    SerialNumber = (uint)BitConverter.ToUInt64(serialBytes, 0);
                }

                if (DeviceReady)
                {
                    VolumeSpaceInfo = VolumeSpaceInfo.GetInfo(RootPathName);
                }
                else
                {
                    VolumeSpaceInfo = new VolumeSpaceInfo();
                }

                DriveType = WinApiFS.GetDriveType(RootPathName);
            }
            finally
            {
                Marshal.FreeHGlobal(ptFileSystemFlags);
                Marshal.FreeHGlobal(ptFileSystemName);
                Marshal.FreeHGlobal(ptMaximumComponentLength);
                Marshal.FreeHGlobal(ptVolumeNameBuffer);
                Marshal.FreeHGlobal(ptVolumeSerialNumber);

                ready = true;
            }
        }
Example #23
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
        }