Commit 6962334d authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[platform] Allow to memory-map empty files

This current fails, since {mmap} fails with EINVAL for empty mappings.
The destructor already has special handling for a {nullptr} mapping, so
we can just use {nullptr} for empty files. We get a similar error on
windows, and can fix it the same way.

On order to make presubmit checks happy, we have to skip copyright
checking and checking for terminating newlines for empty files.

R=mlippautz@chromium.org

Change-Id: I2b73da7ff6df72d8bdd40df1fff6422e0a46881e
Reviewed-on: https://chromium-review.googlesource.com/c/1424861Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58970}
parent 6091e270
......@@ -448,7 +448,8 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
if (FILE* file = fopen(name, "r+")) {
if (fseek(file, 0, SEEK_END) == 0) {
long size = ftell(file); // NOLINT(runtime/int)
if (size >= 0) {
if (size == 0) return new PosixMemoryMappedFile(file, nullptr, 0);
if (size > 0) {
void* const memory =
mmap(OS::GetRandomMmapAddr(), size, PROT_READ | PROT_WRITE,
MAP_SHARED, fileno(file), 0);
......@@ -467,6 +468,7 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name,
size_t size, void* initial) {
if (FILE* file = fopen(name, "w+")) {
if (size == 0) return new PosixMemoryMappedFile(file, 0, 0);
size_t result = fwrite(initial, 1, size, file);
if (result == size && !ferror(file)) {
void* memory = mmap(OS::GetRandomMmapAddr(), result,
......
......@@ -969,20 +969,21 @@ class Win32MemoryMappedFile final : public OS::MemoryMappedFile {
// static
OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
// Open a physical file
// Open a physical file.
HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, 0, nullptr);
if (file == INVALID_HANDLE_VALUE) return nullptr;
DWORD size = GetFileSize(file, nullptr);
if (size == 0) return new Win32MemoryMappedFile(file, nullptr, nullptr, 0);
// Create a file mapping for the physical file
// Create a file mapping for the physical file.
HANDLE file_mapping =
CreateFileMapping(file, nullptr, PAGE_READWRITE, 0, size, nullptr);
if (file_mapping == nullptr) return nullptr;
// Map a view of the file into memory
// Map a view of the file into memory.
void* memory = MapViewOfFile(file_mapping, FILE_MAP_ALL_ACCESS, 0, 0, size);
return new Win32MemoryMappedFile(file, file_mapping, memory, size);
}
......@@ -991,16 +992,17 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::open(const char* name) {
// static
OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name,
size_t size, void* initial) {
// Open a physical file
// Open a physical file.
HANDLE file = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, 0, nullptr);
if (file == nullptr) return nullptr;
// Create a file mapping for the physical file
if (size == 0) return new Win32MemoryMappedFile(file, nullptr, nullptr, 0);
// Create a file mapping for the physical file.
HANDLE file_mapping = CreateFileMapping(file, nullptr, PAGE_READWRITE, 0,
static_cast<DWORD>(size), nullptr);
if (file_mapping == nullptr) return nullptr;
// Map a view of the file into memory
// Map a view of the file into memory.
void* memory = MapViewOfFile(file_mapping, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (memory) memmove(memory, initial, size);
return new Win32MemoryMappedFile(file, file_mapping, memory, size);
......@@ -1009,7 +1011,7 @@ OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name,
Win32MemoryMappedFile::~Win32MemoryMappedFile() {
if (memory_) UnmapViewOfFile(memory_);
CloseHandle(file_mapping_);
if (file_mapping_) CloseHandle(file_mapping_);
CloseHandle(file_);
}
......
......@@ -542,7 +542,7 @@ class SourceProcessor(SourceFileProcessor):
try:
handle = open(file)
contents = handle.read()
if not self.ProcessContents(file, contents):
if len(contents) > 0 and not self.ProcessContents(file, contents):
success = False
violations += 1
finally:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment