sal/osl/w32/tempfile.cxx               |   16 ++++++++
 unotools/source/ucbhelper/tempfile.cxx |   61 +++++++++++++--------------------
 2 files changed, 40 insertions(+), 37 deletions(-)

New commits:
commit 68e2b820489f01c38106a606740ef746c0cf4c9f
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Apr 3 12:17:54 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Apr 3 12:31:33 2024 +0200

    Allow custom "eye catcher" from LO_TESTNAME in tempfiles on Windows
    
    Change-Id: Id8d94af9e03d0c8553d0a7949e4a9259159481cf
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165732
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sal/osl/w32/tempfile.cxx b/sal/osl/w32/tempfile.cxx
index f0065bf2d8ef..30f879573de4 100644
--- a/sal/osl/w32/tempfile.cxx
+++ b/sal/osl/w32/tempfile.cxx
@@ -85,6 +85,20 @@ static oslFileError osl_setup_createTempFile_impl_(
     return osl_error;
 }
 
+static LPCWSTR getEyeCatcher()
+{
+    static const OUString sEyeCatcher = []
+    {
+        OUString eyeCatcher = u"
+#ifdef DBG_UTIL
+        if (const wchar_t* eye = _wgetenv(L"LO_TESTNAME"))
+            eyeCatcher = OUString(o3tl::toU(eye), wcslen(eye) + 1); // 
including terminating nul
+#endif
+        return eyeCatcher;
+    }();
+    return o3tl::toW(sEyeCatcher.getStr());
+}
+
 static oslFileError osl_win32_GetTempFileName_impl_(
     rtl_uString* base_directory, LPWSTR temp_file_name)
 {
@@ -92,7 +106,7 @@ static oslFileError osl_win32_GetTempFileName_impl_(
 
     if (GetTempFileNameW(
             o3tl::toW(rtl_uString_getStr(base_directory)),
-            L"",
+            getEyeCatcher(),
             0,
             temp_file_name) == 0)
     {
diff --git a/unotools/source/ucbhelper/tempfile.cxx 
b/unotools/source/ucbhelper/tempfile.cxx
index 3b2a7c604b4c..149a1413c37a 100644
--- a/unotools/source/ucbhelper/tempfile.cxx
+++ b/unotools/source/ucbhelper/tempfile.cxx
@@ -28,6 +28,7 @@
 #include <unotools/tempfile.hxx>
 #include <rtl/ustring.hxx>
 #include <o3tl/safeint.hxx>
+#include <o3tl/char16_t2wchar_t.hxx>
 #include <osl/mutex.hxx>
 #include <osl/detail/file.h>
 #include <osl/file.hxx>
@@ -273,53 +274,41 @@ OUString lcl_createName(
     return OUString();
 }
 
-OUString CreateTempName_Impl( const OUString* pParent, bool bKeep, bool bDir = 
true )
+const OUString& getEyeCatcher()
 {
-    OUString aEyeCatcher = "lu";
-#ifdef UNX
-#ifdef DBG_UTIL
-    const char* eye = getenv("LO_TESTNAME");
-    if(eye)
+    static const OUString sEyeCatcher = []
     {
-        aEyeCatcher = OUString(eye, strlen(eye), RTL_TEXTENCODING_ASCII_US);
-    }
-#else
-    static const pid_t pid = getpid();
-    static const OUString aPidString = OUString::number(pid);
-    aEyeCatcher += aPidString;
+        OUString eyeCatcher = u"lu"_ustr;
+#ifdef DBG_UTIL
+#ifdef UNX
+        if (const char* eye = getenv("LO_TESTNAME"))
+            eyeCatcher = OUString(eye, strlen(eye), RTL_TEXTENCODING_ASCII_US);
+#elif defined(_WIN32)
+        if (const wchar_t* eye = _wgetenv(L"LO_TESTNAME"))
+            eyeCatcher = OUString(o3tl::toU(eye));
 #endif
+#else
+#ifdef UNX
+        eyeCatcher += OUString::number(getpid());
 #elif defined(_WIN32)
-    static const int pid = _getpid();
-    static const OUString aPidString = OUString::number(pid);
-    aEyeCatcher += aPidString;
+        eyeCatcher += OUString::number(_getpid());
+#endif
 #endif
+        return eyeCatcher;
+    }();
+    return sEyeCatcher;
+}
+
+OUString CreateTempName_Impl( const OUString* pParent, bool bKeep, bool bDir = 
true )
+{
     UniqueTokens t;
-    return lcl_createName( aEyeCatcher, t, u"", pParent, bDir, bKeep,
+    return lcl_createName( getEyeCatcher(), t, u"", pParent, bDir, bKeep,
                            false, false);
 }
 
 OUString CreateTempNameFast()
 {
-    OUString aEyeCatcher = "lu";
-#ifdef UNX
-#ifdef DBG_UTIL
-    const char* eye = getenv("LO_TESTNAME");
-    if(eye)
-    {
-        aEyeCatcher = OUString(eye, strlen(eye), RTL_TEXTENCODING_ASCII_US);
-    }
-#else
-    static const pid_t pid = getpid();
-    static const OUString aPidString = OUString::number(pid);
-    aEyeCatcher += aPidString;
-#endif
-#elif defined(_WIN32)
-    static const int pid = _getpid();
-    static const OUString aPidString = OUString::number(pid);
-    aEyeCatcher += aPidString;
-#endif
-
-    OUString aName = getTempNameBase_Impl() + aEyeCatcher;
+    OUString aName = getTempNameBase_Impl() + getEyeCatcher();
 
     tools::Guid aGuid(tools::Guid::Generate);
 

Reply via email to