From 738cf411e9315d17c7eb8be47ded643a00dfe5c5 Mon Sep 17 00:00:00 2001
From: Caolán McNamara <caolanm@redhat.com>
Date: Wed, 22 Jul 2015 12:01:00 +0100
Subject: Resolves: tdf#88314 close temp file after each thread completes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

and reopen them when we need their data.

That way we don't have as many open files as substreams in the
package, so we don't run out of file handles

Change-Id: Ic124e275abf15f4578c77ee271d185f40cb844b1
Reviewed-on: https://gerrit.libreoffice.org/17289
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
---
 package/inc/ZipOutputEntry.hxx                 | 15 +++++---
 package/source/zipapi/ZipOutputEntry.cxx       | 50 ++++++++++++++++++--------
 package/source/zippackage/ZipPackageStream.cxx |  2 ++
 3 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/package/inc/ZipOutputEntry.hxx b/package/inc/ZipOutputEntry.hxx
index 2a5aa0483ce8..d8d0ee65c6a6 100644
--- a/package/inc/ZipOutputEntry.hxx
+++ b/package/inc/ZipOutputEntry.hxx
@@ -37,7 +37,8 @@ class ZipOutputEntry
 {
     ::com::sun::star::uno::Sequence< sal_Int8 > m_aDeflateBuffer;
     ZipUtils::Deflater m_aDeflater;
-    css::uno::Reference< css::io::XTempFile > m_xTempFile;
+    css::uno::Reference< css::uno::XComponentContext > m_xContext;
+    OUString m_aTempURL;
     css::uno::Reference< css::io::XOutputStream > m_xOutStream;
 
     ::com::sun::star::uno::Reference< ::com::sun::star::xml::crypto::XCipherContext > m_xCipherContext;
@@ -58,14 +59,18 @@ public:
 
     ~ZipOutputEntry();
 
-    css::uno::Reference< css::io::XInputStream > getData();
+    /* This block of methods is for threaded zipping, where we compress to a temp stream, whose
+       data is retrieved via getData */
+    void createBufferFile();
+    void setParallelDeflateException(const ::css::uno::Any &rAny) { m_aParallelDeflateException = rAny; }
+    css::uno::Reference< css::io::XInputStream > getData() const;
+    ::css::uno::Any getParallelDeflateException() const { return m_aParallelDeflateException; }
+    void closeBufferFile();
+
     ZipEntry* getZipEntry() { return m_pCurrentEntry; }
     ZipPackageStream* getZipPackageStream() { return m_pCurrentStream; }
     bool isEncrypt() { return m_bEncryptCurrentEntry; }
 
-    void setParallelDeflateException(const ::css::uno::Any &rAny) { m_aParallelDeflateException = rAny; }
-    ::css::uno::Any getParallelDeflateException() const { return m_aParallelDeflateException; }
-
     void closeEntry();
     void write(const css::uno::Sequence< sal_Int8 >& rBuffer);
 
diff --git a/package/source/zipapi/ZipOutputEntry.cxx b/package/source/zipapi/ZipOutputEntry.cxx
index de44ae46f6b4..aa85b11ee717 100644
--- a/package/source/zipapi/ZipOutputEntry.cxx
+++ b/package/source/zipapi/ZipOutputEntry.cxx
@@ -21,6 +21,8 @@
 
 #include <com/sun/star/io/TempFile.hpp>
 #include <com/sun/star/packages/zip/ZipConstants.hpp>
+#include <com/sun/star/ucb/SimpleFileAccess.hpp>
+#include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
 #include <comphelper/storagehelper.hxx>
 
 #include <osl/time.h>
@@ -49,38 +51,56 @@ ZipOutputEntry::ZipOutputEntry(
         bool bEncrypt)
 : m_aDeflateBuffer(n_ConstBufferSize)
 , m_aDeflater(DEFAULT_COMPRESSION, true)
+, m_xContext(rxContext)
+, m_xOutStream(rxOutput)
 , m_pCurrentEntry(&rEntry)
 , m_nDigested(0)
 , m_bEncryptCurrentEntry(bEncrypt)
 , m_pCurrentStream(pStream)
 {
-    if (rxOutput.is())
-    {
-        m_xOutStream = rxOutput;
-    }
-    else
-    {
-        m_xTempFile = io::TempFile::create(rxContext);
-        m_xOutStream = m_xTempFile->getOutputStream();
-    }
     assert(m_pCurrentEntry->nMethod == DEFLATED && "Use ZipPackageStream::rawWrite() for STORED entries");
     if (m_bEncryptCurrentEntry)
     {
-        m_xCipherContext = ZipFile::StaticGetCipher( rxContext, pStream->GetEncryptionData(), true );
-        m_xDigestContext = ZipFile::StaticGetDigestContextForChecksum( rxContext, pStream->GetEncryptionData() );
+        m_xCipherContext = ZipFile::StaticGetCipher( m_xContext, pStream->GetEncryptionData(), true );
+        m_xDigestContext = ZipFile::StaticGetDigestContextForChecksum( m_xContext, pStream->GetEncryptionData() );
     }
 }
 
 ZipOutputEntry::~ZipOutputEntry()
 {
+    if (!m_aTempURL.isEmpty())
+    {
+        uno::Reference < ucb::XSimpleFileAccess3 > xAccess(ucb::SimpleFileAccess::create(m_xContext));
+        xAccess->kill(m_aTempURL);
+    }
 }
 
-uno::Reference< io::XInputStream > ZipOutputEntry::getData()
+void ZipOutputEntry::createBufferFile()
+{
+    assert(!m_xOutStream.is() && m_aTempURL.isEmpty() &&
+           "should only be called in the threaded mode where there is no existing stream yet");
+    uno::Reference < beans::XPropertySet > xTempFileProps(
+            io::TempFile::create(m_xContext),
+            uno::UNO_QUERY_THROW );
+    xTempFileProps->setPropertyValue("RemoveFile", uno::makeAny(sal_False));
+    uno::Any aUrl = xTempFileProps->getPropertyValue( "Uri" );
+    aUrl >>= m_aTempURL;
+    assert(!m_aTempURL.isEmpty());
+
+    uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(ucb::SimpleFileAccess::create(m_xContext));
+    m_xOutStream = xTempAccess->openFileWrite(m_aTempURL);
+}
+
+void ZipOutputEntry::closeBufferFile()
 {
     m_xOutStream->closeOutput();
-    uno::Reference< io::XSeekable > xTempSeek(m_xOutStream, UNO_QUERY_THROW);
-    xTempSeek->seek(0);
-    return m_xTempFile->getInputStream();
+    m_xOutStream.clear();
+}
+
+uno::Reference< io::XInputStream > ZipOutputEntry::getData() const
+{
+    uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess(ucb::SimpleFileAccess::create(m_xContext));
+    return xTempAccess->openFileRead(m_aTempURL);
 }
 
 void ZipOutputEntry::closeEntry()
diff --git a/package/source/zippackage/ZipPackageStream.cxx b/package/source/zippackage/ZipPackageStream.cxx
index f3c5361c57e5..f28d891a005a 100644
--- a/package/source/zippackage/ZipPackageStream.cxx
+++ b/package/source/zippackage/ZipPackageStream.cxx
@@ -472,6 +472,7 @@ public:
 private:
     virtual void doWork() SAL_OVERRIDE
     {
+        mpEntry->createBufferFile();
         try
         {
             deflateZipEntry(mpEntry, mxInStream);
@@ -481,6 +482,7 @@ private:
         {
             mpEntry->setParallelDeflateException(::cppu::getCaughtException());
         }
+        mpEntry->closeBufferFile();
     }
 };
 
-- 
cgit