diff --git a/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs b/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs index bcbef3e97..7d1de4930 100644 --- a/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Configuration/EasyLoggingConfigFinderTest.cs @@ -141,7 +141,6 @@ public void TestThatConfigFileIsNotUsedIfOthersCanModifyTheConfigFile() // assert Assert.IsNotNull(thrown); Assert.AreEqual(thrown.Message, $"Error due to other users having permission to modify the config file: {s_homeConfigFilePath}"); - t_unixOperations.Verify(u => u.SetFileInfo(s_homeConfigFilePath), Times.Once); } [Test] @@ -209,7 +208,7 @@ private static void MockDirectoriesExist() private static void MockHasFlagReturnsTrue() { t_unixOperations - .Setup(f => f.CheckFileHasPermissions( + .Setup(f => f.CheckFileHasPermissions(s_homeConfigFilePath, It.Is(p => p.Equals(FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite)))) .Returns(true); } diff --git a/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs b/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs index 30aef1ede..a0301e835 100644 --- a/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs +++ b/Snowflake.Data.Tests/UnitTests/Session/EasyLoggingStarterTest.cs @@ -140,7 +140,7 @@ public void TestThatDoesNotFailWhenDirectoryPermissionIsNot600Or700() .Setup(dir => dir.Exists(ConfigPath)) .Returns(false); t_unixOperations - .Setup(unix => unix.GetDirPermissions()) + .Setup(unix => unix.GetDirPermissions(s_expectedLogPath)) .Returns(FileAccessPermissions.AllPermissions); // act @@ -149,7 +149,6 @@ public void TestThatDoesNotFailWhenDirectoryPermissionIsNot600Or700() // assert t_unixOperations.Verify(u => u.CreateDirectoryWithPermissions(s_expectedLogPath, FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR), Times.Once); - t_unixOperations.Verify(u => u.SetDirInfo(s_expectedLogPath), Times.Once); } [Test] diff --git a/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs b/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs index 27fa3a88e..dbc6820b4 100644 --- a/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs +++ b/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs @@ -70,8 +70,7 @@ private void Validate(ClientConfig config) private void CheckForUnknownFields(string fileContent) { // Parse the specified config file and get the key-value pairs from the "common" section - List knownProperties = new List(); - knownProperties = typeof(ClientConfigCommonProps).GetProperties() + List knownProperties = typeof(ClientConfigCommonProps).GetProperties() .Select(property => property.GetCustomAttribute().PropertyName) .ToList(); diff --git a/Snowflake.Data/Core/Session/EasyLoggingStarter.cs b/Snowflake.Data/Core/Session/EasyLoggingStarter.cs index bd0a6eac0..17d6ed26e 100644 --- a/Snowflake.Data/Core/Session/EasyLoggingStarter.cs +++ b/Snowflake.Data/Core/Session/EasyLoggingStarter.cs @@ -129,6 +129,10 @@ private string GetLogPath(string logPath) } else { + if (!Directory.Exists(logPathOrDefault)) + { + Directory.CreateDirectory(logPathOrDefault); + } _unixOperations.CreateDirectoryWithPermissions(pathWithDotnetSubdirectory, FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR); CheckDirPermissionsOnlyAllowUser(pathWithDotnetSubdirectory); @@ -143,8 +147,7 @@ private void CheckDirPermissionsOnlyAllowUser(string dirPath) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return; - _unixOperations.SetDirInfo(dirPath); - var dirPermissions = _unixOperations.GetDirPermissions(); + var dirPermissions = _unixOperations.GetDirPermissions(dirPath); if (dirPermissions != FileAccessPermissions.UserReadWriteExecute && dirPermissions != (FileAccessPermissions.UserRead | FileAccessPermissions.UserExecute)) { diff --git a/Snowflake.Data/Core/Tools/UnixOperations.cs b/Snowflake.Data/Core/Tools/UnixOperations.cs index dd044d180..d0e1fdf36 100644 --- a/Snowflake.Data/Core/Tools/UnixOperations.cs +++ b/Snowflake.Data/Core/Tools/UnixOperations.cs @@ -4,7 +4,6 @@ using Mono.Unix; using Mono.Unix.Native; -using System.IO; namespace Snowflake.Data.Core.Tools { @@ -12,37 +11,21 @@ internal class UnixOperations { public static readonly UnixOperations Instance = new UnixOperations(); - private UnixFileInfo _unixFileInfo; - private UnixDirectoryInfo _unixDirInfo; - - public virtual void SetDirInfo(string path) - { - _unixDirInfo = new UnixDirectoryInfo(path); - } - public virtual void CreateDirectoryWithPermissions(string path, FilePermissions permissions) { - string subPath = Path.GetDirectoryName(path); - if (!Directory.Exists(subPath)) - { - Directory.CreateDirectory(subPath); - } Syscall.mkdir(path, permissions); } - public virtual FileAccessPermissions GetDirPermissions() - { - return _unixDirInfo.FileAccessPermissions; - } - - public virtual void SetFileInfo(string path) + public virtual FileAccessPermissions GetDirPermissions(string path) { - _unixFileInfo = new UnixFileInfo(path); + var dirInfo = new UnixDirectoryInfo(path); + return dirInfo.FileAccessPermissions; } - public virtual bool CheckFileHasPermissions(FileAccessPermissions permissions) + public virtual bool CheckFileHasPermissions(string path, FileAccessPermissions permissions) { - return _unixFileInfo.FileAccessPermissions.HasFlag(permissions); + var fileInfo = new UnixFileInfo(path); + return fileInfo.FileAccessPermissions.HasFlag(permissions); } } }