From b8dfad4117495b896ab2f40a4df1f696173aa646 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Wed, 31 Jul 2024 18:41:00 -0700 Subject: [PATCH] Avoid using stat() when searching for testdata directory --- icu4c/source/test/intltest/intltest.cpp | 40 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/icu4c/source/test/intltest/intltest.cpp b/icu4c/source/test/intltest/intltest.cpp index d42b12d48ad4..59cae12c9a90 100644 --- a/icu4c/source/test/intltest/intltest.cpp +++ b/icu4c/source/test/intltest/intltest.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include "unicode/ctest.h" // for str_timeDelta #include "unicode/curramt.h" @@ -1680,24 +1679,36 @@ const char *IntlTest::getSourceTestData(UErrorCode& /*err*/) { return srcDataDir; } -static bool directoryExists(const char* dirName) { - struct stat sb; - return (stat(dirName, &sb) == 0) && ((sb.st_mode & S_IFMT) == S_IFDIR); +static bool fileExists(const char* fileName) { + // Test for `srcDataDir` existing by checking for `srcDataDir`/message2/valid-tests.json + U_ASSERT(fileName != nullptr); + FILE *f = fopen(fileName, "r"); + if (f) { + fclose(f); + return true; + } + return false; } /** * Returns the path to icu/testdata/ */ const char *IntlTest::getSharedTestData(UErrorCode& err) { +#define SOURCE_TARBALL_TOP U_TOPSRCDIR U_FILE_SEP_STRING ".." U_FILE_SEP_STRING +#define REPO_TOP SOURCE_TARBALL_TOP ".." U_FILE_SEP_STRING +#define FILE_NAME U_FILE_SEP_STRING "message2" U_FILE_SEP_STRING "valid-tests.json" const char *srcDataDir = nullptr; + const char *testFile = nullptr; if (U_SUCCESS(err)) { #ifdef U_TOPSRCDIR // Try U_TOPSRCDIR/../testdata (source tarball) - srcDataDir = U_TOPSRCDIR U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING; - if (!directoryExists(srcDataDir)) { + srcDataDir = SOURCE_TARBALL_TOP "testdata" U_FILE_SEP_STRING; + testFile = SOURCE_TARBALL_TOP "testdata" FILE_NAME; + if (!fileExists(testFile)) { // If that doesn't exist, try U_TOPSRCDIR/../../testdata (in-repo) - srcDataDir = U_TOPSRCDIR U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING; - if (!directoryExists(srcDataDir)) { + srcDataDir = REPO_TOP "testdata" U_FILE_SEP_STRING; + testFile = REPO_TOP "testdata" FILE_NAME; + if (!fileExists(testFile)) { // If neither exists, return null err = U_FILE_ACCESS_ERROR; srcDataDir = nullptr; @@ -1706,11 +1717,14 @@ const char *IntlTest::getSharedTestData(UErrorCode& err) { #else // Try ../../../../testdata (if we're in icu/source/test/intltest) // and ../../../../../../testdata (if we're in icu/source/test/intltest/Platform/(Debug|Release) - srcDataDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING; - if (!directoryExists(srcDataDir)) { - srcDataDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING - ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING; - if (!directoryExists(srcDataDir)) { +#define TOP ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING +#define TOP_TOP ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING TOP + srcDataDir = TOP "testdata" U_FILE_SEP_STRING; + testFile = TOP "testdata" FILE_NAME + if (!fileExists(testFile)) { + srcDataDir = TOP_TOP "testdata" U_FILE_SEP_STRING; + testFile = TOP_TOP "testdata" FILE_NAME; + if (!fileExists(testFile)) { err = U_FILE_ACCESS_ERROR; srcDataDir = nullptr; }