private Attempt <CodeFileDisplay> UpdateFile(CodeFileDisplay file)
        {
            // if the file is an appsettings.json file, save a backup of the original.
            if (file.VirtualPath.InvariantEndsWith("appsettings.json"))
            {
                _fileSystem.CopyFile(file.VirtualPath, $"{file.VirtualPath}.{DateTime.Now:yyyyMMdd-HHmmss}");
            }

            //            file.VirtualPath = file.VirtualPath.EnsureCorrectFileExtension(configFileExtension);
            //            file.Name = file.Name.EnsureCorrectFileExtension(configFileExtension);

            //if (!Path.GetFileNameWithoutExtension(file.VirtualPath).InvariantEquals(Path.GetFileNameWithoutExtension(file.Name)))
            //{
            //    _themesFileSystem.DeleteFile(file.VirtualPath);
            //    string[] strArray = file.VirtualPath.Split('/');
            //    file.VirtualPath = string.Join("/", ((IEnumerable<string>)strArray).Take(strArray.Length - 1)).EnsureEndsWith('/') + file.Name;
            //}

            Attempt <CodeFileDisplay> attempt;

            try
            {
                using (MemoryStream memoryStream = new())
                {
                    using StreamWriter streamWriter = new(memoryStream);
                    streamWriter.Write(file.Content);
                    streamWriter.Flush();
                    _fileSystem.AddFile(file.VirtualPath.TrimStart('/'), memoryStream, true);
                }
                attempt = Attempt <CodeFileDisplay> .Succeed(file);
            } catch (IOException ioe)
            {
                _logger.LogError(ioe, "Could not save file {file}", file.Path);
                attempt = Attempt <CodeFileDisplay> .Fail();
            }

            return(attempt);
        }
示例#2
0
        private static bool TryCopyNativeLibToAppDirectory(ITracer tracer, PhysicalFileSystem fileSystem, string gvfsAppDirectory)
        {
            string installFilePath;
            string appFilePath;

            GetNativeLibPaths(gvfsAppDirectory, out installFilePath, out appFilePath);

            EventMetadata pathMetadata = CreateEventMetadata();

            pathMetadata.Add(nameof(gvfsAppDirectory), gvfsAppDirectory);
            pathMetadata.Add(nameof(installFilePath), installFilePath);
            pathMetadata.Add(nameof(appFilePath), appFilePath);

            if (fileSystem.FileExists(installFilePath))
            {
                tracer.RelatedEvent(EventLevel.Informational, $"{nameof(TryCopyNativeLibToAppDirectory)}_CopyingNativeLib", pathMetadata);

                try
                {
                    fileSystem.CopyFile(installFilePath, appFilePath, overwrite: true);

                    try
                    {
                        Common.NativeMethods.FlushFileBuffers(appFilePath);
                    }
                    catch (Win32Exception e)
                    {
                        EventMetadata metadata = CreateEventMetadata(e);
                        metadata.Add(nameof(appFilePath), appFilePath);
                        metadata.Add(nameof(installFilePath), installFilePath);
                        tracer.RelatedWarning(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: Win32Exception while trying to flush file buffers", Keywords.Telemetry);
                    }
                }
                catch (UnauthorizedAccessException e)
                {
                    EventMetadata metadata = CreateEventMetadata(e);
                    tracer.RelatedError(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: UnauthorizedAccessException caught while trying to copy native lib");
                    return(false);
                }
                catch (DirectoryNotFoundException e)
                {
                    EventMetadata metadata = CreateEventMetadata(e);
                    tracer.RelatedError(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: DirectoryNotFoundException caught while trying to copy native lib");
                    return(false);
                }
                catch (FileNotFoundException e)
                {
                    EventMetadata metadata = CreateEventMetadata(e);
                    tracer.RelatedError(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: FileNotFoundException caught while trying to copy native lib");
                    return(false);
                }
                catch (IOException e)
                {
                    EventMetadata metadata = CreateEventMetadata(e);
                    tracer.RelatedWarning(metadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: IOException caught while trying to copy native lib");

                    if (fileSystem.FileExists(appFilePath))
                    {
                        tracer.RelatedWarning(
                            CreateEventMetadata(),
                            "Could not copy native lib to app directory, but file already exists, continuing with install",
                            Keywords.Telemetry);
                    }
                    else
                    {
                        tracer.RelatedError($"{nameof(TryCopyNativeLibToAppDirectory)}: Failed to copy native lib to app directory");
                        return(false);
                    }
                }
            }
            else
            {
                tracer.RelatedError(pathMetadata, $"{nameof(TryCopyNativeLibToAppDirectory)}: Native lib does not exist in install directory");
                return(false);
            }

            return(true);
        }
示例#3
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);
            }
        }
示例#4
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);
            }
        }