Ejemplo n.º 1
0
        public void DeleteReadonlyFile()
        {
            // Arrange
            var root   = Path.Combine(Path.GetTempPath());
            var path   = Path.GetRandomFileName();
            var target = new PhysicalFileSystem(root);

            using (var memStream = new MemoryStream(
                       System.Text.Encoding.UTF8.GetBytes("hello")))
            {
                target.AddFile(path, memStream);
            }

            // Make the file read-only
            var fullPath = Path.Combine(root, path);

            File.SetAttributes(fullPath, File.GetAttributes(fullPath) | FileAttributes.ReadOnly);
            Assert.True(File.GetAttributes(fullPath).HasFlag(FileAttributes.ReadOnly));

            // Act
            target.DeleteFile(path);

            // Assert
            Assert.True(!File.Exists(fullPath));
        }
Ejemplo n.º 2
0
        public void TearDown()
        {
            _fileSystem = null;
            //Delete all files
            var fs    = new PhysicalFileSystem(SystemDirectories.Scripts);
            var files = fs.GetFiles("", "*.js");

            foreach (var file in files)
            {
                fs.DeleteFile(file);
            }
        }
        public override void TearDown()
        {
            base.TearDown();

            _masterPageFileSystem = null;
            _viewsFileSystem      = null;
            //Delete all files
            var fsMaster    = new PhysicalFileSystem(SystemDirectories.Masterpages);
            var masterPages = fsMaster.GetFiles("", "*.master");

            foreach (var file in masterPages)
            {
                fsMaster.DeleteFile(file);
            }
            var fsViews = new PhysicalFileSystem(SystemDirectories.MvcViews);
            var views   = fsMaster.GetFiles("", "*.cshtml");

            foreach (var file in views)
            {
                fsMaster.DeleteFile(file);
            }
        }
        public void TearDown()
        {
            var testHelper = new TestHelper();

            // Delete all files
            var fsViews = new PhysicalFileSystem(IOHelper, HostingEnvironment, LoggerFactory.CreateLogger <PhysicalFileSystem>(), HostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.MvcViews), HostingEnvironment.ToAbsolute(Constants.SystemDirectories.MvcViews));
            IEnumerable <string> views = fsViews.GetFiles(string.Empty, "*.cshtml");

            foreach (string file in views)
            {
                fsViews.DeleteFile(file);
            }
        }
 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);
     }
 }
        public override void TearDown()
        {
            base.TearDown();

            _fileSystems = null;

            //Delete all files
            var fsViews = new PhysicalFileSystem(SystemDirectories.MvcViews);
            var views   = fsViews.GetFiles("", "*.cshtml");

            foreach (var file in views)
            {
                fsViews.DeleteFile(file);
            }
        }
