Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set FileOpenMode at NodeFactory.FromFile #165

Merged
merged 3 commits into from
Feb 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Yarhl.UnitTests/FileSystem/NodeFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,37 @@ public void CreateFromDirectoryWithDoubleSlashes()

node.Dispose();
}

[Test]
public void ReadFromReadonlyFile()
{
string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
File.WriteAllBytes(tempFile, new byte[] { 0xCA, 0xFE, 0x00, 0xFF });
File.SetAttributes(tempFile, FileAttributes.ReadOnly);

Node node = NodeFactory.FromFile(tempFile, FileOpenMode.Read);
byte[] buffer = new byte[4];
_ = node.Stream.Read(buffer, 0, 4);
node.Dispose();

File.SetAttributes(tempFile, FileAttributes.Normal);
File.Delete(tempFile);
}

[Test]
public void ReadFromReadonlyFileUsingWriteModeThrowsException()
{
string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
File.WriteAllBytes(tempFile, new byte[] { 0xCA, 0xFE, 0x00, 0xFF });
File.SetAttributes(tempFile, FileAttributes.ReadOnly);

Node node = NodeFactory.FromFile(tempFile, FileOpenMode.ReadWrite);
byte[] buffer = new byte[4];
Assert.Throws<UnauthorizedAccessException>(() => _ = node.Stream.Read(buffer, 0, 4));
node.Dispose();

File.SetAttributes(tempFile, FileAttributes.Normal);
File.Delete(tempFile);
}
}
}
20 changes: 12 additions & 8 deletions src/Yarhl/FileSystem/NodeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,14 @@ public static Node FromMemory(string name)
/// </summary>
/// <returns>The node.</returns>
/// <param name="filePath">File path.</param>
public static Node FromFile(string filePath)
/// <param name="mode">The mode to open the file.</param>
public static Node FromFile(string filePath, FileOpenMode mode = FileOpenMode.ReadWrite)
{
if (string.IsNullOrEmpty(filePath))
throw new ArgumentNullException(nameof(filePath));

string filename = Path.GetFileName(filePath);
return FromFile(filePath, filename);
return FromFile(filePath, filename, mode);
}

/// <summary>
Expand All @@ -150,15 +151,15 @@ public static Node FromFile(string filePath)
/// <returns>The node.</returns>
/// <param name="filePath">File path.</param>
/// <param name="nodeName">Node name.</param>
/// <param name="mode">The mode to open the file.</param>
[SuppressMessage(
"Reliability",
"CA2000:Dispose objects before losing scope",
Justification = "Ownserhip dispose transferred")]
public static Node FromFile(string filePath, string nodeName)
public static Node FromFile(string filePath, string nodeName, FileOpenMode mode = FileOpenMode.ReadWrite)
{
// We need to catch if the node creation fails
// for instance for null names, to dispose the stream.
FileOpenMode mode = FileOpenMode.ReadWrite;
var format = new BinaryFormat(DataStreamFactory.FromFile(filePath, mode));
Node node;
try {
Expand All @@ -179,7 +180,8 @@ public static Node FromFile(string filePath, string nodeName)
/// <returns>The container node.</returns>
/// <param name="dirPath">Directory path.</param>
/// <param name="filter">Filter for files in directory.</param>
public static Node FromDirectory(string dirPath, string filter = "*")
/// <param name="mode">The mode to open the files.</param>
public static Node FromDirectory(string dirPath, string filter = "*", FileOpenMode mode = FileOpenMode.ReadWrite)
{
if (string.IsNullOrEmpty(dirPath))
throw new ArgumentNullException(nameof(dirPath));
Expand All @@ -188,7 +190,7 @@ public static Node FromDirectory(string dirPath, string filter = "*")
dirPath = dirPath.Remove(dirPath.Length - 1);

string dirName = Path.GetFileName(dirPath);
return FromDirectory(dirPath, filter, dirName);
return FromDirectory(dirPath, filter, dirName, false, mode);
}

/// <summary>
Expand All @@ -201,6 +203,7 @@ public static Node FromDirectory(string dirPath, string filter = "*")
/// <param name="subDirectories">
/// If <see langword="true" /> it searchs recursively in subdirectories.
/// </param>
/// <param name="mode">The mode to open the files.</param>
[SuppressMessage(
"Reliability",
"CA2000:Dispose objects before losing scope",
Expand All @@ -209,7 +212,8 @@ public static Node FromDirectory(string dirPath, string filter = "*")
string dirPath,
string filter,
string nodeName,
bool subDirectories = false)
bool subDirectories = false,
FileOpenMode mode = FileOpenMode.ReadWrite)
{
var options = subDirectories ?
SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
Expand All @@ -223,7 +227,7 @@ public static Node FromDirectory(string dirPath, string filter = "*")
foreach (string filePath in Directory.GetFiles(dirPath, filter, options)) {
string relParent = Path.GetDirectoryName(filePath)
.Replace(dirPath, string.Empty);
CreateContainersForChild(folder, relParent, FromFile(filePath));
CreateContainersForChild(folder, relParent, FromFile(filePath, mode));
}

foreach (Node node in Navigator.IterateNodes(folder)) {
Expand Down