Skip to content

Commit

Permalink
Merge pull request #17 from maxmind/greg/allow-deletes-of-opened-files
Browse files Browse the repository at this point in the history
Allow delete/renaming of memory-mapped files
  • Loading branch information
eilara committed Jun 30, 2015
2 parents a0d2458 + 792dcff commit 1c2e3b1
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions MaxMind.Db/MemoryMapReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ public MemoryMapReader(string file)

Length = (int) fileInfo.Length;

var mmfName = fileInfo.FullName.Replace("\\", "-");
// Ideally we would use the file ID in the mapName, but it is not
// easily available from C#.
var mapName = $"{fileInfo.FullName.Replace("\\", "-")}-{Length}";
lock (FileLocker)
{
try
{
_memoryMappedFile = MemoryMappedFile.OpenExisting(mmfName, MemoryMappedFileRights.Read);
_memoryMappedFile = MemoryMappedFile.OpenExisting(mapName, MemoryMappedFileRights.Read);
}
catch (Exception ex) when (ex is IOException || ex is NotImplementedException)
{
_memoryMappedFile = MemoryMappedFile.CreateFromFile(file, FileMode.Open,
mmfName, fileInfo.Length, MemoryMappedFileAccess.Read);
using (
var stream = new FileStream(file, FileMode.Open, FileAccess.Read,
FileShare.Delete | FileShare.Read))
{

_memoryMappedFile = MemoryMappedFile.CreateFromFile(stream, mapName, Length,
MemoryMappedFileAccess.Read, null, HandleInheritability.None, false);
}
}
}

Expand Down

0 comments on commit 1c2e3b1

Please sign in to comment.