Ejemplo n.º 7
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);
            }
        }
        public override bool TryUpgrade(ITracer tracer, string enlistmentRoot)
        {
            ModifiedPathsDatabase modifiedPaths = null;

            try
            {
                PhysicalFileSystem fileSystem = new PhysicalFileSystem();

                string modifiedPathsDatabasePath = Path.Combine(enlistmentRoot, GVFSConstants.DotGVFS.Root, GVFSConstants.DotGVFS.Databases.ModifiedPaths);
                string error;
                if (!ModifiedPathsDatabase.TryLoadOrCreate(tracer, modifiedPathsDatabasePath, fileSystem, out modifiedPaths, out error))
                {
                    tracer.RelatedError($"Unable to create the modified paths database. {error}");
                    return(false);
                }

                string sparseCheckoutPath = Path.Combine(enlistmentRoot, GVFSConstants.WorkingDirectoryRootName, GVFSConstants.DotGit.Info.SparseCheckoutPath);
                bool   isRetryable;
                using (FileStream fs = File.OpenRead(sparseCheckoutPath))
                    using (StreamReader reader = new StreamReader(fs))
                    {
                        string entry = reader.ReadLine();
                        while (entry != null)
                        {
                            entry = entry.Trim();
                            if (!string.IsNullOrWhiteSpace(entry))
                            {
                                bool isFolder = entry.EndsWith(GVFSConstants.GitPathSeparatorString);
                                if (!modifiedPaths.TryAdd(entry.Trim(GVFSConstants.GitPathSeparator), isFolder, out isRetryable))
                                {
                                    tracer.RelatedError("Unable to add to the modified paths database.");
                                    return(false);
                                }
                            }

                            entry = reader.ReadLine();
                        }
                    }

                string alwaysExcludePath = Path.Combine(enlistmentRoot, GVFSConstants.WorkingDirectoryRootName, GVFSConstants.DotGit.Info.AlwaysExcludePath);
                if (fileSystem.FileExists(alwaysExcludePath))
                {
                    string alwaysExcludeData = fileSystem.ReadAllText(alwaysExcludePath);

                    char[] carriageReturnOrLineFeed = new[] { '\r', '\n' };
                    int    endPosition = alwaysExcludeData.Length;
                    while (endPosition > 0)
                    {
                        int startPosition = alwaysExcludeData.LastIndexOfAny(carriageReturnOrLineFeed, endPosition - 1);
                        if (startPosition < 0)
                        {
                            startPosition = 0;
                        }

                        string entry = alwaysExcludeData.Substring(startPosition, endPosition - startPosition).Trim();

                        if (entry.EndsWith("*"))
                        {
                            // This is the first entry using the old format and we don't want to process old entries
                            // because we would need folder entries since there isn't a file and that would cause sparse-checkout to
                            // recursively clear skip-worktree bits for everything under that folder
                            break;
                        }

                        // Substring will not return a null and the Trim will get rid of all the whitespace
                        // if there is a length it will be a valid path that we need to process
                        if (entry.Length > 0)
                        {
                            entry = entry.TrimStart('!');
                            bool isFolder = entry.EndsWith(GVFSConstants.GitPathSeparatorString);
                            if (!isFolder)
                            {
                                if (!modifiedPaths.TryAdd(entry.Trim(GVFSConstants.GitPathSeparator), isFolder, out isRetryable))
                                {
                                    tracer.RelatedError("Unable to add to the modified paths database.");
                                    return(false);
                                }
                            }
                        }

                        endPosition = startPosition;
                    }
                }

                modifiedPaths.ForceFlush();
                fileSystem.WriteAllText(sparseCheckoutPath, "/.gitattributes" + Environment.NewLine);
                fileSystem.DeleteFile(alwaysExcludePath);
            }
            catch (IOException ex)
            {
                tracer.RelatedError($"IOException: {ex.ToString()}");
                return(false);
            }
            finally
            {
                if (modifiedPaths != null)
                {
                    modifiedPaths.Dispose();
                    modifiedPaths = null;
                }
            }

            if (!this.TryIncrementMajorVersion(tracer, enlistmentRoot))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 9
0
        public void DeleteReadonlyFile()
        {
            // Arrange
            var root = Path.Combine(Path.GetTempPath());
            var path = Path.GetRandomFileName();
            var target = new PhysicalFileSystem(root);
            using (var memStream = new MemoryStream(
                System.Text.Encoding.UTF8.GetBytes("hello")))
            {
                target.AddFile(path, memStream);
            }

            // Make the file read-only
            var fullPath = Path.Combine(root, path);
            File.SetAttributes(fullPath, File.GetAttributes(fullPath) | FileAttributes.ReadOnly);
            Assert.True(File.GetAttributes(fullPath).HasFlag(FileAttributes.ReadOnly));

            // Act
            target.DeleteFile(path);

            // Assert
            Assert.True(!File.Exists(fullPath));
        }
Ejemplo n.º 10
0
        public void TestFileExceptions()
        {
            var fs       = new PhysicalFileSystem();
            var path     = fs.ConvertPathFromInternal(SystemPath);
            var fileName = $"toto-{Guid.NewGuid()}.txt";
            var filePath = path / fileName;

            fs.CreateFile(filePath).Dispose();
            var filePathNotExist = path / "FileDoesNotExist.txt";
            var systemFilePath   = Path.Combine(SystemPath, fileName);

            // Try to create a folder on an unauthorized location
            try
            {
                // CreateFile
                Assert.Throws <UnauthorizedAccessException>(() => fs.CreateFile("/toto.txt"));

                // Length
                Assert.Throws <UnauthorizedAccessException>(() => fs.GetFileLength("/toto.txt"));

                // ConvertPathFromInternal / ConvertPathToInternal
                Assert.Throws <NotSupportedException>(() => fs.ConvertPathFromInternal(@"\\network\toto.txt"));
                Assert.Throws <NotSupportedException>(() => fs.ConvertPathFromInternal(@"zx:\toto.txt"));

                Assert.Throws <ArgumentException>(() => fs.ConvertPathToInternal(@"/toto.txt"));
                Assert.Throws <ArgumentException>(() => fs.ConvertPathToInternal(@"/mnt/yo/toto.txt"));

                // LastWriteTime, LastAccessTime, CreationTime
                Assert.Throws <DirectoryNotFoundException>(() => fs.GetLastWriteTime("/toto.txt"));
                Assert.Throws <DirectoryNotFoundException>(() => fs.GetLastAccessTime("/toto.txt"));
                Assert.Throws <DirectoryNotFoundException>(() => fs.GetCreationTime("/toto.txt"));

                Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastWriteTime("/", DateTime.Now));
                Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastAccessTime("/", DateTime.Now));
                Assert.Throws <UnauthorizedAccessException>(() => fs.SetCreationTime("/", DateTime.Now));

                Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastWriteTime("/mnt", DateTime.Now));
                Assert.Throws <UnauthorizedAccessException>(() => fs.SetLastAccessTime("/mnt", DateTime.Now));
                Assert.Throws <UnauthorizedAccessException>(() => fs.SetCreationTime("/mnt", DateTime.Now));

                Assert.Throws <DirectoryNotFoundException>(() => fs.SetLastWriteTime("/toto.txt", DateTime.Now));
                Assert.Throws <DirectoryNotFoundException>(() => fs.SetLastAccessTime("/toto.txt", DateTime.Now));
                Assert.Throws <DirectoryNotFoundException>(() => fs.SetCreationTime("/toto.txt", DateTime.Now));

                // FileAttributes
                Assert.Throws <DirectoryNotFoundException>(() => fs.GetAttributes("/toto.txt"));
                Assert.Throws <DirectoryNotFoundException>(() => fs.SetAttributes("/toto.txt", FileAttributes.ReadOnly));
                Assert.Throws <UnauthorizedAccessException>(() => fs.SetAttributes("/", FileAttributes.ReadOnly));

                // CopyFile
                Assert.Throws <UnauthorizedAccessException>(() => fs.CopyFile("/toto.txt", filePath, true));
                Assert.Throws <UnauthorizedAccessException>(() => fs.CopyFile(filePath, "/toto.txt", true));

                // Delete
                Assert.Throws <UnauthorizedAccessException>(() => fs.DeleteFile("/toto.txt"));

                // Move
                Assert.Throws <UnauthorizedAccessException>(() => fs.MoveFile("/toto.txt", filePath));
                Assert.Throws <UnauthorizedAccessException>(() => fs.MoveFile(filePath, "/toto.txt"));

                // ReplaceFile
                Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile("/toto.txt", filePath, filePath, true));
                Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile(filePath, "/toto.txt", filePath, true));
                Assert.Throws <UnauthorizedAccessException>(() => fs.ReplaceFile(filePath, filePath, "/toto.txt", true));

                Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile(filePathNotExist, filePath, filePath, true));
                Assert.Throws <FileNotFoundException>(() => fs.ReplaceFile(filePath, filePathNotExist, filePath, true));
            }
            finally
            {
                SafeDeleteFile(systemFilePath);
            }
        }
