Skip to content

Commit

Permalink
feat: add compatibility for macOS
Browse files Browse the repository at this point in the history
The GetExecutablePath function has been modified to support macOS.
  • Loading branch information
sdvendramini committed Nov 20, 2024
1 parent 95ab86f commit d0aafd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
45 changes: 35 additions & 10 deletions src/agent/src/unix_daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
#include <fstream>
#include <sys/file.h>

#if defined(__linux__)
#include <unistd.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
#else
#error "Unsupported platform"
#endif

namespace fs = std::filesystem;

namespace unix_daemon
Expand All @@ -33,19 +41,16 @@ namespace unix_daemon
}
}

bool LockFileHandler::createDirectory(const std::string_view relativePath) const
bool LockFileHandler::createDirectory(const std::string& path) const
{
try
{
const fs::path curDir = fs::current_path();
const fs::path fullPath = curDir / relativePath;

if (fs::exists(fullPath) && fs::is_directory(fullPath))
if (fs::exists(path) && fs::is_directory(path))
{
return true;
}

fs::create_directories(fullPath);
fs::create_directories(path);
return true;
}
catch (const fs::filesystem_error& e)
Expand Down Expand Up @@ -85,18 +90,38 @@ namespace unix_daemon
std::string LockFileHandler::GetExecutablePath()
{
std::vector<char> pathBuffer(PATH_MAX);
const ssize_t len = readlink("/proc/self/exe", pathBuffer.data(), pathBuffer.size() - 1);

#if defined(__linux__)
ssize_t len = readlink("/proc/self/exe", pathBuffer.data(), pathBuffer.size() - 1);
if (len != -1)
{
pathBuffer[static_cast<std::size_t>(len)] = '\0';
const std::filesystem::path exePath(pathBuffer.data());
return exePath.parent_path().string(); // Return the directory part only
return std::filesystem::path(pathBuffer.data()).parent_path().string();
}
else
{
throw std::runtime_error("Failed to retrieve executable path on Linux");
}

#elif defined(__APPLE__)
uint32_t size = static_cast<uint32_t>(pathBuffer.size());
if (_NSGetExecutablePath(pathBuffer.data(), &size) == 0)
{
return std::filesystem::path(pathBuffer.data()).parent_path().string();
}
else
{
return "";
pathBuffer.resize(size);
if (_NSGetExecutablePath(pathBuffer.data(), &size) == 0)
{
return std::filesystem::path(pathBuffer.data()).parent_path().string();
}
else
{
throw std::runtime_error("Failed to retrieve executable path on macOS");
}
}
#endif
}

LockFileHandler GenerateLockFile()
Expand Down
2 changes: 1 addition & 1 deletion src/agent/src/unix_daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace unix_daemon
/// @brief Creates the directory path for the lock file
/// @param relativePath The relative path for the lock file
/// @return True if the directory is created, false otherwise
bool createDirectory(const std::string_view relativePath) const;
bool createDirectory(const std::string& relativePath) const;

/// @brief Creates the lock file
/// @return True if the lock file is created, false otherwise
Expand Down

0 comments on commit d0aafd4

Please sign in to comment.