1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: localoutputstream.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
33 #include "localoutputstream.hxx"
34 #include <rtl/ustrbuf.hxx>
35 #include <com/sun/star/configuration/backend/InsufficientAccessRightsException.hpp>
37 namespace configmgr
{ namespace localbe
{
39 //==============================================================================
41 //------------------------------------------------------------------------------
43 LocalOutputStream::LocalOutputStream(const rtl::OUString
& aFileUrl
)
44 throw (backend::BackendAccessException
, uno::RuntimeException
)
46 , mTemporaryFileUrl(mFileUrl
)
49 // First, ensure the directory where the file is supposed to be
51 mTemporaryFileUrl
+= rtl::OUString::createFromAscii("_tmp") ;
52 rtl::OUString parentDirectory
= FileHelper::getParentDir(aFileUrl
) ;
54 if (osl::File::RC errorCode
= FileHelper::mkdirs(parentDirectory
))
56 rtl::OUStringBuffer message
;
57 message
.appendAscii("Cannot create directory \"") ;
58 message
.append(parentDirectory
).appendAscii("\". Error is ") ;
59 message
.append(FileHelper::createOSLErrorString(errorCode
));
60 message
.appendAscii(" [").append(sal_Int32(errorCode
)).appendAscii("].") ;
62 rtl::OUString
const sIOMsg
= message
.makeStringAndClear();
63 uno::Any ioe
= uno::makeAny(io::IOException(sIOMsg
,0));
67 case osl::File::E_ACCES
:
68 case osl::File::E_ROFS
:
69 message
.appendAscii("Configuration LocalOutputStream - No Write Access: ");
70 message
.append(sIOMsg
);
71 throw backend::InsufficientAccessRightsException(message
.makeStringAndClear(), NULL
, ioe
) ;
73 case osl::File::E_None
: OSL_ASSERT(!"can't happen");
75 message
.appendAscii("Configuration LocalOutputStream - IO Error: ");
76 message
.append(sIOMsg
);
77 throw backend::BackendAccessException(message
.makeStringAndClear(), NULL
, ioe
) ;
81 osl::File::remove(mTemporaryFileUrl
) ;
82 mWriteFile
= new osl::File(mTemporaryFileUrl
) ;
84 if (osl::File::RC errorCode
= mWriteFile
->open(OpenFlag_Write
| OpenFlag_Create
) )
86 delete mWriteFile
, mWriteFile
= NULL
;
88 rtl::OUStringBuffer message
;
89 message
.appendAscii("Cannot open file \"") ;
90 message
.append(mTemporaryFileUrl
).appendAscii("\" for writing. ");
91 message
.appendAscii("Error is ").append(FileHelper::createOSLErrorString(errorCode
));
92 message
.appendAscii(" [").append(sal_Int32(errorCode
)).appendAscii("].") ;
94 rtl::OUString
const sIOMsg
= message
.makeStringAndClear();
95 uno::Any ioe
= uno::makeAny(io::IOException(sIOMsg
,0));
99 case osl::File::E_EXIST
: // take inability to remove as indicator of missing rights
100 case osl::File::E_ACCES
:
101 case osl::File::E_ROFS
:
102 message
.appendAscii("Configuration LocalOutputStream - No Write Access: ");
103 message
.append(sIOMsg
);
104 throw backend::InsufficientAccessRightsException(message
.makeStringAndClear(), NULL
, ioe
) ;
106 case osl::File::E_None
: OSL_ASSERT(!"can't happen");
108 message
.appendAscii("Configuration LocalOutputStream - IO Error: ");
109 message
.append(sIOMsg
);
110 throw backend::BackendAccessException(message
.makeStringAndClear(), NULL
, ioe
) ;
113 mTemporaryFile
= new OSLOutputStreamWrapper(*mWriteFile
) ;
115 //------------------------------------------------------------------------------
117 LocalOutputStream::~LocalOutputStream()
123 catch (uno::Exception
&)
125 OSL_ENSURE(false,"Exception from closing LocalOutputStream ignored.");
130 //------------------------------------------------------------------------------
132 void LocalOutputStream::finishOutput()
133 throw (backend::BackendAccessException
, uno::RuntimeException
)
139 delete mWriteFile
, mWriteFile
= NULL
;
141 FileHelper::replaceFile(mFileUrl
, mTemporaryFileUrl
) ;
143 catch (io::IOException
& ioe
)
145 rtl::OUStringBuffer message
;
146 message
.appendAscii("Configuration LocalOutputStream - IO Error: ");
147 message
.appendAscii("Cannot finish output to \"").append(mTemporaryFileUrl
) ;
148 message
.appendAscii("\" or copy the result to \"").append(mFileUrl
).appendAscii("\". ");
149 message
.appendAscii("Error is \"").append(ioe
.Message
).appendAscii("\". ");
150 throw backend::BackendAccessException(message
.makeStringAndClear(), *this, uno::makeAny(ioe
));
153 //------------------------------------------------------------------------------
156 uno::Reference
<io::XOutputStream
> LocalOutputStream::getOutputFile()
158 if (!mTemporaryFile
.is())
160 throw io::NotConnectedException(
161 rtl::OUString::createFromAscii("LocalOutputStream: no output file."),
164 return mTemporaryFile
;
166 //------------------------------------------------------------------------------
168 void SAL_CALL
LocalOutputStream::writeBytes(const uno::Sequence
<sal_Int8
>& aData
)
169 throw (io::NotConnectedException
,
170 io::BufferSizeExceededException
,
171 io::IOException
, uno::RuntimeException
)
173 getOutputFile()->writeBytes(aData
) ;
175 //------------------------------------------------------------------------------
177 void SAL_CALL
LocalOutputStream::flush()
178 throw (io::NotConnectedException
,
179 io::BufferSizeExceededException
,
180 io::IOException
, uno::RuntimeException
)
182 getOutputFile()->flush() ;
184 //------------------------------------------------------------------------------
186 void SAL_CALL
LocalOutputStream::closeOutput()
187 throw (io::NotConnectedException
, io::BufferSizeExceededException
,
188 io::IOException
, uno::RuntimeException
)
190 if (mTemporaryFile
.is())
192 mTemporaryFile
->closeOutput() ;
194 mTemporaryFile
.clear();
197 //------------------------------------------------------------------------------
199 } } // configmgr.localbe