From 9bf8cedc535bd5dc37a721574d7c86b73ffd3a2e Mon Sep 17 00:00:00 2001 From: Dust1404 <2477259579@qq.com> Date: Thu, 10 Aug 2023 22:54:19 +0800 Subject: [PATCH] watcher_unix: support symlink --- unix/watcher_linux.cpp | 4 ++++ unix/watcher_macos.mm | 5 +++++ unix/watcher_unix.cpp | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/unix/watcher_linux.cpp b/unix/watcher_linux.cpp index cd113c8..c449229 100644 --- a/unix/watcher_linux.cpp +++ b/unix/watcher_linux.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -96,3 +97,6 @@ ssize_t calculateStaticMemoryUsage(const std::string &fileName) { ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { return memoryLimitInMB * 1024 * 1024; } size_t getMaxRSSInByte(long ru_maxrss) { return ru_maxrss * 1024; } + +char fileNameBuf[PATH_MAX]; +size_t maxPathLength = PATH_MAX; diff --git a/unix/watcher_macos.mm b/unix/watcher_macos.mm index 5e44709..e969f21 100644 --- a/unix/watcher_macos.mm +++ b/unix/watcher_macos.mm @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -147,3 +149,6 @@ ssize_t calculateStaticMemoryUsage(const std::string &fileName) { ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { return memoryLimitInMB * 1024 * (isAppleSilicon ? 4 : 1); } size_t getMaxRSSInByte(long ru_maxrss) { return ru_maxrss / (isAppleSilicon ? 4 : 1); } + +char fileNameBuf[MAX_INPUT]; +size_t maxPathLength = MAX_INPUT; diff --git a/unix/watcher_unix.cpp b/unix/watcher_unix.cpp index bffa505..890ce4e 100644 --- a/unix/watcher_unix.cpp +++ b/unix/watcher_unix.cpp @@ -30,6 +30,8 @@ extern void initWatcher(); extern ssize_t calculateStaticMemoryUsage(const std::string &); extern ssize_t getMemoryRLimit(ssize_t memoryLimitInMB); extern size_t getMaxRSSInByte(long ru_maxrss); +extern char fileNameBuf[]; +extern size_t maxPathLength; /** * argv[1]: QString("\"%1\" %2").arg(executableFile, arguments) in `judgingthread.cpp` 执行命令 @@ -53,7 +55,21 @@ auto main(int /*argc*/, char *argv[]) -> int { std::string fileName(argv[1]); fileName = fileName.substr(1); fileName = fileName.substr(0, fileName.find("\"")); - // TODO: 处理符号链接 + + int rc = readlink(fileName.c_str(), fileNameBuf, maxPathLength); + if (rc > 0) { + fileNameBuf[rc] = '\0'; + if (fileNameBuf[0] == '/') { // absolute path + fileName = fileNameBuf; + } else { // relative path + size_t dir_end = fileName.find_last_of('/'); + if (dir_end == std::string::npos) { + fileName = std::string("./") + fileNameBuf; + } else { + fileName = fileName.substr(0, dir_end + 1) + fileNameBuf; + } + } + } if (memoryLimit > 0) { ssize_t staticMemoryUsage = calculateStaticMemoryUsage(fileName);