From 65f15f9c5a86b7472e8f230d5fadcf5e988ec02d Mon Sep 17 00:00:00 2001 From: Steven Lizano Date: Mon, 29 Jul 2024 14:32:06 -0600 Subject: [PATCH] fix failing on windows OS --- .../Configuration/EasyLoggingConfigParser.cs | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs b/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs index ab30c4cd5..42278792c 100644 --- a/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs +++ b/Snowflake.Data/Configuration/EasyLoggingConfigParser.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; using System.Security; using System.Text; using Microsoft.IdentityModel.Tokens; @@ -52,19 +53,18 @@ private string TryToReadFile(string filePath) try { - FileAccessPermissions forbiddenPermissions = FileAccessPermissions.OtherWrite | FileAccessPermissions.GroupWrite; - var fileInfo = new UnixFileInfo(path: filePath); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + using var streamReader = new StreamReader(filePath, Encoding.Default); + return streamReader.ReadToEnd(); + } + else + { + var handle = VerifyUnixPermissions(filePath); - using var handle = fileInfo.OpenRead(); - if (handle.OwnerUser.UserId != Syscall.geteuid()) - throw new SnowflakeDbException(SFError.INTERNAL_ERROR,"Attempting to read a file not owned by the effective user of the current process"); - if (handle.OwnerGroup.GroupId != Syscall.getegid()) - throw new SnowflakeDbException(SFError.INTERNAL_ERROR,"Attempting to read a file not owned by the effective group of the current process"); - if ((handle.FileAccessPermissions & forbiddenPermissions) != 0) - throw new SnowflakeDbException( SFError.INTERNAL_ERROR,"Attempting to read a file with too broad permissions assigned"); - - using var streamReader = new StreamReader(handle, Encoding.Default); - return streamReader.ReadToEnd(); + using var streamReader = new StreamReader(handle, Encoding.Default); + return streamReader.ReadToEnd(); + } } catch (Exception e) { @@ -74,6 +74,21 @@ private string TryToReadFile(string filePath) } } + private static UnixStream VerifyUnixPermissions(string filePath) + { + FileAccessPermissions forbiddenPermissions = FileAccessPermissions.OtherWrite | FileAccessPermissions.GroupWrite; + var fileInfo = new UnixFileInfo(path: filePath); + + using var handle = fileInfo.OpenRead(); + if (handle.OwnerUser.UserId != Syscall.geteuid()) + throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Attempting to read a file not owned by the effective user of the current process"); + if (handle.OwnerGroup.GroupId != Syscall.getegid()) + throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Attempting to read a file not owned by the effective group of the current process"); + if ((handle.FileAccessPermissions & forbiddenPermissions) != 0) + throw new SnowflakeDbException(SFError.INTERNAL_ERROR, "Attempting to read a file with too broad permissions assigned"); + return handle; + } + private ClientConfig TryToParseFile(string fileContent) { try