Ejemplo n.º 1
0
        public void TestDirectoryExceptions()
        {
            var fs = new PhysicalFileSystem();

            // Try to create a folder on an unauthorized location
            fs.CreateDirectory("/");
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt2"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt/yoyo"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.CreateDirectory("/mnt/c"));

            var drives = fs.EnumerateDirectories("/mnt").ToList();

            Assert.True(drives.Count > 0);

            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory("/", drives[0] / "ShouldNotHappen"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory("/mnt", drives[0] / "ShouldNotHappen"));
            Assert.Throws <DirectoryNotFoundException>(() => fs.MoveDirectory("/mnt2", drives[0] / "ShouldNotHappen"));

            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory(drives[0] / "ShouldNotHappen", "/"));
            Assert.Throws <UnauthorizedAccessException>(() => fs.MoveDirectory(drives[0] / "ShouldNotHappen", "/mnt"));
            Assert.Throws <DirectoryNotFoundException>(() => fs.MoveDirectory(drives[0] / "ShouldNotHappen", "/mnt2"));

            Assert.Throws <UnauthorizedAccessException>(() => fs.DeleteDirectory("/", false));
            Assert.Throws <UnauthorizedAccessException>(() => fs.DeleteDirectory("/mnt", false));
            Assert.Throws <DirectoryNotFoundException>(() => fs.DeleteDirectory("/mnt2", false));
            Assert.Throws <DirectoryNotFoundException>(() => fs.DeleteDirectory("/mnt/yoyo", false));
        }
Ejemplo n.º 2
0
        public void TestDirectory()
        {
            var fs                   = new PhysicalFileSystem();
            var pathInfo             = fs.ConvertPathFromInternal(SystemPath);
            var pathToCreate         = pathInfo / "TestCreateDirectory";
            var systemPathToCreate   = fs.ConvertPathToInternal(pathToCreate);
            var movedDirectory       = pathInfo / "TestCreateDirectoryMoved";
            var systemMovedDirectory = fs.ConvertPathToInternal(movedDirectory);

            try
            {
                // CreateDirectory
                Assert.False(Directory.Exists(systemPathToCreate));
                fs.CreateDirectory(pathToCreate);
                Assert.True(Directory.Exists(systemPathToCreate));

                // DirectoryExists
                Assert.True(fs.DirectoryExists(pathToCreate));
                Assert.False(fs.DirectoryExists(pathToCreate / "not_found"));

                // MoveDirectory
                fs.MoveDirectory(pathToCreate, movedDirectory);
                Assert.False(Directory.Exists(systemPathToCreate));
                Assert.True(fs.DirectoryExists(movedDirectory));

                // Delete the directory
                fs.DeleteDirectory(movedDirectory, false);
                Assert.False(Directory.Exists(systemMovedDirectory));
            }
            finally
            {
                SafeDeleteDirectory(systemPathToCreate);
                SafeDeleteDirectory(systemMovedDirectory);
            }
        }
        public void 建立資料夾()
        {
            using var fileSystem = new PhysicalFileSystem();
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var rootUPath         = fileSystem.ConvertPathFromInternal(rootPath);

            var subName      = "TestFolder";
            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));
            fileSystem.DeleteDirectory(subPath, true);
        }
        public void 列舉根路徑內的子資料夾()
        {
            using var fileSystem = new PhysicalFileSystem();
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var rootUPath         = fileSystem.ConvertPathFromInternal(rootPath);
            var rootUPath1        = fileSystem.ConvertPathToInternal(rootUPath);
            var subName           = "TestFolder";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var directoryEntries = fileSystem.EnumerateDirectoryEntries(subPath);

            foreach (var entry in directoryEntries)
            {
                Console.WriteLine(entry.Path);
            }

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));

            fileSystem.DeleteDirectory(subPath, true);
        }
        public void 修改檔案日期()
        {
            using var fileSystem = new PhysicalFileSystem();
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var rootUPath         = fileSystem.ConvertPathFromInternal(rootPath);
            var subName           = "TestFolder";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subFile2     = $"{subPath}/1/2.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var fileEntry = fileSystem.GetFileEntry(subFile1);

            fileEntry.CreationTime   = new DateTime(1900, 1, 1);
            fileEntry.LastWriteTime  = new DateTime(1900, 1, 2);
            fileEntry.LastAccessTime = new DateTime(1900, 1, 3);

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));

            fileSystem.DeleteDirectory(subPath, true);
        }
