Skip to content

Commit

Permalink
Added file share parameters to RootStorage.Open()
Browse files Browse the repository at this point in the history
  • Loading branch information
conormcg94 committed Dec 1, 2024
1 parent 08c0844 commit 99cede9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 15 additions & 0 deletions OpenMcdf.Tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,19 @@ public void GetAndSetMetadata(Version version)
Assert.AreEqual(1U, storage.StateBits);
}
}

[TestMethod]
[DataRow("MultipleStorage.cfs")]
public void OpenReadOnlyWithSharing(string fileName)
{
using (var rootStorage = RootStorage.Open(fileName, FileMode.Open, FileAccess.ReadWrite, share: FileShare.ReadWrite))
{
Assert.IsTrue(rootStorage.ContainsEntry("MyStorage"));

using (var rootStorage2 = RootStorage.OpenRead(fileName, share: FileShare.ReadWrite))
{
Assert.IsTrue(rootStorage2.ContainsEntry("MyStorage"));
}
}
}
}
9 changes: 5 additions & 4 deletions OpenMcdf/RootStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public static RootStorage Open(string fileName, FileMode mode, StorageModeFlags
return Open(stream, flags);
}

public static RootStorage Open(string fileName, FileMode mode, FileAccess access, StorageModeFlags flags = StorageModeFlags.None)
public static RootStorage Open(string fileName, FileMode mode, FileAccess access, FileShare share = FileShare.ReadWrite,
StorageModeFlags flags = StorageModeFlags.None)
{
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));
Expand All @@ -101,7 +102,7 @@ public static RootStorage Open(string fileName, FileMode mode, FileAccess access
ThrowIfInvalid(access);
ThrowIfLeaveOpen(flags);

FileStream stream = File.Open(fileName, mode, access);
FileStream stream = File.Open(fileName, mode, access, share);
return Open(stream, flags);
}

Expand All @@ -119,14 +120,14 @@ public static RootStorage Open(Stream stream, StorageModeFlags flags = StorageMo
return new RootStorage(rootContextSite, flags);
}

public static RootStorage OpenRead(string fileName, StorageModeFlags flags = StorageModeFlags.None)
public static RootStorage OpenRead(string fileName, FileShare share = FileShare.ReadWrite, StorageModeFlags flags = StorageModeFlags.None)
{
if (fileName is null)
throw new ArgumentNullException(nameof(fileName));

ThrowIfLeaveOpen(flags);

FileStream stream = File.OpenRead(fileName);
FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read, share);
return Open(stream, flags);
}

Expand Down

0 comments on commit 99cede9

Please sign in to comment.