Kaydet (Commit) e1d78e12 authored tarafından Michael Meeks's avatar Michael Meeks

tdf#92764 - make configmgr write less noisy.

Re-factor the TempFile abstraction and add a trivial OStringBuffer to
avoid emitting so many system calls - for writing small fragments of
configuration XML.

Change-Id: Ifbf5982ddb44845b2316087cafab4175a40e03cc
üst d6790de0
...@@ -70,7 +70,7 @@ namespace { ...@@ -70,7 +70,7 @@ namespace {
// </node> // </node>
// </item> // </item>
void dumpWindowsRegistryKey(HKEY hKey, OUString aKeyName, oslFileHandle aFileHandle) void dumpWindowsRegistryKey(HKEY hKey, OUString aKeyName, TempFile &aFileHandle)
{ {
HKEY hCurKey; HKEY hCurKey;
...@@ -200,8 +200,8 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType) ...@@ -200,8 +200,8 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType)
return false; return false;
} }
oslFileHandle aFileHandle; TempFile aFileHandle;
switch (osl::FileBase::createTempFile(0, &aFileHandle, pFileURL)) { switch (osl::FileBase::createTempFile(0, &aFileHandle.handle, pFileURL)) {
case osl::FileBase::E_None: case osl::FileBase::E_None:
break; break;
case osl::FileBase::E_ACCES: case osl::FileBase::E_ACCES:
...@@ -213,6 +213,7 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType) ...@@ -213,6 +213,7 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType)
throw css::uno::RuntimeException( throw css::uno::RuntimeException(
"cannot create temporary file"); "cannot create temporary file");
} }
aFileHandle.url = *pFileURL;
writeData( writeData(
aFileHandle, aFileHandle,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items" "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items"
...@@ -221,7 +222,7 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType) ...@@ -221,7 +222,7 @@ bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType)
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"); " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
dumpWindowsRegistryKey(hKey, "", aFileHandle); dumpWindowsRegistryKey(hKey, "", aFileHandle);
writeData(aFileHandle, "</oor:items>"); writeData(aFileHandle, "</oor:items>");
oslFileError e = osl_closeFile(aFileHandle); oslFileError e = aFileHandle.closeWithoutUnlink();
if (e != osl_File_E_None) if (e != osl_File_E_None)
SAL_WARN("configmgr", "osl_closeFile failed with " << +e); SAL_WARN("configmgr", "osl_closeFile failed with " << +e);
RegCloseKey(hKey); RegCloseKey(hKey);
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include <rtl/ustrbuf.hxx> #include <rtl/ustrbuf.hxx>
#include <rtl/ustring.h> #include <rtl/ustring.h>
#include <rtl/ustring.hxx> #include <rtl/ustring.hxx>
#include <rtl/strbuf.hxx>
#include <sal/log.hxx> #include <sal/log.hxx>
#include <sal/types.h> #include <sal/types.h>
#include <xmlreader/span.hxx> #include <xmlreader/span.hxx>
...@@ -73,18 +74,7 @@ OString convertToUtf8( ...@@ -73,18 +74,7 @@ OString convertToUtf8(
return s; return s;
} }
struct TempFile { } // anonymous namespace
OUString url;
oslFileHandle handle;
bool closed;
TempFile(): handle(0), closed(false) {}
~TempFile();
private:
TempFile(const TempFile&) SAL_DELETED_FUNCTION;
TempFile& operator=(const TempFile&) SAL_DELETED_FUNCTION;
};
TempFile::~TempFile() { TempFile::~TempFile() {
if (handle != 0) { if (handle != 0) {
...@@ -103,21 +93,66 @@ TempFile::~TempFile() { ...@@ -103,21 +93,66 @@ TempFile::~TempFile() {
} }
} }
void writeData_(oslFileHandle handle, char const * begin, sal_Int32 length) { oslFileError TempFile::closeWithoutUnlink() {
assert(length >= 0); flush();
sal_uInt64 n; oslFileError e = osl_closeFile(handle);
if ((osl_writeFile(handle, begin, static_cast< sal_uInt32 >(length), &n) != handle = 0;
osl_File_E_None) || closed = true;
n != static_cast< sal_uInt32 >(length)) return e;
{ }
void TempFile::closeAndRename(const OUString &_url) {
oslFileError e = flush();
if (e != osl_File_E_None) {
throw css::uno::RuntimeException( throw css::uno::RuntimeException(
"write failure"); "cannot write to " + url);
} }
e = osl_closeFile(handle);
closed = true;
if (e != osl_File_E_None) {
throw css::uno::RuntimeException(
"cannot close " + url);
}
if (osl::File::move(url, _url) != osl::FileBase::E_None) {
throw css::uno::RuntimeException(
"cannot move " + url);
}
handle = 0;
} }
void writeValueContent_(oslFileHandle, bool) SAL_DELETED_FUNCTION; oslFileError TempFile::flush() {
oslFileError e = osl_File_E_None;
if (!buffer.isEmpty()) {
sal_uInt64 nBytesWritten = 0;
e = osl_writeFile(handle, buffer.getStr(),
static_cast< sal_uInt32 >(buffer.getLength()),
&nBytesWritten);
if (nBytesWritten != static_cast< sal_uInt32 >(buffer.getLength())) {
// queue up any error / exception until close.
buffer.remove(0, static_cast< sal_Int32 >( nBytesWritten ) );
} else {
buffer.setLength(0);
}
}
return e;
}
void TempFile::writeString(char const *begin, sal_Int32 length) {
buffer.append(begin, length);
if (buffer.getLength() > 0x10000)
flush();
}
namespace {
void writeData_(TempFile &handle, char const * begin, sal_Int32 length) {
assert(length >= 0);
handle.writeString(begin, length);
}
void writeValueContent_(TempFile &, bool) SAL_DELETED_FUNCTION;
// silence lopluign:salbool // silence lopluign:salbool
void writeValueContent_(oslFileHandle handle, sal_Bool value) { void writeValueContent_(TempFile &handle, sal_Bool value) {
if (value) { if (value) {
writeData_(handle, RTL_CONSTASCII_STRINGPARAM("true")); writeData_(handle, RTL_CONSTASCII_STRINGPARAM("true"));
} else { } else {
...@@ -125,28 +160,28 @@ void writeValueContent_(oslFileHandle handle, sal_Bool value) { ...@@ -125,28 +160,28 @@ void writeValueContent_(oslFileHandle handle, sal_Bool value) {
} }
} }
void writeValueContent_(oslFileHandle handle, sal_Int16 value) { void writeValueContent_(TempFile &handle, sal_Int16 value) {
writeData(handle, OString::number(value)); writeData(handle, OString::number(value));
} }
void writeValueContent_(oslFileHandle handle, sal_Int32 value) { void writeValueContent_(TempFile &handle, sal_Int32 value) {
writeData(handle, OString::number(value)); writeData(handle, OString::number(value));
} }
void writeValueContent_(oslFileHandle handle, sal_Int64 value) { void writeValueContent_(TempFile &handle, sal_Int64 value) {
writeData(handle, OString::number(value)); writeData(handle, OString::number(value));
} }
void writeValueContent_(oslFileHandle handle, double value) { void writeValueContent_(TempFile &handle, double value) {
writeData(handle, OString::number(value)); writeData(handle, OString::number(value));
} }
void writeValueContent_(oslFileHandle handle, const OUString& value) { void writeValueContent_(TempFile &handle, const OUString& value) {
writeValueContent(handle, value); writeValueContent(handle, value);
} }
void writeValueContent_( void writeValueContent_(
oslFileHandle handle, css::uno::Sequence< sal_Int8 > const & value) TempFile &handle, css::uno::Sequence< sal_Int8 > const & value)
{ {
for (sal_Int32 i = 0; i < value.getLength(); ++i) { for (sal_Int32 i = 0; i < value.getLength(); ++i) {
static char const hexDigit[16] = { static char const hexDigit[16] = {
...@@ -158,7 +193,7 @@ void writeValueContent_( ...@@ -158,7 +193,7 @@ void writeValueContent_(
} }
template< typename T > void writeSingleValue( template< typename T > void writeSingleValue(
oslFileHandle handle, css::uno::Any const & value) TempFile &handle, css::uno::Any const & value)
{ {
writeData_(handle, RTL_CONSTASCII_STRINGPARAM(">")); writeData_(handle, RTL_CONSTASCII_STRINGPARAM(">"));
T val = T(); T val = T();
...@@ -168,7 +203,7 @@ template< typename T > void writeSingleValue( ...@@ -168,7 +203,7 @@ template< typename T > void writeSingleValue(
} }
template< typename T > void writeListValue( template< typename T > void writeListValue(
oslFileHandle handle, css::uno::Any const & value) TempFile &handle, css::uno::Any const & value)
{ {
writeData_(handle, RTL_CONSTASCII_STRINGPARAM(">")); writeData_(handle, RTL_CONSTASCII_STRINGPARAM(">"));
css::uno::Sequence< T > val; css::uno::Sequence< T > val;
...@@ -183,7 +218,7 @@ template< typename T > void writeListValue( ...@@ -183,7 +218,7 @@ template< typename T > void writeListValue(
} }
template< typename T > void writeItemListValue( template< typename T > void writeItemListValue(
oslFileHandle handle, css::uno::Any const & value) TempFile &handle, css::uno::Any const & value)
{ {
writeData_(handle, RTL_CONSTASCII_STRINGPARAM(">")); writeData_(handle, RTL_CONSTASCII_STRINGPARAM(">"));
css::uno::Sequence< T > val; css::uno::Sequence< T > val;
...@@ -196,7 +231,7 @@ template< typename T > void writeItemListValue( ...@@ -196,7 +231,7 @@ template< typename T > void writeItemListValue(
writeData_(handle, RTL_CONSTASCII_STRINGPARAM("</value>")); writeData_(handle, RTL_CONSTASCII_STRINGPARAM("</value>"));
} }
void writeValue(oslFileHandle handle, Type type, css::uno::Any const & value) { void writeValue(TempFile &handle, Type type, css::uno::Any const & value) {
switch (type) { switch (type) {
case TYPE_BOOLEAN: case TYPE_BOOLEAN:
writeSingleValue< sal_Bool >(handle, value); writeSingleValue< sal_Bool >(handle, value);
...@@ -246,7 +281,7 @@ void writeValue(oslFileHandle handle, Type type, css::uno::Any const & value) { ...@@ -246,7 +281,7 @@ void writeValue(oslFileHandle handle, Type type, css::uno::Any const & value) {
} }
void writeNode( void writeNode(
Components & components, oslFileHandle handle, Components & components, TempFile &handle,
rtl::Reference< Node > const & parent, OUString const & name, rtl::Reference< Node > const & parent, OUString const & name,
rtl::Reference< Node > const & node) rtl::Reference< Node > const & node)
{ {
...@@ -364,7 +399,7 @@ void writeNode( ...@@ -364,7 +399,7 @@ void writeNode(
} }
void writeModifications( void writeModifications(
Components & components, oslFileHandle handle, Components & components, TempFile &handle,
OUString const & parentPathRepresentation, OUString const & parentPathRepresentation,
rtl::Reference< Node > const & parent, OUString const & nodeName, rtl::Reference< Node > const & parent, OUString const & nodeName,
rtl::Reference< Node > const & node, rtl::Reference< Node > const & node,
...@@ -435,11 +470,11 @@ void writeModifications( ...@@ -435,11 +470,11 @@ void writeModifications(
} }
void writeData(oslFileHandle handle, OString const & text) { void writeData(TempFile &handle, OString const & text) {
writeData_(handle, text.getStr(), text.getLength()); writeData_(handle, text.getStr(), text.getLength());
} }
void writeAttributeValue(oslFileHandle handle, OUString const & value) { void writeAttributeValue(TempFile &handle, OUString const & value) {
sal_Int32 i = 0; sal_Int32 i = 0;
sal_Int32 j = i; sal_Int32 j = i;
for (; j < value.getLength(); ++j) { for (; j < value.getLength(); ++j) {
...@@ -484,7 +519,7 @@ void writeAttributeValue(oslFileHandle handle, OUString const & value) { ...@@ -484,7 +519,7 @@ void writeAttributeValue(oslFileHandle handle, OUString const & value) {
writeData(handle, convertToUtf8(value, i, j - i)); writeData(handle, convertToUtf8(value, i, j - i));
} }
void writeValueContent(oslFileHandle handle, OUString const & value) { void writeValueContent(TempFile &handle, OUString const & value) {
sal_Int32 i = 0; sal_Int32 i = 0;
sal_Int32 j = i; sal_Int32 j = i;
for (; j < value.getLength(); ++j) { for (; j < value.getLength(); ++j) {
...@@ -557,7 +592,7 @@ void writeModFile( ...@@ -557,7 +592,7 @@ void writeModFile(
"cannot create temporary file in " + dir); "cannot create temporary file in " + dir);
} }
writeData_( writeData_(
tmp.handle, tmp,
RTL_CONSTASCII_STRINGPARAM( RTL_CONSTASCII_STRINGPARAM(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items" "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items"
" xmlns:oor=\"http://openoffice.org/2001/registry\"" " xmlns:oor=\"http://openoffice.org/2001/registry\""
...@@ -571,22 +606,12 @@ void writeModFile( ...@@ -571,22 +606,12 @@ void writeModFile(
j != data.modifications.getRoot().children.end(); ++j) j != data.modifications.getRoot().children.end(); ++j)
{ {
writeModifications( writeModifications(
components, tmp.handle, "", rtl::Reference< Node >(), j->first, components, tmp, "", rtl::Reference< Node >(), j->first,
data.getComponents().findNode(Data::NO_LAYER, j->first), data.getComponents().findNode(Data::NO_LAYER, j->first),
j->second); j->second);
} }
writeData_(tmp.handle, RTL_CONSTASCII_STRINGPARAM("</oor:items>\n")); writeData_(tmp, RTL_CONSTASCII_STRINGPARAM("</oor:items>\n"));
oslFileError e = osl_closeFile(tmp.handle); tmp.closeAndRename(url);
tmp.closed = true;
if (e != osl_File_E_None) {
throw css::uno::RuntimeException(
"cannot close " + tmp.url);
}
if (osl::File::move(tmp.url, url) != osl::FileBase::E_None) {
throw css::uno::RuntimeException(
"cannot move " + tmp.url);
}
tmp.handle = 0;
} }
} }
......
...@@ -27,9 +27,27 @@ namespace configmgr { ...@@ -27,9 +27,27 @@ namespace configmgr {
class Components; class Components;
struct Data; struct Data;
void writeData(oslFileHandle handle, OString const & text); struct TempFile {
void writeAttributeValue(oslFileHandle handle, OUString const & value); OUString url;
void writeValueContent(oslFileHandle handle, OUString const & value); oslFileHandle handle;
bool closed;
OStringBuffer buffer;
TempFile(): handle(0), closed(false) {}
~TempFile();
void closeAndRename(const OUString &url);
oslFileError flush();
oslFileError closeWithoutUnlink();
void writeString(char const *begin, sal_Int32 length);
private:
TempFile(const TempFile&) SAL_DELETED_FUNCTION;
TempFile& operator=(const TempFile&) SAL_DELETED_FUNCTION;
};
void writeData(TempFile &handle, OString const & text);
void writeAttributeValue(TempFile &handle, OUString const & value);
void writeValueContent(TempFile &handle, OUString const & value);
void writeModFile( void writeModFile(
Components & components, OUString const & url, Data const & data); Components & components, OUString const & url, Data const & data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment