LibreOffice Module sfx2 (master) 1
flatpak.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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
10#include <sal/config.h>
11
12#include <cassert>
13#include <cstdlib>
14#include <cstring>
15
16#include <osl/file.hxx>
17#include <osl/thread.h>
18#include <rtl/textcvt.h>
19#include <rtl/ustring.h>
20#include <rtl/ustring.hxx>
21#include <sal/log.hxx>
22#include <sfx2/flatpak.hxx>
23#include <tools/debug.hxx>
24#include <unotools/tempfile.hxx>
26
28 static auto const flatpak = [] { return std::getenv("LIBO_FLATPAK") != nullptr; }();
29 return flatpak;
30}
31
32namespace {
33
34// Must only be accessed with SolarMutex locked:
35struct {
36 bool created = false;
37 OUString url;
38} temporaryHtmlDirectoryStatus;
39
40}
41
43 assert(url != nullptr);
45 if (!temporaryHtmlDirectoryStatus.created) {
46 // coverity[tainted_return_value] - we trust the contents of this variable
47 auto const env = std::getenv("XDG_CACHE_HOME");
48 if (env == nullptr) {
49 SAL_WARN("sfx.appl", "LIBO_FLATPAK mode but unset XDG_CACHE_HOME");
50 return false;
51 }
52 OUString path;
53 if (!rtl_convertStringToUString(
54 &path.pData, env, std::strlen(env), osl_getThreadTextEncoding(),
55 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
56 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
57 {
59 "sfx.appl",
60 "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << env << "\" encoding");
61 return false;
62 }
63 OUString parent;
64 auto const err = osl::FileBase::getFileURLFromSystemPath(path, parent);
65 if (err != osl::FileBase::E_None) {
67 "sfx.appl",
68 "LIBO_FLATPAK mode failure converting XDG_CACHE_HOME \"" << path << "\" to URL: "
69 << err);
70 return false;
71 }
72 if (!parent.endsWith("/")) {
73 parent += "/";
74 }
75 temporaryHtmlDirectoryStatus.url = utl::CreateTempURL(&parent, true);
76 if (temporaryHtmlDirectoryStatus.url.isEmpty()) {
78 "sfx.appl", "LIBO_FLATPAK mode failure creating temp dir at <" << parent << ">");
79 return false;
80 }
81 temporaryHtmlDirectoryStatus.created = true;
82 }
83 *url = &temporaryHtmlDirectoryStatus.url;
84 return true;
85}
86
89 if (temporaryHtmlDirectoryStatus.created) {
90 if (!utl::UCBContentHelper::Kill(temporaryHtmlDirectoryStatus.url)) {
92 "sfx.appl",
93 "LIBO_FLATPAK mode failure removing directory <"
94 << temporaryHtmlDirectoryStatus.url << ">");
95 }
96 }
97}
98
99/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
#define DBG_TESTSOLARMUTEX()
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
err
bool createTemporaryHtmlDirectory(OUString **url)
Definition: flatpak.cxx:42
bool isFlatpak()
Definition: flatpak.cxx:27
SFX2_DLLPUBLIC void removeTemporaryHtmlDirectory()
Definition: flatpak.cxx:87
UNOTOOLS_DLLPUBLIC bool Kill(OUString const &url)
OUString CreateTempURL(const OUString *pParent, bool bDirectory)