Ejemplo n.º 11
0
        public void TestFile()
        {
            var fs                 = new PhysicalFileSystem();
            var path               = fs.ConvertPathFromInternal(SystemPath);
            var fileName           = $"toto-{Guid.NewGuid()}.txt";
            var filePath           = path / fileName;
            var filePathDest       = path / Path.ChangeExtension(fileName, "dest");
            var filePathBack       = path / Path.ChangeExtension(fileName, "bak");
            var systemFilePath     = Path.Combine(SystemPath, fileName);
            var systemFilePathDest = fs.ConvertPathToInternal(filePathDest);
            var systemFilePathBack = fs.ConvertPathToInternal(filePathBack);

            try
            {
                // CreateFile / OpenFile
                var fileStream = fs.CreateFile(filePath);
                var buffer     = Encoding.UTF8.GetBytes("This is a test");
                fileStream.Write(buffer, 0, buffer.Length);
                fileStream.Dispose();

                // FileLength
                Assert.Equal(buffer.Length, fs.GetFileLength(filePath));

                // LastAccessTime
                // LastWriteTime
                // CreationTime
                Assert.Equal(File.GetLastWriteTime(systemFilePath), fs.GetLastWriteTime(filePath));
                Assert.Equal(File.GetLastAccessTime(systemFilePath), fs.GetLastAccessTime(filePath));
                Assert.Equal(File.GetCreationTime(systemFilePath), fs.GetCreationTime(filePath));

                var lastWriteTime  = DateTime.Now + TimeSpan.FromSeconds(10);
                var lastAccessTime = DateTime.Now + TimeSpan.FromSeconds(11);
                var creationTime   = DateTime.Now + TimeSpan.FromSeconds(12);
                fs.SetLastWriteTime(filePath, lastWriteTime);
                fs.SetLastAccessTime(filePath, lastAccessTime);
                fs.SetCreationTime(filePath, creationTime);
                Assert.Equal(lastWriteTime, fs.GetLastWriteTime(filePath));
                Assert.Equal(lastAccessTime, fs.GetLastAccessTime(filePath));
                Assert.Equal(creationTime, fs.GetCreationTime(filePath));

                // FileAttributes
                Assert.Equal(File.GetAttributes(systemFilePath), fs.GetAttributes(filePath));

                var attributes = fs.GetAttributes(filePath);
                attributes |= FileAttributes.ReadOnly;
                fs.SetAttributes(filePath, attributes);

                Assert.Equal(File.GetAttributes(systemFilePath), fs.GetAttributes(filePath));

                attributes &= ~FileAttributes.ReadOnly;
                fs.SetAttributes(filePath, attributes);
                Assert.Equal(File.GetAttributes(systemFilePath), fs.GetAttributes(filePath));

                // FileExists
                Assert.True(File.Exists(systemFilePath));
                Assert.True(fs.FileExists(filePath));

                // CopyFile
                fs.CopyFile(filePath, filePathDest, true);
                Assert.True(File.Exists(systemFilePathDest));
                Assert.True(fs.FileExists(filePathDest));

                // DeleteFile
                fs.DeleteFile(filePath);
                Assert.False(File.Exists(systemFilePath));
                Assert.False(fs.FileExists(filePath));

                // MoveFile
                fs.MoveFile(filePathDest, filePath);
                Assert.False(File.Exists(systemFilePathDest));
                Assert.False(fs.FileExists(filePathDest));
                Assert.True(File.Exists(systemFilePath));
                Assert.True(fs.FileExists(filePath));

                // ReplaceFile

                // copy file to filePathDest
                fs.CopyFile(filePath, filePathDest, true);

                // Change src file
                var filestream2 = fs.OpenFile(filePath, FileMode.Open, FileAccess.ReadWrite);
                var buffer2     = Encoding.UTF8.GetBytes("This is a test 123");
                filestream2.Write(buffer2, 0, buffer2.Length);
                filestream2.Dispose();
                Assert.Equal(buffer2.Length, fs.GetFileLength(filePath));

                // Perform ReplaceFile
                fs.ReplaceFile(filePath, filePathDest, filePathBack, true);
                Assert.False(fs.FileExists(filePath));
                Assert.True(fs.FileExists(filePathDest));
                Assert.True(fs.FileExists(filePathBack));

                Assert.Equal(buffer2.Length, fs.GetFileLength(filePathDest));
                Assert.Equal(buffer.Length, fs.GetFileLength(filePathBack));

                // RootFileSystem
                fs.GetLastWriteTime("/");
                fs.GetLastAccessTime("/");
                fs.GetCreationTime("/");

                fs.GetLastWriteTime("/mnt");
                fs.GetLastAccessTime("/mnt");
                fs.GetCreationTime("/mnt");

                fs.GetLastWriteTime("/mnt/c");
                fs.GetLastAccessTime("/mnt/c");
                fs.GetCreationTime("/mnt/c");
                fs.GetAttributes("/mnt/c");

                var sysAttr = FileAttributes.Directory | FileAttributes.System | FileAttributes.ReadOnly;
                Assert.True((fs.GetAttributes("/") & (sysAttr)) == sysAttr);
                Assert.True((fs.GetAttributes("/mnt") & (sysAttr)) == sysAttr);
            }
            finally
            {
                SafeDeleteFile(systemFilePath);
                SafeDeleteFile(systemFilePathDest);
                SafeDeleteFile(systemFilePathBack);
            }
        }
