Skip to content

Commit

Permalink
Refactor UnixOperations and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Jan 30, 2024
1 parent 0f266fc commit a5be03f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<FileAccessPermissions>(p => p.Equals(FileAccessPermissions.GroupWrite | FileAccessPermissions.OtherWrite))))
.Returns(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions Snowflake.Data/Configuration/EasyLoggingConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> knownProperties = new List<string>();
knownProperties = typeof(ClientConfigCommonProps).GetProperties()
List<string> knownProperties = typeof(ClientConfigCommonProps).GetProperties()
.Select(property => property.GetCustomAttribute<JsonPropertyAttribute>().PropertyName)
.ToList();

Expand Down
7 changes: 5 additions & 2 deletions Snowflake.Data/Core/Session/EasyLoggingStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
{
Expand Down
29 changes: 6 additions & 23 deletions Snowflake.Data/Core/Tools/UnixOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,28 @@

using Mono.Unix;
using Mono.Unix.Native;
using System.IO;

namespace Snowflake.Data.Core.Tools
{
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);
}
}
}

0 comments on commit a5be03f

Please sign in to comment.