Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions FileSystem/DirectoryIOLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
AllowWritesOnDirectory(path);
TryAllowWritesOnDirectory(path);
Directory.Delete(path, true);
}
}
Expand Down Expand Up @@ -166,8 +166,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
{
string sourcePath = source.TrimEnd('\\', ' ');
string targetPath = target.TrimEnd('\\', ' ');
AllowWritesOnDirectory(sourcePath);
AllowWritesOnDirectory(targetPath);
TryAllowWritesOnDirectory(sourcePath);
TryAllowWritesOnDirectory(targetPath);
var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
.GroupBy(s => Path.GetDirectoryName(s));

Expand Down Expand Up @@ -195,8 +195,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
/// <inheritdoc />
public void Unzip(string zipPath, string destinationDir)
{
AllowWritesOnDirectory(zipPath);
AllowWritesOnDirectory(destinationDir);
TryAllowWritesOnDirectory(zipPath);
TryAllowWritesOnDirectory(destinationDir);
ZipFile.ExtractToDirectory(zipPath, destinationDir);
}

Expand Down Expand Up @@ -233,6 +233,20 @@ public void AllowWritesOnDirectory(string path)
}
}

/// <inheritdoc />
public bool TryAllowWritesOnDirectory(string path)
{
try
{
AllowWritesOnDirectory(path);
return true;
}
catch
{
return false;
}
}

public IDirectoryInfoIO CreateDirectory(string path)
{
var dir = Directory.CreateDirectory(path);
Expand Down
24 changes: 19 additions & 5 deletions FileSystem/DirectoryIOWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
AllowWritesOnDirectory(path);
TryAllowWritesOnDirectory(path);
Directory.Delete(path, true);
}
}
Expand Down Expand Up @@ -170,8 +170,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
{
string sourcePath = source.TrimEnd('\\', ' ');
string targetPath = target.TrimEnd('\\', ' ');
AllowWritesOnDirectory(sourcePath);
AllowWritesOnDirectory(targetPath);
TryAllowWritesOnDirectory(sourcePath);
TryAllowWritesOnDirectory(targetPath);
var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
.GroupBy(s => Path.GetDirectoryName(s));

Expand Down Expand Up @@ -199,8 +199,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
/// <inheritdoc />
public void Unzip(string zipPath, string destinationDir)
{
AllowWritesOnDirectory(zipPath);
AllowWritesOnDirectory(destinationDir);
TryAllowWritesOnDirectory(zipPath);
TryAllowWritesOnDirectory(destinationDir);
ZipFile.ExtractToDirectory(zipPath, destinationDir);
}

Expand Down Expand Up @@ -245,6 +245,20 @@ public void AllowWritesOnDirectory(string path)
}
}

/// <inheritdoc />
public bool TryAllowWritesOnDirectory(string path)
{
try
{
AllowWritesOnDirectory(path);
return true;
}
catch
{
return false;
}
}

