2002-05-27 11:08:47 -05:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2005-09-07 22:05:17 -05:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
2005-09-07 22:05:17 -05:00
|
|
|
* $RCSfile: localoutputstream.cxx,v $
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
2006-09-16 09:12:32 -05:00
|
|
|
* $Revision: 1.6 $
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
2006-09-16 09:12:32 -05:00
|
|
|
* last change: $Author: obo $ $Date: 2006-09-16 15:12:32 $
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
2005-09-07 22:05:17 -05:00
|
|
|
* The Contents of this file are made available subject to
|
|
|
|
* the terms of GNU Lesser General Public License Version 2.1.
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
|
|
|
*
|
2005-09-07 22:05:17 -05:00
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2005 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
2005-09-07 22:05:17 -05:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
2005-09-07 22:05:17 -05:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
2005-09-07 22:05:17 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
2002-05-27 11:08:47 -05:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2006-09-16 09:12:32 -05:00
|
|
|
// MARKER(update_precomp.py): autogen include statement, do not remove
|
|
|
|
#include "precompiled_configmgr.hxx"
|
|
|
|
|
2002-05-27 11:08:47 -05:00
|
|
|
#ifndef CONFIGMGR_LOCALBE_LOCALOUTPUTSTREAM_HXX_
|
|
|
|
#include "localoutputstream.hxx"
|
|
|
|
#endif // CONFIGMGR_LOCALBE_LOCALOUTPUTSTREAM_HXX_
|
|
|
|
|
2002-06-17 08:31:44 -05:00
|
|
|
#ifndef _RTL_USTRBUF_HXX_
|
|
|
|
#include <rtl/ustrbuf.hxx>
|
|
|
|
#endif // _RTL_USTRBUF_HXX_
|
|
|
|
|
2003-04-17 07:29:55 -05:00
|
|
|
#ifndef _COM_SUN_STAR_CONFIGURATION_BACKEND_INSUFFICIENTACCESSRIGHTSEXCEPTION_HPP_
|
|
|
|
#include <com/sun/star/configuration/backend/InsufficientAccessRightsException.hpp>
|
|
|
|
#endif
|
|
|
|
|
2002-06-17 08:31:44 -05:00
|
|
|
namespace configmgr { namespace localbe {
|
2002-05-27 11:08:47 -05:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
LocalOutputStream::LocalOutputStream(const rtl::OUString& aFileUrl)
|
2003-04-17 07:29:55 -05:00
|
|
|
throw (backend::BackendAccessException, uno::RuntimeException)
|
|
|
|
: mFileUrl(aFileUrl)
|
|
|
|
, mTemporaryFileUrl(mFileUrl)
|
|
|
|
, mWriteFile(NULL)
|
|
|
|
{
|
2002-06-17 08:31:44 -05:00
|
|
|
// First, ensure the directory where the file is supposed to be
|
|
|
|
// put exists.
|
2002-07-11 11:17:42 -05:00
|
|
|
mTemporaryFileUrl += rtl::OUString::createFromAscii("_tmp") ;
|
2002-06-17 08:31:44 -05:00
|
|
|
rtl::OUString parentDirectory = FileHelper::getParentDir(aFileUrl) ;
|
|
|
|
|
2003-04-17 07:29:55 -05:00
|
|
|
if (osl::File::RC errorCode = FileHelper::mkdirs(parentDirectory))
|
|
|
|
{
|
2002-06-17 08:31:44 -05:00
|
|
|
rtl::OUStringBuffer message ;
|
2003-04-17 07:29:55 -05:00
|
|
|
message.appendAscii("Cannot create directory \"") ;
|
|
|
|
message.append(parentDirectory).appendAscii("\". Error is ") ;
|
|
|
|
message.append(FileHelper::createOSLErrorString(errorCode));
|
|
|
|
message.appendAscii(" [").append(sal_Int32(errorCode)).appendAscii("].") ;
|
|
|
|
|
|
|
|
rtl::OUString const sIOMsg = message.makeStringAndClear();
|
|
|
|
uno::Any ioe = uno::makeAny(io::IOException(sIOMsg,0));
|
|
|
|
|
|
|
|
switch (errorCode)
|
|
|
|
{
|
|
|
|
case osl::File::E_ACCES:
|
|
|
|
case osl::File::E_ROFS:
|
|
|
|
message.appendAscii("Configuration LocalOutputStream - No Write Access: ");
|
|
|
|
message.append(sIOMsg);
|
|
|
|
throw backend::InsufficientAccessRightsException(message.makeStringAndClear(), NULL, ioe) ;
|
|
|
|
|
|
|
|
case osl::File::E_None: OSL_ASSERT(!"can't happen");
|
|
|
|
default:
|
|
|
|
message.appendAscii("Configuration LocalOutputStream - IO Error: ");
|
|
|
|
message.append(sIOMsg);
|
|
|
|
throw backend::BackendAccessException(message.makeStringAndClear(), NULL, ioe) ;
|
|
|
|
}
|
2002-06-17 08:31:44 -05:00
|
|
|
}
|
2003-04-17 07:29:55 -05:00
|
|
|
|
|
|
|
osl::File::remove(mTemporaryFileUrl) ;
|
2002-06-17 08:31:44 -05:00
|
|
|
mWriteFile = new osl::File(mTemporaryFileUrl) ;
|
2002-05-27 11:08:47 -05:00
|
|
|
|
2003-04-17 07:29:55 -05:00
|
|
|
if (osl::File::RC errorCode = mWriteFile->open(OpenFlag_Write | OpenFlag_Create) )
|
2002-07-11 11:17:42 -05:00
|
|
|
{
|
|
|
|
delete mWriteFile, mWriteFile = NULL;
|
2003-04-17 07:29:55 -05:00
|
|
|
|
|
|
|
rtl::OUStringBuffer message ;
|
|
|
|
message.appendAscii("Cannot open file \"") ;
|
|
|
|
message.append(mTemporaryFileUrl).appendAscii("\" for writing. ");
|
|
|
|
message.appendAscii("Error is ").append(FileHelper::createOSLErrorString(errorCode));
|
|
|
|
message.appendAscii(" [").append(sal_Int32(errorCode)).appendAscii("].") ;
|
|
|
|
|
|
|
|
rtl::OUString const sIOMsg = message.makeStringAndClear();
|
|
|
|
uno::Any ioe = uno::makeAny(io::IOException(sIOMsg,0));
|
|
|
|
|
|
|
|
switch (errorCode)
|
|
|
|
{
|
|
|
|
case osl::File::E_EXIST: // take inability to remove as indicator of missing rights
|
|
|
|
case osl::File::E_ACCES:
|
|
|
|
case osl::File::E_ROFS:
|
|
|
|
message.appendAscii("Configuration LocalOutputStream - No Write Access: ");
|
|
|
|
message.append(sIOMsg);
|
|
|
|
throw backend::InsufficientAccessRightsException(message.makeStringAndClear(), NULL, ioe) ;
|
|
|
|
|
|
|
|
case osl::File::E_None: OSL_ASSERT(!"can't happen");
|
|
|
|
default:
|
|
|
|
message.appendAscii("Configuration LocalOutputStream - IO Error: ");
|
|
|
|
message.append(sIOMsg);
|
|
|
|
throw backend::BackendAccessException(message.makeStringAndClear(), NULL, ioe) ;
|
|
|
|
}
|
2002-05-27 11:08:47 -05:00
|
|
|
}
|
|
|
|
mTemporaryFile = new OSLOutputStreamWrapper(*mWriteFile) ;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2003-04-17 07:29:55 -05:00
|
|
|
LocalOutputStream::~LocalOutputStream()
|
2002-07-11 11:17:42 -05:00
|
|
|
{
|
2003-04-17 07:29:55 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
this->closeOutput();
|
2002-07-11 11:17:42 -05:00
|
|
|
// if (mWriteFile) FileHelper::removeFile(mTemporaryFileUrl) ;
|
2003-04-17 07:29:55 -05:00
|
|
|
}
|
|
|
|
catch (uno::Exception&)
|
|
|
|
{
|
|
|
|
OSL_ENSURE(false,"Exception from closing LocalOutputStream ignored.");
|
|
|
|
}
|
|
|
|
|
|
|
|
delete mWriteFile;
|
2002-07-11 11:17:42 -05:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void LocalOutputStream::finishOutput()
|
2003-04-17 07:29:55 -05:00
|
|
|
throw (backend::BackendAccessException, uno::RuntimeException)
|
2002-07-11 11:17:42 -05:00
|
|
|
{
|
|
|
|
if (mWriteFile)
|
2003-04-17 07:29:55 -05:00
|
|
|
try
|
2002-07-11 11:17:42 -05:00
|
|
|
{
|
|
|
|
this->closeOutput();
|
|
|
|
delete mWriteFile, mWriteFile = NULL;
|
|
|
|
|
|
|
|
FileHelper::replaceFile(mFileUrl, mTemporaryFileUrl) ;
|
|
|
|
}
|
2003-04-17 07:29:55 -05:00
|
|
|
catch (io::IOException& ioe)
|
|
|
|
{
|
|
|
|
rtl::OUStringBuffer message ;
|
|
|
|
message.appendAscii("Configuration LocalOutputStream - IO Error: ");
|
|
|
|
message.appendAscii("Cannot finish output to \"").append(mTemporaryFileUrl) ;
|
|
|
|
message.appendAscii("\" or copy the result to \"").append(mFileUrl).appendAscii("\". ");
|
|
|
|
message.appendAscii("Error is \"").append(ioe.Message).appendAscii("\". ");
|
|
|
|
throw backend::BackendAccessException(message.makeStringAndClear(), *this, uno::makeAny(ioe));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
inline
|
|
|
|
uno::Reference<io::XOutputStream> LocalOutputStream::getOutputFile()
|
|
|
|
{
|
|
|
|
if (!mTemporaryFile.is())
|
|
|
|
{
|
|
|
|
throw io::NotConnectedException(
|
|
|
|
rtl::OUString::createFromAscii("LocalOutputStream: no output file."),
|
|
|
|
*this);
|
|
|
|
}
|
|
|
|
return mTemporaryFile;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SAL_CALL LocalOutputStream::writeBytes(const uno::Sequence<sal_Int8>& aData)
|
|
|
|
throw (io::NotConnectedException,
|
|
|
|
io::BufferSizeExceededException,
|
|
|
|
io::IOException, uno::RuntimeException)
|
|
|
|
{
|
|
|
|
getOutputFile()->writeBytes(aData) ;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void SAL_CALL LocalOutputStream::flush()
|
|
|
|
throw (io::NotConnectedException,
|
|
|
|
io::BufferSizeExceededException,
|
|
|
|
io::IOException, uno::RuntimeException)
|
|
|
|
{
|
|
|
|
getOutputFile()->flush() ;
|
2002-05-27 11:08:47 -05:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2003-04-17 07:29:55 -05:00
|
|
|
void SAL_CALL LocalOutputStream::closeOutput()
|
2002-05-27 11:08:47 -05:00
|
|
|
throw (io::NotConnectedException, io::BufferSizeExceededException,
|
|
|
|
io::IOException, uno::RuntimeException)
|
|
|
|
{
|
2002-07-11 11:17:42 -05:00
|
|
|
if (mTemporaryFile.is())
|
|
|
|
{
|
|
|
|
mTemporaryFile->closeOutput() ;
|
|
|
|
|
|
|
|
mTemporaryFile.clear();
|
|
|
|
}
|
2002-05-27 11:08:47 -05:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2002-06-17 08:31:44 -05:00
|
|
|
} } // configmgr.localbe
|