Skip to content

Commit

Permalink
Merge pull request #9664
Browse files Browse the repository at this point in the history
4e54b19 check for windows NTFS compression on database files (eversinc33)
  • Loading branch information
tobtoht committed Jan 18, 2025
2 parents 2226d4f + 4e54b19 commit 09d95c2
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/blockchain_db/lmdb/db_lmdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
#include "db_lmdb.h"

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/format.hpp>
#include <boost/circular_buffer.hpp>
#include <memory> // std::unique_ptr
#include <cstring> // memcpy

#ifdef WIN32
#include <winioctl.h>
#endif

#include "string_tools.h"
#include "common/util.h"
#include "common/pruning.h"
Expand Down Expand Up @@ -1320,6 +1325,54 @@ BlockchainLMDB::BlockchainLMDB(bool batch_transactions): BlockchainDB()
m_hardfork = nullptr;
}

#ifdef WIN32
static bool disable_ntfs_compression(const boost::filesystem::path& filepath)
{
DWORD file_attributes = ::GetFileAttributesW(filepath.c_str());
if (file_attributes == INVALID_FILE_ATTRIBUTES)
{
MERROR("Failed to get " << filepath.string() << " file attributes. Error: " << ::GetLastError());
return false;
}

if (!(file_attributes & FILE_ATTRIBUTE_COMPRESSED))
return true; // not compressed

LOG_PRINT_L1("Disabling NTFS compression for " << filepath.string());
HANDLE file_handle = ::CreateFileW(
filepath.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr,
OPEN_EXISTING,
boost::filesystem::is_directory(filepath) ? FILE_FLAG_BACKUP_SEMANTICS : 0, // Needed to open handles to directories
nullptr
);

if (file_handle == INVALID_HANDLE_VALUE)
{
MERROR("Failed to open handle: " << filepath.string() << ". Error: " << ::GetLastError());
return false;
}

USHORT compression_state = COMPRESSION_FORMAT_NONE;
DWORD bytes_returned;
BOOL ok = ::DeviceIoControl(
file_handle,
FSCTL_SET_COMPRESSION,
&compression_state,
sizeof(compression_state),
nullptr,
0,
&bytes_returned,
nullptr
);

::CloseHandle(file_handle);
return ok;
}
#endif

void BlockchainLMDB::open(const std::string& filename, const int db_flags)
{
int result;
Expand All @@ -1346,6 +1399,17 @@ void BlockchainLMDB::open(const std::string& filename, const int db_flags)
throw DB_ERROR("Database could not be opened");
}

#ifdef WIN32
// ensure NTFS compression is disabled on the directory and database file to avoid corruption of the blockchain
if (!disable_ntfs_compression(filename))
LOG_PRINT_L0("Failed to disable NTFS compression on folder: " << filename << ". Error: " << ::GetLastError());
boost::filesystem::path datafile(filename);
datafile /= CRYPTONOTE_BLOCKCHAINDATA_FILENAME;
boost::filesystem::ofstream(datafile).close(); // touch the file to ensure it exists
if (!disable_ntfs_compression(datafile))
throw DB_ERROR("Database file is NTFS compressend and compression could not be disabled");
#endif

boost::optional<bool> is_hdd_result = tools::is_hdd(filename.c_str());
if (is_hdd_result)
{
Expand Down

0 comments on commit 09d95c2

Please sign in to comment.