public IDirectoryInfoIO CreateDirectory(string path)
{
var dir = Directory.CreateDirectory(path);
Expand Down
25 changes: 17 additions & 8 deletions FileSystem/FileIOLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class FileIOLinux : IFileIO
/// <inheritdoc />
public void AppendAllText(string filePath, string fileContent)
{
AllowWritesOnFile(filePath);
TryAllowWritesOnFile(filePath);
File.AppendAllText(filePath, fileContent);
}

Expand All @@ -19,7 +19,7 @@ public void DeleteFile(string path)
{
if (File.Exists(path))
{
AllowWritesOnFile(path);
TryAllowWritesOnFile(path);
File.Delete(path);
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public string GetParentDirectory(string relativePath)
public void MoveFile(string filePath, string sourceFolder, string targetFolder, bool forceReplace)
{
string targetFile = Path.Combine(targetFolder, Path.GetFileName(filePath));
AllowWritesOnFile(sourceFolder);
TryAllowWritesOnFile(sourceFolder);

if (!File.Exists(targetFile))
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public string ReadAllText(string filePath, System.Text.Encoding encoding)
/// <inheritdoc />
public void WriteAllText(string filePath, string fileContent)
{
AllowWritesOnFile(filePath);
TryAllowWritesOnFile(filePath);
File.WriteAllText(filePath, fileContent);
}

Expand All @@ -154,12 +154,21 @@ public FileStream Create(string path, int bufferSize, FileOptions options)
return File.Create(path, bufferSize, options);
}

private static void AllowWritesOnFile(string path)
private static bool TryAllowWritesOnFile(string path)
{
var file = new FileInfo(path);
if (file.Exists)
try
{
file.Attributes = FileAttributes.Normal;
var file = new FileInfo(path);
if (file.Exists)
{
file.Attributes = FileAttributes.Normal;
}

return true;
}
catch
{
return false;
}
}
}
Expand Down
49 changes: 29 additions & 20 deletions FileSystem/FileIOWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class FileIOWin : IFileIO
/// <inheritdoc />
public void AppendAllText(string filePath, string fileContent)
{
AllowWritesOnFile(filePath);
TryAllowWritesOnFile(filePath);
File.AppendAllText(filePath, fileContent);
}

Expand All @@ -22,7 +22,7 @@ public void DeleteFile(string path)
{
if (File.Exists(path))
{
AllowWritesOnFile(path);
TryAllowWritesOnFile(path);
File.Delete(path);
}
}
Expand Down Expand Up @@ -77,14 +77,14 @@ public void MoveFile(string filePath, string sourceFolder, string targetFolder,

if (!File.Exists(targetFile))
{
AllowWritesOnFile(sourceFolder);
TryAllowWritesOnFile(sourceFolder);
File.Move(filePath, targetFile);
}
else
{
if (forceReplace)
{
AllowWritesOnFile(sourceFolder);
TryAllowWritesOnFile(sourceFolder);
DeleteFile(targetFile);
File.Move(filePath, targetFile);
}
Expand All @@ -99,7 +99,7 @@ public void MoveFile(string filePath, string sourceFolder, string targetFolder,
/// <inheritdoc />
public byte[] ReadAllBytes(string filePath)
{
AllowWritesOnFile(filePath);
TryAllowWritesOnFile(filePath);
if (File.Exists(filePath))
{
return File.ReadAllBytes(filePath);
Expand All @@ -113,7 +113,7 @@ public byte[] ReadAllBytes(string filePath)
/// <inheritdoc />
public string ReadAllText(string filePath)
{
AllowWritesOnFile(filePath);
TryAllowWritesOnFile(filePath);
if (File.Exists(filePath))
{
return File.ReadAllText(filePath);
Expand All @@ -127,7 +127,7 @@ public string ReadAllText(string filePath)
/// <inheritdoc />
public string ReadAllText(string filePath, System.Text.Encoding encoding)
{
AllowWritesOnFile(filePath);
TryAllowWritesOnFile(filePath);
if (File.Exists(filePath))
{
return File.ReadAllText(filePath, encoding);
Expand All @@ -141,7 +141,7 @@ public string ReadAllText(string filePath, System.Text.Encoding encoding)
/// <inheritdoc />
public void WriteAllText(string filePath, string fileContent)
{
AllowWritesOnFile(filePath);
TryAllowWritesOnFile(filePath);
File.WriteAllText(filePath, fileContent);
}

Expand All @@ -160,22 +160,31 @@ public System.IO.FileStream Create(string path, int bufferSize, System.IO.FileOp
return File.Create(path, bufferSize, options);
}

private static void AllowWritesOnFile(string path)
private static bool TryAllowWritesOnFile(string path)
{
// DirectoryInfo can be created from a file path
//FileSecurity fileSec = new FileSecurity(path,AccessControlSections.All);
//if (AccessPermissions.HasWritePermissionOnDir(fileSec)) return;
var file = new FileInfo(path);
try
{
// DirectoryInfo can be created from a file path
//FileSecurity fileSec = new FileSecurity(path,AccessControlSections.All);
//if (AccessPermissions.HasWritePermissionOnDir(fileSec)) return;
var file = new FileInfo(path);

if (file.Exists)
{
file.Attributes = System.IO.FileAttributes.Normal;
}

if (file.Exists)
//if (!AccessPermissions.WaitOnWritePermission(fileSec, new TimeSpan(0, 0, 10)))
//{
// throw new TimeoutException("Could not get write access on: " + path);
//}

return true;
}
catch
{
file.Attributes = System.IO.FileAttributes.Normal;
return false;
}

//if (!AccessPermissions.WaitOnWritePermission(fileSec, new TimeSpan(0, 0, 10)))
//{
// throw new TimeoutException("Could not get write access on: " + path);
//}
}
}
}
6 changes: 6 additions & 0 deletions FileSystem/IDirectoryIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ public interface IDirectoryIO
/// <param name="path">Path to the directory</param>
void AllowWritesOnDirectory(string path);

/// <summary>
/// Allow writes to be performed on the directory and its contents.
/// </summary>
/// <param name="path">Path to the directory</param>
bool TryAllowWritesOnDirectory(string path);

/// <summary>Creates all directories and subdirectories in the specified path unless they already exist.</summary>
/// <param name="path">The directory to create.</param>
/// <exception cref="T:System.IO.IOException">The directory specified by <paramref name="path" /> is a file.
Expand Down