Files
MMKV/Core/MemoryFile.cpp
T

268 lines
7.4 KiB
C++

/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2018 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"
#include "InterProcessLock.h"
#include "MMBuffer.h"
#include "MMKVLog.h"
#include "ScopedLock.hpp"
#include <cerrno>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace std;
namespace mmkv {
MemoryFile::MemoryFile(const std::string &path, size_t size)
: m_name(path), m_fd(-1), m_ptr(nullptr), m_size(0) {
m_fd = open(m_name.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, S_IRWXU);
if (m_fd < 0) {
MMKVError("fail to open:%s, %s", m_name.c_str(), strerror(errno));
} else {
FileLock fileLock(m_fd);
InterProcessLock lock(&fileLock, ExclusiveLockType);
SCOPEDLOCK(lock);
struct stat st = {0};
if (fstat(m_fd, &st) != -1) {
m_size = static_cast<size_t>(st.st_size);
}
if (m_size < DEFAULT_MMAP_SIZE) {
m_size = static_cast<size_t>(DEFAULT_MMAP_SIZE);
if (ftruncate(m_fd, m_size) != 0 || !zeroFillFile(m_fd, 0, m_size)) {
MMKVError("fail to truncate [%s] to size %zu, %s", m_name.c_str(), m_size,
strerror(errno));
close(m_fd);
m_fd = -1;
removeFile(m_name);
return;
}
}
m_ptr = (char *) mmap(nullptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (m_ptr == MAP_FAILED) {
MMKVError("fail to mmap [%s], %s", m_name.c_str(), strerror(errno));
close(m_fd);
m_fd = -1;
m_ptr = nullptr;
}
}
}
MemoryFile::~MemoryFile() {
if (m_ptr) {
munmap(m_ptr, m_size);
m_ptr = nullptr;
}
if (m_fd >= 0) {
close(m_fd);
m_fd = -1;
}
}
bool MemoryFile::truncate(size_t size) {
if (!isFileValid()) {
return false;
}
if (size == m_size) {
return true;
}
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;
}
if (::ftruncate(m_fd, m_size) != 0) {
MMKVError("fail to truncate [%s] to size %zu, %s", m_name.c_str(), m_size, strerror(errno));
m_size = oldSize;
return false;
}
if (m_size > oldSize) {
if (!zeroFillFile(m_fd, oldSize, m_size - oldSize)) {
MMKVError("fail to zeroFile [%s] to size %zu, %s", m_name.c_str(), m_size,
strerror(errno));
m_size = oldSize;
return false;
}
}
if (munmap(m_ptr, oldSize) != 0) {
MMKVError("fail to munmap [%s], %s", m_name.c_str(), strerror(errno));
}
m_ptr = (char *) mmap(m_ptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (m_ptr == MAP_FAILED) {
MMKVError("fail to mmap [%s], %s", m_name.c_str(), strerror(errno));
m_ptr = nullptr;
return false;
}
return true;
}
bool MemoryFile::msync(SyncFlag syncFlag) {
if (m_ptr) {
auto ret = ::msync(m_ptr, m_size, syncFlag ? MMAP_SYNC : MMAP_ASYNC);
if (ret == 0) {
return true;
}
MMKVError("fail to msync [%s], %s", m_name.c_str(), strerror(errno));
}
return false;
}
#pragma mark - file
bool isFileExist(const string &nsFilePath) {
if (nsFilePath.empty()) {
return false;
}
struct stat temp = {0};
return lstat(nsFilePath.c_str(), &temp) == 0;
}
bool mkPath(char *path) {
struct stat sb = {0};
bool done = false;
char *slash = path;
while (!done) {
slash += strspn(slash, "/");
slash += strcspn(slash, "/");
done = (*slash == '\0');
*slash = '\0';
if (stat(path, &sb) != 0) {
if (errno != ENOENT || mkdir(path, 0777) != 0) {
MMKVWarning("%s : %s", path, strerror(errno));
return false;
}
} else if (!S_ISDIR(sb.st_mode)) {
MMKVWarning("%s: %s", path, strerror(ENOTDIR));
return false;
}
*slash = '/';
}
return true;
}
bool createFile(const std::string &filePath) {
bool ret = false;
// try create at once
auto fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, S_IRWXU);
if (fd >= 0) {
close(fd);
ret = true;
} else {
// create parent dir
char *path = strdup(filePath.c_str());
if (!path) {
return false;
}
auto ptr = strrchr(path, '/');
if (ptr) {
*ptr = '\0';
}
if (mkPath(path)) {
// try again
fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, S_IRWXU);
if (fd >= 0) {
close(fd);
ret = true;
} else {
MMKVWarning("fail to create file %s, %s", filePath.c_str(), strerror(errno));
}
}
free(path);
}
return ret;
}
bool removeFile(const string &nsFilePath) {
int ret = unlink(nsFilePath.c_str());
if (ret != 0) {
MMKVError("remove file failed. filePath=%s, err=%s", nsFilePath.c_str(), strerror(errno));
return false;
}
return true;
}
MMBuffer *readWholeFile(const char *path) {
MMBuffer *buffer = nullptr;
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
auto fileLength = lseek(fd, 0, SEEK_END);
if (fileLength > 0) {
buffer = new MMBuffer(static_cast<size_t>(fileLength));
lseek(fd, 0, SEEK_SET);
auto readSize = read(fd, buffer->getPtr(), static_cast<size_t>(fileLength));
if (readSize != -1) {
//fileSize = readSize;
} else {
MMKVWarning("fail to read %s: %s", path, strerror(errno));
delete buffer;
buffer = nullptr;
}
}
close(fd);
} else {
MMKVWarning("fail to open %s: %s", path, strerror(errno));
}
return buffer;
}
bool zeroFillFile(int fd, size_t startPos, size_t size) {
if (fd < 0) {
return false;
}
if (lseek(fd, startPos, SEEK_SET) < 0) {
MMKVError("fail to lseek fd[%d], error:%s", fd, strerror(errno));
return false;
}
static const char zeros[4096] = {0};
while (size >= sizeof(zeros)) {
if (write(fd, zeros, sizeof(zeros)) < 0) {
MMKVError("fail to write fd[%d], error:%s", fd, strerror(errno));
return false;
}
size -= sizeof(zeros);
}
if (size > 0) {
if (write(fd, zeros, size) < 0) {
MMKVError("fail to write fd[%d], error:%s", fd, strerror(errno));
return false;
}
}
return true;
}
} // namespace mmkv