in src/common/IO/FileSystem.cs [238:394]
public void MoveFile(string sourceFileName, string destFileName)
=> File.Move(sourceFileName, destFileName);
/// <summary>
/// <see cref="IFileSystem.MoveDirectory"/>
/// </summary>
public void MoveDirectory(string sourceDirName, string destDirName)
=> Directory.Move(sourceDirName, destDirName);
/// <summary>
/// <see cref="IFileSystem.OpenFile"/>
/// </summary>
public Stream OpenFile(string path, FileMode mode)
=> File.Open(path, mode);
/// <summary>
/// <see cref="IFileSystem.OpenFileForRead"/>
/// </summary>
public Stream OpenFileForRead(string path)
=> File.OpenRead(path);
/// <summary>
/// <see cref="IFileSystem.OpenFileForWrite"/>
/// </summary>
public Stream OpenFileForWrite(string path)
=> File.OpenWrite(path);
/// <summary>
/// <see cref="IFileSystem.ReadAllTextFromFile(string, int)"/>
/// </summary>
public string ReadAllTextFromFile(string path, int maxAttempts = 1)
=> ReadFileWithRetries(() => File.ReadAllText(path), maxAttempts);
/// <summary>
/// <see cref="IFileSystem.ReadAllTextFromFile(string, Encoding, int)"/>
/// </summary>
public string ReadAllTextFromFile(string path, Encoding encoding, int maxAttempts = 1)
=> ReadFileWithRetries(() => File.ReadAllText(path, encoding), maxAttempts);
/// <summary>
/// <see cref="IFileSystem.ReadAllBytesFromFile"/>
/// </summary>
public byte[] ReadAllBytesFromFile(string path)
=> File.ReadAllBytes(path);
/// <summary>
/// <see cref="IFileSystem.ReadAllLinesFromFile(string)"/>
/// </summary>
public string[] ReadAllLinesFromFile(string path)
=> File.ReadAllLines(path);
/// <summary>
/// <see cref="IFileSystem.ReadAllLinesFromFile(string, Encoding)"/>
/// </summary>
public string[] ReadAllLinesFromFile(string path, Encoding encoding)
=> File.ReadAllLines(path, encoding);
/// <summary>
/// <see cref="IFileSystem.SetFileLastWriteTimeUtc"/>
/// </summary>
public void SetFileLastWriteTimeUtc(string path, DateTime dateTime)
=> File.SetLastWriteTimeUtc(path, dateTime);
/// <summary>
/// <see cref="IFileSystem.SetAccessPermissions"/>
/// </summary>
public void SetAccessPermissions(string path, FileSystemRights userAccess, Action<string> logCallback, CancellationToken cancellationToken, string loggedOnUserName = null)
{
if (!this.FileExists(path) && !this.DirectoryExists(path))
{
return;
}
if (this._platform.IsOSX || this._platform.IsLinux)
{
if (!string.IsNullOrWhiteSpace(loggedOnUserName))
{
// Set the logged-on user to be the owner of the file
string chownExecutablePath = _platform.IsOSX ? "/usr/sbin/chown" : "chown";
int chownExitCode = _platform.Execute(
executable: chownExecutablePath,
command: $"\"{loggedOnUserName}\" \"{path}\"",
logCallback: logCallback,
envVariables: null,
timeout: TimeSpan.FromSeconds(10),
cancellationToken: cancellationToken,
out string chownOutput);
if (chownExitCode != 0)
{
throw new InvalidOperationException($"'{chownExecutablePath} \"{loggedOnUserName}\" \"{path}\"' returned exit code '{chownExitCode}'. Output: '{chownOutput}'");
}
}
// Give owner the specified access permissions
var userMode = string.Empty;
if ((userAccess & FileSystemRights.Read) != 0)
{
userMode += "r";
}
if ((userAccess & FileSystemRights.Write) != 0)
{
userMode += "w";
}
if ((userAccess & FileSystemRights.ExecuteFile) != 0)
{
userMode += "x";
}
// By default, remove all permissions from "group" and "other"
var mode = string.IsNullOrEmpty(userMode) ? "go-rwx" : $"go-rwx,u={userMode}";
string chmodExecutablePath = _platform.IsOSX ? "/bin/chmod" : "chmod";
int chmodExitCode = _platform.Execute(
executable: chmodExecutablePath,
command: $"{mode} \"{path}\"",
logCallback: logCallback,
envVariables: null,
timeout: TimeSpan.FromSeconds(10),
cancellationToken: cancellationToken,
out string chmodOutput);
if (chmodExitCode != 0)
{
throw new InvalidOperationException($"'{chmodExecutablePath} {mode} \"{path}\"' returned exit code '{chmodExitCode}'. Output: '{chmodOutput}'");
}
}
else if (this._platform.IsWindows)
{
FileSystemSecurity rules;
FileSystemInfo info;
if (this.FileExists(path))
{
rules = new FileSecurity();
info = new FileInfo(path);
}
else
{
rules = new DirectorySecurity();
info = new DirectoryInfo(path);
}
var builtInAdmin = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null).Translate(typeof(NTAccount));
loggedOnUserName = string.IsNullOrWhiteSpace(loggedOnUserName) ? WindowsIdentity.GetCurrent().Name : loggedOnUserName;
var currentUser = new NTAccount(loggedOnUserName);
rules.AddAccessRule(new FileSystemAccessRule(builtInAdmin, FileSystemRights.FullControl, AccessControlType.Allow));
rules.AddAccessRule(new FileSystemAccessRule(currentUser, userAccess, AccessControlType.Allow));
rules.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); // Disable rules inherited from the parent directory
FileSystemAclExtensions.SetAccessControl((dynamic)info, (dynamic)rules);
}
else
{
throw new NotSupportedException($"Unrecognized platform. Cannot set permissions on path '{path}'.");
}
}