Python 写入映射文件 - 奇怪的行为

Python writing into a mapped file - strange behaviour

我有一个 Python 脚本可以成功地将二进制数据写入文件:

iterable_array = [i + 32 for i in range(50)]
file_handle = open("a.bin", "wb")
bytes_to_write = bytearray(iterable_array)
file_handle.write(bytes_to_write)
file_handle.close()

但是,我收到以下错误:

Traceback (most recent call last):
File "python_100_chars.py", line 20, in <module>
file_handle = open("a.bin", "wb")
OSError: [Errno 22] Invalid argument: 'a.bin'

当我在执行以下创建文件映射并在按键后读取数据的程序(源代码最初来自 Microsoft 文档)时尝试编写:

HANDLE hFile = CreateFileA( "a.bin",
                            GENERIC_READ | GENERIC_WRITE,
                            FILE_SHARE_WRITE | FILE_SHARE_READ,
                            NULL,
                            CREATE_ALWAYS,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
             hFile,    // use paging file
             NULL,                    // default security
             PAGE_EXECUTE_READWRITE,          // read/write access
             0,                       // maximum object size (high-order DWORD)
             BUF_SIZE,                // maximum object size (low-order DWORD)
             szName);                 // name of mapping object

if (hMapFile == NULL)
{
   _tprintf(TEXT("Could not create file mapping object (%d).\n"),
          GetLastError());
   return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object
                     FILE_MAP_ALL_ACCESS, // read/write permission
                     0,
                     0,
                     BUF_SIZE);
if (pBuf == NULL)
{
   _tprintf(TEXT("Could not map view of file (%d).\n"),
          GetLastError());

    CloseHandle(hMapFile);

   return 1;
}


 _getch(); 

 printf("string inside file:%s",(char *)((void *)pBuf));
UnmapViewOfFile(pBuf);

CloseHandle(hMapFile);

我已经测试过我可以通过以下方式使用基本 I/O 写入内存映射文件(并查看结果):

HANDLE hFile =  CreateFileA(         "a.bin",
                               GENERIC_WRITE,
          FILE_SHARE_WRITE | FILE_SHARE_READ,
                                        NULL,
                               OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL ,
                                        NULL);
char *p = "bonjour";
DWORD bw;
WriteFile(   hFile,
                 p,
                 8,
               &bw,
               NULL);

感谢您的任何反馈!

我没有 Windows 所以我不能完全测试行为,但我相信这是因为 Windows 不完全遵循 POSIX 语义和 open(name, 'wb')将使用 CREATE_ALWAYS which would conflict with the file being mapped in another process. The ab mode could work too, but... as the Python documentation says

打开现有文件,而不是 截断

'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position).

不幸的是,open 函数使用 C 语义作为标志,因此无法将所需的模式指定为 "open the file for random access writing only without truncating the file",因此您能做的最好是 "open it for both reading and writing without truncating the file" , 即 r+b.


正如用户 Eryk Sun 指出的那样,问题是 Windows 根本不支持 file truncation 如果存在任何现有的内存映射:

If CreateFileMapping is called to create a file mapping object for hFile, UnmapViewOfFile must be called first to unmap all views and call CloseHandle to close the file mapping object before you can call SetEndOfFile.

同样,没有比现有文件大的 space 的映射 - 如果映射长于文件大小,则扩展文件...


在 POSIX 正确的平台上,

这样的程序
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>


int main(int argc, char const *argv[])
{
    struct stat s;
    int fd = open("a.bin", O_RDWR);
    fstat(fd, &s);
    unsigned char *d = mmap(0, s.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    getchar();

    for (int i = 0; i < s.st_size; i++) {
        putchar(d[i]);
    }

    putchar('\n');
    fflush(stdout);
    getchar();
}

不会干扰 运行 上述 Python 程序。但是,如果此 C 程序访问 window 内的文件,而它已被截断为 0,则将在 C 进程中引发 SIGBUS 信号。