Skip to content

Commit

Permalink
add Mac OSX compile support
Browse files Browse the repository at this point in the history
  • Loading branch information
XUranus committed Nov 12, 2023
1 parent 12ef292 commit 7bd007b
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 48 deletions.
18 changes: 14 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,20 @@ if(${CMAKE_HOST_WIN32})
uuid
Rpcrt4
)
else()
elseif(${CMAKE_HOST_LINUX})
target_link_libraries(
${VOLUMEPROTECT_DYNAMIC_LIBRARY_TARGET}
minijson
minilogger
# third part dependency (linux gcc)
uuid
)
else()
target_link_libraries(
${VOLUMEPROTECT_DYNAMIC_LIBRARY_TARGET}
minijson
minilogger
)
endif()


Expand Down Expand Up @@ -136,14 +142,20 @@ if(${CMAKE_HOST_WIN32})
uuid
Rpcrt4
)
else()
elseif(${CMAKE_HOST_LINUX})
target_link_libraries(
${VOLUMEPROTECT_STATIC_LIBRARY_TARGET}
minijson_static
minilogger_static
# third part dependency (linux gcc)
uuid
)
else()
target_link_libraries(
${VOLUMEPROTECT_STATIC_LIBRARY_TARGET}
minijson_static
minilogger_static
)
endif()

# set -DCMAKE_BUILD_TYPE=Debug to enable LLT, set -DCOVERAGE=ON to enable code coverage
Expand Down Expand Up @@ -187,8 +199,6 @@ else()
target_link_libraries(
vbackup
volumebackup
# openssl
crypto
pthread
# third part dependency provided by XUranus
minijson_static
Expand Down
5 changes: 2 additions & 3 deletions cli/vbackup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ void InitLogger(const CliArgs& cliArgs)
conf.target = LoggerTarget::FILE;
conf.archiveFilesNumMax = 10;
conf.fileName = "vbackup.log";
#ifdef __linux__
conf.logDirPath = "/tmp/LoggerTest";
#endif
#ifdef _WIN32
conf.logDirPath = R"(C:\LoggerTest)";
#else
conf.logDirPath = "/tmp/LoggerTest";
#endif
Logger::GetInstance()->SetLogLevel(LoggerLevel::DEBUG);
if (!Logger::GetInstance()->Init(conf)) {
Expand Down
2 changes: 1 addition & 1 deletion include/common/BlockingQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <mutex>
#include <condition_variable>

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "Logger.h"

template<typename T>
Expand Down
20 changes: 10 additions & 10 deletions include/common/VolumeProtectMacros.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* @file VolumeProtectMacros.h
* @brief This file is used as a PCH, and define some utils missing in STL of CXX11.
* @copyright Copyright 2023 XUranus. All rights reserved.
Expand Down Expand Up @@ -54,20 +54,20 @@ using ErrCodeType = int;
#define VOLUMEPROTECT_API __attribute__((__visibility__("default")))
#endif

// check platform macro conflict
#ifdef __linux__
#ifdef _WIN32
static_assert(false, "conflict macro, both __linux__ and _WIN32 defined!");
// macro POSIXAPI used for Linux/AIX/Solaris/MacOSX, these platform use posix api
#if defined(__linux__) || defined(__APPLE__) || defined(__sun__) || defined(_AIX)
#ifndef POSIXAPI
#define POSIXAPI
#endif
#endif

// check if any of the platform macro defined
#ifndef __linux__
#ifndef _WIN32
static_assert(false, "platform unsupported, none of __linux__ and _WIN32 defined!");
#endif
#if defined(POSIXAPI) && defined(_WIN32)
static_assert(false, "both windows platform macro _WIN32 and posix platform macro POSIXAPI defined!")
#endif

#if !defined(POSIXAPI) && !defined(_WIN32)
static_assert(false, "none of windows platform macro _WIN32 or posix platform macro POSIXAPI defined"!)
#endif

// check if make_unique defined
#ifndef __cpp_lib_make_unique
Expand Down
2 changes: 1 addition & 1 deletion include/common/VolumeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef VOLUMEBACKUP_BACKUP_UTIL_H
#define VOLUMEBACKUP_BACKUP_UTIL_H

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
// external logger/json library
#include "Json.h"
#include "Logger.h"
Expand Down
2 changes: 1 addition & 1 deletion include/native/FileSystemAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef VOLUMEBACKUP_NATIVE_FILESYSTEM_API_HEADER
#define VOLUMEBACKUP_NATIVE_FILESYSTEM_API_HEADER

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"

namespace volumeprotect {
/**
Expand Down
2 changes: 1 addition & 1 deletion include/native/RawIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef VOLUMEBACKUP_NATIVE_RAW_IO_HEADER
#define VOLUMEBACKUP_NATIVE_RAW_IO_HEADER

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "VolumeProtector.h"
#include <string>

Expand Down
3 changes: 2 additions & 1 deletion include/native/linux/DeviceMapperControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
* @license This project is released under the Apache License.
* @author XUranus([email protected])
*/
#ifdef __linux__

#ifndef VOLUMEBACKUP_DM_DEVICE_MAPPER_CONTROL_H
#define VOLUMEBACKUP_DM_DEVICE_MAPPER_CONTROL_H

#ifdef __linux__

#include <cstdint>
#include <string>
#include <vector>
Expand Down
7 changes: 4 additions & 3 deletions include/native/linux/PosixRawIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
* @author XUranus([email protected])
*/

#ifdef __linux__

#ifndef VOLUMEBACKUP_NATIVE_POSIX_RAW_IO_HEADER
#define VOLUMEBACKUP_NATIVE_POSIX_RAW_IO_HEADER

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"

#ifdef POSIXAPI

#include "RawIO.h"

// Raw I/O Reader/Writer for *unix platform posix API implementation
Expand Down
2 changes: 1 addition & 1 deletion include/native/win32/Win32RawIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <Windows.h>
#include <winioctl.h>

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
#include "RawIO.h"

// define customed win32 error codes, starts from 0x80600000
Expand Down
2 changes: 1 addition & 1 deletion include/native/win32/Win32VirtualDiskMountProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#ifndef VOLUMEBACKUP_WIN32_VIRTUALDISK_MOUNT_PROVIDER_HEADER
#define VOLUMEBACKUP_WIN32_VIRTUALDISK_MOUNT_PROVIDER_HEADER

#include "VolumeProtectMacros.h"
#include "common/VolumeProtectMacros.h"
// external logger/json library
#include "Json.h"
#include "VolumeUtils.h"
Expand Down
29 changes: 16 additions & 13 deletions src/native/FileSystemAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@
* @author XUranus([email protected])
*/

#include "common/VolumeProtectMacros.h"

#include <cerrno>
#include <cstdio>

#ifdef __linux__
#include <sys/mount.h>
#include <mntent.h>

#ifdef POSIXAPI
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/fs.h>
#include <unistd.h>
#include <dirent.h>
#endif

#ifdef _WIN32
#ifdef __linux__
#include <sys/mount.h>
#include <mntent.h>
#include <linux/fs.h>
#endif

#ifdef _WIN32
#include <locale>
#include <codecvt>

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
Expand Down Expand Up @@ -97,7 +99,7 @@ const char* SystemApiException::what() const noexcept

bool fsapi::IsFileExists(const std::string& path)
{
#ifdef __linux__
#ifdef POSIXAPI
struct stat st;
return ::stat(path.c_str(), &st) == 0;
#endif
Expand All @@ -110,7 +112,7 @@ bool fsapi::IsFileExists(const std::string& path)

uint64_t fsapi::GetFileSize(const std::string& path)
{
#ifdef __linux__
#ifdef POSIXAPI
struct stat st;
return ::stat(path.c_str(), &st) == 0 ? st.st_size : 0;
#endif
Expand All @@ -137,7 +139,8 @@ bool fsapi::IsDirectoryExists(const std::string& path)
return true;
}
return ::CreateDirectoryW(wpath.c_str(), nullptr) != 0;
#else
#endif
#ifdef POSIXAPI
DIR* dir = ::opendir(path.c_str());
if (dir) {
closedir(dir);
Expand Down Expand Up @@ -305,7 +308,7 @@ bool fsapi::IsVolumeExists(const std::string& volumePath)
bool fsapi::CreateEmptyFile(const std::string& dirPath, const std::string& filename)
{
std::string fullpath = dirPath + SEPARATOR + filename;
#ifdef __linux__
#ifdef POSIXAPI
int fd = ::open(fullpath.c_str(), O_CREAT | O_WRONLY, 0644);
if (fd == -1) {
return false;
Expand Down Expand Up @@ -333,7 +336,7 @@ bool fsapi::CreateEmptyFile(const std::string& dirPath, const std::string& filen
bool fsapi::RemoveFile(const std::string& dirPath, const std::string& filename)
{
std::string fullpath = dirPath + SEPARATOR + filename;
#ifdef __linux__
#ifdef POSIXAPI
if (::access(fullpath.c_str(), F_OK) == 0 && ::unlink(fullpath.c_str()) < 0) {
return false;
}
Expand All @@ -346,7 +349,7 @@ bool fsapi::RemoveFile(const std::string& dirPath, const std::string& filename)

uint32_t fsapi::ProcessorsNum()
{
#ifdef __linux__
#ifdef POSIXAPI
auto processorCount = sysconf(_SC_NPROCESSORS_ONLN);
return processorCount <= 0 ? DEFAULT_PROCESSORS_NUM : processorCount;
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/native/RawIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @author XUranus([email protected])
*/

#include "common/VolumeProtectMacros.h"
#include "VolumeProtector.h"
#include <cerrno>
#include <cstdint>
Expand All @@ -23,7 +24,7 @@ using OsPlatformRawDataReader = rawio::win32::Win32RawDataReader;
using OsPlatformRawDataWriter = rawio::win32::Win32RawDataWriter;
#endif

#ifdef __linux__
#ifdef POSIXAPI
#include "linux/PosixRawIO.h"
using OsPlatformRawDataReader = rawio::posix::PosixRawDataReader;
using OsPlatformRawDataWriter = rawio::posix::PosixRawDataWriter;
Expand Down
5 changes: 2 additions & 3 deletions src/native/TaskResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#include "native/RawIO.h"
#include "native/FileSystemAPI.h"

#ifdef __linux__
#include "native/linux/PosixRawIO.h"
#endif

#ifdef _WIN32
#include "native/win32/Win32RawIO.h"
#else
#include "native/linux/PosixRawIO.h"
#endif

using namespace volumeprotect;
Expand Down
5 changes: 3 additions & 2 deletions src/native/linux/PosixRawIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
* @author XUranus([email protected])
*/

#ifdef __linux__
#include "common/VolumeProtectMacros.h"

#ifdef POSIXAPI

#include <cerrno>
#include <cstdio>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/fs.h>
#include <unistd.h>
#include <dirent.h>

Expand Down
4 changes: 2 additions & 2 deletions src/task/VolumeBlockHasher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ void VolumeBlockHasher::ComputeSHA256(uint8_t* data, uint32_t len, uint8_t* outp
EVP_MD_CTX_free(mdctx);
return;
}
#endif

#ifdef _WIN32
#else

void VolumeBlockHasher::ComputeSHA256(uint8_t* data, uint32_t len, uint8_t* output, uint32_t outputLen)
{
// TODO
Expand Down

0 comments on commit 7bd007b

Please sign in to comment.