Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2026 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "audiopluginsloadguard.h"
23 :
24 : #include <functional>
25 : #include <sstream>
26 :
27 : #include "log.h"
28 :
29 : using namespace muse;
30 : using namespace muse::audioplugins;
31 :
32 : static const std::string SENTINEL_SUFFIX = ".loading";
33 :
34 1 : Ret AudioPluginsLoadGuard::beginLoad(const PluginResourceId& resourceId)
35 : {
36 1 : IF_ASSERT_FAILED(!resourceId.empty()) {
37 0 : return make_ret(Ret::Code::UnknownError);
38 : }
39 :
40 1 : Ret ret = fileSystem()->makePath(sentinelsDirPath());
41 1 : if (!ret) {
42 0 : LOGE() << "Failed to create plugin load guard dir: " << ret.toString();
43 0 : return ret;
44 : }
45 :
46 1 : ByteArray data(resourceId.data(), resourceId.size());
47 1 : ret = fileSystem()->writeFile(sentinelFilePath(resourceId), data);
48 1 : if (!ret) {
49 0 : LOGE() << "Failed to write plugin load sentinel for " << resourceId << ": " << ret.toString();
50 : }
51 :
52 1 : return ret;
53 1 : }
54 :
55 1 : void AudioPluginsLoadGuard::endLoad(const PluginResourceId& resourceId)
56 : {
57 1 : IF_ASSERT_FAILED(!resourceId.empty()) {
58 0 : return;
59 : }
60 :
61 1 : Ret ret = fileSystem()->remove(sentinelFilePath(resourceId));
62 1 : if (!ret) {
63 0 : LOGE() << "Failed to remove plugin load sentinel for " << resourceId << ": " << ret.toString();
64 : }
65 1 : }
66 :
67 2 : PluginResourceIdList AudioPluginsLoadGuard::danglingLoads() const
68 : {
69 2 : const io::path_t dir = sentinelsDirPath();
70 2 : if (!fileSystem()->exists(dir)) {
71 1 : return {};
72 : }
73 :
74 4 : const RetVal<io::paths_t> files = fileSystem()->scanFiles(dir, { "*" + SENTINEL_SUFFIX }, io::ScanMode::FilesInCurrentDir);
75 1 : if (!files.ret) {
76 0 : LOGE() << "Failed to scan plugin load guard dir: " << files.ret.toString();
77 0 : return {};
78 : }
79 :
80 1 : PluginResourceIdList result;
81 1 : result.reserve(files.val.size());
82 :
83 3 : for (const io::path_t& file : files.val) {
84 2 : RetVal<ByteArray> data = fileSystem()->readFile(file);
85 2 : if (!data.ret) {
86 0 : LOGE() << "Failed to read plugin load sentinel " << file.toStdString() << ": " << data.ret.toString();
87 0 : continue;
88 : }
89 :
90 : // the file name is a hash; the content holds the actual id
91 2 : std::string resourceId(reinterpret_cast<const char*>(data.val.constData()), data.val.size());
92 2 : if (!resourceId.empty()) {
93 2 : result.push_back(std::move(resourceId));
94 : }
95 2 : }
96 :
97 1 : return result;
98 3 : }
99 :
100 0 : Ret AudioPluginsLoadGuard::clearDanglingLoads()
101 : {
102 0 : const io::path_t dir = sentinelsDirPath();
103 0 : if (!fileSystem()->exists(dir)) {
104 0 : return make_ok();
105 : }
106 :
107 0 : return fileSystem()->clear(dir);
108 0 : }
109 :
110 5 : io::path_t AudioPluginsLoadGuard::sentinelsDirPath() const
111 : {
112 5 : return globalConfiguration()->userAppDataPath() + "/audio_plugins_loading";
113 : }
114 :
115 2 : io::path_t AudioPluginsLoadGuard::sentinelFilePath(const PluginResourceId& resourceId) const
116 : {
117 : // ids may contain characters that are not valid in file names; use a hash
118 : // for the name and keep the authoritative id in the file content
119 2 : std::ostringstream name;
120 2 : name << std::hex << std::hash<std::string> {}(resourceId);
121 6 : return sentinelsDirPath() + "/" + name.str() + SENTINEL_SUFFIX;
122 2 : }
|