summaryrefslogtreecommitdiffstats
path: root/desktop/source/pkgchk/unopkg/unopkg_misc.cxx
blob: 553952917748218d5c87735ddc9c0f9657bdf17a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#include <string_view>

#include <config_folders.h>

#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>
#include <rtl/bootstrap.hxx>
#include <rtl/ustrbuf.hxx>
#include <sal/log.hxx>
#include <osl/process.h>
#include <osl/file.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/bootstrap.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <comphelper/sequence.hxx>
#include <comphelper/processfactory.hxx>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/ucb/UniversalContentBroker.hpp>

#include <strings.hrc>
#include "unopkg_shared.h"
#include <dp_identifier.hxx>
#include <dp_misc.h>
#include <dp_shared.hxx>
#include <lockfile.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::ucb;

namespace unopkg {

OUString toString( OptionInfo const * info )
{
    assert(info != nullptr);
    OUStringBuffer buf("--");
    buf.appendAscii(info->m_name);
    if (info->m_short_option != '\0')
    {
        buf.append(" (short -" + OUStringChar(info->m_short_option) + ")");
    }
    if (info->m_has_argument)
        buf.append(" <argument>" );
    return buf.makeStringAndClear();
}


OptionInfo const * getOptionInfo(
    OptionInfo const * list,
    OUString const & opt )
{
    for ( ; list->m_name != nullptr; ++list )
    {
        OptionInfo const & option_info = *list;
        if (!opt.isEmpty())
        {
            if (opt.equalsAsciiL(
                    option_info.m_name, option_info.m_name_length ))
            {
                return &option_info;
            }
        }
    }
    SAL_WARN( "desktop", opt );
    return nullptr;
}


bool isOption( OptionInfo const * option_info, sal_uInt32 * pIndex )
{
    assert(option_info != nullptr);
    if (osl_getCommandArgCount() <= *pIndex)
        return false;

    OUString arg;
    osl_getCommandArg( *pIndex, &arg.pData );
    sal_Int32 len = arg.getLength();

    if (len < 2 || arg[ 0 ] != '-')
        return false;

    if (len == 2 && arg[ 1 ] == option_info->m_short_option)
    {
        ++(*pIndex);
        dp_misc::TRACE(__FILE__ ": identified option \'\'"
            + OUStringChar( option_info->m_short_option ) + "\n");
        return true;
    }
    if (arg[ 1 ] == '-' && rtl_ustr_ascii_compare(
            arg.pData->buffer + 2, option_info->m_name ) == 0)
    {
        ++(*pIndex);
        dp_misc::TRACE(__FILE__ ": identified option \'"
            + OUString::createFromAscii(option_info->m_name) + "\'\n");
        return true;
    }
    return false;
}


bool isBootstrapVariable(sal_uInt32 * pIndex)
{
    OSL_ASSERT(osl_getCommandArgCount() >=  *pIndex);

    OUString arg;
    osl_getCommandArg(*pIndex, &arg.pData);
    if (arg.match("-env:"))
    {
        ++(*pIndex);
        return true;
    }
    return false;
}


bool readArgument(
    OUString * pValue, OptionInfo const * option_info, sal_uInt32 * pIndex )
{
    if (isOption( option_info, pIndex ))
    {
        if (*pIndex < osl_getCommandArgCount())
        {
            OSL_ASSERT( pValue != nullptr );
            osl_getCommandArg( *pIndex, &pValue->pData );
            dp_misc::TRACE(__FILE__ ": argument value: "
                + *pValue + "\n");
            ++(*pIndex);
            return true;
        }
        --(*pIndex);
    }
    return false;
}


OUString const & getExecutableDir()
{
    static const OUString EXEC =
        []()
        {
            OUString path;
            if (osl_getExecutableFile( &path.pData ) != osl_Process_E_None) {
                throw RuntimeException("cannot locate executable directory!",nullptr);
            }
            return path.copy( 0, path.lastIndexOf( '/' ) );
        }();
    return EXEC;
}


OUString const & getProcessWorkingDir()
{
    static const OUString WORKING =
        []()
        {
            OUString workingDir;
            utl::Bootstrap::getProcessWorkingDir(workingDir);
            return workingDir;
        }();
    return WORKING;
}


OUString makeAbsoluteFileUrl(
    OUString const & sys_path, OUString const & base_url )
{
    // system path to file url
    OUString file_url;
    oslFileError rc = osl_getFileURLFromSystemPath( sys_path.pData, &file_url.pData );
    if ( rc != osl_File_E_None) {
        OUString tempPath;
        if ( osl_getSystemPathFromFileURL( sys_path.pData, &tempPath.pData) != osl_File_E_None )
        {
            throw RuntimeException("cannot get file url from system path: " +
                sys_path );
        }
        file_url = sys_path;
    }

    OUString abs;
    if (osl_getAbsoluteFileURL(
            base_url.pData, file_url.pData, &abs.pData ) != osl_File_E_None)
    {
        throw RuntimeException(
            "making absolute file url failed: \"" + base_url
            + "\" (base-url) and \"" + file_url + "\" (file-url)!" );
    }
    return abs[ abs.getLength() -1 ] == '/'
        ? abs.copy( 0, abs.getLength() -1 ) : abs;
}


namespace {


void printf_space( sal_Int32 space )
{
    while (space--)
        dp_misc::writeConsole(u"  ");
}


void printf_line(
    std::u16string_view name, std::u16string_view value, sal_Int32 level )
{
    printf_space( level );
    dp_misc::writeConsole(Concat2View(OUString::Concat(name) + ": " + value + "\n"));
}


void printf_package(
    Reference<deployment::XPackage> const & xPackage,
    Reference<XCommandEnvironment> const & xCmdEnv, sal_Int32 level )
{
    beans::Optional< OUString > id(
        level == 0
        ? beans::Optional< OUString >(
            true, dp_misc::getIdentifier( xPackage ) )
        : xPackage->getIdentifier() );
    if (id.IsPresent)
        printf_line( u"Identifier", id.Value, level );
    OUString version(xPackage->getVersion());
    if (!version.isEmpty())
        printf_line( u"Version", version, level + 1 );
    printf_line( u"URL", xPackage->getURL(), level + 1 );

    beans::Optional< beans::Ambiguous<sal_Bool> > option(
        xPackage->isRegistered( Reference<task::XAbortChannel>(), xCmdEnv ) );
    OUString value;
    if (option.IsPresent) {
        beans::Ambiguous<sal_Bool> const & reg = option.Value;
        if (reg.IsAmbiguous)
            value = "unknown";
        else
            value = reg.Value ? std::u16string_view(u"yes") : std::u16string_view(u"no");
    }
    else
        value = "n/a";
    printf_line( u"is registered", value, level + 1 );

    const Reference<deployment::XPackageTypeInfo> xPackageType(
        xPackage->getPackageType() );
    OSL_ASSERT( xPackageType.is() );
    if (xPackageType.is()) {
        printf_line( u"Media-Type", xPackageType->getMediaType(), level + 1 );
    }
    printf_line( u"Description", xPackage->getDescription(), level + 1 );
    if (!xPackage->isBundle())
        return;

    Sequence< Reference<deployment::XPackage> > seq(
        xPackage->getBundle( Reference<task::XAbortChannel>(), xCmdEnv ) );
    printf_space( level + 1 );
    dp_misc::writeConsole(u"bundled Packages: {\n");
    std::vector<Reference<deployment::XPackage> >vec_bundle;
    ::comphelper::sequenceToContainer(vec_bundle, seq);
    printf_packages( vec_bundle, std::vector<bool>(vec_bundle.size()),
                     xCmdEnv, level + 2 );
    printf_space( level + 1 );
    dp_misc::writeConsole(u"}\n");
}

} // anon namespace

static void printf_unaccepted_licenses(
    Reference<deployment::XPackage> const & ext)
{
        OUString id(
            dp_misc::getIdentifier(ext) );
        printf_line( u"Identifier", id, 0 );
        printf_space(1);
        dp_misc::writeConsole(u"License not accepted\n\n");
}


void printf_packages(
    std::vector< Reference<deployment::XPackage> > const & allExtensions,
    std::vector<bool> const & vecUnaccepted,
    Reference<XCommandEnvironment> const & xCmdEnv, sal_Int32 level )
{
    OSL_ASSERT(allExtensions.size() == vecUnaccepted.size());

    if (allExtensions.empty())
    {
        printf_space( level );
        dp_misc::writeConsole(u"<none>\n");
    }
    else
    {
        int index = 0;
        for (auto const& extension : allExtensions)
        {
            if (vecUnaccepted[index])
                printf_unaccepted_licenses(extension);
            else
                printf_package( extension, xCmdEnv, level );
            dp_misc::writeConsole(u"\n");
            ++index;
        }
    }
}


namespace {


Reference<XComponentContext> bootstrapStandAlone()
{
    Reference<XComponentContext> xContext =
        ::cppu::defaultBootstrap_InitialComponentContext();

    Reference<lang::XMultiServiceFactory> xServiceManager(
        xContext->getServiceManager(), UNO_QUERY_THROW );
    // set global process service factory used by unotools config helpers
    ::comphelper::setProcessServiceFactory( xServiceManager );

    // Initialize the UCB (for backwards compatibility, in case some code still
    // uses plain createInstance w/o args directly to obtain an instance):
    UniversalContentBroker::create( xContext );

    return xContext;
}


Reference<XComponentContext> connectToOffice(
    Reference<XComponentContext> const & xLocalComponentContext,
    bool verbose )
{
    OUString pipeId( ::dp_misc::generateRandomPipeId() );
    OUString acceptArg = "--accept=pipe,name=" + pipeId + ";urp;";

    Sequence<OUString> args { "--nologo", "--nodefault", acceptArg };
    OUString appURL( getExecutableDir() + "/soffice" );

    if (verbose)
    {
        dp_misc::writeConsole(Concat2View(
            "Raising process: " + appURL +
            "\nArguments: --nologo --nodefault " + args[2] +
            "\n"));
    }

    ::dp_misc::raiseProcess( appURL, args );

    if (verbose)
        dp_misc::writeConsole(u"OK.  Connecting...");

    OUString sUnoUrl = "uno:pipe,name=" + pipeId + ";urp;StarOffice.ComponentContext";
    Reference<XComponentContext> xRet(
        ::dp_misc::resolveUnoURL(
            sUnoUrl, xLocalComponentContext ),
        UNO_QUERY_THROW );
    if (verbose)
        dp_misc::writeConsole(u"OK.\n");

    return xRet;
}

} // anon namespace

/** returns the path to the lock file used by unopkg.
    @return the path. An empty string signifies an error.
*/
static OUString getLockFilePath()
{
    OUString ret;
    OUString sBootstrap("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}");
    rtl::Bootstrap::expandMacros(sBootstrap);
    OUString sAbs;
    if (::osl::File::E_None ==  ::osl::File::getAbsoluteFileURL(
        sBootstrap, ".lock", sAbs))
    {
        if (::osl::File::E_None ==
            ::osl::File::getSystemPathFromFileURL(sAbs, sBootstrap))
        {
            ret = sBootstrap;
        }
    }

    return ret;
}

Reference<XComponentContext> getUNO(
    bool verbose, bool bGui, const OUString& sTempDir,
    Reference<XComponentContext> & out_localContext)
{
    // do not create any user data (for the root user) in --shared mode:
    if (!sTempDir.isEmpty())
        rtl::Bootstrap::set("UserInstallation", sTempDir);

    // hold lock during process runtime:
    static ::desktop::Lockfile s_lockfile( false /* no IPC server */ );
    Reference<XComponentContext> xComponentContext( bootstrapStandAlone() );
    out_localContext = xComponentContext;
    if (::dp_misc::office_is_running()) {
        xComponentContext.set(
            connectToOffice( xComponentContext, verbose ) );
    }
    else
    {
        if (! s_lockfile.check( nullptr ))
        {
            OUString sMsg(DpResId(RID_STR_CONCURRENTINSTANCE));
            OUString sError(DpResId(RID_STR_UNOPKG_ERROR));

            sMsg += "\n" + getLockFilePath();

            if (bGui)
            {
                //We show a message box or print to the console that there
                //is another instance already running
                if ( ! InitVCL() )
                    throw RuntimeException( "Cannot initialize VCL!" );
                {
                    std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(nullptr,
                                                               VclMessageType::Warning, VclButtonsType::Ok,
                                                               sMsg));
                    xWarn->set_title(utl::ConfigManager::getProductName());
                    xWarn->run();
                }
                DeInitVCL();
            }

            throw LockFileException(sError + sMsg);
        }
    }

    return xComponentContext;
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */