Skip to content

Commit

Permalink
use boost::filesystem::resize_file (rime#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj authored and graphemecluster committed Aug 26, 2023
1 parent 0e76655 commit 9117226
Showing 1 changed file with 1 addition and 26 deletions.
27 changes: 1 addition & 26 deletions src/rime/dict/mapped_file.cc
Expand Up @@ -12,31 +12,6 @@
#include <boost/interprocess/mapped_region.hpp>
#include <rime/dict/mapped_file.h>

#ifdef BOOST_RESIZE_FILE

#define RESIZE_FILE boost::filesystem::resize_file

#else

#ifdef _WIN32
#include <windows.h>
#define RESIZE_FILE(P, SZ) (resize_file_api(P, SZ) != 0)
static BOOL resize_file_api(const char* p, boost::uintmax_t size) {
HANDLE handle = CreateFileA(p, GENERIC_WRITE, 0, 0, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
LARGE_INTEGER sz;
sz.QuadPart = size;
return handle != INVALID_HANDLE_VALUE &&
::SetFilePointerEx(handle, sz, 0, FILE_BEGIN) &&
::SetEndOfFile(handle) && ::CloseHandle(handle);
}
#else
#include <unistd.h>
#define RESIZE_FILE(P, SZ) (::truncate(P, SZ) == 0)
#endif // _WIN32

#endif // BOOST_RESIZE_FILE

namespace rime {

class MappedFileImpl {
Expand Down Expand Up @@ -155,7 +130,7 @@ bool MappedFile::Resize(size_t capacity) {
if (IsOpen())
Close();
try {
RESIZE_FILE(file_name_.c_str(), capacity);
boost::filesystem::resize_file(file_name_.c_str(), capacity);
} catch (...) {
return false;
}
Expand Down

0 comments on commit 9117226

Please sign in to comment.