Skip to content

Commit

Permalink
update pre
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed Oct 27, 2023
1 parent 7943d5e commit 499a0cb
Show file tree
Hide file tree
Showing 12 changed files with 1,085 additions and 19 deletions.
2 changes: 2 additions & 0 deletions cmake_modules/compiler_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ if (APPLE)
endif ()
endif ()

add_compile_options(-fsanitize=address -Wall)

# set build architecture for non-Apple platforms
if (NOT APPLE)
set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING "build architecture for non-Apple platforms" FORCE)
Expand Down
16 changes: 8 additions & 8 deletions locale/lithium.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Lithium \n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2023-10-20 23:53+0800\n"
"POT-Creation-Date: 2023-10-27 18:56+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -238,30 +238,30 @@ msgstr ""
msgid "Set server port to %d"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:70
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:81
msgid "Failed to load Lithium App , error : {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:82
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:93
msgid "Get config value: {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:88
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:99
msgid "Set {} to {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:275
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:286
msgid "Failed to run chai command : {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:293
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:304
msgid "Failed to run chai multi command {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:306
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:317
msgid "Failed to load chaiscript file {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:319
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:330
msgid "Failed to run chai script {}"
msgstr ""
14 changes: 7 additions & 7 deletions locale/po/en_US.UTF-8/lithium.po
Original file line number Diff line number Diff line change
Expand Up @@ -219,30 +219,30 @@ msgstr ""
msgid "Set server port to %d"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:70
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:81
msgid "Failed to load Lithium App , error : {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:82
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:93
msgid "Get config value: {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:88
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:99
msgid "Set {} to {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:275
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:286
msgid "Failed to run chai command : {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:293
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:304
msgid "Failed to run chai multi command {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:306
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:317
msgid "Failed to load chaiscript file {}"
msgstr ""

#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:319
#: E:/msys64/home/Qrm/OpenAPT/src/LithiumApp.cpp:330
msgid "Failed to run chai script {}"
msgstr ""
11 changes: 7 additions & 4 deletions src/core/base/hydrogencom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ void IDLog(const char *fmt, ...)
double time_ns()
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 0;

#if defined(HAVE_TIMESPEC_GET)
timespec_get(&ts, TIME_UTC);
#elif defined(HAVE_CLOCK_GETTIME)
Expand Down Expand Up @@ -557,9 +560,9 @@ int tty_read_expanded(int fd, char *buf, int nbytes, long timeout_seconds, long

if (tty_debug)
{
IDLog("%d bytes read and %d bytes remaining...\n", bytesRead, numBytesToRead - bytesRead);
IDLog("%lu bytes read and %lu bytes remaining...\n", bytesRead, numBytesToRead - bytesRead);
int i = 0;
for (i = *nbytes_read; i < (*nbytes_read + bytesRead); i++)
for (i = *nbytes_read; static_cast<long>(i) < static_cast<long>((*nbytes_read + bytesRead)); i++)
IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, i, (unsigned char)buf[i], buf[i]);
}

Expand Down Expand Up @@ -704,7 +707,7 @@ int tty_read_section_expanded(int fd, char *buf, char stop_char, long timeout_se
return tty_read_section_expanded(fd, buf, stop_char, timeout_seconds, timeout_microseconds, nbytes_read);
}

for (int index = 8; index < bytesRead; index++)
for (int index = 8; static_cast<long>(index) < static_cast<long>(bytesRead); index++)
{
(*nbytes_read)++;

Expand All @@ -726,7 +729,7 @@ int tty_read_section_expanded(int fd, char *buf, char stop_char, long timeout_se
if (bytesRead < 0)
return TTY_READ_ERROR;

for (int index = 0; index < bytesRead; index++)
for (int index = 0; static_cast<long>(index) < static_cast<long>(bytesRead); index++)
{
(*nbytes_read)++;

Expand Down
236 changes: 236 additions & 0 deletions src/modules/system/dirw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
* dirw.cpp
*
* Copyright (C) 2023 Max Qian <lightapt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/*************************************************
Copyright: 2023 Max Qian. All rights reserved
Author: Max Qian
E-mail: [email protected]
Date: 2023-10-27
Description: Folder Watcher
**************************************************/

#include "dirw.hpp"

#include "loguru/loguru.hpp"

FolderMonitor::FolderMonitor(const std::string &folderPath)
: m_folderPath(folderPath), m_isMonitoring(false) {}

void FolderMonitor::StartMonitoring()
{
if (m_isMonitoring)
{
LOG_F(ERROR, "Folder monitor is already running.");
return;
}

m_isMonitoring = true;

// 创建监视线程
m_monitorThread = std::thread([this]()
{
while (m_isMonitoring) {
MonitorFolderChanges();
std::this_thread::sleep_for(std::chrono::seconds(1));
} });
}

void FolderMonitor::StopMonitoring()
{
if (!m_isMonitoring)
{
LOG_F(WARNING, "Folder monitor is not running.");
return;
}

m_isMonitoring = false;

// 等待监视线程退出
if (m_monitorThread.joinable())
{
m_monitorThread.join();
}
}

void FolderMonitor::RegisterFileChangeEventCallback(FileChangeEventCallback callback)
{
m_fileChangeEventCallback = callback;
}

void FolderMonitor::MonitorFolderChanges()
{
#ifdef _WIN32
// Windows平台下的文件夹监视器实现
HANDLE hDir = CreateFile(m_folderPath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

if (hDir == INVALID_HANDLE_VALUE)
{
LOG_F(ERROR, "Failed to open folder: {}", m_folderPath);
return;
}

DWORD dwBytesReturned;
FILE_NOTIFY_INFORMATION buffer[1024];

BOOL result = ReadDirectoryChangesW(hDir, &buffer, sizeof(buffer), TRUE,
FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME, &dwBytesReturned, NULL, NULL);

if (!result)
{
LOG_F(ERROR, "Failed to start monitoring folder: {}", m_folderPath);
CloseHandle(hDir);
return;
}

while (result && m_isMonitoring)
{
DWORD offset = 0;
FILE_NOTIFY_INFORMATION *pInfo = nullptr;

do
{
pInfo = reinterpret_cast<FILE_NOTIFY_INFORMATION *>(reinterpret_cast<char *>(&buffer) + offset);

std::wstring fileName(pInfo->FileName, pInfo->FileNameLength / sizeof(wchar_t));
std::string utf8FileName(fileName.begin(), fileName.end());

std::string filePath = m_folderPath + "\\" + utf8FileName;

if (pInfo->Action == FILE_ACTION_MODIFIED)
{
if (m_fileChangeEventCallback)
{
m_fileChangeEventCallback(filePath);
}
}
else if (pInfo->Action == FILE_ACTION_ADDED || pInfo->Action == FILE_ACTION_RENAMED_NEW_NAME)
{
if (m_fileChangeEventCallback)
{
m_fileChangeEventCallback(filePath);
}
}
else if (pInfo->Action == FILE_ACTION_REMOVED || pInfo->Action == FILE_ACTION_RENAMED_OLD_NAME)
{
// 文件被删除或重命名
// 可以在此处执行相应的处理操作
}

offset += pInfo->NextEntryOffset;
} while (pInfo->NextEntryOffset != 0 && m_isMonitoring);

result = ReadDirectoryChangesW(hDir, &buffer, sizeof(buffer), TRUE,
FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME, &dwBytesReturned, NULL, NULL);
}

CloseHandle(hDir);
#else
// Linux平台下的文件夹监视器实现
int fd = inotify_init();
if (fd == -1)
{
LOG_F(ERROR, "Failed to initialize inotify.");
return;
}

int wd = inotify_add_watch(fd, m_folderPath.c_str(),
IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVE | IN_MOVED_FROM | IN_MOVED_TO);

if (wd == -1)
{
LOG_F(ERROR, "Failed to add watch for folder: ", m_folderPath);
close(fd);
return;
}

char buffer[4096];

while (m_isMonitoring)
{
ssize_t len = read(fd, buffer, sizeof(buffer));
if (len == -1)
{
LOG_F(ERROR, "Failed to read events from inotify.");
break;
}

const struct inotify_event *event = nullptr;
for (char *ptr = buffer; ptr < buffer + len; ptr += sizeof(struct inotify_event) + event->len)
{
event = reinterpret_cast<const struct inotify_event *>(ptr);

std::string fileName(event->name);
std::string filePath = m_folderPath + "/" + fileName;

if (event->mask & IN_MODIFY)
{
if (m_fileChangeEventCallback)
{
m_fileChangeEventCallback(filePath);
}
}
else if (event->mask & (IN_CREATE | IN_MOVED_TO))
{
if (m_fileChangeEventCallback)
{
m_fileChangeEventCallback(filePath);
}
}
else if (event->mask & (IN_DELETE | IN_MOVED_FROM))
{
// 文件被删除或移动
// 可以在此处执行相应的处理操作
}
}
}

inotify_rm_watch(fd, wd);
close(fd);
#endif
}

/*
// 示例用法
int main()
{
FolderMonitor folderMonitor("test");
// 注册文件变更事件的处理函数
folderMonitor.RegisterFileChangeEventCallback([](const std::string &filePath)
{ std::cout << "File changed: " << filePath << std::endl; });
// 启动监视器
folderMonitor.StartMonitoring();
// 持续运行一段时间
std::this_thread::sleep_for(std::chrono::seconds(60));
// 停止监视器
folderMonitor.StopMonitoring();
return 0;
}
*/
Loading

0 comments on commit 499a0cb

Please sign in to comment.