comphelper/source/misc/backupfilehelper.cxx |   37 +++++++++++++++-------------
 1 file changed, 21 insertions(+), 16 deletions(-)

New commits:
commit 7a4772096d307bbfa7842ad7346d63d633ba6c23
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Sep 19 18:49:29 2021 +0200
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sun Sep 19 20:06:57 2021 +0200

    Fix moving temp file in tryDisableHWAcceleration on Windows
    
    In my debug build, using safe mode dialog, Disable hardware acceleration
    setting does not apply. In BackupFileHelper::tryDisableHWAcceleration,
    osl::File::move fails with osl_File_E_ACCES, and internally GetLastError
    after MoveFileExW reports ERROR_SHARING_VIOLATION.
    
    The problem is that the tempfile is still in use by xTempFile. Closing
    it, and making sure that the file doesn't get removed in the dtor, fixes
    the problem.
    
    In case something goes wrong, the tempfile would get removed in app
    cleanup anyway, when session temp directory gets removed.
    
    Change-Id: I4568f60b2b50a4038cba2e7a65033cb57e8b6edb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122274
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/comphelper/source/misc/backupfilehelper.cxx 
b/comphelper/source/misc/backupfilehelper.cxx
index 807ca5878608..d03662381939 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -1910,26 +1910,31 @@ namespace comphelper
         xRootElement->appendChild(lcl_getConfigElement(xDocument, 
"/org.openoffice.Office.Common/VCL",
                                                        "ForceSkiaRaster", 
"true"));
 
-        // write back
-        uno::Reference< xml::sax::XSAXSerializable > xSerializer(xDocument, 
uno::UNO_QUERY);
+        OUString aTempURL;
+        {
+            // use the scope to make sure that the temp file gets properly 
closed before move
 
-        if (!xSerializer.is())
-            return;
+            // write back
+            uno::Reference< xml::sax::XSAXSerializable > 
xSerializer(xDocument, uno::UNO_QUERY);
 
-        // create a SAXWriter
-        uno::Reference< xml::sax::XWriter > const xSaxWriter = 
xml::sax::Writer::create(xContext);
-        uno::Reference< io::XStream > xTempFile = 
io::TempFile::create(xContext);
-        uno::Reference< io::XOutputStream > xOutStrm = 
xTempFile->getOutputStream();
+            if (!xSerializer.is())
+                return;
 
-        // set output stream and do the serialization
-        xSaxWriter->setOutputStream(xOutStrm);
-        xSerializer->serialize(xSaxWriter, uno::Sequence< beans::StringPair 
>());
+            // create a SAXWriter
+            uno::Reference< xml::sax::XWriter > const xSaxWriter = 
xml::sax::Writer::create(xContext);
+            uno::Reference< io::XTempFile > xTempFile = 
io::TempFile::create(xContext);
+            xTempFile->setRemoveFile(false); // avoid removal of tempfile when 
leaving the scope
+            uno::Reference< io::XOutputStream > xOutStrm = 
xTempFile->getOutputStream();
 
-        // get URL from temp file
-        uno::Reference < beans::XPropertySet > xTempFileProps(xTempFile, 
uno::UNO_QUERY);
-        uno::Any aUrl = xTempFileProps->getPropertyValue("Uri");
-        OUString aTempURL;
-        aUrl >>= aTempURL;
+            // set output stream and do the serialization
+            xSaxWriter->setOutputStream(xOutStrm);
+            xSerializer->serialize(xSaxWriter, uno::Sequence< 
beans::StringPair >());
+
+            // get URL from temp file
+            uno::Reference < beans::XPropertySet > xTempFileProps(xTempFile, 
uno::UNO_QUERY);
+            uno::Any aUrl = xTempFileProps->getPropertyValue("Uri");
+            aUrl >>= aTempURL;
+        }
 
         // copy back file
         if (aTempURL.isEmpty() || !DirectoryHelper::fileExists(aTempURL))

Reply via email to