public void CanDeleteFileWithDenyACL()
        {
            string file      = GetFullPath("File with space");
            string directory = Path.GetDirectoryName(file);

            File.WriteAllText(file, string.Empty);
            try
            {
                FileInfo     fi            = new FileInfo(file);
                FileSecurity accessControl = fi.GetAccessControl(AccessControlSections.All);
                accessControl.PurgeAccessRules(WindowsIdentity.GetCurrent().User);
                accessControl.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().User, FileSystemRights.FullControl, AccessControlType.Deny));
                fi.SetAccessControl(accessControl);
                DirectoryInfo     di = new DirectoryInfo(directory);
                DirectorySecurity ds = di.GetAccessControl(AccessControlSections.All);
                ds.PurgeAccessRules(WindowsIdentity.GetCurrent().User);
                ds.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().User, FileSystemRights.CreateFiles, AccessControlType.Deny));
                di.SetAccessControl(ds);

                XAssert.IsTrue(File.Exists(file));
                FileUtilities.DeleteFile(file);
                XAssert.IsFalse(File.Exists(file));
            }
            finally
            {
                DirectoryInfo     di = new DirectoryInfo(directory);
                DirectorySecurity ds = di.GetAccessControl(AccessControlSections.All);
                ds.PurgeAccessRules(WindowsIdentity.GetCurrent().User);
                ds.AddAccessRule(new FileSystemAccessRule(WindowsIdentity.GetCurrent().User, FileSystemRights.FullControl, AccessControlType.Allow));
                di.SetAccessControl(ds);
                di.Delete(true);
            }
        }
        public void CanDeleteWithOtherOpenHardlinks()
        {
            string hardlink1 = Path.Combine(TemporaryDirectory, "hardlink1");

            File.WriteAllText(hardlink1, "asdf");

            string hardlink2 = Path.Combine(TemporaryDirectory, "hardlink2");

            XAssert.IsTrue(CreateHardLinkIfSupported(hardlink2, hardlink1));

            // Open a handle to hardlink2 without FileShare Delete mode
            using (new FileStream(hardlink2, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                BuildXLException exception = null;
                try
                {
                    FileUtilities.DeleteFile(hardlink1);
                }
                catch (BuildXLException ex)
                {
                    exception = ex;
                }
                // Successfully delete file
                XAssert.IsTrue(exception == null);
                XAssert.IsFalse(File.Exists(hardlink1));
            }
        }
        public void CanDeleteReadonlyFile()
        {
            string file = GetFullPath("File");

            File.WriteAllText(file, string.Empty);
            SetReadonlyFlag(file);

            XAssert.IsTrue(File.Exists(file));
            FileUtilities.DeleteFile(file);
            XAssert.IsFalse(File.Exists(file));
        }
        public void CanDeleteRunningExecutableLink()
        {
            string exeLink = GetFullPath("DummyWaiterLink");

            XAssert.IsTrue(CreateHardLinkIfSupported(link: exeLink, linkTarget: DummyWaiter.GetDummyWaiterExeLocation()));

            using (var waiter = DummyWaiter.RunAndWait())
            {
                XAssert.IsTrue(File.Exists(exeLink));
                FileUtilities.DeleteFile(exeLink);
                XAssert.IsFalse(File.Exists(exeLink));
            }
        }
Exemple #5
0
        public void CanDeleteMemoryMappedFile()
        {
            string file = GetFullPath("File");
            string link = GetFullPath("link");

            WithNewFileMemoryMapped(
                file,
                () =>
            {
                if (!CreateHardLinkIfSupported(link: link, linkTarget: file))
                {
                    return;
                }

                XAssert.IsTrue(File.Exists(link));
                FileUtilities.DeleteFile(link);
                XAssert.IsFalse(File.Exists(link));
            });
        }
        public void CanDeleteReadonlyDenyWriteAttribute()
        {
            string file      = GetFullPath("File");
            string directory = Path.GetDirectoryName(file);

            File.WriteAllText(file, string.Empty);

            // Make the file readonly & deny modifying attributes
            FileInfo fi = new FileInfo(file);

            fi.IsReadOnly = true;
            FileSecurity accessControl = fi.GetAccessControl(AccessControlSections.All);

            accessControl.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.WriteAttributes, AccessControlType.Deny));
            fi.SetAccessControl(accessControl);

            // see if we can delete it
            XAssert.IsTrue(File.Exists(file));
            FileUtilities.DeleteFile(file);
            XAssert.IsFalse(File.Exists(file));
        }
        public void MoveTempDeletionCanReplaceRunningExecutable()
        {
            // Make a copy of DummyWaiter.exe to use as the test subject for deleting a running executable.
            // Keep the copy in the same directory as the original since it will need runtime dlls
            string dummyWaiterLocation = DummyWaiter.GetDummyWaiterExeLocation();
            string exeCopy             = dummyWaiterLocation + ".copy.exe";

            File.Copy(dummyWaiterLocation, exeCopy);

            using (var waiter = DummyWaiter.RunAndWait(exeCopy))
            {
                BuildXLException caughtException = null;
                try
                {
                    FileUtilities.DeleteFile(exeCopy);
                }
                catch (BuildXLException ex)
                {
                    caughtException = ex;
                }

                XAssert.IsNotNull(caughtException, "Expected deletion without a tempCleaner to fail");
                XAssert.IsTrue(File.Exists(exeCopy));

                caughtException = null;


                try
                {
                    FileUtilities.DeleteFile(exeCopy, tempDirectoryCleaner: MoveDeleteCleaner);
                }
                catch (BuildXLException ex)
                {
                    caughtException = ex;
                }

                XAssert.IsNull(caughtException, "Expected deletion with a MoveDeleteCleaner to succeed");
                XAssert.IsFalse(File.Exists(exeCopy));
            }
        }