SET(CXX_STD "c++11")
endif()
-SET(COMPILE_BASE_FLAGS "-g -fPIC -Werror -Wall -Wl,--as-needed -Wl,--no-whole-archive")
+EXECUTE_PROCESS(COMMAND getconf LFS_CFLAGS OUTPUT_VARIABLE LFS_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
+EXECUTE_PROCESS(COMMAND getconf LFS_LDFLAGS OUTPUT_VARIABLE LFS_LDFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
+EXECUTE_PROCESS(COMMAND getconf LFS_LIBS OUTPUT_VARIABLE LFS_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+SET(COMPILE_BASE_FLAGS "-g -fPIC -Werror -Wall -Wl,--as-needed -Wl,--no-whole-archive ${LFS_CFLAGS}")
SET(CMAKE_C_FLAGS_PROFILING "${COMPILE_BASE_FLAGS} -O0 -pg")
SET(CMAKE_CXX_FLAGS_PROFILING "${COMPILE_BASE_FLAGS} -O0 -pg -std=${CXX_STD} -fno-rtti")
SET(CMAKE_C_FLAGS_DEBUG "${COMPILE_BASE_FLAGS} -O0 -ggdb")
INCLUDE_DIRECTORIES(SYSTEM ${SERVER_DEPS_INCLUDE_DIRS} ${ODE_SERVER} ${PROJECT_SOURCE_DIR})
-TARGET_LINK_LIBRARIES(${SERVER_NAME} ${SERVER_DEPS_LIBRARIES} pthread)
+TARGET_LINK_LIBRARIES(${SERVER_NAME} ${SERVER_DEPS_LIBRARIES} ${LFS_LDFLAGS} ${LFS_LIBS} pthread)
SET_TARGET_PROPERTIES(${SERVER_NAME} PROPERTIES COMPILE_FLAGS "-fPIE")
SET_TARGET_PROPERTIES(${SERVER_NAME} PROPERTIES LINK_FLAGS "-pie")
return;
}
-int BlockDevice::discard(const Block &block)
+int BlockDevice::discard(off_t offset, off_t length)
{
int devFd = 0;
unsigned long long range[2];
return -1;
}
- range[0] = block.physicalOffset;
- range[1] = block.length;
+ range[0] = (unsigned long long)offset;
+ range[1] = (unsigned long long)length;
int ret = ::ioctl(devFd, BLKDISCARD, &range);
return ret;
}
-int BlockDevice::secDiscard(const Block &block)
+int BlockDevice::secDiscard(off_t offset, off_t length)
{
int devFd = 0;
unsigned long long range[2];
return -1;
}
- range[0] = block.physicalOffset;
- range[1] = block.length;
+ range[0] = (unsigned long long)offset;
+ range[1] = (unsigned long long)length;
int ret = ::ioctl(devFd, BLKSECDISCARD, &range);
return name;
}
-int BlockDevice::getSize()
+blksize_t BlockDevice::getBlockSize()
{
if (name == "") {
throw runtime::Exception("Cannot get device name");
namespace ode {
-class Block {
-public:
- template <typename OffsetType, typename LengthType>
- Block(OffsetType offset, LengthType len) : physicalOffset(offset), length(len) {}
- ~Block() {}
-
- unsigned long long physicalOffset;
- unsigned long long length;
-};
-
class BlockDevice final {
public:
typedef std::unordered_map<std::string, std::string> DeviceList;
int open(int flags);
void close(int descriptor);
- int discard(const Block &block);
- int secDiscard(const Block &block);
+ int discard(off_t offset, off_t length);
+ int secDiscard(off_t offset, off_t length);
const std::string &getName() const;
- int getSize();
+ blksize_t getBlockSize();
private:
void createDeviceList();
private:
std::string name;
- int blockSize;
+ blksize_t blockSize;
DeviceList deviceList;
};
namespace {
-size_t getFileSystemSize(const std::string &src)
+blkcnt_t getBlockCount(const std::string &src)
{
int fd = open(src.c_str(), O_RDONLY);
if (fd < 0)
return 0;
- size_t size = 0;
+ unsigned long long size = 0;
if ((ioctl(fd, BLKGETSIZE, &size)) == -1)
size = 0;
close(fd);
- return size;
+ return (blkcnt_t)size;
}
const std::string convertToHex(const DMCryptEngine::data &binary)
const std::string createCryptoBlkDev(const std::string &realBlkDev, const std::string &mountName, const DMCryptEngine::data &key, std::string cryptoTypeName)
{
- size_t fileSystemSize = getFileSystemSize(realBlkDev);
+ auto blockCount = getBlockCount(realBlkDev);
std::string cryptoBlkDev;
int fd = -1;
dmIo->target_count = 1;
dmTs->status = 0;
dmTs->sector_start = 0;
- dmTs->length = fileSystemSize;
+ dmTs->length = blockCount;
::memset(dmTs->target_type, 0, sizeof(dmTs->target_type));
::strncpy(dmTs->target_type, "crypt", sizeof(dmTs->target_type) - 1);
}
void copyInPlace(const std::string &source, const std::string &destination,
- const std::function<bool(unsigned int)> &isTarget,
+ const std::function<bool(blkcnt_t)> &isTarget,
const std::function<void(int, int)> &addProgress)
{
Ext4Tool ext4tool(source);
- const size_t srcBlockSize = ext4tool.getBlockSize();
- const size_t srcTotalBlockCount = ext4tool.getTotalBlockCount();
+ const blksize_t srcBlockSize = ext4tool.getBlockSize();
+ const blkcnt_t srcTotalBlockCount = ext4tool.getTotalBlockCount();
char buf[srcBlockSize];
runtime::File src(source, O_RDONLY);
runtime::File dst(destination, O_WRONLY);
- for (size_t n = 0, pos = 0; n < srcTotalBlockCount;
- n++, pos += srcBlockSize) {
+ off_t pos = 0;
+ for (blkcnt_t n = 0; n < srcTotalBlockCount; n++, pos += srcBlockSize) {
if (!isTarget(n)) {
continue;
}
// should be encrypted here.
auto cryptoBlkDev = createCryptoBlkDev(source, DM_DEFAULT_LABEL_NAME, sanitizeKey(key), DM_DEFAULT_CRYPTO_NAME);
- std::function<bool(unsigned int)> isTarget;
+ std::function<bool(blkcnt_t)> isTarget;
if (!(options & OPTION_INCLUDE_UNUSED_REGION)) {
INFO("FastEncryption: Disabled");
isTarget = std::bind(&Ext4Tool::isUsedBlock, &ext4Source, _1);
Ext4Tool ext4CryptoBlkDev(cryptoBlkDev);
ext4CryptoBlkDev.forceCleanUp();
- std::function<bool(unsigned int)> isTarget;
+ std::function<bool(blkcnt_t)> isTarget;
if (!(options & OPTION_INCLUDE_UNUSED_REGION)) {
INFO("FastEncryption: Disabled");
isTarget = std::bind(&Ext4Tool::isUsedBlock, &ext4CryptoBlkDev, _1);
namespace {
-unsigned long long getAvailableSpace(const std::string& mountPoint)
+off_t getAvailableSpace(const std::string& mountPoint)
{
struct statfs statbuf;
if (::statfs(mountPoint.c_str(), &statbuf)) {
throw runtime::Exception("Failed to access " + mountPoint);
}
- return (unsigned long long)statbuf.f_bfree * statbuf.f_bsize;
+ return statbuf.f_bfree * statbuf.f_bsize;
}
bool wasEncrypted(const std::string &path)
#endif
}
-unsigned long long getEncryptedSize(const runtime::File &file) {
- unsigned long long originalSize = file.size();
- unsigned long long result = 0;
+off_t getEncryptedSize(const runtime::File &file) {
+ off_t originalSize = file.size();
+ off_t result = 0;
if (originalSize % 4096) {
originalSize = (1 + originalSize / 4096) * 4096;
//TODO : block size have to not hard-coded.
// If there is a better way, the followings have to be changed.
- unsigned int blockSize = 4096;
+ blksize_t blockSize = 4096;
if (result % blockSize) {
result = (1 + result / blockSize) * blockSize;
}
return result;
}
-unsigned long long getDecryptedSize(const runtime::File &file) {
- unsigned long long originalSize = file.size();
- unsigned long long result = originalSize;
+off_t getDecryptedSize(const runtime::File &file) {
+ off_t originalSize = file.size();
+ off_t result = originalSize;
if (wasEncrypted(file.getPath())) {
if (originalSize > 2 * 4096) {
//TODO : block size have to not hard-coded.
// If there is a better way, the followings have to be changed.
- unsigned int blockSize = 4096;
+ blksize_t blockSize = 4096;
if (result % blockSize) {
result = (1 + result / blockSize) * blockSize;
}
}
bool isEnoughToCopyInPlace(const std::string& path,
- const std::function<unsigned long long(const runtime::File&)> getSizeFunc)
+ const std::function<off_t(const runtime::File&)> getSizeFunc)
{
- unsigned long long availableSpace = getAvailableSpace(path);
+ off_t availableSpace = getAvailableSpace(path);
std::function<bool(const std::string &path)> check;
check = [&check, &getSizeFunc, availableSpace](const std::string &path) {
void copyInPlace(const std::string& source, const std::string& destination,
const std::string& temp,
const std::function<bool(const std::string&)> &isTarget,
- const std::function<void(unsigned long long)> &addProgress)
+ const std::function<void(off_t)> &addProgress)
{
for (runtime::DirectoryIterator iter(source), end;
iter != end; ++iter) {
}
try {
- unsigned long long totalSize = getAvailableSpace(source), current;
+ off_t totalSize = getAvailableSpace(source), current;
runtime::File tempDir(destination + "/" ENCRYPTION_TMP_DIR);
if (tempDir.exists()) {
[](const std::string &file) {
return true;
},
- [¤t, &totalSize, this](unsigned long long size) {
+ [¤t, &totalSize, this](off_t size) {
current += size;
this->progress.update(current * 100 / totalSize);
});
progress.update(0);
try {
- unsigned long long totalSize = getAvailableSpace(source), current;
+ off_t totalSize = getAvailableSpace(source), current;
runtime::File tempDir(source + "/" ENCRYPTION_TMP_DIR);
runtime::File tempMountpoint(tempDir.getPath() + "/mount");
copyInPlace(tempMountpoint.getPath(), source,
tempDir.getPath(), wasEncrypted,
- [¤t, &totalSize, this](unsigned long long size) {
+ [¤t, &totalSize, this](off_t size) {
current += size;
this->progress.update(current * 100 / totalSize);
});
return true;
}
-unsigned long long getUsedSpace(const std::string& mountPoint)
+off_t getUsedSpace(const std::string& mountPoint)
{
struct statfs statbuf;
if (::statfs(mountPoint.c_str(), &statbuf)) {
throw runtime::Exception("Failed to access " + mountPoint);
}
- return (unsigned long long)(statbuf.f_blocks - statbuf.f_bfree) * statbuf.f_bsize;
+ return (off_t)(statbuf.f_blocks - statbuf.f_bfree) * statbuf.f_bsize;
}
-unsigned long long getAvailableSpace(const std::string& mountPoint)
+off_t getAvailableSpace(const std::string& mountPoint)
{
struct statfs statbuf;
if (::statfs(mountPoint.c_str(), &statbuf)) {
throw runtime::Exception("Failed to access " + mountPoint);
}
- return (unsigned long long)statbuf.f_bfree * statbuf.f_bsize;
+ return (off_t)statbuf.f_bfree * statbuf.f_bsize;
}
static void copyDac(const std::string& srcPath, const std::string& destPath)
bool isEnoughToCopyInPlace(const std::string& path)
{
- unsigned long long availableSpace = getAvailableSpace(path);
+ off_t availableSpace = getAvailableSpace(path);
std::function<bool(const std::string &path)> check;
check = [&check, availableSpace](const std::string &path) {
}
void copyInPlace(const std::string& source, const std::string& destination,
- const std::function<void(unsigned long long)> &addProgress)
+ const std::function<void(off_t)> &addProgress)
{
for (runtime::DirectoryIterator iter(source), end; iter != end; ++iter) {
if (iter->getPath() == destination) {
encrypted.makeDirectory();
setPolicy(encrypted.getPath(), sanitizedKey);
- unsigned long long totalSize = getUsedSpace(source), current = 0;
+ off_t totalSize = getUsedSpace(source), current = 0;
copyInPlace(destination, encrypted.getPath(),
- [¤t, &totalSize, this](unsigned long long size) {
+ [¤t, &totalSize, this](off_t size) {
current += size;
this->progress.update(current, totalSize, 1);
});
runtime::File encrypted(destination + "/" ENCRYPTION_DIR);
- unsigned long long totalSize = getUsedSpace(source), current = 0;
+ off_t totalSize = getUsedSpace(source), current = 0;
copyInPlace(encrypted.getPath(), destination,
- [¤t, &totalSize, this](unsigned long long size) {
+ [¤t, &totalSize, this](off_t size) {
current += size;
this->progress.update(current, totalSize, 1);
});
int MMCEraseEngine::cleanDevice(const std::string &path)
{
BlockDevice blockDevice(path);
- unsigned totalBlock, blockSize;
-
+ blkcnt_t totalBlock;
+ blksize_t blockSize;
Ext4Tool ext4Tool(blockDevice.getName());
- totalBlock = ext4Tool.getTotalBlockCount();
- blockSize = (unsigned int)blockDevice.getSize();
- for (unsigned int i = 0; i < totalBlock; i++) {
+ totalBlock = (blkcnt_t)ext4Tool.getTotalBlockCount();
+ blockSize = blockDevice.getBlockSize();
+
+ for (blkcnt_t i = 0; i < totalBlock; i++) {
if (!ext4Tool.isUsedBlock(i)) {
- Block block(i * blockSize, blockSize);
- blockDevice.discard(block);
+ blockDevice.discard(i*blockSize, blockSize);
}
progressBar.update(i, totalBlock, 1);
}
int MMCEraseEngine::eraseDevice(const std::string &path)
{
+ blkcnt_t totalBlock;
+ blksize_t blockSize;
+
runtime::File file(path, O_WRONLY);
if (!file.exists()) {
throw runtime::Exception("Target doesn't exist");
BlockDevice blockDevice(path);
Ext4Tool ext4Tool(blockDevice.getName());
- unsigned int totalBlock = ext4Tool.getTotalBlockCount();
- unsigned int blockSize = (unsigned int)blockDevice.getSize();
+ totalBlock = ext4Tool.getTotalBlockCount();
+ blockSize = blockDevice.getBlockSize();
- for (unsigned int i = 0; i < totalBlock; i++) {
- Block block(i * blockSize, blockSize);
- blockDevice.discard(block);
+ for (blkcnt_t i = 0; i < totalBlock; i++) {
+ blockDevice.discard(i * blockSize, blockSize);
progressBar.update(i, totalBlock, 1);
}
int MMCEraseEngine::eraseFile(const std::string &path)
{
int ret = 0, fd = 0;
- int extentBlockCount = 0;
+ blkcnt_t extentBlockCount = 0;
char buf[4096] = "";
struct fiemap *fmap = (struct fiemap *)buf;
struct fiemap_extent *fm_ext = NULL;
extentBlockCount = (int)fmap->fm_mapped_extents;
BlockDevice blockDevice(path);
- for (int i = 0; i < extentBlockCount; i++) {
- Block block(fm_ext[i].fe_physical, fm_ext[i].fe_length);
- ret = blockDevice.secDiscard(block);
+ for (blkcnt_t i = 0; i < extentBlockCount; i++) {
+ ret = blockDevice.secDiscard(fm_ext[i].fe_physical, fm_ext[i].fe_length);
if (ret < 0) {
throw runtime::Exception("failed to erase the device");
}