MemoryMappedFiles.MemoryMappedFile.CreateFromFile不会扩展linux/mono下的文件

Dar*_*ren 6 mono f#

在windows下,当容量参数(最后一个参数)设置为比底层文件大时,此F#代码将文件从12个字节扩展到655346个字节.这似乎是扩展内存映射文件最干净的方法.在mono/linux下,它抛出一个ArgumentException:capacity,除非该文件与映射的容量一样长.是否有一种干净的方式来获取单声道扩展文件,或者我必须先预先扩展文件才能映射?

let Main () =
    let path = "parts.pash"
    let l = 65536L
    let mm =    MemoryMappedFiles.MemoryMappedFile.CreateFromFile(path,FileMode.OpenOrCreate,"pashmap",l)

    ()

Main()
Run Code Online (Sandbox Code Playgroud)

错误信息

未处理的异常信息:System.ArgumentException:容量在System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(System.String路径,的FileMode模式,System.String MAPNAME,Int64的容量,MemoryMappedFileAccess访问)[0x00000]中:在System.IO.MemoryMappedFiles 0 .MemoryMappedFile.CreateFromFile(System.String路径,的FileMode模式,System.String MAPNAME,Int64的容量)[0x00000]中:0在Program.Main()[0x00000]中:0为$ Program.main @()[0x00000在:0

单声道版本:

[daz@clowder pash]$ mono --version
Mono JIT compiler version 2.10.1 (tarball Mon Apr  4 10:40:52 PDT 2011)
Copyright (C) 2002-2011 Novell, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)
Run Code Online (Sandbox Code Playgroud)

编辑:似乎内存映射的不同底层行为在API中公开,因此您需要将文件自己扩展到正确的长度以进行平台中立

let f = File.Open(path,FileMode.Append,FileAccess.Write)
let pad = l- FileInfo(path).Length
let padding = Array.create (int32 pad) 0uy
f.Write(padding,0,int pad)
f.Close()
Run Code Online (Sandbox Code Playgroud)

Guv*_*nte 4

查看 .NET 实现,发现CreateFromFile没有该功能的实现。据我所知,除了参数检查之外,它是 Windows API 调用的一个精简包装。

因此,创建更大文件的能力更多地是 .NET 领域的巧合,因此,如果底层操作系统不允许类似的功能,Mono 删除该能力也就不足为奇了。

更简单地说,不太可能,因为 .NET 版本在技术上也不会扩展该文件,但 Windows API 会扩展该文件。