Make sure we open with the requested access

This commit is contained in:
duckdoom4
2023-09-20 07:13:11 +02:00
parent 54fe0dd778
commit 377663c2e5

View File

@@ -134,14 +134,17 @@ public Stream OpenFile(FileSystemPath path, FileMode mode = FileMode.Open, FileA
// The file exists, now we need to check if we can open it with the requested access
// readonly fs requires special handling for write requests.
// if readonly access is requested, we can open it if the fs is readonly
if (!fs.IsReadOnly)
return fs.OpenFile(path, FileMode.Open, access);
var readStream = fs.OpenFile(path);
if (!fs.IsReadOnly || access == FileAccess.Read)
if (access == FileAccess.Read)
return readStream;
// For write-only access, we can just create a new empty file
IFileSystem writableFs = GetFirstWritable();
writableFs.CreateDirectoryRecursive(path.ParentPath);
var writeStream = writableFs.CreateFile(path);
var writeStream = writableFs.OpenFile(path, FileMode.Create, access);
if (access == FileAccess.Write)
return writeStream;