Example #1
0
        private static FindFileHandle BeginFind(string searchPattern, out WindowsNative.WIN32_FIND_DATA findData)
        {
            FindFileHandle handle = null;

            if (KtmTransaction.IsInTransaction)
            {
                handle = WindowsNative.FindFirstFileTransacted(
                    searchPattern
                    , WindowsNative.FINDEX_INFO_LEVELS.FindExInfoBasic
                    , out findData
                    , WindowsNative.FINDEX_SEARCH_OPS.FindExSearchNameMatch
                    , IntPtr.Zero
                    , 0                                 // 1 - case sensitive
                    , KtmTransaction.Current.Hanlde);
            }
            else
            {
                handle = WindowsNative.FindFirstFile(searchPattern, out findData);
            }

            if (handle.IsInvalid)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != WindowsNative.ERROR_FILE_NOT_FOUND)
                {
                    WindowsNative.HandleWindowsError(errorCode);
                }

                return(null);
            }

            return(handle);
        }
Example #2
0
        internal static void Move(string sourcePath, string destinationPath)
        {
            var longPathSource      = new LongPath(sourcePath);
            var longPathDestination = new LongPath(destinationPath);

            bool result;

            if (KtmTransaction.IsInTransaction)
            {
                result = WindowsNative.MoveFileTransacted(
                    longPathSource.PathString
                    , longPathDestination.PathString
                    , IntPtr.Zero
                    , IntPtr.Zero
                    , WindowsNative.MoveFileFlags.NONE
                    , KtmTransaction.Current.Hanlde);
            }
            else
            {
                result = WindowsNative.MoveFile(longPathSource.PathString, longPathDestination.PathString);
            }
            if (!result)
            {
                WindowsNative.HandleWindowsError();
            }
        }
Example #3
0
        /// <summary>
        ///		<see cref="IFileProvider.Copy"/>
        /// </summary>
        public void Copy(string sourcePath, string destinationPath, bool overwrite)
        {
            var longPathSource      = new LongPath(sourcePath);
            var longPathDestination = new LongPath(destinationPath);

            bool result;

            if (KtmTransaction.IsInTransaction)
            {
                bool cancel = false;
                result = WindowsNative.CopyFileTransacted(
                    longPathSource.PathString
                    , longPathDestination.PathString
                    , IntPtr.Zero
                    , IntPtr.Zero
                    , ref cancel
                    , WindowsNative.CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS
                    , KtmTransaction.Current.Hanlde);
            }
            else
            {
                result = WindowsNative.CopyFile(longPathSource.PathString, longPathDestination.PathString, !overwrite);
            }
            if (!result)
            {
                WindowsNative.HandleWindowsError();
            }
        }
Example #4
0
        /// <remarks>
        ///     Will return WindowsNative.INVALID_FILE_ATTRIBUTES if failed and throwException is false.
        /// </remarks>
        /// <exception cref='Win32Exception'>
        ///     Call to native windows function failed.
        /// </exception>
        public FileAttributes GetFileAttributes(bool throwException)
        {
            FileAttributes retval = WindowsNative.INVALID_FILE_ATTRIBUTES;

            if (!KtmTransaction.IsInTransaction)
            {
                retval = WindowsNative.GetFileAttributes(PathString);
            }
            else
            {
                WindowsNative.WIN32_FILE_ATTRIBUTE_DATA attrData = new WindowsNative.WIN32_FILE_ATTRIBUTE_DATA();
                if (WindowsNative.GetFileAttributesTransacted(
                        PathString
                        , WindowsNative.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard
                        , out attrData
                        , KtmTransaction.Current.Hanlde))
                {
                    retval = attrData.dwFileAttributes;
                }
            }

            Check.DoAssertLambda(!throwException || retval != WindowsNative.INVALID_FILE_ATTRIBUTES, () => new Win32Exception());

            return(retval);
        }
Example #5
0
 /// <summary>
 ///     Rollback the transaction
 /// </summary>
 /// <exception cref="Win32Exception">
 ///     Rollback failed.
 /// </exception>
 public void Rollback()
 {
     if (WindowsNative.RollbackTransaction(this))
     {
         WindowsNative.HandleWindowsError();
     }
 }
Example #6
0
        /// <summary>
        ///     Commit the transaction
        /// </summary>
        /// <exception cref="Win32Exception">
        ///     Commit failed.
        /// </exception>
        public void Commit()
        {
            bool committed = WindowsNative.CommitTransaction(this);

            //OnTransactionEnded(committed);
            if (!committed)
            {
                WindowsNative.HandleWindowsError();
            }
        }
