/* * Tencent is pleased to support the open source community by making * MMKV available. * * Copyright (C) 2019 THL A29 Limited, a Tencent company. * All rights reserved. * * Licensed under the BSD 3-Clause License (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of * the License at * * https://opensource.org/licenses/BSD-3-Clause * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "MemoryFile.h" #ifdef MMKV_WIN32 # include "InterProcessLock.h" # include "MMBuffer.h" # include "MMKVLog.h" # include "ScopedLock.hpp" # include "ThreadLock.h" # include # include # include using namespace std; namespace fs = std::filesystem; namespace mmkv { static bool getFileSize(MMKVFileHandle_t fd, size_t &size); static bool getFileSize(const wchar_t *filename, size_t &size); static bool ftruncate(MMKVFileHandle_t file, size_t size); File::File(MMKVPath_t path, OpenFlag flag) : m_path(std::move(path)), m_utf8Path(MMKVPath_t2String(m_path)), m_fd(INVALID_HANDLE_VALUE), m_flag(flag) { open(); } static pair OpenFlag2NativeFlag(OpenFlag flag) { int access = 0, create = OPEN_EXISTING; if ((flag & OpenFlagRWMask) == OpenFlag::ReadWrite) { access = (GENERIC_READ | GENERIC_WRITE); } else if (flag & OpenFlag::ReadOnly) { access |= GENERIC_READ; } else if (flag & OpenFlag::WriteOnly) { access |= GENERIC_WRITE; } if (flag & OpenFlag::Create) { create = OPEN_ALWAYS; } if (flag & OpenFlag::Excel) { access = CREATE_NEW; } if (flag & OpenFlag::Truncate) { access = CREATE_ALWAYS; } return {access, create}; } bool File::open() { if (isFileValid()) { return true; } auto pair = OpenFlag2NativeFlag(m_flag); m_fd = CreateFile(m_path.c_str(), pair.first, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, pair.second, FILE_ATTRIBUTE_NORMAL, nullptr); if (!isFileValid()) { MMKVError("fail to open:[%s], flag %x, error %d", m_utf8Path.c_str(), m_flag, GetLastError()); return false; } MMKVInfo("open fd[%p], flag %x, %s", m_fd, m_flag, m_utf8Path.c_str()); return true; } void File::close() { if (isFileValid()) { MMKVInfo("closing fd[%p], %s", m_fd, m_utf8Path.c_str()); if (CloseHandle(m_fd)) { m_fd = INVALID_HANDLE_VALUE; } else { MMKVError("fail to close [%s], %d", m_utf8Path.c_str(), GetLastError()); } } } size_t File::getActualFileSize() const { size_t size = 0; if (isFileValid()) { mmkv::getFileSize(m_fd, size); } else { mmkv::getFileSize(m_path.c_str(), size); } return size; } MemoryFile::MemoryFile(MMKVPath_t path, size_t expectedCapacity, bool readOnly, bool mayflyFD) : m_diskFile(std::move(path), readOnly ? OpenFlag::ReadOnly : (OpenFlag::ReadWrite | OpenFlag::Create)) , m_fileMapping(nullptr) , m_ptr(nullptr) , m_size(0) , m_readOnly(readOnly) , m_isMayflyFD(mayflyFD) { reloadFromFile(expectedCapacity); } bool MemoryFile::truncate(size_t size, FileLock *fileLock) { if (m_isMayflyFD) { openIfNeeded(); } if (!m_diskFile.isFileValid()) { return false; } if (size == m_size) { return true; } if (m_readOnly) { // truncate readonly file not allow return false; } auto oldSize = m_size; m_size = size; // round up to (n * pagesize) if (m_size < DEFAULT_MMAP_SIZE || (m_size % DEFAULT_MMAP_SIZE != 0)) { m_size = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE; } // Win32 won't ftruncate a file if there's active file mmapping/handle, we have to unmmap/close ahead bool needMMapOnFailure = false; if (m_ptr) { // if we have a valid file mapping before, we should restore it regardless needMMapOnFailure = true; if (!UnmapViewOfFile(m_ptr)) { MMKVError("fail to munmap [%s], %d", m_diskFile.getUTF8Path().c_str(), GetLastError()); } m_ptr = nullptr; } if (m_fileMapping) { CloseHandle(m_fileMapping); m_fileMapping = nullptr; } if (!ftruncate(m_diskFile.getFd(), m_size)) { MMKVError("fail to truncate [%s] to size %zu", m_diskFile.getUTF8Path().c_str(), m_size); m_size = oldSize; if (needMMapOnFailure) { mmapOrCleanup(fileLock); } return false; } if (m_size > oldSize) { if (!zeroFillFile(m_diskFile.getFd(), oldSize, m_size - oldSize)) { MMKVError("fail to zeroFile [%s] to size %zu", m_diskFile.getUTF8Path().c_str(), m_size); m_size = oldSize; if (needMMapOnFailure) { mmapOrCleanup(fileLock); } return false; } } return mmapOrCleanup(fileLock); } bool MemoryFile::msync(SyncFlag syncFlag) { if (m_readOnly) { // there's no point in msync() readonly memory return true; } if (m_ptr) { if (FlushViewOfFile(m_ptr, m_size)) { if (syncFlag == MMKV_SYNC && openIfNeeded()) { auto ret = FlushFileBuffers(m_diskFile.getFd()); if (!ret) { MMKVError("fail to FlushFileBuffers [%s]:%d", m_diskFile.getUTF8Path().c_str(), GetLastError()); } cleanMayflyFD(); return ret; } return true; } MMKVError("fail to FlushViewOfFile [%s]:%d", m_diskFile.getUTF8Path().c_str(), GetLastError()); return false; } return false; } bool MemoryFile::mmapOrCleanup(FileLock *fileLock) { auto mode = m_readOnly ? PAGE_READONLY : PAGE_READWRITE; m_fileMapping = CreateFileMapping(m_diskFile.getFd(), nullptr, mode, 0, 0, nullptr); if (!m_fileMapping) { MMKVError("fail to CreateFileMapping [%s], mode %x, %d", m_diskFile.getUTF8Path().c_str(), mode, GetLastError()); return false; } else { auto viewMode = m_readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS; m_ptr = (char*)MapViewOfFile(m_fileMapping, viewMode, 0, 0, 0); if (!m_ptr) { MMKVError("fail to mmap [%s], mode %x, %d", m_diskFile.getUTF8Path().c_str(), viewMode, GetLastError()); doCleanMemoryCache(true); return false; } MMKVInfo("mmap to address [%p], [%s]", m_ptr, m_diskFile.getUTF8Path().c_str()); if (m_isMayflyFD && fileLock) { fileLock->destroyAndUnLock(); } cleanMayflyFD(); return true; } } void MemoryFile::reloadFromFile(size_t expectedCapacity) { if (isFileValid()) { MMKVWarning("calling reloadFromFile while the cache [%s] is still valid", m_diskFile.getUTF8Path().c_str()); assert(0); clearMemoryCache(); } if (openIfNeeded()) { FileLock fileLock(m_diskFile.getFd()); InterProcessLock lock(&fileLock, SharedLockType); SCOPED_LOCK(&lock); mmkv::getFileSize(m_diskFile.getFd(), m_size); size_t expectedSize = std::max(DEFAULT_MMAP_SIZE, roundUp(expectedCapacity, DEFAULT_MMAP_SIZE)); // round up to (n * pagesize) if (!m_readOnly && (m_size < expectedSize || (m_size % DEFAULT_MMAP_SIZE != 0))) { InterProcessLock exclusiveLock(&fileLock, ExclusiveLockType); SCOPED_LOCK(&exclusiveLock); size_t roundSize = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE;; roundSize = std::max(expectedSize, roundSize); truncate(roundSize, &fileLock); } else { mmapOrCleanup(&fileLock); } } } void MemoryFile::doCleanMemoryCache(bool forceClean) { if (m_ptr) { UnmapViewOfFile(m_ptr); m_ptr = nullptr; } if (m_fileMapping) { CloseHandle(m_fileMapping); m_fileMapping = nullptr; } m_diskFile.close(); } bool MemoryFile::openIfNeeded() { if (!m_diskFile.isFileValid()) { return m_diskFile.open(); } return true; } void MemoryFile::cleanMayflyFD() { if (m_isMayflyFD) { if (m_diskFile.isFileValid()) { m_diskFile.close(); } if (m_fileMapping) { CloseHandle(m_fileMapping); m_fileMapping = nullptr; } } } size_t MemoryFile::getActualFileSize() { if (!m_isMayflyFD && !m_diskFile.isFileValid()) { return 0; } return m_diskFile.getActualFileSize(); } MMKVFileHandle_t MemoryFile::getFd() { if (m_isMayflyFD) { openIfNeeded(); } return m_diskFile.getFd(); } size_t getPageSize() { SYSTEM_INFO system_info; GetSystemInfo(&system_info); return system_info.dwPageSize; } MMKVPath_t absolutePath(const MMKVPath_t& path) { fs::path relative_path(path); fs::path absolute_path = fs::absolute(relative_path); try { fs::path normalized = fs::weakly_canonical(absolute_path); return normalized.wstring(); } catch (std::exception &e) { const auto &utf8Path = MMKVPath_t2String(absolute_path.wstring()); MMKVError("fail to weakly_canonical() path %s, error: %s", utf8Path.c_str(), e.what()); } return absolute_path.wstring(); } bool isFileExist(const MMKVPath_t &nsFilePath) { if (nsFilePath.empty()) { return false; } auto attribute = GetFileAttributes(nsFilePath.c_str()); return (attribute != INVALID_FILE_ATTRIBUTES); } bool mkPath(const MMKVPath_t &str) { wchar_t *path = _wcsdup(str.c_str()); bool done = false; wchar_t *slash = path; while (!done) { slash += wcsspn(slash, L"\\"); slash += wcscspn(slash, L"\\"); done = (*slash == L'\0'); *slash = L'\0'; auto attribute = GetFileAttributes(path); if (attribute == INVALID_FILE_ATTRIBUTES) { if (!CreateDirectory(path, nullptr)) { const auto &utf8Path = MMKVPath_t2String(str); MMKVError("fail to create dir:%s, %d", utf8Path.c_str(), GetLastError()); free(path); return false; } } else if (!(attribute & FILE_ATTRIBUTE_DIRECTORY)) { const auto &utf8Path = MMKVPath_t2String(str); MMKVError("%s attribute:%d not a directory", utf8Path.c_str(), attribute); free(path); return false; } *slash = L'\\'; } free(path); return true; } MMBuffer *readWholeFile(const MMKVPath_t &nsFilePath) { MMBuffer *buffer = nullptr; auto fd = CreateFile(nsFilePath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); if (fd != INVALID_HANDLE_VALUE) { size_t fileLength = 0; getFileSize(fd, fileLength); if (fileLength > 0) { buffer = new MMBuffer(fileLength); SetFilePointer(fd, 0, 0, FILE_BEGIN); auto ptr = static_cast(buffer->getPtr()); size_t offset = 0; while (offset < fileLength) { auto remaining = fileLength - offset; auto requestSize = remaining > MAXDWORD ? MAXDWORD : static_cast(remaining); DWORD readSize = 0; if (!ReadFile(fd, ptr + offset, requestSize, &readSize, nullptr)) { const auto &utf8Path = MMKVPath_t2String(nsFilePath); MMKVWarning("fail to read all of %s, read %zu of %zu bytes, error:%d", utf8Path.c_str(), offset, fileLength, GetLastError()); delete buffer; buffer = nullptr; break; } if (readSize == 0) { const auto &utf8Path = MMKVPath_t2String(nsFilePath); MMKVWarning("fail to read all of %s, read %zu of %zu bytes: no progress", utf8Path.c_str(), offset, fileLength); delete buffer; buffer = nullptr; break; } offset += readSize; } } CloseHandle(fd); } else { const auto &utf8Path = MMKVPath_t2String(nsFilePath); MMKVWarning("fail to open %s: %d", utf8Path.c_str(), GetLastError()); } return buffer; } bool zeroFillFile(MMKVFileHandle_t file, size_t startPos, size_t size) { if (file == INVALID_HANDLE_VALUE) { return false; } if (size == 0) { return true; } LARGE_INTEGER position = {}; position.QuadPart = startPos; if (!SetFilePointerEx(file, position, nullptr, FILE_BEGIN)) { MMKVError("fail to lseek fd[%p], error:%d", file, GetLastError()); return false; } static const char zeros[4096] = {}; while (size > 0) { auto requestSize = size > sizeof(zeros) ? static_cast(sizeof(zeros)) : static_cast(size); DWORD bytesWritten = 0; if (!WriteFile(file, zeros, requestSize, &bytesWritten, nullptr)) { MMKVError("fail to write fd[%p], error:%d", file, GetLastError()); return false; } if (bytesWritten == 0) { MMKVError("fail to write fd[%p]: no progress", file); return false; } size -= bytesWritten; } return true; } static bool ftruncate(MMKVFileHandle_t file, size_t size) { LARGE_INTEGER large = {}; large.QuadPart = size; if (SetFilePointerEx(file, large, 0, FILE_BEGIN)) { if (SetEndOfFile(file)) { return true; } MMKVError("fail to SetEndOfFile:%d", GetLastError()); return false; } else { MMKVError("fail to SetFilePointer:%d", GetLastError()); return false; } } static bool getFileSize(MMKVFileHandle_t fd, size_t &size) { LARGE_INTEGER filesize = {}; if (GetFileSizeEx(fd, &filesize)) { size = static_cast(filesize.QuadPart); return true; } return false; } bool getFileSize(const wchar_t *filename, size_t &size) { WIN32_FILE_ATTRIBUTE_DATA fileAttr = {}; if (GetFileAttributesEx(filename, GetFileExInfoStandard, &fileAttr)) { size = ((ULONGLONG)fileAttr.nFileSizeHigh << 32) | fileAttr.nFileSizeLow; return true; } return false; } static pair createUniqueTempFile(const wchar_t *prefix) { wchar_t lpTempPathBuffer[MAX_PATH]; // Gets the temp path env string (no guarantee it's a valid path). auto dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer); if (dwRetVal > MAX_PATH || (dwRetVal == 0)) { MMKVError("GetTempPath failed %d", GetLastError()); return {L"", INVALID_HANDLE_VALUE}; } // Generates a temporary file name. wchar_t szTempFileName[MAX_PATH]; if (!GetTempFileName(lpTempPathBuffer, prefix, 0, szTempFileName)) { MMKVError("GetTempFileName failed %d", GetLastError()); return {L"", INVALID_HANDLE_VALUE}; } auto hTempFile = CreateFile(szTempFileName, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); const auto &utf8Path = MMKVPath_t2String(szTempFileName); if (hTempFile == INVALID_HANDLE_VALUE) { MMKVError("fail to create unique temp file [%s], %d", utf8Path.c_str(), GetLastError()); return {L"", INVALID_HANDLE_VALUE}; } MMKVDebug("create unique temp file [%s] with fd[%p]", utf8Path.c_str(), hTempFile); return {MMKVPath_t(szTempFileName), hTempFile}; } bool tryAtomicRename(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) { if (MoveFileEx(srcPath.c_str(), dstPath.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) == 0) { const auto &utf8SrcPath = MMKVPath_t2String(srcPath); const auto &utf8DstPath = MMKVPath_t2String(dstPath); MMKVError("MoveFileEx [%s] to [%s] failed %d", utf8SrcPath.c_str(), utf8DstPath.c_str(), GetLastError()); return false; } return true; } bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD, bool needTruncate) { if (dstFD == INVALID_HANDLE_VALUE) { return false; } bool ret = false; File srcFile(srcPath, OpenFlag::ReadOnly); if (!srcFile.isFileValid()) { return false; } auto bufferSize = getPageSize(); auto buffer = (char *) malloc(bufferSize); if (!buffer) { MMKVError("fail to malloc size %zu, %d(%s)", bufferSize, errno, strerror(errno)); goto errorOut; } SetFilePointer(dstFD, 0, 0, FILE_BEGIN); // the Win32 platform don't have sendfile()/fcopyfile() equivalent, do it the hard way while (true) { DWORD sizeRead = 0; if (!ReadFile(srcFile.getFd(), buffer, (DWORD) bufferSize, &sizeRead, nullptr)) { MMKVError("fail to read %s: %d", srcFile.getUTF8Path().c_str(), GetLastError()); goto errorOut; } DWORD sizeWrite = 0; if (!WriteFile(dstFD, buffer, sizeRead, &sizeWrite, nullptr)) { MMKVError("fail to write fd [%d], %d", dstFD, GetLastError()); goto errorOut; } if (sizeRead < bufferSize) { break; } } if (needTruncate) { size_t dstFileSize = 0; getFileSize(dstFD, dstFileSize); auto srcFileSize = srcFile.getActualFileSize(); if ((dstFileSize != srcFileSize) && !ftruncate(dstFD, static_cast(srcFileSize))) { MMKVError("fail to truncate [%d] to size [%zu]", dstFD, srcFileSize); goto errorOut; } } ret = true; MMKVInfo("copy content from %s to fd[%d] finish", srcFile.getUTF8Path().c_str(), dstFD); errorOut: free(buffer); return ret; } // copy to a temp file then rename it // this is the best we can do on Win32 bool copyFile(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) { auto pair = createUniqueTempFile(L"MMKV"); auto tmpFD = pair.second; auto &tmpPath = pair.first; if (tmpFD == INVALID_HANDLE_VALUE) { return false; } bool renamed = false; if (copyFileContent(srcPath, tmpFD, false)) { const auto &utf8SrcPath = MMKVPath_t2String(srcPath); const auto &utf8TmpPath = MMKVPath_t2String(tmpPath); MMKVInfo("copied file [%s] to [%s]", utf8SrcPath.c_str(), utf8TmpPath.c_str()); CloseHandle(tmpFD); renamed = tryAtomicRename(tmpPath.c_str(), dstPath.c_str()); if (renamed) { const auto &utf8DstPath = MMKVPath_t2String(dstPath); MMKVInfo("copyfile [%s] to [%s] finish.", utf8SrcPath.c_str(), utf8DstPath.c_str()); } } else { CloseHandle(tmpFD); } if (!renamed) { DeleteFile(tmpPath.c_str()); } return renamed; } bool copyFileContent(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) { File dstFile(dstPath, OpenFlag::WriteOnly | OpenFlag::Create | OpenFlag::Truncate); if (!dstFile.isFileValid()) { return false; } auto ret = copyFileContent(srcPath, dstFile.getFd(), false); if (!ret) { MMKVError("fail to copyfile(): target file %s", dstFile.getUTF8Path().c_str()); } else { const auto &utf8SrcPath = MMKVPath_t2String(srcPath); MMKVInfo("copy content from %s to [%s] finish", utf8SrcPath.c_str(), dstFile.getUTF8Path().c_str()); } return ret; } bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD) { return copyFileContent(srcPath, dstFD, true); } void walkInDir(const MMKVPath_t &dirPath, WalkType type, const std::function &walker) { wchar_t szDir[MAX_PATH]; StringCchCopy(szDir, MAX_PATH, dirPath.c_str()); StringCchCat(szDir, MAX_PATH, L"\\*"); WIN32_FIND_DATA ffd; auto hFind = FindFirstFile(szDir, &ffd); if (hFind == INVALID_HANDLE_VALUE) { return; } do { if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (type & WalkFolder) { if (wcscmp(ffd.cFileName, L".") == 0 || wcscmp(ffd.cFileName, L"..") == 0) { continue; } walker(dirPath + L"\\" + ffd.cFileName, WalkFolder); } } else if (type & WalkFile) { walker(dirPath + L"\\" + ffd.cFileName, WalkFile); } } while (FindNextFile(hFind, &ffd) != 0); auto dwError = GetLastError(); if (dwError != ERROR_NO_MORE_FILES) { MMKVError("WalkInDir fail %d", dwError); } FindClose(hFind); } bool isDiskOfMMAPFileCorrupted(MemoryFile *file, bool &needReportReadFail) { // make sure the file is valid __try { auto filesize = file->getFileSize(); volatile uint8_t* ptr = (uint8_t*) file->getMemory(); // check the head of every page for (size_t index = 0; index < filesize; index += DEFAULT_MMAP_SIZE) { volatile uint8_t byte = ptr[index]; MMKVDebug("%zu byte of the file: 0x%x", index, byte); } // check the very last byte of the file if (filesize > 1) { volatile uint8_t byte = ptr[filesize - 1]; MMKVDebug("%zu byte of the file: 0x%x", filesize - 1, byte); } } __except ((GetExceptionCode() == EXCEPTION_IN_PAGE_ERROR || GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { needReportReadFail = true; DWORD errorCode = GetExceptionCode(); MMKVError("fail to mmap [%s], %d", file->getUTF8Path().c_str(), errorCode); return true; } return false; } bool deleteFile(const MMKVPath_t &path) { if (!DeleteFile(path.c_str())) { const auto &utf8Path = MMKVPath_t2String(path); MMKVError("failed to delete file [%s], %d", utf8Path.c_str(), GetLastError()); return false; } return true; } std::optional getUniqueFileName(const MMKVPath_t &folder, const MMKVPath_t &prefix) { // Buffer for the resulting path wchar_t tempFileName[MAX_PATH]; UINT uUnique = 0; UINT result = GetTempFileName(folder.c_str(), prefix.c_str(), uUnique, tempFileName); if (result == 0) { const auto &utf8Folder = MMKVPath_t2String(folder); MMKVError("failed to GetTempFileName file [%s], %d", utf8Folder.c_str(), GetLastError()); return std::nullopt; } return std::wstring(tempFileName); } } // namespace mmkv std::wstring string2MMKVPath_t(const std::string &str) { auto length = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0); auto buffer = new wchar_t[length]; MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buffer, length); wstring result(buffer); delete[] buffer; return result; } std::string MMKVPath_t2String(const MMKVPath_t &str) { auto length = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, nullptr, 0, 0, 0); auto buffer = new char[length]; WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, buffer, length, 0, 0); string result(buffer); delete[] buffer; return result; } #endif // MMKV_WIN32