<commit_before>﻿#region

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;

#endregion

namespace MaxMind.Db
{
    internal sealed class MemoryMapBuffer : Buffer
    {
        private static readonly object FileLocker = new object();
        private readonly MemoryMappedFile _memoryMappedFile;
        private readonly MemoryMappedViewAccessor _view;
        private bool _disposed;

        internal MemoryMapBuffer(string file) : this(file, new FileInfo(file))
        {
        }

        private MemoryMapBuffer(string file, FileInfo fileInfo) : base((int)fileInfo.Length)
        {
            // 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(mapName, MemoryMappedFileRights.Read);
                }
#if !NETSTANDARD1_4
                catch (Exception ex) when (ex is IOException || ex is NotImplementedException)
#else           // Note that PNSE is only required by .NetStandard1.0, see the subsequent comment for more context
                catch (Exception ex) when (ex is IOException || ex is NotImplementedException || ex is PlatformNotSupportedException)
#endif
                {
                    var stream = new FileStream(file, FileMode.Open, FileAccess.Read,
                                                FileShare.Delete | FileShare.Read);
#if !NETSTANDARD1_4
                    _memoryMappedFile = MemoryMappedFile.CreateFromFile(stream, mapName, Length,
                            MemoryMappedFileAccess.Read, null, HandleInheritability.None, false);
#else

                    // In .NET Core, named maps are not supported for Unices yet: https://github.com/dotnet/corefx/issues/1329
                    // When executed on unsupported platform, we get the PNSE. In which case, we consruct the memory map by
                    // setting mapName to null.
                    if (ex is PlatformNotSupportedException)<commit_msg>Make MemoryMappedFile load into global namespace and set security to allow all - closes #33<commit_after>﻿#region

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;

#endregion

namespace MaxMind.Db
{
    internal sealed class MemoryMapBuffer : Buffer
    {
        private static readonly object FileLocker = new object();
        private readonly MemoryMappedFile _memoryMappedFile;
        private readonly MemoryMappedViewAccessor _view;
        private bool _disposed;

        internal MemoryMapBuffer(string file) : this(file, new FileInfo(file))
        {
        }

        private MemoryMapBuffer(string file, FileInfo fileInfo) : base((int)fileInfo.Length)
        {
            // Ideally we would use the file ID in the mapName, but it is not
            // easily available from C#.
            var mapName = $"Global\\{fileInfo.FullName.Replace("\\", "-")}-{Length}";
            lock (FileLocker)
            {
                try
                {
                    _memoryMappedFile = MemoryMappedFile.OpenExisting(mapName, MemoryMappedFileRights.Read);
                }
#if !NETSTANDARD1_4
                catch (Exception ex) when (ex is IOException || ex is NotImplementedException)
#else           // Note that PNSE is only required by .NetStandard1.0, see the subsequent comment for more context
                catch (Exception ex) when (ex is IOException || ex is NotImplementedException || ex is PlatformNotSupportedException)
#endif
                {
                    var stream = new FileStream(file, FileMode.Open, FileAccess.Read,
                                                FileShare.Delete | FileShare.Read);
#if !NETSTANDARD1_4
                    var security = new MemoryMappedFileSecurity();
                    security.AddAccessRule(
                        new System.Security.AccessControl.AccessRule<MemoryMappedFileRights>(
                            new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null),
                            MemoryMappedFileRights.Read,
                            System.Security.AccessControl.AccessControlType.Allow));
                    _memoryMappedFile = MemoryMappedFile.CreateFromFile(stream, mapName, Length,
                            MemoryMappedFileAccess.Read, security, HandleInheritability.None, false);
#else

                    // In .NET Core, named maps are not supported for Unices yet: https://github.com/dotnet/corefx/issues/1329
                    // When executed on unsupported platform, we get the PNSE. In which case, we consruct the memory map by
                    // setting mapName to null.
                    if (ex is PlatformNotSupportedException)
{'fraction_non_alphanumeric': 0.07252836304700162, 'fraction_numerical': 0.004457050243111832, 'mean_word_length': 2.041897720271103, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5602906', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1182, 'n_words': 372}