/src/libreoffice/package/source/xstor/xfactory.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #include <sal/config.h> |
21 | | #include <sal/log.hxx> |
22 | | |
23 | | #include <com/sun/star/ucb/SimpleFileAccess.hpp> |
24 | | #include <com/sun/star/embed/ElementModes.hpp> |
25 | | #include <com/sun/star/embed/StorageFormats.hpp> |
26 | | #include <com/sun/star/beans/PropertyValue.hpp> |
27 | | #include <com/sun/star/io/IOException.hpp> |
28 | | #include <com/sun/star/io/TempFile.hpp> |
29 | | #include <com/sun/star/io/XSeekable.hpp> |
30 | | |
31 | | #include <comphelper/propertyvalue.hxx> |
32 | | #include <comphelper/storagehelper.hxx> |
33 | | #include <cppuhelper/supportsservice.hxx> |
34 | | #include <cppuhelper/weak.hxx> |
35 | | #include <osl/diagnose.h> |
36 | | #include <unotools/tempfile.hxx> |
37 | | |
38 | | #include "xfactory.hxx" |
39 | | #include "xstorage.hxx" |
40 | | |
41 | | using namespace ::com::sun::star; |
42 | | |
43 | | #if OSL_DEBUG_LEVEL > 0 |
44 | | #define THROW_WHERE SAL_WHERE |
45 | | #else |
46 | 0 | #define THROW_WHERE "" |
47 | | #endif |
48 | | |
49 | | static bool CheckPackageSignature_Impl( const uno::Reference< io::XInputStream >& xInputStream, |
50 | | const uno::Reference< io::XSeekable >& xSeekable ) |
51 | 65.4k | { |
52 | 65.4k | if ( !xInputStream.is() || !xSeekable.is() ) |
53 | 0 | throw uno::RuntimeException(); |
54 | | |
55 | 65.4k | if ( xSeekable->getLength() ) |
56 | 65.4k | { |
57 | 65.4k | uno::Sequence< sal_Int8 > aData( 4 ); |
58 | 65.4k | xSeekable->seek( 0 ); |
59 | 65.4k | sal_Int32 nRead = xInputStream->readBytes( aData, 4 ); |
60 | 65.4k | xSeekable->seek( 0 ); |
61 | | |
62 | | // TODO/LATER: should the disk spanned files be supported? |
63 | | // 0x50, 0x4b, 0x07, 0x08 |
64 | 65.4k | return ( nRead == 4 && aData[0] == 0x50 && aData[1] == 0x4b && aData[2] == 0x03 && aData[3] == 0x04 ); |
65 | 65.4k | } |
66 | 18 | else |
67 | 18 | return true; // allow to create a storage based on empty stream |
68 | 65.4k | } |
69 | | |
70 | | |
71 | | |
72 | | uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstance() |
73 | 166k | { |
74 | | // TODO: reimplement TempStream service to support XStream interface |
75 | 166k | uno::Reference < io::XStream > xTempStream(new utl::TempFileFastService); |
76 | | |
77 | 166k | return cppu::getXWeak(new OStorage(xTempStream, embed::ElementModes::READWRITE, |
78 | 166k | uno::Sequence<beans::PropertyValue>(), m_xContext, |
79 | 166k | embed::StorageFormats::PACKAGE)); |
80 | 166k | } |
81 | | |
82 | | uno::Reference< uno::XInterface > SAL_CALL OStorageFactory::createInstanceWithArguments( |
83 | | const uno::Sequence< uno::Any >& aArguments ) |
84 | 65.4k | { |
85 | | // The request for storage can be done with up to three arguments |
86 | | |
87 | | // The first argument specifies a source for the storage |
88 | | // it can be URL, XStream, XInputStream. |
89 | | // The second value is a mode the storage should be open in. |
90 | | // And the third value is a media descriptor. |
91 | | |
92 | 65.4k | sal_Int32 nArgNum = aArguments.getLength(); |
93 | 65.4k | OSL_ENSURE( nArgNum < 4, "Wrong parameter number" ); |
94 | | |
95 | 65.4k | if ( !nArgNum ) |
96 | 0 | return createInstance(); |
97 | | |
98 | | // first try to retrieve storage open mode if any |
99 | | // by default the storage will be open in readonly mode |
100 | 65.4k | sal_Int32 nStorageMode = embed::ElementModes::READ; |
101 | 65.4k | if ( nArgNum >= 2 ) |
102 | 65.4k | { |
103 | 65.4k | if( !( aArguments[1] >>= nStorageMode ) ) |
104 | 0 | { |
105 | 0 | OSL_FAIL( "Wrong second argument!" ); |
106 | 0 | throw lang::IllegalArgumentException(); // TODO: |
107 | 0 | } |
108 | | // it's always possible to read written storage in this implementation |
109 | 65.4k | nStorageMode |= embed::ElementModes::READ; |
110 | 65.4k | } |
111 | | |
112 | 65.4k | if ( ( nStorageMode & embed::ElementModes::TRUNCATE ) == embed::ElementModes::TRUNCATE |
113 | 0 | && ( nStorageMode & embed::ElementModes::WRITE ) != embed::ElementModes::WRITE ) |
114 | 0 | throw lang::IllegalArgumentException(); // TODO: |
115 | | |
116 | | // retrieve storage source stream |
117 | 65.4k | OUString aURL; |
118 | 65.4k | uno::Reference< io::XStream > xStream; |
119 | 65.4k | uno::Reference< io::XInputStream > xInputStream; |
120 | | |
121 | 65.4k | if ( aArguments[0] >>= aURL ) |
122 | 0 | { |
123 | 0 | if ( aURL.isEmpty() ) |
124 | 0 | { |
125 | 0 | OSL_FAIL( "Empty URL is provided!" ); |
126 | 0 | throw lang::IllegalArgumentException(); // TODO: |
127 | 0 | } |
128 | | |
129 | 0 | if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:") ) |
130 | 0 | { |
131 | 0 | OSL_FAIL( "Packages URL's are not valid for storages!" ); // ??? |
132 | 0 | throw lang::IllegalArgumentException(); // TODO: |
133 | 0 | } |
134 | | |
135 | 0 | uno::Reference < ucb::XSimpleFileAccess3 > xTempAccess( |
136 | 0 | ucb::SimpleFileAccess::create( |
137 | 0 | m_xContext ) ); |
138 | |
|
139 | 0 | if ( nStorageMode & embed::ElementModes::WRITE ) |
140 | 0 | xStream = xTempAccess->openFileReadWrite( aURL ); |
141 | 0 | else |
142 | 0 | xInputStream = xTempAccess->openFileRead( aURL ); |
143 | 0 | } |
144 | 65.4k | else if ( !( aArguments[0] >>= xStream ) && !( aArguments[0] >>= xInputStream ) ) |
145 | 0 | { |
146 | 0 | OSL_FAIL( "Wrong first argument!" ); |
147 | 0 | throw uno::Exception(u"wrong first arg"_ustr, nullptr); // TODO: Illegal argument |
148 | 0 | } |
149 | | |
150 | | // retrieve mediadescriptor and set storage properties |
151 | 65.4k | uno::Sequence< beans::PropertyValue > aDescr; |
152 | 65.4k | uno::Sequence< beans::PropertyValue > aPropsToSet; |
153 | | |
154 | 65.4k | sal_Int32 nStorageType = embed::StorageFormats::PACKAGE; |
155 | | |
156 | 65.4k | if ( nArgNum >= 3 ) |
157 | 65.4k | { |
158 | 65.4k | if( aArguments[2] >>= aDescr ) |
159 | 65.4k | { |
160 | 65.4k | if ( !aURL.isEmpty() ) |
161 | 0 | { |
162 | 0 | aPropsToSet = { comphelper::makePropertyValue(u"URL"_ustr, aURL) }; |
163 | 0 | } |
164 | | |
165 | 65.4k | sal_Int32 nNumArgs = 1; |
166 | 65.4k | for (const auto& rProp : aDescr) |
167 | 65.4k | { |
168 | 65.4k | if ( rProp.Name == "InteractionHandler" |
169 | 65.4k | || rProp.Name == "Password" |
170 | 65.4k | || rProp.Name == "RepairPackage" |
171 | 65.4k | || rProp.Name == "StatusIndicator" ) |
172 | 0 | { |
173 | 0 | aPropsToSet.realloc( ++nNumArgs ); |
174 | 0 | auto pPropsToSet = aPropsToSet.getArray(); |
175 | 0 | pPropsToSet[nNumArgs-1].Name = rProp.Name; |
176 | 0 | pPropsToSet[nNumArgs-1].Value = rProp.Value; |
177 | 0 | } |
178 | 65.4k | else if ( rProp.Name == "StorageFormat" ) |
179 | 65.4k | { |
180 | 65.4k | OUString aFormatName; |
181 | 65.4k | sal_Int32 nFormatID = 0; |
182 | 65.4k | if ( rProp.Value >>= aFormatName ) |
183 | 65.4k | { |
184 | 65.4k | if ( aFormatName == PACKAGE_STORAGE_FORMAT_STRING ) |
185 | 0 | nStorageType = embed::StorageFormats::PACKAGE; |
186 | 65.4k | else if ( aFormatName == ZIP_STORAGE_FORMAT_STRING ) |
187 | 46.3k | nStorageType = embed::StorageFormats::ZIP; |
188 | 19.1k | else if ( aFormatName == OFOPXML_STORAGE_FORMAT_STRING ) |
189 | 19.1k | nStorageType = embed::StorageFormats::OFOPXML; |
190 | 0 | else |
191 | 0 | throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 ); |
192 | 65.4k | } |
193 | 0 | else if ( rProp.Value >>= nFormatID ) |
194 | 0 | { |
195 | 0 | if ( nFormatID != embed::StorageFormats::PACKAGE |
196 | 0 | && nFormatID != embed::StorageFormats::ZIP |
197 | 0 | && nFormatID != embed::StorageFormats::OFOPXML ) |
198 | 0 | throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 ); |
199 | | |
200 | 0 | nStorageType = nFormatID; |
201 | 0 | } |
202 | 0 | else |
203 | 0 | throw lang::IllegalArgumentException( THROW_WHERE, uno::Reference< uno::XInterface >(), 1 ); |
204 | 65.4k | } |
205 | 0 | else if (rProp.Name == "NoFileSync") |
206 | 0 | { |
207 | | // Forward NoFileSync to the storage. |
208 | 0 | aPropsToSet.realloc(++nNumArgs); |
209 | 0 | auto pPropsToSet = aPropsToSet.getArray(); |
210 | 0 | pPropsToSet[nNumArgs - 1].Name = rProp.Name; |
211 | 0 | pPropsToSet[nNumArgs - 1].Value = rProp.Value; |
212 | 0 | } |
213 | 0 | else |
214 | 0 | OSL_FAIL( "Unacceptable property, will be ignored!" ); |
215 | 65.4k | } |
216 | 65.4k | } |
217 | 0 | else |
218 | 0 | { |
219 | 0 | OSL_FAIL( "Wrong third argument!" ); |
220 | 0 | throw uno::Exception(u"wrong 3rd arg"_ustr, nullptr); // TODO: Illegal argument |
221 | 0 | } |
222 | | |
223 | 65.4k | } |
224 | | |
225 | | // create storage based on source |
226 | 65.4k | if ( xInputStream.is() ) |
227 | 65.4k | { |
228 | | // if xInputStream is set the storage should be open from it |
229 | 65.4k | if ( nStorageMode & embed::ElementModes::WRITE ) |
230 | 0 | throw uno::Exception(u"storagemode==write"_ustr, nullptr); // TODO: access denied |
231 | | |
232 | 65.4k | uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY ); |
233 | 65.4k | if ( !xSeekable.is() ) |
234 | 0 | { |
235 | | // TODO: wrap stream to let it be seekable |
236 | 0 | OSL_FAIL( "Nonseekable streams are not supported for now!" ); |
237 | 0 | } |
238 | | |
239 | 65.4k | if ( !CheckPackageSignature_Impl( xInputStream, xSeekable ) ) |
240 | 12.2k | throw io::IOException(u"package signature check failed, probably not a package file"_ustr, nullptr); // TODO: this is not a package file |
241 | | |
242 | 53.1k | return cppu::getXWeak( |
243 | 53.1k | new OStorage(xInputStream, nStorageMode, aPropsToSet, m_xContext, nStorageType)); |
244 | 65.4k | } |
245 | 61 | else if ( xStream.is() ) |
246 | 61 | { |
247 | 61 | if ( ( ( nStorageMode & embed::ElementModes::WRITE ) && !xStream->getOutputStream().is() ) |
248 | 61 | || !xStream->getInputStream().is() ) |
249 | 0 | throw uno::Exception(u"access denied"_ustr, nullptr); // TODO: access denied |
250 | | |
251 | 61 | uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY ); |
252 | 61 | if ( !xSeekable.is() ) |
253 | 0 | { |
254 | | // TODO: wrap stream to let it be seekable |
255 | 0 | OSL_FAIL( "Nonseekable streams are not supported for now!" ); |
256 | 0 | } |
257 | | |
258 | 61 | if ( !CheckPackageSignature_Impl( xStream->getInputStream(), xSeekable ) ) |
259 | 10 | throw io::IOException(); // TODO: this is not a package file |
260 | | |
261 | 51 | return cppu::getXWeak( |
262 | 51 | new OStorage(xStream, nStorageMode, aPropsToSet, m_xContext, nStorageType)); |
263 | 61 | } |
264 | | |
265 | 0 | throw uno::Exception(u"no input stream or regular stream"_ustr, nullptr); // general error during creation |
266 | 65.4k | } |
267 | | |
268 | | OUString SAL_CALL OStorageFactory::getImplementationName() |
269 | 0 | { |
270 | 0 | return u"com.sun.star.comp.embed.StorageFactory"_ustr; |
271 | 0 | } |
272 | | |
273 | | sal_Bool SAL_CALL OStorageFactory::supportsService( const OUString& ServiceName ) |
274 | 0 | { |
275 | 0 | return cppu::supportsService(this, ServiceName); |
276 | 0 | } |
277 | | |
278 | | uno::Sequence< OUString > SAL_CALL OStorageFactory::getSupportedServiceNames() |
279 | 0 | { |
280 | 0 | return { u"com.sun.star.embed.StorageFactory"_ustr, |
281 | 0 | u"com.sun.star.comp.embed.StorageFactory"_ustr }; |
282 | 0 | } |
283 | | |
284 | | |
285 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* |
286 | | package_OStorageFactory_get_implementation( |
287 | | css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&) |
288 | 13 | { |
289 | 13 | return cppu::acquire(new OStorageFactory(context)); |
290 | 13 | } |
291 | | |
292 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |