From 7eb2fa5b7ec91864dd2b613ddeb1bd3e46232dd0 Mon Sep 17 00:00:00 2001 From: xiongpan Date: Wed, 3 Dec 2025 09:40:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0hdiff=5Fdir=E5=9C=A8qnx?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E7=9A=84=E7=9B=AE=E5=BD=95=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91,=E4=BD=BF=E7=94=A8=5F=5FQNX=5F=5F=E5=AE=8F?= =?UTF-8?q?=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dirDiffPatch/dir_diff/dir_diff_tools.cpp | 111 ++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/dirDiffPatch/dir_diff/dir_diff_tools.cpp b/dirDiffPatch/dir_diff/dir_diff_tools.cpp index 1434344f..c28dfc1b 100644 --- a/dirDiffPatch/dir_diff/dir_diff_tools.cpp +++ b/dirDiffPatch/dir_diff/dir_diff_tools.cpp @@ -102,7 +102,116 @@ void hdiff_dirClose(hdiff_TDirHandle dirHandle){ } } -#else // _WIN32 +#elif defined(_QNX_) + +struct _hdiff_TFindFileData +{ + DIR* handle; + char dirPath[hpatch_kPathMaxSize]; // 保存目录路径 + struct dirent* entry; // 当前目录项 + char fullPath[hpatch_kPathMaxSize]; // 完整路径 + char subName_utf8[hpatch_kPathMaxSize]; // 文件名(UTF-8) +}; + +hdiff_TDirHandle hdiff_dirOpenForRead(const char* dir_utf8) +{ + if(!dir_utf8 || *dir_utf8 == '\0') + { + return 0; + } + + struct _hdiff_TFindFileData* finder = + (struct _hdiff_TFindFileData*)malloc(sizeof(struct _hdiff_TFindFileData)); + if (finder == nullptr) + { + return 0; + } + + // 保存目录路径 + strncpy(finder->dirPath, dir_utf8, hpatch_kPathMaxSize - 1); + finder->dirPath[hpatch_kPathMaxSize - 1] = '\0'; + + finder->handle = opendir(dir_utf8); + if (finder->handle == nullptr) { + // 处理特殊情况:目录存在但为空 + struct stat sb; + if (stat(dir_utf8, &sb) == 0 && S_ISDIR(sb.st_mode)) { + // 目录存在但为空,返回有效句柄 + return finder; + } + free(finder); + return 0; + } + + // 读取第一个条目(类似 FindFirstFile) + finder->entry = readdir(finder->handle); + return finder; +} + +hpatch_BOOL hdiff_dirNext(hdiff_TDirHandle dirHandle,hpatch_TPathType *out_type,const char** out_subName_utf8) +{ + assert(dirHandle != 0); + struct _hdiff_TFindFileData* finder = (struct _hdiff_TFindFileData*)dirHandle; + // 检查目录是否已关闭或遍历完毕 + if (finder->handle == nullptr) + { + *out_subName_utf8 = 0; + return hpatch_TRUE; // 退出 + } + + // 遍历所有目录项 + while ((finder->entry = readdir(finder->handle)) != nullptr) + { + // 跳过 . 和 .. + if (strcmp(finder->entry->d_name, ".") == 0 || + strcmp(finder->entry->d_name, "..") == 0) { + continue; + } + + // 获取文件名 + strncpy(finder->subName_utf8, finder->entry->d_name, hpatch_kPathMaxSize - 1); + finder->subName_utf8[hpatch_kPathMaxSize - 1] = '\0'; + *out_subName_utf8 = finder->subName_utf8; + + // 构造完整路径以用于 stat + snprintf(finder->fullPath, hpatch_kPathMaxSize, "%s/%s", + finder->dirPath, + finder->entry->d_name); + + // 使用 stat 获取文件类型 + struct stat sb; + if (stat(finder->fullPath, &sb) == 0) + { + if (S_ISDIR(sb.st_mode)) { + *out_type = kPathType_dir; + *out_subName_utf8 = finder->fullPath; + } else { + *out_type = kPathType_file; + *out_subName_utf8 = finder->fullPath; + } + return hpatch_TRUE; + } + } + + // 遍历完毕 + *out_subName_utf8 = 0; + return hpatch_TRUE; + +} + +void hdiff_dirClose(hdiff_TDirHandle dirHandle) +{ + struct _hdiff_TFindFileData* finder = (struct _hdiff_TFindFileData*)dirHandle; + if (finder != NULL) + { + if (finder->handle != NULL) { + closedir(finder->handle); + } + free(finder); + } +} + +#else hdiff_TDirHandle hdiff_dirOpenForRead(const char* dir_utf8){ hdiff_TDirHandle h=opendir(dir_utf8); From 44edc8f71e05996b3713cb0b21d29fa534921439 Mon Sep 17 00:00:00 2001 From: xiongpan Date: Fri, 5 Dec 2025 10:18:10 +0800 Subject: [PATCH 2/2] only add operation for no d_type available on platform like qnx in hdiff_dirNext funtion --- dirDiffPatch/dir_diff/dir_diff_tools.cpp | 138 +++++------------------ 1 file changed, 31 insertions(+), 107 deletions(-) diff --git a/dirDiffPatch/dir_diff/dir_diff_tools.cpp b/dirDiffPatch/dir_diff/dir_diff_tools.cpp index c28dfc1b..4c1aa0b9 100644 --- a/dirDiffPatch/dir_diff/dir_diff_tools.cpp +++ b/dirDiffPatch/dir_diff/dir_diff_tools.cpp @@ -102,132 +102,55 @@ void hdiff_dirClose(hdiff_TDirHandle dirHandle){ } } -#elif defined(_QNX_) +#else // _WIN32 -struct _hdiff_TFindFileData -{ - DIR* handle; - char dirPath[hpatch_kPathMaxSize]; // 保存目录路径 - struct dirent* entry; // 当前目录项 - char fullPath[hpatch_kPathMaxSize]; // 完整路径 - char subName_utf8[hpatch_kPathMaxSize]; // 文件名(UTF-8) -}; - -hdiff_TDirHandle hdiff_dirOpenForRead(const char* dir_utf8) -{ - if(!dir_utf8 || *dir_utf8 == '\0') - { - return 0; - } - - struct _hdiff_TFindFileData* finder = - (struct _hdiff_TFindFileData*)malloc(sizeof(struct _hdiff_TFindFileData)); - if (finder == nullptr) - { - return 0; - } - - // 保存目录路径 - strncpy(finder->dirPath, dir_utf8, hpatch_kPathMaxSize - 1); - finder->dirPath[hpatch_kPathMaxSize - 1] = '\0'; - - finder->handle = opendir(dir_utf8); - if (finder->handle == nullptr) { - // 处理特殊情况:目录存在但为空 - struct stat sb; - if (stat(dir_utf8, &sb) == 0 && S_ISDIR(sb.st_mode)) { - // 目录存在但为空,返回有效句柄 - return finder; - } - free(finder); - return 0; - } - - // 读取第一个条目(类似 FindFirstFile) - finder->entry = readdir(finder->handle); - return finder; +hdiff_TDirHandle hdiff_dirOpenForRead(const char* dir_utf8){ + hdiff_TDirHandle h=opendir(dir_utf8); + if (!h) return 0; //error + return h; } -hpatch_BOOL hdiff_dirNext(hdiff_TDirHandle dirHandle,hpatch_TPathType *out_type,const char** out_subName_utf8) -{ - assert(dirHandle != 0); - struct _hdiff_TFindFileData* finder = (struct _hdiff_TFindFileData*)dirHandle; - // 检查目录是否已关闭或遍历完毕 - if (finder->handle == nullptr) - { - *out_subName_utf8 = 0; - return hpatch_TRUE; // 退出 +hpatch_BOOL hdiff_dirNext(hdiff_TDirHandle dirHandle,hpatch_TPathType *out_type,const char** out_subName_utf8){ + assert(dirHandle!=0); + DIR* pdir =(DIR*)dirHandle; + struct dirent* pdirent = readdir(pdir); + if (pdirent==0){ + *out_subName_utf8=0; //finish + return hpatch_TRUE; } - // 遍历所有目录项 - while ((finder->entry = readdir(finder->handle)) != nullptr) +#ifdef __QNX__ + static char fullName_utf8[hpatch_kPathMaxSize]; + static char subName_utf8[hpatch_kPathMaxSize]; + strncpy(subName_utf8, pdirent->d_name, hpatch_kPathMaxSize - 1); + subName_utf8[hpatch_kPathMaxSize - 1] = '\0'; + *out_subName_utf8 = subName_utf8; + while(pdirent = readdir(pdir)) { - // 跳过 . 和 .. - if (strcmp(finder->entry->d_name, ".") == 0 || - strcmp(finder->entry->d_name, "..") == 0) { + if (strcmp(pdirent->d_name, ".") == 0 || strcmp(pdirent->d_name, "..") == 0) + { continue; } - - // 获取文件名 - strncpy(finder->subName_utf8, finder->entry->d_name, hpatch_kPathMaxSize - 1); - finder->subName_utf8[hpatch_kPathMaxSize - 1] = '\0'; - *out_subName_utf8 = finder->subName_utf8; - - // 构造完整路径以用于 stat - snprintf(finder->fullPath, hpatch_kPathMaxSize, "%s/%s", - finder->dirPath, - finder->entry->d_name); - - // 使用 stat 获取文件类型 + snprintf(fullName_utf8, hpatch_kPathMaxSize, "%s/%s", subName_utf8, pdirent->d_name); struct stat sb; - if (stat(finder->fullPath, &sb) == 0) + if (stat(fullName_utf8, &sb) == 0) { - if (S_ISDIR(sb.st_mode)) { + if (S_ISDIR(sb.st_mode)) + { *out_type = kPathType_dir; - *out_subName_utf8 = finder->fullPath; - } else { + *out_subName_utf8 = fullName_utf8; + } + else + { *out_type = kPathType_file; - *out_subName_utf8 = finder->fullPath; + *out_subName_utf8 = fullName_utf8; } return hpatch_TRUE; } } - - // 遍历完毕 *out_subName_utf8 = 0; return hpatch_TRUE; - -} - -void hdiff_dirClose(hdiff_TDirHandle dirHandle) -{ - struct _hdiff_TFindFileData* finder = (struct _hdiff_TFindFileData*)dirHandle; - if (finder != NULL) - { - if (finder->handle != NULL) { - closedir(finder->handle); - } - free(finder); - } -} - #else - -hdiff_TDirHandle hdiff_dirOpenForRead(const char* dir_utf8){ - hdiff_TDirHandle h=opendir(dir_utf8); - if (!h) return 0; //error - return h; -} - -hpatch_BOOL hdiff_dirNext(hdiff_TDirHandle dirHandle,hpatch_TPathType *out_type,const char** out_subName_utf8){ - assert(dirHandle!=0); - DIR* pdir =(DIR*)dirHandle; - struct dirent* pdirent = readdir(pdir); - if (pdirent==0){ - *out_subName_utf8=0; //finish - return hpatch_TRUE; - } - if (pdirent->d_type==DT_DIR){ *out_type=kPathType_dir; *out_subName_utf8=pdirent->d_name; @@ -239,6 +162,7 @@ hpatch_BOOL hdiff_dirNext(hdiff_TDirHandle dirHandle,hpatch_TPathType *out_type, }else{ return hdiff_dirNext(dirHandle,out_type,out_subName_utf8); } +#endif } void hdiff_dirClose(hdiff_TDirHandle dirHandle){