Ejemplo n.º 6
0
        private void MigrateUpgradeLogsToDirectoryWithFreshACLs()
        {
            string upgradeLogsPath     = ProductUpgraderInfo.GetLogDirectoryPath();
            string tempUpgradeLogsPath = Path.Combine(
                Path.GetDirectoryName(upgradeLogsPath),
                ProductUpgraderInfo.LogDirectory + "_" + Guid.NewGuid().ToString("N"));

            this.tracer.RelatedInfo($"{nameof(this.MigrateUpgradeLogsToDirectoryWithFreshACLs)}: Renaming '{upgradeLogsPath}' to '{tempUpgradeLogsPath}'");
            Directory.Move(upgradeLogsPath, tempUpgradeLogsPath);

            this.tracer.RelatedInfo($"{nameof(this.MigrateUpgradeLogsToDirectoryWithFreshACLs)}: Creating new '{upgradeLogsPath}' directory with appropriate ACLs");
            DirectorySecurity upgradeLogsSecurity = this.GetUpgradeLogsDirectorySecurity(upgradeLogsPath);

            Directory.CreateDirectory(upgradeLogsPath, upgradeLogsSecurity);

            try
            {
                DirectoryInfo tempDirectoryInfo = new DirectoryInfo(tempUpgradeLogsPath);

                this.tracer.RelatedInfo($"Moving directories from '{tempUpgradeLogsPath}' to '{upgradeLogsPath}'");
                foreach (DirectoryInfo logDirectoryInfo in tempDirectoryInfo.EnumerateDirectories(searchPattern: "*", searchOption: SearchOption.TopDirectoryOnly))
                {
                    Directory.Move(logDirectoryInfo.FullName, Path.Combine(upgradeLogsPath, logDirectoryInfo.Name));
                }

                this.tracer.RelatedInfo($"Moving files from '{tempUpgradeLogsPath}' to '{upgradeLogsPath}'");
                foreach (FileInfo logFileInfo in tempDirectoryInfo.EnumerateFiles(searchPattern: "*", searchOption: SearchOption.TopDirectoryOnly))
                {
                    File.Move(logFileInfo.FullName, Path.Combine(upgradeLogsPath, logFileInfo.Name));
                }

                FileSystemInfo[] remainingChildren = tempDirectoryInfo.GetFileSystemInfos();
                if (remainingChildren.Length > 0)
                {
                    this.tracer.RelatedWarning(
                        $"{nameof(this.MigrateUpgradeLogsToDirectoryWithFreshACLs)}: Skipping delete of old directory, {remainingChildren.Length} items still present on disk");
                }
                else
                {
                    PhysicalFileSystem fileSystem = new PhysicalFileSystem();
                    fileSystem.DeleteDirectory(tempUpgradeLogsPath, recursive: false);
                }
            }
            catch (Exception e)
            {
                EventMetadata metadata = new EventMetadata();
                metadata.Add("Exception", e.ToString());
                metadata.Add(nameof(tempUpgradeLogsPath), tempUpgradeLogsPath);
                metadata.Add(nameof(upgradeLogsPath), upgradeLogsPath);

                this.tracer.RelatedWarning(
                    metadata,
                    $"{nameof(this.MigrateUpgradeLogsToDirectoryWithFreshACLs)}: Caught exception migrating files from the old upgrade log directory");
            }
        }
 private void Purge(PhysicalFileSystem fs, string path)
 {
     var files = fs.GetFiles(path, "*.js");
     foreach (var file in files)
     {
         fs.DeleteFile(file);
     }
     var dirs = fs.GetDirectories(path);
     foreach (var dir in dirs)
     {
         Purge(fs, dir);
         fs.DeleteDirectory(dir);
     }
 }
Ejemplo n.º 8
0
        protected bool TryDeleteFolder(ITracer tracer, string folderName)
        {
            try
            {
                PhysicalFileSystem fileSystem = new PhysicalFileSystem();
                fileSystem.DeleteDirectory(folderName);
            }
            catch (Exception e)
            {
                tracer.RelatedError("Failed to delete folder {0}: {1}", folderName, e.ToString());
                return(true);
            }

            return(true);
        }
Ejemplo n.º 9
0
        private void Purge(PhysicalFileSystem fs, string path)
        {
            IEnumerable <string> files = fs.GetFiles(path, "*.cshtml");

            foreach (string file in files)
            {
                fs.DeleteFile(file);
            }

            IEnumerable <string> dirs = fs.GetDirectories(path);

            foreach (string dir in dirs)
            {
                Purge(fs, dir);
                fs.DeleteDirectory(dir);
            }
        }