Ejemplo n.º 12
0
        public override bool TryUpgrade(ITracer tracer, string enlistmentRoot)
        {
            ModifiedPathsDatabase modifiedPaths = null;

            try
            {
                PhysicalFileSystem fileSystem = new PhysicalFileSystem();

                string modifiedPathsDatabasePath = Path.Combine(enlistmentRoot, GVFSConstants.DotGVFS.Root, GVFSConstants.DotGVFS.Databases.ModifiedPaths);
                string error;
                if (!ModifiedPathsDatabase.TryLoadOrCreate(tracer, modifiedPathsDatabasePath, fileSystem, out modifiedPaths, out error))
                {
                    tracer.RelatedError($"Unable to create the modified paths database. {error}");
                    return(false);
                }

                string sparseCheckoutPath = Path.Combine(enlistmentRoot, GVFSConstants.WorkingDirectoryRootName, GVFSConstants.DotGit.Info.SparseCheckoutPath);
                IEnumerable <string> sparseCheckoutLines = fileSystem.ReadAllText(sparseCheckoutPath).Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                bool isRetryable;
                foreach (string entry in sparseCheckoutLines)
                {
                    bool isFolder = entry.EndsWith(GVFSConstants.GitPathSeparatorString);
                    if (!modifiedPaths.TryAdd(entry.Trim(GVFSConstants.GitPathSeparator), isFolder, out isRetryable))
                    {
                        tracer.RelatedError("Unable to add to the modified paths database.");
                        return(false);
                    }
                }

                string alwaysExcludePath = Path.Combine(enlistmentRoot, GVFSConstants.WorkingDirectoryRootName, GVFSConstants.DotGit.Info.AlwaysExcludePath);
                if (fileSystem.FileExists(alwaysExcludePath))
                {
                    string[] alwaysExcludeLines = fileSystem.ReadAllText(alwaysExcludePath).Split(new string[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = alwaysExcludeLines.Length - 1; i >= 0; i--)
                    {
                        string entry = alwaysExcludeLines[i];
                        if (entry.EndsWith("*"))
                        {
                            // This is the first entry using the old format and we don't want to process old entries
                            // because we would need folder entries since there isn't a file and that would cause sparse-checkout to
                            // recursively clear skip-worktree bits for everything under that folder
                            break;
                        }

                        entry = entry.TrimStart('!');
                        bool isFolder = entry.EndsWith(GVFSConstants.GitPathSeparatorString);
                        if (!isFolder)
                        {
                            if (!modifiedPaths.TryAdd(entry.Trim(GVFSConstants.GitPathSeparator), isFolder, out isRetryable))
                            {
                                tracer.RelatedError("Unable to add to the modified paths database.");
                                return(false);
                            }
                        }
                    }
                }

                modifiedPaths.ForceFlush();
                fileSystem.WriteAllText(sparseCheckoutPath, "/.gitattributes" + Environment.NewLine);
                fileSystem.DeleteFile(alwaysExcludePath);
            }
            catch (IOException ex)
            {
                tracer.RelatedError($"IOException: {ex.ToString()}");
                return(false);
            }
            finally
            {
                if (modifiedPaths != null)
                {
                    modifiedPaths.Dispose();
                    modifiedPaths = null;
                }
            }

            if (!this.TryIncrementMajorVersion(tracer, enlistmentRoot))
            {
                return(false);
            }

            return(true);
        }