public void CanMoveDirectory()
        {
            string dir1 = dllPath.CombineAssert("a");
            string dir2 = dllPath.Combine("b");

            Assert.That(Directory.Exists(dir2), Is.False);
            Assert.That(File.Exists(dir2), Is.False, "Lingering files should not be allowed to disrupt the testing.");


            string aFile = dir1.Combine("file");

            File.WriteAllText(aFile, "I should also be moved.");
            infosCreated.Add(aFile);

            using (var t = new FileTransaction("moving tx"))
            {
                t.Begin();

                (t as IDirectoryAdapter).Move(dir1, dir2);
                Assert.IsFalse(Directory.Exists(dir2), "The directory should not yet exist.");

                t.Commit();
                Assert.That(Directory.Exists(dir2), "Now after committing it should.");
                infosCreated.Add(dir2);

                Assert.That(File.Exists(dir2.Combine(Path.GetFileName(aFile))), "And so should the file in the directory.");
            }
        }
Exemple #2
0
        void IFileAdapter.Move(string originalFilePath, string newFilePath)
        {
            // case 1, the new file path is a folder
            if (((IDirectoryAdapter)this).Exists(newFilePath))
            {
                MoveFileTransacted(originalFilePath, newFilePath.Combine(Path.GetFileName(originalFilePath)), IntPtr.Zero, IntPtr.Zero, MoveFileFlags.CopyAllowed,
                                   _TransactionHandle);
                return;
            }

            // case 2, its not a folder, so assume it's a file.
            MoveFileTransacted(originalFilePath, newFilePath, IntPtr.Zero, IntPtr.Zero, MoveFileFlags.CopyAllowed,
                               _TransactionHandle);
        }
Exemple #3
0
        private bool recurseFiles(string path,
                                  Func <string, bool> operationOnFiles,
                                  Func <string, bool> operationOnDirectories)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (path == string.Empty)
            {
                throw new ArgumentException("You can't pass an empty string.");
            }

            WIN32_FIND_DATA findData;
            bool            addPrefix = !path.StartsWith(@"\\?\");
            bool            ok        = true;

            string pathWithoutSufflix = addPrefix ? @"\\?\" + Path.GetFullPath(path) : Path.GetFullPath(path);

            path = pathWithoutSufflix + "\\*";

            using (var findHandle = FindFirstFileTransactedW(path, out findData))
            {
                if (findHandle.IsInvalid)
                {
                    return(false);
                }

                do
                {
                    var subPath = pathWithoutSufflix.Combine(findData.cFileName);

                    if ((findData.dwFileAttributes & (uint)FileAttributes.Directory) != 0)
                    {
                        if (findData.cFileName != "." && findData.cFileName != "..")
                        {
                            ok &= deleteRecursive(subPath);
                        }
                    }
                    else
                    {
                        ok = ok && operationOnFiles(subPath);
                    }
                }while (FindNextFile(findHandle, out findData));
            }

            return(ok && operationOnDirectories(pathWithoutSufflix));
        }
Exemple #4
0
        /// <summary>Creates a directory at the path given.</summary>
        ///<param name="path">The path to create the directory at.</param>
        bool IDirectoryAdapter.Create(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            AssertState(TransactionStatus.Active);

            path = Path.NormDirSepChars(CleanPathEnd(path));

            // we don't need to re-create existing folders.
            if (((IDirectoryAdapter)this).Exists(path))
            {
                return(true);
            }

            var nonExistent = new Stack <string>();

            nonExistent.Push(path);

            var curr = path;

            while (!((IDirectoryAdapter)this).Exists(curr) &&
                   (curr.Contains(System.IO.Path.DirectorySeparatorChar) ||
                    curr.Contains(System.IO.Path.AltDirectorySeparatorChar)))
            {
                curr = Path.GetPathWithoutLastBit(curr);

                if (!((IDirectoryAdapter)this).Exists(curr))
                {
                    nonExistent.Push(curr);
                }
            }

            while (nonExistent.Count > 0)
            {
                if (!createDirectoryTransacted(nonExistent.Pop()))
                {
                    var win32Exception = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new TransactionException(string.Format("Failed to create directory \"{1}\" at path \"{0}\". "
                                                                 + "See inner exception for more details.", path, curr),
                                                   win32Exception);
                }
            }

            return(false);
        }