Example #7
0
        private static IEnumerable <string> EnumerateFileSystemIterator(
            LongPath path, string mask, bool enumerateDirectories, bool enumerateFiles)
        {
            if (string.IsNullOrEmpty(mask) || mask == ".")
            {
                mask = "*";
            }
            string unprefixedPath = path.UnPrefixedPathString;

            WindowsNative.WIN32_FIND_DATA findData;
            using (FindFileHandle handle = BeginFind(Path.Combine(path.PathString, mask), out findData))
            {
                if (handle == null)
                {
                    yield break;
                }

                do
                {
                    string fileName = findData.cFileName;

                    if (IsDirectory(findData.dwFileAttributes))
                    {
                        if (enumerateDirectories && IsRealFolder(fileName))
                        {
                            yield return(Path.Combine(unprefixedPath, fileName));
                        }
                    }
                    else
                    {
                        if (enumerateFiles)
                        {
                            yield return(Path.Combine(unprefixedPath, fileName));
                        }
                    }
                }               while (WindowsNative.FindNextFile(handle, out findData));

                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode != WindowsNative.ERROR_NO_MORE_FILES)
                {
                    WindowsNative.HandleWindowsError(errorCode);
                }
            }
        }
Example #8
0
        public void Delete(string path)
        {
            LongPath longPath = new LongPath(path);

            bool result;

            if (KtmTransaction.IsInTransaction)
            {
                result = WindowsNative.RemoveDirectoryTransacted(longPath.PathString, KtmTransaction.Current.Hanlde);
            }
            else
            {
                result = WindowsNative.RemoveDirectory(longPath.PathString);
            }
            if (!result)
            {
                WindowsNative.HandleWindowsError();
            }
        }
Example #9
0
        public LongPath(string path)
        {
            Check.DoRequireArgumentNotNull(path, "path");
            Check.DoCheckArgument(!string.IsNullOrEmpty(path), string.Empty, "path");

            StringBuilder bld  = new StringBuilder(path.Length + 5);
            uint          size = WindowsNative.GetFullPathName(path, (uint)bld.Capacity, bld, IntPtr.Zero);

            if (size > bld.Capacity)
            {
                bld.Capacity = (int)size + 5;
                size         = WindowsNative.GetFullPathName(path, size, bld, IntPtr.Zero);
            }

            Check.DoAssertLambda(size > 0, () => WindowsNative.GetLastErrorException("path"));
            Check.DoAssertLambda(size <= WindowsNative.MAX_LONG_PATH, () => new PathTooLongException());

            bld.Insert(0, WindowsNative.LongPathPrefix);

            PathString = bld.ToString();
        }
Example #10
0
        private static SafeFileHandle GetFileHandle(LongPath path, FileMode mode, FileAccess access, FileShare share, FileOptions options)
        {
            SafeFileHandle handle;

            if (KtmTransaction.IsInTransaction)
            {
                WindowsNative.FileMode   internalMode   = WindowsNative.TranslateFileMode(mode);
                WindowsNative.FileShare  internalShare  = WindowsNative.TranslateFileShare(share);
                WindowsNative.FileAccess internalAccess = WindowsNative.TranslateFileAccess(access);

                KtmTransactionHandle ktmTx = KtmTransaction.Current.Hanlde;

                // Create the transacted file using P/Invoke.
                handle = WindowsNative.CreateFileTransacted(
                    path.PathString,
                    internalAccess,
                    internalShare,
                    IntPtr.Zero,
                    internalMode,
                    WindowsNative.EFileAttributes.Normal,
                    IntPtr.Zero,
                    KtmTransaction.Current.Hanlde,
                    IntPtr.Zero,
                    IntPtr.Zero);
            }
            else
            {
                WindowsNative.EFileAccess underlyingAccess = WindowsNative.GetUnderlyingAccess(access);
                handle = WindowsNative.CreateFile(path.PathString, underlyingAccess, (uint)share, IntPtr.Zero, (uint)mode, (uint)options, IntPtr.Zero);
            }

            if (handle.IsInvalid)
            {
                WindowsNative.HandleWindowsError();
            }

            return(handle);
        }
Example #11
0
        public void Create(string path)
        {
            LongPath longPath = new LongPath(path);

            bool result;

            if (KtmTransaction.IsInTransaction)
            {
                result = WindowsNative.CreateDirectoryTransacted(null, longPath.PathString, IntPtr.Zero, KtmTransaction.Current.Hanlde);
            }
            else
            {
                result = WindowsNative.CreateDirectory(longPath.PathString, IntPtr.Zero);
            }
            if (!result)
            {
                int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                if (errorCode != WindowsNative.ERROR_ALREADY_EXISTS || !longPath.Exists())
                {
                    WindowsNative.HandleWindowsError(errorCode);
                }
            }
        }
Example #12
0
 protected override bool ReleaseHandle()
 {
     return(WindowsNative.FindClose(base.handle));
 }
Example #13
0
 /// <summary>
 ///     Start local KTM transaction
 /// </summary>
 /// <returns>
 ///     New transaction handle
 /// </returns>
 public static KtmTransactionHandle CreateLocalTransaction()
 {
     return(new KtmTransactionHandle(WindowsNative.CreateTransaction(IntPtr.Zero, IntPtr.Zero, 0, 0, 0, 0, null)));
 }