Rebased ref, commits from common ancestor:
commit 8efb8df5bd42b0fc6474bd695df231c0b3b6cec8
Author: Armin Le Grand <[email protected]>
Date: Fri Sep 30 17:54:25 2016 +0200
profilesafe: Corrected error copying existing entry
Change-Id: I5a624c09fec4e4278314e13fc5f693ac085d5e61
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index 5d920d8..5270306 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -103,7 +103,7 @@ namespace
{
sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
sal_uInt64 nBytesTransfer(0);
- sal_uInt64 nSize(getFullFileSize());
+ sal_uInt64 nSize(getPackFileSize());
// set offset in source file - when this is zero, a new file
is to be added
if (osl::File::E_None == maFile->setPos(osl_Pos_Absolut,
sal_Int64(getOffset())))
@@ -140,7 +140,7 @@ namespace
sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
sal_uInt8 aBuffer[BACKUP_FILE_HELPER_BLOCK_SIZE];
sal_uInt64 nBytesTransfer(0);
- sal_uInt64 nSize(getFullFileSize());
+ sal_uInt64 nSize(getPackFileSize());
std::unique_ptr< z_stream > zstream(new z_stream);
memset(zstream.get(), 0, sizeof(*zstream));
@@ -294,21 +294,22 @@ namespace
public:
+ // create new, uncompressed entry
PackedFileEntry(
sal_uInt32 nFullFileSize,
- sal_uInt32 nOffset,
sal_uInt32 nCrc32,
FileSharedPtr& rFile,
bool bDoCompress)
: mnFullFileSize(nFullFileSize),
mnPackFileSize(nFullFileSize),
- mnOffset(nOffset),
+ mnOffset(0),
mnCrc32(nCrc32),
maFile(rFile),
mbDoCompress(bDoCompress)
{
}
+ // create entry to be loaded as header (read_header)
PackedFileEntry()
: mnFullFileSize(0),
mnPackFileSize(0),
@@ -724,12 +725,11 @@ namespace
nCrc32 = createCrc32(rFileCandidate, 0);
}
- // create a file entry for a new file. Offset is set to 0 to
mark
- // the entry as new file entry
+ // create a file entry for a new file. Offset is set
automatically
+ // to 0 to mark the entry as new file entry
maPackedFileEntryVector.push_back(
PackedFileEntry(
static_cast< sal_uInt32 >(nFileSize),
- 0,
nCrc32,
rFileCandidate,
bCompress));
commit 17219aded607838248e072f594f7eadb5891aaf3
Author: Armin Le Grand <[email protected]>
Date: Fri Sep 30 10:12:20 2016 +0200
profilesafe: Added flag for compress
Added more handy flag for choose if added files
should be compressed, some cleanups#
Change-Id: I9c4f5aff58a2807605a9fa1fdd650b7fd64c7ba3
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index e02a855..5d920d8 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -669,7 +669,7 @@ namespace
return bRetval;
}
- bool tryPush(FileSharedPtr& rFileCandidate)
+ bool tryPush(FileSharedPtr& rFileCandidate, bool bCompress)
{
sal_uInt64 nFileSize(0);
@@ -726,17 +726,13 @@ namespace
// create a file entry for a new file. Offset is set to 0 to
mark
// the entry as new file entry
- // the compress flag decides if entries should be compressed
when
- // they get written to the target package
- static bool bUseCompression(true);
-
maPackedFileEntryVector.push_back(
PackedFileEntry(
static_cast< sal_uInt32 >(nFileSize),
0,
nCrc32,
rFileCandidate,
- bUseCompression));
+ bCompress));
mbChanged = true;
}
@@ -837,14 +833,14 @@ namespace comphelper
return OUString(maBase + "/." + maName + ".pack");
}
- bool BackupFileHelper::tryPush()
+ bool BackupFileHelper::tryPush(bool bCompress)
{
if (splitBaseURL() && baseFileExists())
{
PackedFile aPackedFile(getName());
FileSharedPtr aBaseFile(new osl::File(mrBaseURL));
- if (aPackedFile.tryPush(aBaseFile))
+ if (aPackedFile.tryPush(aBaseFile, bCompress))
{
// reduce to allowed number and flush
aPackedFile.tryReduceToNumBackups(mnNumBackups);
diff --git a/configmgr/source/components.cxx b/configmgr/source/components.cxx
index 206d346..17f09a3 100644
--- a/configmgr/source/components.cxx
+++ b/configmgr/source/components.cxx
@@ -613,8 +613,6 @@ Components::Components(
Components::~Components()
{
- SAL_WARN("configmgr", "################# Components::~Components()
#####################");
-
// get flag if _exit was already called which is a sign to not to secure
user config
const bool
bExitWasCalled(comphelper::BackupFileHelper::getExitWasCalled());
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
index 7050500..08c7203 100644
--- a/desktop/source/app/app.cxx
+++ b/desktop/source/app/app.cxx
@@ -569,11 +569,14 @@ void Desktop::Init()
SetBootstrapError( BE_OFFICECONFIG_BROKEN, e.Message );
}
+ // test code for ProfileSafeMode to allow testing the fail
+ // of loading the office configuration initially. To use,
+ // either set to true and compile, or set a breakpoint
+ // in debugger and change the local bool
static bool bTryHardOfficeconfigBroken(false);
if (bTryHardOfficeconfigBroken)
{
- SAL_WARN("configmgr", "################# Desktop::Init()
#####################");
SetBootstrapError(BE_OFFICECONFIG_BROKEN, OUString());
}
}
diff --git a/include/comphelper/backupfilehelper.hxx
b/include/comphelper/backupfilehelper.hxx
index 2c6cc25..7cb2023 100644
--- a/include/comphelper/backupfilehelper.hxx
+++ b/include/comphelper/backupfilehelper.hxx
@@ -21,10 +21,23 @@ namespace comphelper
{
/** Helper class to backup/restore a single file
*
+ * This is a general class to manage backups/restore of the file
+ * given by the URL. The container holding the backups is created
+ * aside the original file, e.g for 'test.txt' a container
+ * called '.test.pack' will be used. If it was not yet backed-up
+ * this container file will be created at the 1st backup and deleted
+ * when the last gets removed. The container holds a stack with a
+ * maximum given number (in the constructor) of copies, these are by
+ * default compressed, but don't have to be (see tryPush).
+ *
+ * Due to being on a low system level here, no UNO API and not much
+ * other tooling can be used, as a consequence for the container a
+ * own simple format is used and e.g. the zip lib directly.
+ *
* You need to hand over the URL of the file to look at and
* a maximum number of allowed copies. That number is internally
- * limited to a max of 10 (see implementation). The number of
- * allowed copies is limited to [1..max].
+ * limited to a absolute max of 10 (see implementation). The number
+ * of allowed copies is limited to [1..max].
*
* Calling tryPush() will check if there is no backup yet or if
* there is one that the file has changed. If yes, a new copy is
@@ -102,10 +115,14 @@ namespace comphelper
* Also may cleanup older backups when NumBackups given in the
* constructor has changed.
*
+ * @param bCompress
+ * Defines if the new backup will be compressed when
+ * added. Default is true
+ *
* @return bool
* returns true if a new backup was actually created
*/
- bool tryPush();
+ bool tryPush(bool bCompress = true);
/** finds out if a restore is possible
*
commit 5924421f24c1d230cb8cc1d39181dc1b3911db75
Author: Armin Le Grand <[email protected]>
Date: Thu Sep 29 18:00:35 2016 +0200
profilesafe: Add values for SecureUserConfig
Added default values for SecureUserConfig values
to soffice.ini/rc which enable the mechanism and
set a maximum of three backups of the users
registrymodifications.xcu file in packed form
Change-Id: I5204b13bd5adc6a43ee228a18b304e8c7aaaab63
diff --git a/instsetoo_native/CustomTarget_setup.mk
b/instsetoo_native/CustomTarget_setup.mk
index 173a8c6..26fc427 100644
--- a/instsetoo_native/CustomTarget_setup.mk
+++ b/instsetoo_native/CustomTarget_setup.mk
@@ -104,6 +104,7 @@ $(call
gb_CustomTarget_get_workdir,instsetoo_native/setup)/$(call gb_Helper_get_
) > $@
# for release-builds (building installers) adjust values in openoffice.lst.in
+# Added 'SecureUserConfig' flags to enable and safe three
registrymodifications.xcu versions
$(call gb_CustomTarget_get_workdir,instsetoo_native/setup)/$(call
gb_Helper_get_rcfile,soffice) :
$(call gb_Output_announce,$(subst $(WORKDIR)/,,$@),$(true),ECH,1)
( \
@@ -118,6 +119,8 @@ $(call
gb_CustomTarget_get_workdir,instsetoo_native/setup)/$(call gb_Helper_get_
&& echo 'ProgressTextBaseline=145' \
&& echo 'ProgressTextColor=255,255,255' \
&& echo 'URE_BOOTSTRAP=$${ORIGIN}/$(call
gb_Helper_get_rcfile,fundamental)' \
+ && echo 'SecureUserConfig=true' \
+ && echo 'SecureUserConfigNumCopies=3' \
) > $@
$(call gb_CustomTarget_get_workdir,instsetoo_native/setup)/$(call
gb_Helper_get_rcfile,uno) :
commit cee78aca6d291d459bf418d27f607b086b7850ff
Author: Armin Le Grand <[email protected]>
Date: Thu Sep 29 15:28:15 2016 +0200
profilesafe: clang warnings corrected
Change-Id: Ieb059baeea323bb48ec5b1cf6b8df09da97cfb93
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index 4d89f7f..e02a855 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -112,12 +112,12 @@ namespace
{
const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
- if (osl::File::E_None !=
maFile->read(reinterpret_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
{
break;
}
- if (osl_File_E_None != osl_writeFile(rTargetHandle,
reinterpret_cast<const void*>(aArray), nToTransfer, &nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ if (osl_File_E_None != osl_writeFile(rTargetHandle,
static_cast<const void*>(aArray), nToTransfer, &nBytesTransfer) ||
nBytesTransfer != nToTransfer)
{
break;
}
@@ -155,7 +155,7 @@ namespace
{
const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
- if (osl::File::E_None !=
maFile->read(reinterpret_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
{
break;
}
@@ -179,7 +179,7 @@ namespace
{
const sal_uInt64
nAvailable(BACKUP_FILE_HELPER_BLOCK_SIZE - zstream->avail_out);
- if (osl_File_E_None !=
osl_writeFile(rTargetHandle, reinterpret_cast<const void*>(aBuffer),
nAvailable, &nBytesTransfer) || nBytesTransfer != nAvailable)
+ if (osl_File_E_None !=
osl_writeFile(rTargetHandle, static_cast<const void*>(aBuffer), nAvailable,
&nBytesTransfer) || nBytesTransfer != nAvailable)
{
bOkay = false;
}
@@ -238,7 +238,7 @@ namespace
{
const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
- if (osl::File::E_None !=
maFile->read(reinterpret_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
{
break;
}
@@ -262,7 +262,7 @@ namespace
{
const sal_uInt64
nAvailable(BACKUP_FILE_HELPER_BLOCK_SIZE - zstream->avail_out);
- if (osl_File_E_None !=
osl_writeFile(rTargetHandle, reinterpret_cast<const void*>(aBuffer),
nAvailable, &nBytesTransfer) || nBytesTransfer != nAvailable)
+ if (osl_File_E_None !=
osl_writeFile(rTargetHandle, static_cast<const void*>(aBuffer), nAvailable,
&nBytesTransfer) || nBytesTransfer != nAvailable)
{
bOkay = false;
}
commit 39b437f65b39da09a8861bbe869c0115c848c159
Author: Armin Le Grand <[email protected]>
Date: Thu Sep 29 09:57:06 2016 +0200
profilesafe: warnings corrected
Change-Id: I7ec1c3030bc2705a7114678a4f84acdc78b88cbc
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index bf9a669..4d89f7f 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -167,9 +167,9 @@ namespace
zstream->avail_out =
BACKUP_FILE_HELPER_BLOCK_SIZE;
zstream->next_out = reinterpret_cast<unsigned
char*>(aBuffer);
#if !defined Z_PREFIX
- const sal_uInt64
nRetval(deflate(zstream.get(), nSize == nToTransfer ? Z_FINISH : Z_NO_FLUSH));
+ const sal_Int64 nRetval(deflate(zstream.get(),
nSize == nToTransfer ? Z_FINISH : Z_NO_FLUSH));
#else
- const sal_uInt64
nRetval(z_deflate(zstream.get(), nSize == nToTransfer ? Z_FINISH : Z_NO_FLUSH));
+ const sal_Int64
nRetval(z_deflate(zstream.get(), nSize == nToTransfer ? Z_FINISH : Z_NO_FLUSH));
#endif
if (Z_STREAM_ERROR == nRetval)
{
@@ -250,9 +250,9 @@ namespace
zstream->avail_out =
BACKUP_FILE_HELPER_BLOCK_SIZE;
zstream->next_out = reinterpret_cast<unsigned
char*>(aBuffer);
#if !defined Z_PREFIX
- const sal_uInt64
nRetval(inflate(zstream.get(), Z_NO_FLUSH));
+ const sal_Int64 nRetval(inflate(zstream.get(),
Z_NO_FLUSH));
#else
- const sal_uInt64
nRetval(z_inflate(zstream.get(), Z_NO_FLUSH));
+ const sal_Int64
nRetval(z_inflate(zstream.get(), Z_NO_FLUSH));
#endif
if (Z_STREAM_ERROR == nRetval)
{
commit 74a5e331fdaf4f5f517bbdb33534c08c00533ec7
Author: Armin Le Grand <[email protected]>
Date: Wed Sep 28 18:14:51 2016 +0200
profilesafe: Added zip deflate/inflate
Stack of files can now deflate/inflate using zip
library, unfortunately have to use direct due to
Deflater/Inflater not being available, had to add
linking against zip.lib. Checked compressed and
uncompressed creation/usage of backup stack
Change-Id: I042cd808c023048a7d7b37c0cc6339820507be29
diff --git a/comphelper/Library_comphelper.mk b/comphelper/Library_comphelper.mk
index 59869a7..344e186 100644
--- a/comphelper/Library_comphelper.mk
+++ b/comphelper/Library_comphelper.mk
@@ -40,6 +40,7 @@ $(eval $(call gb_Library_use_externals,comphelper,\
boost_headers \
icuuc \
icu_headers \
+ zlib \
))
$(eval $(call gb_Library_use_libraries,comphelper,\
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index 8fed178..bf9a669 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -14,9 +14,10 @@
#include <comphelper/backupfilehelper.hxx>
#include <rtl/crc.h>
#include <deque>
+#include <zlib.h>
typedef std::shared_ptr< osl::File > FileSharedPtr;
-static const sal_uInt32 BACKUP_FILE_HELPER_BLOCK_SIZE = 1024;
+static const sal_uInt32 BACKUP_FILE_HELPER_BLOCK_SIZE = 16384;
namespace
{
@@ -86,38 +87,246 @@ namespace
namespace
{
- struct PackedFileEntry
+ class PackedFileEntry
{
private:
- sal_uInt32 mnSize; // size in bytes
- sal_uInt32 mnOffset; // offset in File (zero identifies new
file)
- sal_uInt32 mnCrc32; // checksum
- FileSharedPtr maFile; // file where to find the data (at
offset)
+ sal_uInt32 mnFullFileSize; // size in bytes of unpacked
original file
+ sal_uInt32 mnPackFileSize; // size in bytes in file backup
package (smaller if compressed, same if not)
+ sal_uInt32 mnOffset; // offset in File (zero identifies
new file)
+ sal_uInt32 mnCrc32; // checksum
+ FileSharedPtr maFile; // file where to find the data (at
offset)
+ bool mbDoCompress; // flag if this file is scheduled
to be compredded when written
+
+ bool copy_content_straight(oslFileHandle& rTargetHandle)
+ {
+ if (maFile && osl::File::E_None ==
maFile->open(osl_File_OpenFlag_Read))
+ {
+ sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
+ sal_uInt64 nBytesTransfer(0);
+ sal_uInt64 nSize(getFullFileSize());
+
+ // set offset in source file - when this is zero, a new file
is to be added
+ if (osl::File::E_None == maFile->setPos(osl_Pos_Absolut,
sal_Int64(getOffset())))
+ {
+ while (nSize != 0)
+ {
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
+
+ if (osl::File::E_None !=
maFile->read(reinterpret_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
+
+ if (osl_File_E_None != osl_writeFile(rTargetHandle,
reinterpret_cast<const void*>(aArray), nToTransfer, &nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
+
+ nSize -= nToTransfer;
+ }
+ }
+
+ maFile->close();
+ return (0 == nSize);
+ }
+
+ return false;
+ }
+
+ bool copy_content_compress(oslFileHandle& rTargetHandle)
+ {
+ if (maFile && osl::File::E_None ==
maFile->open(osl_File_OpenFlag_Read))
+ {
+ sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
+ sal_uInt8 aBuffer[BACKUP_FILE_HELPER_BLOCK_SIZE];
+ sal_uInt64 nBytesTransfer(0);
+ sal_uInt64 nSize(getFullFileSize());
+ std::unique_ptr< z_stream > zstream(new z_stream);
+ memset(zstream.get(), 0, sizeof(*zstream));
+
+ if (Z_OK == deflateInit(zstream.get(), Z_BEST_COMPRESSION))
+ {
+ // set offset in source file - when this is zero, a new
file is to be added
+ if (osl::File::E_None == maFile->setPos(osl_Pos_Absolut,
sal_Int64(getOffset())))
+ {
+ bool bOkay(true);
+
+ while (bOkay && nSize != 0)
+ {
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
+
+ if (osl::File::E_None !=
maFile->read(reinterpret_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
+
+ zstream->avail_in = nToTransfer;
+ zstream->next_in = reinterpret_cast<unsigned
char*>(aArray);
+
+ do {
+ zstream->avail_out =
BACKUP_FILE_HELPER_BLOCK_SIZE;
+ zstream->next_out = reinterpret_cast<unsigned
char*>(aBuffer);
+#if !defined Z_PREFIX
+ const sal_uInt64
nRetval(deflate(zstream.get(), nSize == nToTransfer ? Z_FINISH : Z_NO_FLUSH));
+#else
+ const sal_uInt64
nRetval(z_deflate(zstream.get(), nSize == nToTransfer ? Z_FINISH : Z_NO_FLUSH));
+#endif
+ if (Z_STREAM_ERROR == nRetval)
+ {
+ bOkay = false;
+ }
+ else
+ {
+ const sal_uInt64
nAvailable(BACKUP_FILE_HELPER_BLOCK_SIZE - zstream->avail_out);
+
+ if (osl_File_E_None !=
osl_writeFile(rTargetHandle, reinterpret_cast<const void*>(aBuffer),
nAvailable, &nBytesTransfer) || nBytesTransfer != nAvailable)
+ {
+ bOkay = false;
+ }
+ }
+ } while (bOkay && 0 == zstream->avail_out);
+
+ if (!bOkay)
+ {
+ break;
+ }
+
+ nSize -= nToTransfer;
+ }
+
+#if !defined Z_PREFIX
+ deflateEnd(zstream.get());
+#else
+ z_deflateEnd(zstream.get());
+#endif
+ }
+ }
+
+ maFile->close();
+
+ // get compressed size and add to entry
+ if (mnFullFileSize == mnPackFileSize && mnFullFileSize ==
zstream->total_in)
+ {
+ mnPackFileSize = zstream->total_out;
+ }
+
+ return (0 == nSize);
+ }
+
+ return false;
+ }
+
+ bool copy_content_uncompress(oslFileHandle& rTargetHandle)
+ {
+ if (maFile && osl::File::E_None ==
maFile->open(osl_File_OpenFlag_Read))
+ {
+ sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
+ sal_uInt8 aBuffer[BACKUP_FILE_HELPER_BLOCK_SIZE];
+ sal_uInt64 nBytesTransfer(0);
+ sal_uInt64 nSize(getPackFileSize());
+ std::unique_ptr< z_stream > zstream(new z_stream);
+ memset(zstream.get(), 0, sizeof(*zstream));
+
+ if (Z_OK == inflateInit(zstream.get()))
+ {
+ // set offset in source file - when this is zero, a new
file is to be added
+ if (osl::File::E_None == maFile->setPos(osl_Pos_Absolut,
sal_Int64(getOffset())))
+ {
+ bool bOkay(true);
+
+ while (bOkay && nSize != 0)
+ {
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
+
+ if (osl::File::E_None !=
maFile->read(reinterpret_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
+
+ zstream->avail_in = nToTransfer;
+ zstream->next_in = reinterpret_cast<unsigned
char*>(aArray);
+
+ do {
+ zstream->avail_out =
BACKUP_FILE_HELPER_BLOCK_SIZE;
+ zstream->next_out = reinterpret_cast<unsigned
char*>(aBuffer);
+#if !defined Z_PREFIX
+ const sal_uInt64
nRetval(inflate(zstream.get(), Z_NO_FLUSH));
+#else
+ const sal_uInt64
nRetval(z_inflate(zstream.get(), Z_NO_FLUSH));
+#endif
+ if (Z_STREAM_ERROR == nRetval)
+ {
+ bOkay = false;
+ }
+ else
+ {
+ const sal_uInt64
nAvailable(BACKUP_FILE_HELPER_BLOCK_SIZE - zstream->avail_out);
+
+ if (osl_File_E_None !=
osl_writeFile(rTargetHandle, reinterpret_cast<const void*>(aBuffer),
nAvailable, &nBytesTransfer) || nBytesTransfer != nAvailable)
+ {
+ bOkay = false;
+ }
+ }
+ } while (bOkay && 0 == zstream->avail_out);
+
+ if (!bOkay)
+ {
+ break;
+ }
+
+ nSize -= nToTransfer;
+ }
+
+#if !defined Z_PREFIX
+ deflateEnd(zstream.get());
+#else
+ z_deflateEnd(zstream.get());
+#endif
+ }
+ }
+
+ maFile->close();
+ return (0 == nSize);
+ }
+
+ return false;
+ }
+
public:
PackedFileEntry(
- sal_uInt32 nSize,
+ sal_uInt32 nFullFileSize,
sal_uInt32 nOffset,
sal_uInt32 nCrc32,
- FileSharedPtr& rFile)
- : mnSize(nSize),
+ FileSharedPtr& rFile,
+ bool bDoCompress)
+ : mnFullFileSize(nFullFileSize),
+ mnPackFileSize(nFullFileSize),
mnOffset(nOffset),
mnCrc32(nCrc32),
- maFile(rFile)
+ maFile(rFile),
+ mbDoCompress(bDoCompress)
{
}
PackedFileEntry()
- : mnSize(0),
+ : mnFullFileSize(0),
+ mnPackFileSize(0),
mnOffset(0),
mnCrc32(0),
- maFile()
+ maFile(),
+ mbDoCompress(false)
+ {
+ }
+
+ sal_uInt32 getFullFileSize() const
{
+ return mnFullFileSize;
}
- sal_uInt32 getSize() const
+ sal_uInt32 getPackFileSize() const
{
- return mnSize;
+ return mnPackFileSize;
}
sal_uInt32 getOffset() const
@@ -142,10 +351,10 @@ namespace
sal_uInt8 aArray[4];
sal_uInt64 nBaseRead(0);
- // read and compute entry size
+ // read and compute full file size
if (osl::File::E_None ==
maFile->read(static_cast<void*>(aArray), 4, nBaseRead) && 4 == nBaseRead)
{
- mnSize = (sal_uInt32(aArray[0]) << 24) +
(sal_uInt32(aArray[1]) << 16) + (sal_uInt32(aArray[2]) << 8) +
sal_uInt32(aArray[3]);
+ mnFullFileSize = (sal_uInt32(aArray[0]) << 24) +
(sal_uInt32(aArray[1]) << 16) + (sal_uInt32(aArray[2]) << 8) +
sal_uInt32(aArray[3]);
}
else
{
@@ -157,6 +366,20 @@ namespace
{
mnCrc32 = (sal_uInt32(aArray[0]) << 24) +
(sal_uInt32(aArray[1]) << 16) + (sal_uInt32(aArray[2]) << 8) +
sal_uInt32(aArray[3]);
}
+ else
+ {
+ return false;
+ }
+
+ // read and compute packed size
+ if (osl::File::E_None ==
maFile->read(static_cast<void*>(aArray), 4, nBaseRead) && 4 == nBaseRead)
+ {
+ mnPackFileSize = (sal_uInt32(aArray[0]) << 24) +
(sal_uInt32(aArray[1]) << 16) + (sal_uInt32(aArray[2]) << 8) +
sal_uInt32(aArray[3]);
+ }
+ else
+ {
+ return false;
+ }
return true;
}
@@ -169,18 +392,18 @@ namespace
sal_uInt8 aArray[4];
sal_uInt64 nBaseWritten(0);
- // write size
- aArray[0] = sal_uInt8((mnSize & 0xff000000) >> 24);
- aArray[1] = sal_uInt8((mnSize & 0x00ff0000) >> 16);
- aArray[2] = sal_uInt8((mnSize & 0x0000ff00) >> 8);
- aArray[3] = sal_uInt8(mnSize & 0x000000ff);
+ // write full file size
+ aArray[0] = sal_uInt8((mnFullFileSize & 0xff000000) >> 24);
+ aArray[1] = sal_uInt8((mnFullFileSize & 0x00ff0000) >> 16);
+ aArray[2] = sal_uInt8((mnFullFileSize & 0x0000ff00) >> 8);
+ aArray[3] = sal_uInt8(mnFullFileSize & 0x000000ff);
if (osl_File_E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
{
return false;
}
- // for each entry, write crc32
+ // write crc32
aArray[0] = sal_uInt8((mnCrc32 & 0xff000000) >> 24);
aArray[1] = sal_uInt8((mnCrc32 & 0x00ff0000) >> 16);
aArray[2] = sal_uInt8((mnCrc32 & 0x0000ff00) >> 8);
@@ -191,55 +414,51 @@ namespace
return false;
}
+ // write packed file size
+ aArray[0] = sal_uInt8((mnPackFileSize & 0xff000000) >> 24);
+ aArray[1] = sal_uInt8((mnPackFileSize & 0x00ff0000) >> 16);
+ aArray[2] = sal_uInt8((mnPackFileSize & 0x0000ff00) >> 8);
+ aArray[3] = sal_uInt8(mnPackFileSize & 0x000000ff);
+
+ if (osl_File_E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
+ {
+ return false;
+ }
+
return true;
}
- bool copy_content(oslFileHandle& rTargetHandle, bool bInflate)
+ bool copy_content(oslFileHandle& rTargetHandle, bool bUncompress)
{
- if (maFile && osl::File::E_None ==
maFile->open(osl_File_OpenFlag_Read))
+ if (bUncompress)
{
- sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
- sal_uInt64 nBytesTransfer(0);
- sal_uInt64 nSize(getSize());
- const bool bNewFile(0 == getOffset());
-
- // set offset in source file - when this is zero, a new file
is to be added
- if (osl::File::E_None == maFile->setPos(osl_Pos_Absolut,
sal_Int64(getOffset())))
+ if (getFullFileSize() == getPackFileSize())
{
- if (!bInflate)
- {
- // copy-back, deflate file
- }
- else if (bNewFile)
- {
- // new file gets added, inflate initially
- }
-
- while (nSize != 0)
- {
- const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
-
- if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
- {
- break;
- }
-
- if (osl_File_E_None != osl_writeFile(rTargetHandle,
static_cast<const void*>(aArray), nToTransfer, &nBytesTransfer) ||
nBytesTransfer != nToTransfer)
- {
- break;
- }
-
- nSize -= nToTransfer;
- }
+ // not compressed, just copy
+ return copy_content_straight(rTargetHandle);
+ }
+ else
+ {
+ // compressed, need to uncompress on copy
+ return copy_content_uncompress(rTargetHandle);
+ }
+ }
+ else if (0 == getOffset())
+ {
+ if (mbDoCompress)
+ {
+ // compressed wanted, need to compress on copy
+ return copy_content_compress(rTargetHandle);
+ }
+ else
+ {
+ // not compressed, straight copy
+ return copy_content_straight(rTargetHandle);
}
-
- maFile->close();
-
- return (0 == nSize);
}
else
{
- return false;
+ return copy_content_straight(rTargetHandle);
}
}
};
@@ -288,13 +507,13 @@ namespace
// if there are entries (and less than max),
read them
if (nEntries >= 1 && nEntries <= 10)
{
- // offset in souce file starts with 8Byte
for header+numEntries and
- // 8byte for each entry (size and crc32)
- sal_uInt32 nOffset(8 + (8 * nEntries));
+ // offset in souce file starts with 8 Byte
for header + numEntries and
+ // 12 byte for each entry (size, crc32 and
PackedSize)
+ sal_uInt32 nOffset(8 + (12 * nEntries));
for (sal_uInt32 a(0); a < nEntries; a++)
{
- // create new entry, read header (size
and crc) and
+ // create new entry, read header
(size, crc and PackedSize),
// set offset and source file
PackedFileEntry aEntry;
@@ -304,7 +523,7 @@ namespace
maPackedFileEntryVector.push_back(aEntry);
// increase offset for next entry
- nOffset += aEntry.getSize();
+ nOffset +=
aEntry.getPackFileSize();
}
else
{
@@ -373,24 +592,56 @@ namespace
// write number of entries
if (osl_File_E_None == osl_writeFile(aHandle,
static_cast<const void*>(aArray), 4, &nBaseWritten) && 4 == nBaseWritten)
{
- // write headers
- for (auto& candidateA : maPackedFileEntryVector)
+ if (bRetval)
+ {
+ // write placeholder for headers. Due to the
fact that
+ // PackFileSize for newly added files gets set
during
+ // writing the content entry, write headers
after content
+ // is written. To do so, write placeholders
here. We know
+ // the number of entries to write
+ const sal_uInt32 nWriteSize(3 *
maPackedFileEntryVector.size());
+ aArray[0] = aArray[1] = aArray[2] = aArray[3]
= 0;
+
+ for (sal_uInt32 a(0); bRetval && a <
nWriteSize; a++)
+ {
+ if (osl_File_E_None !=
osl_writeFile(aHandle, static_cast<const void*>(aArray), 4, &nBaseWritten) || 4
!= nBaseWritten)
+ {
+ bRetval = false;
+ }
+ }
+ }
+
+ if (bRetval)
+ {
+ // write contents - this may adapt
PackFileSize for new
+ // files
+ for (auto& candidate : maPackedFileEntryVector)
+ {
+ if (!candidate.copy_content(aHandle,
false))
+ {
+ bRetval = false;
+ break;
+ }
+ }
+ }
+
+ if (bRetval)
{
- if (!candidateA.write_header(aHandle))
+ // seek back to header start (at position 8)
+ if (osl_File_E_None != osl_setFilePos(aHandle,
osl_Pos_Absolut, sal_Int64(8)))
{
- // error
bRetval = false;
- break;
}
}
if (bRetval)
{
- // write contents
- for (auto& candidateB :
maPackedFileEntryVector)
+ // write headers
+ for (auto& candidate : maPackedFileEntryVector)
{
- if (!candidateB.copy_content(aHandle,
true))
+ if (!candidate.write_header(aHandle))
{
+ // error
bRetval = false;
break;
}
@@ -442,7 +693,7 @@ namespace
// already backups there, check if different from last entry
const PackedFileEntry& aLastEntry =
maPackedFileEntryVector.back();
- if (aLastEntry.getSize() != static_cast<sal_uInt32>(nFileSize))
+ if (aLastEntry.getFullFileSize() !=
static_cast<sal_uInt32>(nFileSize))
{
// different size, different file
bNeedToAdd = true;
@@ -475,12 +726,17 @@ namespace
// create a file entry for a new file. Offset is set to 0 to
mark
// the entry as new file entry
+ // the compress flag decides if entries should be compressed
when
+ // they get written to the target package
+ static bool bUseCompression(true);
+
maPackedFileEntryVector.push_back(
PackedFileEntry(
static_cast< sal_uInt32 >(nFileSize),
0,
nCrc32,
- rFileCandidate));
+ rFileCandidate,
+ bUseCompression));
mbChanged = true;
}
@@ -497,7 +753,10 @@ namespace
// already backups there, check if different from last entry
PackedFileEntry& aLastEntry = maPackedFileEntryVector.back();
- bRetval = aLastEntry.copy_content(rHandle, false);
+ // here the uncompress flag has to be determined, true
+ // means to add the file compressed, false means to add it
+ // uncompressed
+ bRetval = aLastEntry.copy_content(rHandle, true);
if (bRetval)
{
commit f25fe6cf2059f59dea37954a478adf7f7a4860be
Author: Armin Le Grand <[email protected]>
Date: Wed Sep 28 12:31:44 2016 +0200
profilesafe: fixes to setPos and warnings
Change-Id: Ib286e2a3f25b0085a1e3ae4f50c9ff1ff3a5dcf5
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index b679431..8fed178 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -57,22 +57,23 @@ namespace
// set offset in source file - should be zero due to crc32 should
// only be needed to be created for new entries, gets loaded with
old
// ones
- rCandidate->setPos(0, sal_Int64(nOffset));
-
- while (nSize != 0)
+ if (osl::File::E_None == rCandidate->setPos(osl_Pos_Absolut,
sal_Int64(nOffset)))
{
- const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
-
- if (osl::File::E_None ==
rCandidate->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) &&
nBytesTransfer == nToTransfer)
- {
- // add to crc and reduce size
- nCrc32 = rtl_crc32(nCrc32, static_cast<void*>(aArray),
static_cast<sal_uInt32>(nBytesTransfer));
- nSize -= nToTransfer;
- }
- else
+ while (nSize != 0)
{
- // error - reset to zero again
- nSize = nCrc32 = 0;
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
+
+ if (osl::File::E_None ==
rCandidate->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) &&
nBytesTransfer == nToTransfer)
+ {
+ // add to crc and reduce size
+ nCrc32 = rtl_crc32(nCrc32, static_cast<void*>(aArray),
static_cast<sal_uInt32>(nBytesTransfer));
+ nSize -= nToTransfer;
+ }
+ else
+ {
+ // error - reset to zero again
+ nSize = nCrc32 = 0;
+ }
}
}
@@ -174,7 +175,7 @@ namespace
aArray[2] = sal_uInt8((mnSize & 0x0000ff00) >> 8);
aArray[3] = sal_uInt8(mnSize & 0x000000ff);
- if (osl::File::E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
+ if (osl_File_E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
{
return false;
}
@@ -185,7 +186,7 @@ namespace
aArray[2] = sal_uInt8((mnCrc32 & 0x0000ff00) >> 8);
aArray[3] = sal_uInt8(mnCrc32 & 0x000000ff);
- if (osl::File::E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
+ if (osl_File_E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
{
return false;
}
@@ -200,35 +201,36 @@ namespace
sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
sal_uInt64 nBytesTransfer(0);
sal_uInt64 nSize(getSize());
-
- // set offset in source file - when this is zero, a new file
is to be added
const bool bNewFile(0 == getOffset());
- maFile->setPos(0, sal_Int64(getOffset()));
- if (!bInflate)
- {
- // copy-back, deflate file
- }
- else if (bNewFile)
- {
- // new file gets added, inflate initially
- }
-
- while (nSize != 0)
+ // set offset in source file - when this is zero, a new file
is to be added
+ if (osl::File::E_None == maFile->setPos(osl_Pos_Absolut,
sal_Int64(getOffset())))
{
- const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
-
- if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ if (!bInflate)
{
- break;
+ // copy-back, deflate file
}
-
- if (osl::File::E_None != osl_writeFile(rTargetHandle,
static_cast<const void*>(aArray), nToTransfer, &nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ else if (bNewFile)
{
- break;
+ // new file gets added, inflate initially
}
- nSize -= nToTransfer;
+ while (nSize != 0)
+ {
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
+
+ if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
+
+ if (osl_File_E_None != osl_writeFile(rTargetHandle,
static_cast<const void*>(aArray), nToTransfer, &nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
+
+ nSize -= nToTransfer;
+ }
}
maFile->close();
@@ -250,13 +252,13 @@ namespace
class PackedFile
{
private:
- const OUString& mrURL;
+ const OUString maURL;
PackedFileEntryVector maPackedFileEntryVector;
bool mbChanged;
public:
PackedFile(const OUString& rURL)
- : mrURL(rURL),
+ : maURL(rURL),
maPackedFileEntryVector(),
mbChanged(false)
{
@@ -328,7 +330,7 @@ namespace
if (maPackedFileEntryVector.empty())
{
// on error or no data get rid of pack file
- osl::File::remove(mrURL);
+ osl::File::remove(maURL);
}
}
@@ -339,7 +341,7 @@ namespace
if (maPackedFileEntryVector.empty())
{
// get rid of (now?) empty pack file
- osl::File::remove(mrURL);
+ osl::File::remove(maURL);
}
else if (mbChanged)
{
@@ -360,7 +362,7 @@ namespace
aArray[3] = 'K';
// write File_ID
- if (osl::File::E_None == osl_writeFile(aHandle,
static_cast<const void*>(aArray), 4, &nBaseWritten) && 4 == nBaseWritten)
+ if (osl_File_E_None == osl_writeFile(aHandle,
static_cast<const void*>(aArray), 4, &nBaseWritten) && 4 == nBaseWritten)
{
const sal_uInt32 nSize(maPackedFileEntryVector.size());
aArray[0] = sal_uInt8((nSize & 0xff000000) >> 24);
@@ -369,7 +371,7 @@ namespace
aArray[3] = sal_uInt8(nSize & 0x000000ff);
// write number of entries
- if (osl::File::E_None == osl_writeFile(aHandle,
static_cast<const void*>(aArray), 4, &nBaseWritten) && 4 == nBaseWritten)
+ if (osl_File_E_None == osl_writeFile(aHandle,
static_cast<const void*>(aArray), 4, &nBaseWritten) && 4 == nBaseWritten)
{
// write headers
for (auto& candidateA : maPackedFileEntryVector)
@@ -405,8 +407,8 @@ namespace
{
// copy over existing file by first deleting original
// and moving the temp file to old original
- osl::File::remove(mrURL);
- osl::File::move(aTempURL, mrURL);
+ osl::File::remove(maURL);
+ osl::File::move(aTempURL, maURL);
}
// delete temp file (in all cases - it may be moved already)
commit 55e8689823c37b5018979865355181bce16f733e
Author: Armin Le Grand <[email protected]>
Date: Wed Sep 28 11:38:27 2016 +0200
profilesafe: diverse adaptions
Use Vector for better mem performance for sorting,
defined static const for buffer size of manipulation,
prepare inflate/deflate usages
Change-Id: I864a6c2505dcf5e2c8ee265a5c04af01b65c3af4
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index 628ec2b..b679431 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -16,6 +16,7 @@
#include <deque>
typedef std::shared_ptr< osl::File > FileSharedPtr;
+static const sal_uInt32 BACKUP_FILE_HELPER_BLOCK_SIZE = 1024;
namespace
{
@@ -47,7 +48,7 @@ namespace
if (rCandidate && osl::File::E_None ==
rCandidate->open(osl_File_OpenFlag_Read))
{
- sal_uInt8 aArray[1024];
+ sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
sal_uInt64 nBytesTransfer(0);
sal_uInt64 nSize(0);
@@ -60,7 +61,7 @@ namespace
while (nSize != 0)
{
- const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)1024));
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
if (osl::File::E_None ==
rCandidate->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) &&
nBytesTransfer == nToTransfer)
{
@@ -192,20 +193,30 @@ namespace
return true;
}
- bool copy_content(oslFileHandle& rTargetHandle)
+ bool copy_content(oslFileHandle& rTargetHandle, bool bInflate)
{
if (maFile && osl::File::E_None ==
maFile->open(osl_File_OpenFlag_Read))
{
- sal_uInt8 aArray[1024];
+ sal_uInt8 aArray[BACKUP_FILE_HELPER_BLOCK_SIZE];
sal_uInt64 nBytesTransfer(0);
sal_uInt64 nSize(getSize());
// set offset in source file - when this is zero, a new file
is to be added
+ const bool bNewFile(0 == getOffset());
maFile->setPos(0, sal_Int64(getOffset()));
+ if (!bInflate)
+ {
+ // copy-back, deflate file
+ }
+ else if (bNewFile)
+ {
+ // new file gets added, inflate initially
+ }
+
while (nSize != 0)
{
- const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)1024));
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)BACKUP_FILE_HELPER_BLOCK_SIZE));
if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
{
@@ -376,7 +387,7 @@ namespace
// write contents
for (auto& candidateB :
maPackedFileEntryVector)
{
- if (!candidateB.copy_content(aHandle))
+ if (!candidateB.copy_content(aHandle,
true))
{
bRetval = false;
break;
@@ -484,7 +495,7 @@ namespace
// already backups there, check if different from last entry
PackedFileEntry& aLastEntry = maPackedFileEntryVector.back();
- bRetval = aLastEntry.copy_content(rHandle);
+ bRetval = aLastEntry.copy_content(rHandle, false);
if (bRetval)
{
diff --git a/configmgr/source/writemodfile.cxx
b/configmgr/source/writemodfile.cxx
index 15c9ffa..381ab00 100644
--- a/configmgr/source/writemodfile.cxx
+++ b/configmgr/source/writemodfile.cxx
@@ -401,10 +401,10 @@ void writeNode(
}
// helpers to allow sorting of configmgr::Modifications::Node
-typedef std::pair< const rtl::OUString, configmgr::Modifications::Node >
aPairEntry;
+typedef std::pair< const rtl::OUString, configmgr::Modifications::Node >
ModNodePairEntry;
struct PairEntrySorter
{
- bool operator() (const aPairEntry* pValue1, const aPairEntry* pValue2)
const
+ bool operator() (const ModNodePairEntry* pValue1, const ModNodePairEntry*
pValue2) const
{
return pValue1->first.compareTo(pValue2->first) < 0;
}
@@ -472,19 +472,20 @@ void writeModifications(
// copy configmgr::Modifications::Node's to a sortable list. Use
pointers
// to just reference the data instead of copying it
- std::list< const aPairEntry* > aPairEntryList;
+ std::vector< const ModNodePairEntry* > ModNodePairEntryVector;
+ ModNodePairEntryVector.reserve(modifications.children.size());
for (const auto& rCand : modifications.children)
{
- aPairEntryList.push_back(&rCand);
+ ModNodePairEntryVector.push_back(&rCand);
}
// sort the list
- aPairEntryList.sort(PairEntrySorter());
+ std::sort(ModNodePairEntryVector.begin(),
ModNodePairEntryVector.end(), PairEntrySorter());
// now use the list to write entries in sorted order
// instead of random as from the unordered map
- for (const auto & i : aPairEntryList)
+ for (const auto & i : ModNodePairEntryVector)
{
writeModifications(
components, handle, pathRep, node, i->first,
@@ -637,19 +638,20 @@ void writeModFile(
// copy configmgr::Modifications::Node's to a sortable list. Use pointers
// to just reference the data instead of copying it
- std::list< const aPairEntry* > aPairEntryList;
+ std::vector< const ModNodePairEntry* > ModNodePairEntryVector;
+
ModNodePairEntryVector.reserve(data.modifications.getRoot().children.size());
for (const auto& rCand : data.modifications.getRoot().children)
{
- aPairEntryList.push_back(&rCand);
+ ModNodePairEntryVector.push_back(&rCand);
}
// sort the list
- aPairEntryList.sort(PairEntrySorter());
+ std::sort(ModNodePairEntryVector.begin(), ModNodePairEntryVector.end(),
PairEntrySorter());
// now use the list to write entries in sorted order
// instead of random as from the unordered map
- for (const auto& j : aPairEntryList)
+ for (const auto& j : ModNodePairEntryVector)
{
writeModifications(
components, tmp, "", rtl::Reference< Node >(), j->first,
commit 5808fff4819b81b8c2e258d8b511344cdfda7d77
Author: Armin Le Grand <[email protected]>
Date: Fri Sep 23 18:21:54 2016 +0200
profilesafe: backups in single *.pack file
Enhanced helper classes for BackupFileHelper to
allow writing a stack of rescued last valid
configuration files to a single file package.
Added configuration values for enabling this and
defining the number of entries, added max entry
limitation. Using FileSize and CRC32 now to dectect
if config file did change. To make this work I added
sorting to writing the configuration so that with no
change the same configuration file is written (this
was not the case before)
Change-Id: I6a175d3d9cb5b947410e8cd00772a521dd81182f
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index 7a353b8..628ec2b 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -9,232 +9,657 @@
#include <sal/config.h>
-#include <comphelper/backupfilehelper.hxx>
#include <rtl/ustring.hxx>
+#include <rtl/bootstrap.hxx>
+#include <comphelper/backupfilehelper.hxx>
+#include <rtl/crc.h>
+#include <deque>
-namespace comphelper
-{
- sal_uInt16 BackupFileHelper::mnMaxAllowedBackups = 10;
- bool BackupFileHelper::mbExitWasCalled = false;
+typedef std::shared_ptr< osl::File > FileSharedPtr;
- BackupFileHelper::BackupFileHelper(
- const OUString& rBaseURL,
- sal_uInt16 nNumBackups)
- : mrBaseURL(rBaseURL),
- mnNumBackups(::std::min(::std::max(nNumBackups, sal_uInt16(1)),
mnMaxAllowedBackups)),
- maBase(),
- maExt(),
- maBaseFile(rBaseURL),
- mbBaseFileIsOpen(false)
+namespace
+{
+ OUString splitAtLastToken(const OUString& rSrc, sal_Unicode aToken,
OUString& rRight)
{
- }
+ const sal_Int32 nIndex(rSrc.lastIndexOf(aToken));
+ OUString aRetval;
- void BackupFileHelper::setExitWasCalled()
- {
- mbExitWasCalled = true;
- }
+ if (-1 == nIndex)
+ {
+ aRetval = rSrc;
+ }
+ else if (nIndex > 0)
+ {
+ aRetval = rSrc.copy(0, nIndex);
+ }
- bool BackupFileHelper::getExitWasCalled()
- {
- return mbExitWasCalled;
+ if (rSrc.getLength() > nIndex + 1)
+ {
+ rRight = rSrc.copy(nIndex + 1);
+ }
+
+ return aRetval;
}
- bool BackupFileHelper::tryPush()
+ sal_uInt32 createCrc32(FileSharedPtr& rCandidate, sal_uInt32 nOffset)
{
- if (isDifferentOrNew())
+ sal_uInt32 nCrc32(0);
+
+ if (rCandidate && osl::File::E_None ==
rCandidate->open(osl_File_OpenFlag_Read))
{
- // the new file is different or new, create a new first backup
- // rename/move/cleanup other backup files, make space for new
first backup
- push();
+ sal_uInt8 aArray[1024];
+ sal_uInt64 nBytesTransfer(0);
+ sal_uInt64 nSize(0);
- // copy new one to now free 1st position
- osl::File::copy(mrBaseURL, getName(1));
+ rCandidate->getSize(nSize);
- return true;
+ // set offset in source file - should be zero due to crc32 should
+ // only be needed to be created for new entries, gets loaded with
old
+ // ones
+ rCandidate->setPos(0, sal_Int64(nOffset));
+
+ while (nSize != 0)
+ {
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)1024));
+
+ if (osl::File::E_None ==
rCandidate->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) &&
nBytesTransfer == nToTransfer)
+ {
+ // add to crc and reduce size
+ nCrc32 = rtl_crc32(nCrc32, static_cast<void*>(aArray),
static_cast<sal_uInt32>(nBytesTransfer));
+ nSize -= nToTransfer;
+ }
+ else
+ {
+ // error - reset to zero again
+ nSize = nCrc32 = 0;
+ }
+ }
+
+ rCandidate->close();
}
- return false;
+ return nCrc32;
}
+}
- bool BackupFileHelper::isPopPossible()
+namespace
+{
+ struct PackedFileEntry
{
- return firstExists();
- }
+ private:
+ sal_uInt32 mnSize; // size in bytes
+ sal_uInt32 mnOffset; // offset in File (zero identifies new
file)
+ sal_uInt32 mnCrc32; // checksum
+ FileSharedPtr maFile; // file where to find the data (at
offset)
+
+ public:
+ PackedFileEntry(
+ sal_uInt32 nSize,
+ sal_uInt32 nOffset,
+ sal_uInt32 nCrc32,
+ FileSharedPtr& rFile)
+ : mnSize(nSize),
+ mnOffset(nOffset),
+ mnCrc32(nCrc32),
+ maFile(rFile)
+ {
+ }
- bool BackupFileHelper::tryPop()
- {
- if (firstExists())
+ PackedFileEntry()
+ : mnSize(0),
+ mnOffset(0),
+ mnCrc32(0),
+ maFile()
{
- // first copy exists, copy over original and delete
- const OUString aOneName(getName(1));
- maBaseFile.close();
- osl::File::copy(aOneName, mrBaseURL);
- osl::File::remove(aOneName);
+ }
- // rename/move/cleanup other backup files
- pop();
+ sal_uInt32 getSize() const
+ {
+ return mnSize;
+ }
- return true;
+ sal_uInt32 getOffset() const
+ {
+ return mnOffset;
}
- return false;
- }
+ sal_uInt32 getCrc32() const
+ {
+ return mnCrc32;
+ }
- rtl::OUString BackupFileHelper::getName(sal_uInt16 n)
- {
- if (maExt.isEmpty())
+ bool read_header(
+ FileSharedPtr& rFile,
+ sal_uInt32 nOffset)
{
- return OUString(maBase + "_" + OUString::number(n));
+ mnOffset = nOffset;
+ maFile = rFile;
+
+ if (maFile)
+ {
+ sal_uInt8 aArray[4];
+ sal_uInt64 nBaseRead(0);
+
+ // read and compute entry size
+ if (osl::File::E_None ==
maFile->read(static_cast<void*>(aArray), 4, nBaseRead) && 4 == nBaseRead)
+ {
+ mnSize = (sal_uInt32(aArray[0]) << 24) +
(sal_uInt32(aArray[1]) << 16) + (sal_uInt32(aArray[2]) << 8) +
sal_uInt32(aArray[3]);
+ }
+ else
+ {
+ return false;
+ }
+
+ // read and compute entry crc32
+ if (osl::File::E_None ==
maFile->read(static_cast<void*>(aArray), 4, nBaseRead) && 4 == nBaseRead)
+ {
+ mnCrc32 = (sal_uInt32(aArray[0]) << 24) +
(sal_uInt32(aArray[1]) << 16) + (sal_uInt32(aArray[2]) << 8) +
sal_uInt32(aArray[3]);
+ }
+
+ return true;
+ }
+
+ return false;
}
- return OUString(maBase + "_" + OUString::number(n) + "." + maExt);
- }
+ bool write_header(oslFileHandle& rHandle)
+ {
+ sal_uInt8 aArray[4];
+ sal_uInt64 nBaseWritten(0);
- bool BackupFileHelper::firstExists()
- {
- if (baseFileOpen() && splitBaseURL())
+ // write size
+ aArray[0] = sal_uInt8((mnSize & 0xff000000) >> 24);
+ aArray[1] = sal_uInt8((mnSize & 0x00ff0000) >> 16);
+ aArray[2] = sal_uInt8((mnSize & 0x0000ff00) >> 8);
+ aArray[3] = sal_uInt8(mnSize & 0x000000ff);
+
+ if (osl::File::E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
+ {
+ return false;
+ }
+
+ // for each entry, write crc32
+ aArray[0] = sal_uInt8((mnCrc32 & 0xff000000) >> 24);
+ aArray[1] = sal_uInt8((mnCrc32 & 0x00ff0000) >> 16);
+ aArray[2] = sal_uInt8((mnCrc32 & 0x0000ff00) >> 8);
+ aArray[3] = sal_uInt8(mnCrc32 & 0x000000ff);
+
+ if (osl::File::E_None != osl_writeFile(rHandle, static_cast<const
void*>(aArray), 4, &nBaseWritten) || 4 != nBaseWritten)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ bool copy_content(oslFileHandle& rTargetHandle)
{
- // check if 1st copy exists
- osl::File aOneFile(getName(1));
- const osl::FileBase::RC
aResult(aOneFile.open(osl_File_OpenFlag_Read));
+ if (maFile && osl::File::E_None ==
maFile->open(osl_File_OpenFlag_Read))
+ {
+ sal_uInt8 aArray[1024];
+ sal_uInt64 nBytesTransfer(0);
+ sal_uInt64 nSize(getSize());
+
+ // set offset in source file - when this is zero, a new file
is to be added
+ maFile->setPos(0, sal_Int64(getOffset()));
+
+ while (nSize != 0)
+ {
+ const sal_uInt64 nToTransfer(std::min(nSize,
(sal_uInt64)1024));
+
+ if (osl::File::E_None !=
maFile->read(static_cast<void*>(aArray), nToTransfer, nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
- return (osl::File::E_None == aResult);
+ if (osl::File::E_None != osl_writeFile(rTargetHandle,
static_cast<const void*>(aArray), nToTransfer, &nBytesTransfer) ||
nBytesTransfer != nToTransfer)
+ {
+ break;
+ }
+
+ nSize -= nToTransfer;
+ }
+
+ maFile->close();
+
+ return (0 == nSize);
+ }
+ else
+ {
+ return false;
+ }
}
+ };
+}
- return false;
- }
+namespace
+{
+ typedef ::std::deque< PackedFileEntry > PackedFileEntryVector;
- void BackupFileHelper::pop()
+ class PackedFile
{
- for (sal_uInt16 a(2); a < mnMaxAllowedBackups + 1; a++)
+ private:
+ const OUString& mrURL;
+ PackedFileEntryVector maPackedFileEntryVector;
+ bool mbChanged;
+
+ public:
+ PackedFile(const OUString& rURL)
+ : mrURL(rURL),
+ maPackedFileEntryVector(),
+ mbChanged(false)
{
- const OUString aSourceName(getName(a));
+ FileSharedPtr aSourceFile(new osl::File(rURL));
- if (a > mnNumBackups + 1)
+ if (osl::File::E_None == aSourceFile->open(osl_File_OpenFlag_Read))
{
- // try to delete that file, it is out of scope
- osl::File::remove(aSourceName);
+ sal_uInt64 nBaseLen(0);
+ aSourceFile->getSize(nBaseLen);
+
+ // we need at least File_ID and num entries -> 8byte
+ if (8 < nBaseLen)
+ {
+ sal_uInt8 aArray[4];
+ sal_uInt64 nBaseRead(0);
+
+ // read and check File_ID
+ if (osl::File::E_None == aSourceFile->read(static_cast<
void* >(aArray), 4, nBaseRead) && 4 == nBaseRead)
+ {
+ if ('P' == aArray[0] && 'A' == aArray[1] && 'C' ==
aArray[2] && 'K' == aArray[3])
+ {
+ // read and compute num entries in this file
+ if (osl::File::E_None ==
aSourceFile->read(static_cast<void*>(aArray), 4, nBaseRead) && 4 == nBaseRead)
+ {
+ sal_uInt32 nEntries((sal_uInt32(aArray[0]) <<
24) + (sal_uInt32(aArray[1]) << 16) + (sal_uInt32(aArray[2]) << 8) +
sal_uInt32(aArray[3]));
+
+ // if there are entries (and less than max),
read them
+ if (nEntries >= 1 && nEntries <= 10)
+ {
+ // offset in souce file starts with 8Byte
for header+numEntries and
+ // 8byte for each entry (size and crc32)
+ sal_uInt32 nOffset(8 + (8 * nEntries));
+
+ for (sal_uInt32 a(0); a < nEntries; a++)
+ {
+ // create new entry, read header (size
and crc) and
+ // set offset and source file
+ PackedFileEntry aEntry;
+
+ if (aEntry.read_header(aSourceFile,
nOffset))
+ {
+ // add to local data
+
maPackedFileEntryVector.push_back(aEntry);
+
+ // increase offset for next entry
+ nOffset += aEntry.getSize();
+ }
+ else
+ {
+ // error
+ nEntries = 0;
+ }
+ }
+
+ if (0 == nEntries)
+ {
+ // on read error clear local data
+ maPackedFileEntryVector.clear();
+ }
+ }
+ }
+ }
+ }
+ }
+
+ aSourceFile->close();
}
- else
+
+ if (maPackedFileEntryVector.empty())
{
- // rename that file by decreasing index by one
- osl::File::move(aSourceName, getName(a - 1));
+ // on error or no data get rid of pack file
+ osl::File::remove(mrURL);
}
}
- }
- void BackupFileHelper::push()
- {
- for (sal_uInt16 a(0); a < mnMaxAllowedBackups; a++)
+ bool flush()
+ {
+ bool bRetval(true);
+
+ if (maPackedFileEntryVector.empty())
+ {
+ // get rid of (now?) empty pack file
+ osl::File::remove(mrURL);
+ }
+ else if (mbChanged)
+ {
+ // need to create a new pack file, do this in a temp file to
which data
+ // will be copied from local file (so keep it here until this
is done)
+ oslFileHandle aHandle;
+ OUString aTempURL;
+
+ // open target temp file
+ if (osl::File::E_None ==
osl::FileBase::createTempFile(nullptr, &aHandle, &aTempURL))
+ {
+ sal_uInt8 aArray[4];
+ sal_uInt64 nBaseWritten(0);
+
+ aArray[0] = 'P';
+ aArray[1] = 'A';
+ aArray[2] = 'C';
+ aArray[3] = 'K';
+
+ // write File_ID
+ if (osl::File::E_None == osl_writeFile(aHandle,
static_cast<const void*>(aArray), 4, &nBaseWritten) && 4 == nBaseWritten)
+ {
+ const sal_uInt32 nSize(maPackedFileEntryVector.size());
+ aArray[0] = sal_uInt8((nSize & 0xff000000) >> 24);
+ aArray[1] = sal_uInt8((nSize & 0x00ff0000) >> 16);
+ aArray[2] = sal_uInt8((nSize & 0x0000ff00) >> 8);
+ aArray[3] = sal_uInt8(nSize & 0x000000ff);
+
+ // write number of entries
+ if (osl::File::E_None == osl_writeFile(aHandle,
static_cast<const void*>(aArray), 4, &nBaseWritten) && 4 == nBaseWritten)
+ {
+ // write headers
+ for (auto& candidateA : maPackedFileEntryVector)
+ {
+ if (!candidateA.write_header(aHandle))
+ {
+ // error
+ bRetval = false;
+ break;
+ }
+ }
+
+ if (bRetval)
+ {
+ // write contents
+ for (auto& candidateB :
maPackedFileEntryVector)
+ {
+ if (!candidateB.copy_content(aHandle))
+ {
+ bRetval = false;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // close temp file (in all cases)
+ osl_closeFile(aHandle);
+
+ if (bRetval)
+ {
+ // copy over existing file by first deleting original
+ // and moving the temp file to old original
+ osl::File::remove(mrURL);
+ osl::File::move(aTempURL, mrURL);
+ }
+
+ // delete temp file (in all cases - it may be moved already)
+ osl::File::remove(aTempURL);
+ }
+
+ return bRetval;
+ }
+
+ bool tryPush(FileSharedPtr& rFileCandidate)
{
- const sal_uInt16 nIndex(mnMaxAllowedBackups - a);
- const OUString aSourceName(getName(nIndex));
+ sal_uInt64 nFileSize(0);
+
+ if (rFileCandidate && osl::File::E_None ==
rFileCandidate->open(osl_File_OpenFlag_Read))
+ {
+ rFileCandidate->getSize(nFileSize);
+ rFileCandidate->close();
+ }
- if (nIndex >= mnNumBackups)
+ if (0 == nFileSize)
{
- // try to delete that file, it is out of scope
- osl::File::remove(aSourceName);
+ // empty file offered
+ return false;
+ }
+
+ bool bNeedToAdd(false);
+ sal_uInt32 nCrc32(0);
+
+ if (!maPackedFileEntryVector.empty())
+ {
+ // already backups there, check if different from last entry
+ const PackedFileEntry& aLastEntry =
maPackedFileEntryVector.back();
+
+ if (aLastEntry.getSize() != static_cast<sal_uInt32>(nFileSize))
+ {
+ // different size, different file
+ bNeedToAdd = true;
+ }
+ else
+ {
+ // same size, check crc32
+ nCrc32 = createCrc32(rFileCandidate, 0);
+
+ if (nCrc32 != aLastEntry.getCrc32())
+ {
+ // different crc, different file
+ bNeedToAdd = true;
+ }
+ }
}
else
{
- // rename that file by increasing index by one
- osl::File::move(aSourceName, getName(nIndex + 1));
+ // no backup yet, add
+ bNeedToAdd = true;
+ }
+
+ if (bNeedToAdd)
+ {
+ // create crc32 if not yet done
+ if (0 == nCrc32)
+ {
+ nCrc32 = createCrc32(rFileCandidate, 0);
+ }
+
+ // create a file entry for a new file. Offset is set to 0 to
mark
+ // the entry as new file entry
+ maPackedFileEntryVector.push_back(
+ PackedFileEntry(
+ static_cast< sal_uInt32 >(nFileSize),
+ 0,
+ nCrc32,
+ rFileCandidate));
+
+ mbChanged = true;
}
+
+ return bNeedToAdd;
}
- }
- bool BackupFileHelper::isDifferentOrNew()
- {
- if (baseFileOpen() && splitBaseURL())
+ bool tryPop(oslFileHandle& rHandle)
{
- osl::File aLastFile(getName(1));
- const osl::FileBase::RC
aResult(aLastFile.open(osl_File_OpenFlag_Read));
- bool bDifferentOrNew(false);
+ bool bRetval(false);
- if (osl::File::E_None == aResult)
+ if (!maPackedFileEntryVector.empty())
{
- // exists, check for being equal
- bDifferentOrNew = !equalsBase(aLastFile);
+ // already backups there, check if different from last entry
+ PackedFileEntry& aLastEntry = maPackedFileEntryVector.back();
+
+ bRetval = aLastEntry.copy_content(rHandle);
+
+ if (bRetval)
+ {
+ maPackedFileEntryVector.pop_back();
+ mbChanged = true;
+ }
+
+ return bRetval;
}
- else if (osl::File::E_NOENT == aResult)
+
+ return false;
+ }
+
+ void tryReduceToNumBackups(sal_uInt16 nNumBackups)
+ {
+ while (maPackedFileEntryVector.size() > nNumBackups)
{
- // does not exist - also copy
- bDifferentOrNew = true;
+ maPackedFileEntryVector.pop_front();
+ mbChanged = true;
}
+ }
- return bDifferentOrNew;
+ bool empty()
+ {
+ return maPackedFileEntryVector.empty();
}
+ };
+}
- return false;
+namespace comphelper
+{
+ sal_uInt16 BackupFileHelper::mnMaxAllowedBackups = 10;
+ bool BackupFileHelper::mbExitWasCalled = false;
+
+ BackupFileHelper::BackupFileHelper(
+ const OUString& rBaseURL,
+ sal_uInt16 nNumBackups)
+ : mrBaseURL(rBaseURL),
+ mnNumBackups(::std::min(::std::max(nNumBackups, sal_uInt16(1)),
mnMaxAllowedBackups)),
+ maBase(),
+ maName(),
+ maExt()
+ {
}
- bool BackupFileHelper::equalsBase(osl::File& rLastFile)
+ void BackupFileHelper::setExitWasCalled()
{
- sal_uInt64 nBaseLen(0);
- sal_uInt64 nLastLen(0);
- maBaseFile.getSize(nBaseLen);
- rLastFile.getSize(nLastLen);
+ mbExitWasCalled = true;
+ }
- if (nBaseLen == nLastLen)
+ bool BackupFileHelper::getExitWasCalled()
+ {
+ return mbExitWasCalled;
+ }
+
+ bool BackupFileHelper::getSecureUserConfig(sal_uInt16&
rnSecureUserConfigNumCopies)
+ {
+ // init to not active
+ bool bRetval(false);
+ rnSecureUserConfigNumCopies = 0;
+ OUString sTokenOut;
+
+ if (rtl::Bootstrap::get("SecureUserConfig", sTokenOut))
+ {
+ bRetval = sTokenOut.toBoolean();
+ }
+
+ if (bRetval && rtl::Bootstrap::get("SecureUserConfigNumCopies",
sTokenOut))
{
- // same filesize -> need to check content
- sal_uInt8 aArrayOld[1024];
- sal_uInt8 aArrayLast[1024];
- sal_uInt64 nBytesReadBase(0);
- sal_uInt64 nBytesReadLast(0);
- bool bDiffers(false);
+ rnSecureUserConfigNumCopies = static_cast< sal_uInt16
>(sTokenOut.toUInt32());
+ }
- // both rewind on start
- maBaseFile.setPos(0, 0);
- rLastFile.setPos(0, 0);
+ return bRetval;
+ }
- while (!bDiffers
- && osl::File::E_None ==
maBaseFile.read(static_cast<void*>(aArrayOld), 1024, nBytesReadBase)
- && osl::File::E_None ==
rLastFile.read(static_cast<void*>(aArrayLast), 1024, nBytesReadLast)
- && 0 != nBytesReadBase
- && nBytesReadBase == nBytesReadLast)
+ rtl::OUString BackupFileHelper::getName()
+ {
+ return OUString(maBase + "/." + maName + ".pack");
+ }
+
+ bool BackupFileHelper::tryPush()
+ {
+ if (splitBaseURL() && baseFileExists())
+ {
+ PackedFile aPackedFile(getName());
+ FileSharedPtr aBaseFile(new osl::File(mrBaseURL));
+
+ if (aPackedFile.tryPush(aBaseFile))
{
- bDiffers = memcmp(aArrayOld, aArrayLast, nBytesReadBase);
+ // reduce to allowed number and flush
+ aPackedFile.tryReduceToNumBackups(mnNumBackups);
+ aPackedFile.flush();
+
+ return true;
}
+ }
- return !bDiffers;
+ return false;
+ }
+
+ bool BackupFileHelper::isPopPossible()
+ {
+ if (splitBaseURL() && baseFileExists())
+ {
+ PackedFile aPackedFile(getName());
+
+ return !aPackedFile.empty();
}
return false;
}
- bool BackupFileHelper::splitBaseURL()
+ bool BackupFileHelper::tryPop()
{
- if (maBase.isEmpty() && !mrBaseURL.isEmpty())
+ if (splitBaseURL() && baseFileExists())
{
- const sal_Int32 nIndex(mrBaseURL.lastIndexOf('.'));
+ PackedFile aPackedFile(getName());
- if (-1 == nIndex)
+ if (!aPackedFile.empty())
{
- maBase = mrBaseURL;
- }
- else if (nIndex > 0)
- {
- maBase = mrBaseURL.copy(0, nIndex);
+ oslFileHandle aHandle;
+ OUString aTempURL;
+
+ // open target temp file
+ if (osl::File::E_None ==
osl::FileBase::createTempFile(nullptr, &aHandle, &aTempURL))
+ {
+ bool bRetval(aPackedFile.tryPop(aHandle));
+
+ // close temp file (in all cases)
+ osl_closeFile(aHandle);
+
+ if (bRetval)
+ {
+ // copy over existing file by first deleting original
+ // and moving the temp file to old original
+ osl::File::remove(mrBaseURL);
+ osl::File::move(aTempURL, mrBaseURL);
+
+ // reduce to allowed number and flush
+ aPackedFile.tryReduceToNumBackups(mnNumBackups);
+ aPackedFile.flush();
+ }
+
+ // delete temp file (in all cases - it may be moved
already)
+ osl::File::remove(aTempURL);
+
+ return bRetval;
+ }
}
+ }
- if (mrBaseURL.getLength() > nIndex + 1)
- {
- maExt = mrBaseURL.copy(nIndex + 1);
- }
+ return false;
+ }
+
+ bool BackupFileHelper::splitBaseURL()
+ {
+ if (maBase.isEmpty() && !mrBaseURL.isEmpty())
+ {
+ // split URL at extension and at last path separator
+ maBase = splitAtLastToken(splitAtLastToken(mrBaseURL, '.', maExt),
'/', maName);
}
- return !maBase.isEmpty();
+ return !maBase.isEmpty() && !maName.isEmpty();
}
- bool BackupFileHelper::baseFileOpen()
+ bool BackupFileHelper::baseFileExists()
{
- if (!mbBaseFileIsOpen && !mrBaseURL.isEmpty())
+ if (!mrBaseURL.isEmpty())
{
- mbBaseFileIsOpen = (osl::File::E_None ==
maBaseFile.open(osl_File_OpenFlag_Read));
+ FileSharedPtr aBaseFile(new osl::File(mrBaseURL));
+
+ return (osl::File::E_None ==
aBaseFile->open(osl_File_OpenFlag_Read));
}
- return mbBaseFileIsOpen;
+ return false;
}
}
diff --git a/configmgr/source/components.cxx b/configmgr/source/components.cxx
index a193da0..206d346 100644
--- a/configmgr/source/components.cxx
+++ b/configmgr/source/components.cxx
@@ -643,18 +643,22 @@ Components::~Components()
(*i)->setAlive(false);
}
- // test backup of registrymodifications
- static bool bFeatureSecureUserConfig(true);
-
if (!bExitWasCalled &&
- bFeatureSecureUserConfig &&
ModificationTarget::File == modificationTarget_ &&
!modificationFileUrl_.isEmpty())
{
- static sal_uInt16 nNumCopies(5);
- comphelper::BackupFileHelper aBackupFileHelper(modificationFileUrl_,
nNumCopies);
+ // test backup of registrymodifications
+ sal_uInt16 nSecureUserConfigNumCopies(0);
+
+ // read configuration from soffice.ini
+ const bool
bSecureUserConfig(comphelper::BackupFileHelper::getSecureUserConfig(nSecureUserConfigNumCopies));
- aBackupFileHelper.tryPush();
+ if (bSecureUserConfig)
+ {
+ comphelper::BackupFileHelper
aBackupFileHelper(modificationFileUrl_, nSecureUserConfigNumCopies);
+
+ aBackupFileHelper.tryPush();
+ }
}
}
diff --git a/configmgr/source/writemodfile.cxx
b/configmgr/source/writemodfile.cxx
index 1d20c18..15c9ffa 100644
--- a/configmgr/source/writemodfile.cxx
+++ b/configmgr/source/writemodfile.cxx
@@ -400,6 +400,16 @@ void writeNode(
}
}
+// helpers to allow sorting of configmgr::Modifications::Node
+typedef std::pair< const rtl::OUString, configmgr::Modifications::Node >
aPairEntry;
+struct PairEntrySorter
+{
+ bool operator() (const aPairEntry* pValue1, const aPairEntry* pValue2)
const
+ {
+ return pValue1->first.compareTo(pValue2->first) < 0;
+ }
+};
+
void writeModifications(
Components & components, TempFile &handle,
OUString const & parentPathRepresentation,
@@ -459,11 +469,26 @@ void writeModifications(
OUString pathRep(
parentPathRepresentation + "/" +
Data::createSegment(node->getTemplateName(), nodeName));
- for (const auto & i : modifications.children)
+
+ // copy configmgr::Modifications::Node's to a sortable list. Use
pointers
+ // to just reference the data instead of copying it
+ std::list< const aPairEntry* > aPairEntryList;
+
+ for (const auto& rCand : modifications.children)
+ {
+ aPairEntryList.push_back(&rCand);
+ }
+
+ // sort the list
+ aPairEntryList.sort(PairEntrySorter());
+
+ // now use the list to write entries in sorted order
+ // instead of random as from the unordered map
+ for (const auto & i : aPairEntryList)
{
writeModifications(
- components, handle, pathRep, node, i.first,
- node->getMember(i.first), i.second);
+ components, handle, pathRep, node, i->first,
+ node->getMember(i->first), i->second);
}
}
}
@@ -601,9 +626,30 @@ void writeModFile(
//TODO: Do not write back information about those removed items that did
not
// come from the .xcs/.xcu files, anyway (but had been added dynamically
// instead):
- for (Modifications::Node::Children::const_iterator j(
- data.modifications.getRoot().children.begin());
- j != data.modifications.getRoot().children.end(); ++j)
+
+ // For profilesafemode it is necessary to detect changes in the
+ // registrymodifications file, this is done based on file size in bytes
and crc32.
+ // Unfortunately this write is based on writing unordered map entries,
which creates
+ // valid and semantically equal XML-Files, bubt with different crc32
checksums. For
+ // the future usage it will be preferrable to have easily comparable
config files
+ // which is guaranteed by writing the entries in sorted order. Indeed with
this change
+ // (and in the recursive writeModifications call) the same config files
get written
+
+ // copy configmgr::Modifications::Node's to a sortable list. Use pointers
+ // to just reference the data instead of copying it
+ std::list< const aPairEntry* > aPairEntryList;
+
+ for (const auto& rCand : data.modifications.getRoot().children)
+ {
+ aPairEntryList.push_back(&rCand);
+ }
+
+ // sort the list
+ aPairEntryList.sort(PairEntrySorter());
+
+ // now use the list to write entries in sorted order
+ // instead of random as from the unordered map
+ for (const auto& j : aPairEntryList)
{
writeModifications(
components, tmp, "", rtl::Reference< Node >(), j->first,
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
index c6ac423..7050500 100644
--- a/desktop/source/app/app.cxx
+++ b/desktop/source/app/app.cxx
@@ -948,11 +948,13 @@ void Desktop::HandleBootstrapErrors(
else if ( aBootstrapError == BE_OFFICECONFIG_BROKEN )
{
// test restore of registrymodifications
- static bool bFeatureSecureUserConfig(true);
- static sal_uInt16 nNumCopies(5);
+ sal_uInt16 nSecureUserConfigNumCopies(0);
bool bFireOriginalError(true);
- if (bFeatureSecureUserConfig)
+ // read configuration from soffice.ini
+ const bool
bSecureUserConfig(comphelper::BackupFileHelper::getSecureUserConfig(nSecureUserConfigNumCopies));
+
+ if (bSecureUserConfig)
{
// try to asccess user layer configuration file
OUString conf("${CONFIGURATION_LAYERS}");
@@ -977,7 +979,7 @@ void Desktop::HandleBootstrapErrors(
if (!aUser.isEmpty())
{
- comphelper::BackupFileHelper aBackupFileHelper(aUser,
nNumCopies);
+ comphelper::BackupFileHelper aBackupFileHelper(aUser,
nSecureUserConfigNumCopies);
if (aBackupFileHelper.isPopPossible())
{
diff --git a/include/comphelper/backupfilehelper.hxx
b/include/comphelper/backupfilehelper.hxx
index f407d2b..2c6cc25 100644
--- a/include/comphelper/backupfilehelper.hxx
+++ b/include/comphelper/backupfilehelper.hxx
@@ -15,6 +15,7 @@
#include <comphelper/comphelperdllapi.h>
#include <rtl/ustring.hxx>
#include <osl/file.hxx>
+#include <memory>
namespace comphelper
{
@@ -45,12 +46,11 @@ namespace comphelper
{
private:
// internal data
- const OUString& mrBaseURL;
- sal_uInt16 mnNumBackups;
- OUString maBase;
- OUString maExt;
- osl::File maBaseFile;
- bool mbBaseFileIsOpen;
+ const OUString& mrBaseURL;
+ sal_uInt16 mnNumBackups;
+ OUString maBase;
+ OUString maName;
+ OUString maExt;
// internal flag if _exit() was called already - a hint to evtl.
// not create copies of potentially not well-defined data. This
@@ -67,6 +67,10 @@ namespace comphelper
public:
/** Constructor to handle Backups of the given file
*
+ * @param rxContext
+ * ComponentContext to use internally; needs to be handed
+ * over due to usages after DeInit() and thus no access
+ * anymore using comphelper::getProcessComponentContext()
* @param rBaseURL
* URL to an existing file that needs to be backed up
*
@@ -77,12 +81,21 @@ namespace comphelper
* It is used in tryPush() and tryPop() calls to cleanup/
* reduce the number of existing backups
*/
- BackupFileHelper(const OUString& rBaseURL, sal_uInt16 nNumBackups = 5);
+ BackupFileHelper(
+ const OUString& rBaseURL,
+ sal_uInt16 nNumBackups = 5);
- // allow to set flag when app had to call _exit()
+ // allow to set static global flag when app had to call _exit()
static void setExitWasCalled();
static bool getExitWasCalled();
+ // static helper to read config values - these are derived from
+ // soffice.ini due to cui not being available in all cases. The
+ // boolean SecureUserConfig is returned.
+ // Default for SecureUserConfig is false
+ // Default for SecureUserConfigNumCopies is 0 (zero)
+ static bool getSecureUserConfig(sal_uInt16&
rnSecureUserConfigNumCopies);
+
/** tries to create a new backup, if there is none yet, or if the
* last differs from the base file. It will then put a new verion
* on the 'stack' of copies and evtl. delete the oldest backup.
@@ -113,14 +126,9 @@ namespace comphelper
private:
// internal helper methods
- rtl::OUString getName(sal_uInt16 n);
- bool firstExists();
- void pop();
- void push();
- bool isDifferentOrNew();
- bool equalsBase(osl::File& rLastFile);
bool splitBaseURL();
- bool baseFileOpen();
+ bool baseFileExists();
+ rtl::OUString getName();
};
}
commit 7cdb641a7387eb6c5781cc58997462b8beffb1b8
Author: Armin Le Grand <[email protected]>
Date: Thu Sep 15 15:51:21 2016 +0200
profilesafe: make dialog open on linux
Change-Id: Idacec97ec2f097af9bd22a8a67b410c7677d17f1
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
index 9656375..c6ac423 100644
--- a/desktop/source/app/app.cxx
+++ b/desktop/source/app/app.cxx
@@ -981,6 +981,12 @@ void Desktop::HandleBootstrapErrors(
if (aBackupFileHelper.isPopPossible())
{
+ // for linux (and probably others?) we need to instantiate
XDesktop2
+ // to be able to open a *.ui-file based dialog, so do this
here locally.
+ // does no harm on win, so better always do this (in error
case only anyways)
+ Reference< XComponentContext > xLocalContext =
::comphelper::getProcessComponentContext();
+ Reference< XDesktop2 > xDesktop =
css::frame::Desktop::create(xLocalContext);
+
ScopedVclPtrInstance< MessageDialog > aQueryShouldRestore(
Application::GetDefDialogParent(),
"QueryTryToRestoreConfigurationDialog",
commit 24d773b875b6b8a62ea9ff39d97ef7dfb5b59651
Author: Armin Le Grand <[email protected]>
Date: Thu Sep 15 12:27:28 2016 +0200
profilesafe: Added adaptions
Added code to detect/test exceptions, tested execution,
added code to detect if _exit() was already called on all
systems to avoid writing config and creating backup in that
state (win calls destructors of static objects on _exit(),
linux does not which is intended)
Change-Id: I01df80c714ee964646da23cc3b2b081b74ecfb4b
diff --git a/comphelper/source/misc/backupfilehelper.cxx
b/comphelper/source/misc/backupfilehelper.cxx
index 778f0a0..7a353b8 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -15,6 +15,7 @@
namespace comphelper
{
sal_uInt16 BackupFileHelper::mnMaxAllowedBackups = 10;
+ bool BackupFileHelper::mbExitWasCalled = false;
BackupFileHelper::BackupFileHelper(
const OUString& rBaseURL,
@@ -28,6 +29,16 @@ namespace comphelper
{
}
+ void BackupFileHelper::setExitWasCalled()
+ {
+ mbExitWasCalled = true;
+ }
+
+ bool BackupFileHelper::getExitWasCalled()
+ {
+ return mbExitWasCalled;
+ }
+
bool BackupFileHelper::tryPush()
{
if (isDifferentOrNew())
diff --git a/configmgr/source/components.cxx b/configmgr/source/components.cxx
index f7c473c..a193da0 100644
--- a/configmgr/source/components.cxx
+++ b/configmgr/source/components.cxx
@@ -613,25 +613,48 @@ Components::Components(
Components::~Components()
{
- flushModifications();
+ SAL_WARN("configmgr", "################# Components::~Components()
#####################");
+
+ // get flag if _exit was already called which is a sign to not to secure
user config
+ const bool
bExitWasCalled(comphelper::BackupFileHelper::getExitWasCalled());
+
+#ifndef WNT
+ // we can add a SAL_WARN here for other systems where the destructor gets
called after
+ // an _exit() call - which should not happen. Still safe - the
getExitWasCalled() is
+ // used, but a hint that _exit behaves different on a system
+ SAL_WARN_IF(bExitWasCalled, "configmgr", "Components::~Components() called
after _exit() call");
+#endif
+
+ if (bExitWasCalled)
+ {
+ osl::MutexGuard g(*lock_);
+
+ if (writeThread_.is())
+ {
+ writeThread_->join();
+ }
+ }
+ else
+ {
+ flushModifications();
+ }
+
for (WeakRootSet::iterator i(roots_.begin()); i != roots_.end(); ++i) {
(*i)->setAlive(false);
}
- // test backup of registrymodifications (currently off)
- static bool bFeatureSecureUserConfig(false);
+ // test backup of registrymodifications
+ static bool bFeatureSecureUserConfig(true);
- if (bFeatureSecureUserConfig && ModificationTarget::File ==
modificationTarget_ && !modificationFileUrl_.isEmpty())
+ if (!bExitWasCalled &&
+ bFeatureSecureUserConfig &&
+ ModificationTarget::File == modificationTarget_ &&
+ !modificationFileUrl_.isEmpty())
{
static sal_uInt16 nNumCopies(5);
comphelper::BackupFileHelper aBackupFileHelper(modificationFileUrl_,
nNumCopies);
- aBackupFileHelper.tryPush();
- static bool bTryPop(false);
- if (bTryPop)
- {
- aBackupFileHelper.tryPop();
- }
+ aBackupFileHelper.tryPush();
}
}
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
index 3456db2..9656375 100644
--- a/desktop/source/app/app.cxx
+++ b/desktop/source/app/app.cxx
@@ -81,6 +81,7 @@
#include <comphelper/configuration.hxx>
#include <comphelper/fileurl.hxx>
#include <comphelper/processfactory.hxx>
+#include <comphelper/backupfilehelper.hxx>
#include <unotools/bootstrap.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/moduleoptions.hxx>
@@ -567,6 +568,14 @@ void Desktop::Init()
{
SetBootstrapError( BE_OFFICECONFIG_BROKEN, e.Message );
}
+
+ static bool bTryHardOfficeconfigBroken(false);
+
+ if (bTryHardOfficeconfigBroken)
+ {
+ SAL_WARN("configmgr", "################# Desktop::Init()
#####################");
+ SetBootstrapError(BE_OFFICECONFIG_BROKEN, OUString());
+ }
}
if ( true )
@@ -938,15 +947,87 @@ void Desktop::HandleBootstrapErrors(
}
else if ( aBootstrapError == BE_OFFICECONFIG_BROKEN )
... etc. - the rest is truncated
_______________________________________________
Libreoffice-commits mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits