GCC Code Coverage Report


Directory: ./
File: settings.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 346 390 88.7%
Functions: 34 35 97.1%
Branches: 369 680 54.3%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2025 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12 #include "fpp-json.h"
13
14 #include <atomic>
15 #include <cerrno>
16 #include <condition_variable>
17 #include <cstring>
18 #include <ctype.h>
19 #include <fcntl.h>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <pwd.h>
24 #include <shared_mutex>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string>
28 #include <sys/file.h>
29 #include <unistd.h>
30 #include <utility>
31 #include <vector>
32
33 #include "common.h"
34 #include "log.h"
35 #include "settings.h"
36
37 #ifdef PLATFORM_OSX
38 #include <mach-o/dyld.h>
39 #endif
40 #include <sys/fcntl.h>
41
42 // Built on first use and never destroyed. Both are reached from other static
43 // objects: AES67Manager's constructor asks for the media directory during
44 // static initialization, and ~PluginManager -> CommandManager::Cleanup() asks
45 // again when fppd leaves through exit(). File-scope strings could be used
46 // there before they were constructed, or after they were destroyed.
47 31743 static std::string& FPPBinDir() {
48
5/10
✓ Branch 0 taken 3322 times.
✓ Branch 1 taken 28421 times.
✓ Branch 2 taken 3322 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3322 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3322 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
31743 static std::string* dir = new std::string();
49 31743 return *dir;
50 }
51 1812055 static std::string& FPPMediaDir() {
52
5/10
✓ Branch 0 taken 3322 times.
✓ Branch 1 taken 1808733 times.
✓ Branch 2 taken 3322 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3322 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3322 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
1812055 static std::string* dir = new std::string();
53 1812055 return *dir;
54 }
55
56 31743 std::string getFPPDDir(const std::string& path) {
57 31743 std::string& FPP_BIN_DIR = FPPBinDir();
58
2/2
✓ Branch 0 taken 3322 times.
✓ Branch 1 taken 28421 times.
31743 if (FPP_BIN_DIR == "") {
59 char exePath[2048];
60 3322 exePath[0] = 0;
61 #ifdef PLATFORM_OSX
62 uint32_t len = sizeof(exePath);
63 _NSGetExecutablePath(exePath, &len);
64 #else
65 3322 ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath) - 1);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3322 times.
3322 if (len == -1) {
67 ✗ LogWarn(VB_GENERAL, "readlink /proc/self/exe failed: %s\n", strerror(errno));
68 ✗ exePath[0] = '\0';
69 } else {
70 3322 exePath[len] = '\0';
71 }
72 #endif
73
1/2
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
3322 FPP_BIN_DIR = exePath;
74
2/4
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3322 times.
✗ Branch 3 not taken.
3322 FPP_BIN_DIR = FPP_BIN_DIR.substr(0, FPP_BIN_DIR.rfind("/"));
75
2/4
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3322 times.
✗ Branch 3 not taken.
3322 FPP_BIN_DIR = FPP_BIN_DIR.substr(0, FPP_BIN_DIR.rfind("/"));
76 }
77 31743 return FPP_BIN_DIR + path;
78 }
79 1812055 std::string getFPPMediaDir(const std::string& path) {
80 1812055 std::string& FPP_MEDIA_DIR = FPPMediaDir();
81
2/2
✓ Branch 0 taken 3322 times.
✓ Branch 1 taken 1808733 times.
1812055 if (FPP_MEDIA_DIR == "") {
82
2/4
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3322 times.
✗ Branch 3 not taken.
6644 std::string filename = getFPPDDir("/www/media_root.txt");
83
1/2
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
3322 std::string fc = "";
84
3/4
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2722 times.
✓ Branch 3 taken 600 times.
3322 if (FileExists(filename)) {
85
1/2
✓ Branch 0 taken 2722 times.
✗ Branch 1 not taken.
2722 fc = GetFileContents(filename);
86
1/2
✓ Branch 0 taken 2722 times.
✗ Branch 1 not taken.
2722 FPP_MEDIA_DIR = fc;
87
1/2
✓ Branch 0 taken 2722 times.
✗ Branch 1 not taken.
2722 TrimWhiteSpace(FPP_MEDIA_DIR);
88 }
89
3/4
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 600 times.
✓ Branch 3 taken 2722 times.
3322 if (FPP_MEDIA_DIR == "") {
90 #ifdef PLATFORM_OSX
91 // getpwuid() is not thread-safe; use getpwuid_r(). Also guards
92 // against a NULL deref when neither $HOME nor the passwd entry exist.
93 const char* homedir = getenv("HOME");
94 struct passwd pwd;
95 struct passwd* pwres = nullptr;
96 char pbuf[16384];
97 if (homedir == NULL &&
98 getpwuid_r(getuid(), &pwd, pbuf, sizeof(pbuf), &pwres) == 0 && pwres) {
99 homedir = pwres->pw_dir;
100 }
101 if (homedir) {
102 FPP_MEDIA_DIR = homedir;
103 FPP_MEDIA_DIR += "/Documents/fpp";
104 }
105 #else
106
1/2
✓ Branch 0 taken 600 times.
✗ Branch 1 not taken.
600 FPP_MEDIA_DIR = "/home/fpp/media";
107 #endif
108 }
109
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 2716 times.
3322 if (FPP_MEDIA_DIR != fc) {
110
1/2
✓ Branch 0 taken 606 times.
✗ Branch 1 not taken.
606 PutFileContents(filename, FPP_MEDIA_DIR);
111 }
112 3322 }
113 1812055 return FPP_MEDIA_DIR + path;
114 }
115
116 class SettingListener {
117 public:
118 std::string id;
119 std::function<void(const std::string&)> callback;
120 };
121 class SettingsConfig {
122 public:
123 SettingsConfig();
124 ~SettingsConfig();
125
126 void Init();
127
128 // written by the settings-file reload, read unlocked by getFPPmode()
129 std::atomic<FPPMode> fppMode;
130
131 std::string getSetting(const std::string& setting, const std::string& defaultVal);
132 int getSettingInt(const std::string& setting, int defaultVal);
133
134 void setSetting(const std::string& key, const std::string& value, bool persist);
135
136 int LoadSettings(const std::string& base, bool logChanges = false);
137
138 void registerSettingsListener(const std::string& id, const std::string& setting, std::function<void(const std::string&)>&& cb);
139 void unregisterSettingsListener(const std::string& id, const std::string& setting);
140
141 private:
142 bool setSettingLocked(const std::string& key, const std::string& value, bool logInfo);
143 bool persistSetting(const std::string& key, const std::string& value);
144 void notifyListeners(const std::map<std::string, std::string>& changed);
145 void LoadSettingsInfo();
146
147 Json::Value settingsInfo;
148 Json::Value settings;
149 std::shared_mutex settingsMutex;
150 // Serializes setSetting(persist) so the file is written in the same order
151 // the in-memory values changed. Taken before settingsMutex, never after.
152 std::mutex persistMutex;
153
154 std::map<std::string, std::list<SettingListener>> settingsListeners;
155 std::shared_mutex settingsListenersMutex;
156 // Listener calls copied out by notifyListeners() and not yet returned,
157 // per (setting, id): unregisterSettingsListener() waits for its own.
158 // listenerCallsLock is a leaf lock; no callback runs under it.
159 std::map<std::pair<std::string, std::string>, int> listenerCallsInFlight;
160 std::mutex listenerCallsLock;
161 std::condition_variable listenerCallFinished;
162 void retireListenerCalls(const std::vector<std::pair<SettingListener, const std::string*>>& calls,
163 size_t from, size_t to);
164 };
165 // Nonzero while this thread is inside a settings listener: a listener that
166 // unregisters (itself or another) must not wait on a call that may be its
167 // own, or one waiting on it.
168 static thread_local int t_inSettingsListener = 0;
169 static SettingsConfig settings;
170
171
1/2
✓ Branch 0 taken 3322 times.
✗ Branch 1 not taken.
3322 SettingsConfig::SettingsConfig() {
172 3322 }
173
174 670 SettingsConfig::~SettingsConfig() {
175 670 }
176
177 2995 void SettingsConfig::Init() {
178
2/2
✓ Branch 0 taken 2716 times.
✓ Branch 1 taken 279 times.
2995 if (settingsInfo.empty()) {
179 2716 LoadSettingsInfo();
180 2716 fppMode = PLAYER_MODE;
181
182 // load UUID File
183
3/6
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2716 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2716 times.
✗ Branch 5 not taken.
5432 if (access(getFPPDDir("/scripts/get_uuid").c_str(), F_OK) == 0) {
184 FILE* uuid_fp;
185 char temp_uuid[500];
186
3/6
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2716 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2716 times.
✗ Branch 5 not taken.
2716 uuid_fp = popen(getFPPDDir("/scripts/get_uuid").c_str(), "r");
187
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2715 times.
2716 if (uuid_fp == NULL) {
188
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_SETTING, "Couldn't execute get_uuid\n");
189 } else {
190
3/4
✓ Branch 0 taken 2715 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2714 times.
✓ Branch 3 taken 1 times.
2715 if (fgets(temp_uuid, sizeof(temp_uuid), uuid_fp) != NULL) {
191 2714 int pos = strlen(temp_uuid) - 1;
192 // Strip newline
193
2/4
✓ Branch 0 taken 2714 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2714 times.
✗ Branch 3 not taken.
2714 if (pos >= 0 && '\n' == temp_uuid[pos]) {
194 2714 temp_uuid[pos] = '\0';
195 }
196
3/6
✓ Branch 0 taken 2714 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2714 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2714 times.
✗ Branch 5 not taken.
5428 settings["SystemUUID"] = std::string(temp_uuid);
197 }
198
1/2
✓ Branch 0 taken 2715 times.
✗ Branch 1 not taken.
2715 pclose(uuid_fp);
199 }
200 }
201
202 // Set to unknown if not found
203
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2714 times.
2716 if (!settings.isMember("SystemUUID")) {
204
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 settings["SystemUUID"] = "Unknown";
205 2 LogWarn(VB_SETTING, "Couldn't find UUID\n");
206 }
207 }
208 2995 }
209 60989 void SettingsConfig::registerSettingsListener(const std::string& id, const std::string& setting, std::function<void(const std::string&)>&& cb) {
210
1/2
✓ Branch 0 taken 60989 times.
✗ Branch 1 not taken.
60989 std::unique_lock<std::shared_mutex> lock(settingsListenersMutex);
211 60989 SettingListener listener;
212
1/2
✓ Branch 0 taken 60989 times.
✗ Branch 1 not taken.
60989 listener.id = id;
213 60989 listener.callback = std::move(cb);
214
215
3/4
✓ Branch 0 taken 60989 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60968 times.
✓ Branch 3 taken 21 times.
60989 if (settingsListeners.find(setting) == settingsListeners.end()) {
216
1/2
✓ Branch 0 taken 60968 times.
✗ Branch 1 not taken.
60968 settingsListeners[setting] = std::list<SettingListener>();
217 }
218
2/4
✓ Branch 0 taken 60989 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60989 times.
✗ Branch 3 not taken.
60989 settingsListeners[setting].push_back(listener);
219
1/2
✓ Branch 0 taken 60989 times.
✗ Branch 1 not taken.
60989 LogDebug(VB_SETTING, "Registered settings listener for %s setting with id %s\n", setting.c_str(), id.c_str());
220 60989 }
221 44745 void SettingsConfig::unregisterSettingsListener(const std::string& id, const std::string& setting) {
222 {
223
1/2
✓ Branch 0 taken 44745 times.
✗ Branch 1 not taken.
44745 std::unique_lock<std::shared_mutex> lock(settingsListenersMutex);
224
3/4
✓ Branch 0 taken 44745 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39343 times.
✓ Branch 3 taken 5402 times.
44745 if (settingsListeners.find(setting) != settingsListeners.end()) {
225
1/2
✓ Branch 0 taken 39343 times.
✗ Branch 1 not taken.
39343 auto& listeners = settingsListeners[setting];
226
2/2
✓ Branch 0 taken 39364 times.
✓ Branch 1 taken 39343 times.
78707 for (auto it = listeners.begin(); it != listeners.end();) {
227
2/2
✓ Branch 0 taken 39322 times.
✓ Branch 1 taken 42 times.
39364 if (it->id == id) {
228 39322 it = listeners.erase(it);
229 } else {
230 42 ++it;
231 }
232 }
233
2/2
✓ Branch 0 taken 39301 times.
✓ Branch 1 taken 42 times.
39343 if (listeners.empty()) {
234
1/2
✓ Branch 0 taken 39301 times.
✗ Branch 1 not taken.
39301 settingsListeners.erase(setting);
235 }
236 }
237 44745 }
238 // Listeners are called from a copy with no lock held, so one copied out
239 // before the erase above can still be running, or about to run. Its
240 // owner frees what it captured once this returns (an output deleted by a
241 // reload), so wait for those calls to finish. Not under
242 // settingsListenersMutex: a running listener may register or unregister.
243
1/2
✓ Branch 0 taken 44745 times.
✗ Branch 1 not taken.
44745 if (t_inSettingsListener == 0) {
244
1/2
✓ Branch 0 taken 44745 times.
✗ Branch 1 not taken.
44745 std::unique_lock<std::mutex> lock(listenerCallsLock);
245
1/2
✓ Branch 0 taken 44745 times.
✗ Branch 1 not taken.
44745 auto key = std::make_pair(setting, id);
246
1/2
✓ Branch 0 taken 44745 times.
✗ Branch 1 not taken.
89491 listenerCallFinished.wait(lock, [this, &key]() { return listenerCallsInFlight.count(key) == 0; });
247 44745 }
248 44745 }
249 77 void SettingsConfig::retireListenerCalls(const std::vector<std::pair<SettingListener, const std::string*>>& calls,
250 size_t from, size_t to) {
251
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 40 times.
77 if (from >= to) {
252 37 return;
253 }
254 {
255
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 std::lock_guard<std::mutex> lock(listenerCallsLock);
256
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 40 times.
80 for (size_t i = from; i < to; i++) {
257
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 auto f = listenerCallsInFlight.find(std::make_pair(*calls[i].second, calls[i].first.id));
258
3/6
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
40 if (f != listenerCallsInFlight.end() && --f->second <= 0) {
259
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 listenerCallsInFlight.erase(f);
260 }
261 }
262 40 }
263 40 listenerCallFinished.notify_all();
264 }
265
266 2716 void SettingsConfig::LoadSettingsInfo() {
267
2/4
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2716 times.
✗ Branch 3 not taken.
2716 std::string settingsJson = getFPPDDir("/www/settings.json");
268
2/4
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2716 times.
✗ Branch 3 not taken.
2716 if (FileExists(settingsJson)) {
269
1/2
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
2716 settingsInfo = LoadJsonFromFile(settingsJson, JsonRoot::Object);
270
271
2/4
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2716 times.
✗ Branch 3 not taken.
2716 Json::Value s = settingsInfo["settings"];
272
1/2
✓ Branch 0 taken 2716 times.
✗ Branch 1 not taken.
2716 Json::Value::Members memberNames = s.getMemberNames();
273
274
2/2
✓ Branch 0 taken 703444 times.
✓ Branch 1 taken 2716 times.
706160 for (int i = 0; i < memberNames.size(); i++) {
275
3/6
✓ Branch 0 taken 703444 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 703444 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 703444 times.
✗ Branch 5 not taken.
703444 std::string type = s[memberNames[i]]["type"].asString();
276
1/2
✓ Branch 0 taken 703444 times.
✗ Branch 1 not taken.
703444 std::string def = "";
277
278
4/6
✓ Branch 0 taken 703444 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 703444 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 366660 times.
✓ Branch 5 taken 336784 times.
703444 if (s[memberNames[i]].isMember("default")) {
279
3/6
✓ Branch 0 taken 366660 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 366660 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 366660 times.
✗ Branch 5 not taken.
366660 def = s[memberNames[i]]["default"].asString();
280 } else {
281
5/6
✓ Branch 0 taken 336784 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 268884 times.
✓ Branch 3 taken 67900 times.
✓ Branch 4 taken 70616 times.
✓ Branch 5 taken 266168 times.
605668 if ((type == "checkbox") ||
282
3/4
✓ Branch 0 taken 268884 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2716 times.
✓ Branch 3 taken 266168 times.
268884 (type == "number")) {
283
1/2
✓ Branch 0 taken 70616 times.
✗ Branch 1 not taken.
141232 def = std::string("0");
284 } else {
285
1/2
✓ Branch 0 taken 266168 times.
✗ Branch 1 not taken.
532336 def = std::string("");
286 }
287 }
288
289
2/4
✓ Branch 0 taken 703444 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 703444 times.
✗ Branch 3 not taken.
703444 settings[memberNames[i]] = def;
290 // The following will never be logged due to startup order
291
1/2
✓ Branch 0 taken 703444 times.
✗ Branch 1 not taken.
703444 LogExcess(VB_SETTING, "Setting default for '%s' setting to '%s'\n",
292 memberNames[i].c_str(), def.c_str());
293 703444 }
294 2716 }
295 #ifdef PLATFORM_OSX
296 char hostname[256];
297 gethostname(hostname, 256);
298 std::string hn = hostname;
299 int idx = hn.find(".");
300 if (idx != std::string::npos) {
301 hn = hn.substr(0, idx);
302 }
303 settings["HostName"] = hn;
304 #endif
305 2716 }
306 26313 void SettingsConfig::setSetting(const std::string& key, const std::string& value, bool persist) {
307 26313 std::unique_lock<std::mutex> persistLock(persistMutex, std::defer_lock);
308
2/2
✓ Branch 0 taken 944 times.
✓ Branch 1 taken 25369 times.
26313 if (persist) {
309 // The file is line based and nothing escapes a value, so a line break
310 // would start a new setting. Same rule as WriteSettingToFile().
311
2/4
✓ Branch 0 taken 944 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 944 times.
1888 if (value.find_first_of(std::string("\r\n\0", 3)) != std::string::npos) {
312 ✗ LogWarn(VB_SETTING, "Not saving setting %s: value contains a line break or NUL\n", key.c_str());
313 ✗ return;
314 }
315
1/2
✓ Branch 0 taken 944 times.
✗ Branch 1 not taken.
944 persistLock.lock();
316 }
317
1/2
✓ Branch 0 taken 26313 times.
✗ Branch 1 not taken.
26313 std::unique_lock<std::shared_mutex> lock(settingsMutex);
318
3/4
✓ Branch 0 taken 26313 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6268 times.
✓ Branch 3 taken 20045 times.
26313 if (setSettingLocked(key, value, false)) {
319
1/2
✓ Branch 0 taken 6268 times.
✗ Branch 1 not taken.
6268 lock.unlock();
320
2/2
✓ Branch 0 taken 899 times.
✓ Branch 1 taken 5369 times.
6268 if (persist) {
321
1/2
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
899 persistSetting(key, value);
322
1/2
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
899 persistLock.unlock();
323 }
324
4/8
✓ Branch 0 taken 6268 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6268 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6268 times.
✓ Branch 5 taken 6268 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
18804 notifyListeners({ { key, value } });
325 }
326
2/8
✓ Branch 0 taken 6268 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26313 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
32581 }
327 // Rewrites one key in the settings file under a single LOCK_EX held across
328 // the read and the write, so a concurrent writer (the PHP side holds the
329 // same lock) can't slip in between and have its change lost. Called without
330 // settingsMutex: LoadSettings() takes settingsMutex and then the file lock.
331 899 bool SettingsConfig::persistSetting(const std::string& key, const std::string& value) {
332
3/6
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 899 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 899 times.
✗ Branch 5 not taken.
899 int fd = open(FPP_FILE_SETTINGS.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0666);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
899 if (fd == -1) {
334 ✗ LogWarn(VB_SETTING, "Could not open %s to save %s: %s\n", FPP_FILE_SETTINGS.c_str(), key.c_str(), strerror(errno));
335 ✗ return false;
336 }
337
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 899 times.
899 while (flock(fd, LOCK_EX) == -1 && errno == EINTR) {
338 }
339 899 std::string contents;
340 char buf[4096];
341 ssize_t n;
342
3/4
✓ Branch 0 taken 1798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 899 times.
✓ Branch 3 taken 899 times.
1798 while ((n = read(fd, buf, sizeof(buf))) != 0) {
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
899 if (n < 0) {
344 ✗ if (errno == EINTR) {
345 ✗ continue;
346 }
347 ✗ LogWarn(VB_SETTING, "Could not read %s to save %s: %s\n", FPP_FILE_SETTINGS.c_str(), key.c_str(), strerror(errno));
348 ✗ flock(fd, LOCK_UN);
349 ✗ close(fd);
350 ✗ return false;
351 }
352
1/2
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
899 contents.append(buf, n);
353 }
354
355 // Match the key the way LoadSettings() reads it (the text before the
356 // first '=', trimmed) rather than searching for "key = " anywhere, which
357 // also hit a key that ends with this one or a value containing that text.
358 // The first line for the key is replaced and any later duplicates are
359 // dropped, since the last one would win on the next load.
360
3/6
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 899 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 899 times.
✗ Branch 5 not taken.
899 const std::string newLine = key + " = \"" + value + "\"\n";
361 899 std::string out;
362
1/2
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
899 out.reserve(contents.size() + newLine.size());
363 899 bool found = false;
364 899 size_t pos = 0;
365
2/2
✓ Branch 0 taken 14178 times.
✓ Branch 1 taken 899 times.
15077 while (pos < contents.size()) {
366 14178 size_t end = contents.find('\n', pos);
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14178 times.
14178 end = (end == std::string::npos) ? contents.size() : end + 1;
368
1/2
✓ Branch 0 taken 14178 times.
✗ Branch 1 not taken.
14178 std::string line = contents.substr(pos, end - pos);
369 14178 pos = end;
370 14178 size_t eq = line.find('=');
371
1/2
✓ Branch 0 taken 14178 times.
✗ Branch 1 not taken.
14178 if (eq != std::string::npos) {
372
2/4
✓ Branch 0 taken 14178 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14178 times.
✗ Branch 3 not taken.
14178 char* k = trimwhitespace(line.substr(0, eq).c_str());
373
1/2
✓ Branch 0 taken 14178 times.
✗ Branch 1 not taken.
14178 bool match = (key == k);
374 14178 free(k);
375
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 13407 times.
14178 if (match) {
376
1/2
✓ Branch 0 taken 771 times.
✗ Branch 1 not taken.
771 if (!found) {
377
1/2
✓ Branch 0 taken 771 times.
✗ Branch 1 not taken.
771 out += newLine;
378 771 found = true;
379 }
380 771 continue;
381 }
382 }
383
1/2
✓ Branch 0 taken 13407 times.
✗ Branch 1 not taken.
13407 out += line;
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13407 times.
13407 if (out.back() != '\n') {
385 ✗ out += '\n';
386 }
387
2/2
✓ Branch 0 taken 13407 times.
✓ Branch 1 taken 771 times.
14178 }
388
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 771 times.
899 if (!found) {
389
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 out += newLine;
390 }
391
392 899 bool ok = ftruncate(fd, 0) == 0;
393 899 const char* p = out.data();
394 899 size_t left = out.size();
395 899 off_t off = 0;
396
3/4
✓ Branch 0 taken 1798 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 899 times.
✓ Branch 3 taken 899 times.
1798 while (ok && left > 0) {
397
1/2
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
899 ssize_t w = pwrite(fd, p, left, off);
398
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
899 if (w < 0 && errno == EINTR) {
399 ✗ continue;
400 }
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
899 if (w <= 0) {
402 ✗ ok = false;
403 ✗ break;
404 }
405 899 p += w;
406 899 left -= w;
407 899 off += w;
408 }
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 899 times.
899 if (!ok) {
410 ✗ LogWarn(VB_SETTING, "Could not write %s to save %s: %s\n", FPP_FILE_SETTINGS.c_str(), key.c_str(), strerror(errno));
411 }
412 899 flock(fd, LOCK_UN);
413
1/2
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
899 close(fd);
414
3/6
✓ Branch 0 taken 899 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 899 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 899 times.
✗ Branch 5 not taken.
899 SetFilePerms(FPP_FILE_SETTINGS);
415 899 return ok;
416 899 }
417 // Listeners run with no settings lock held. They take their own locks and
418 // read settings back (MultiSync holds its lock and calls getSetting, which
419 // takes settingsMutex), and settingsMutex is held while settingsListenersMutex
420 // is taken, so calling them under either lock closes a cycle. It also lets a
421 // listener register or unregister listeners. Each call is counted until it
422 // returns, for unregisterSettingsListener() to wait on.
423 9235 void SettingsConfig::notifyListeners(const std::map<std::string, std::string>& changed) {
424 9235 std::vector<std::pair<SettingListener, const std::string*>> toCall;
425 {
426
1/2
✓ Branch 0 taken 9235 times.
✗ Branch 1 not taken.
9235 std::shared_lock<std::shared_mutex> listenersLock(settingsListenersMutex);
427
1/2
✓ Branch 0 taken 9235 times.
✗ Branch 1 not taken.
9235 std::lock_guard<std::mutex> callsLock(listenerCallsLock);
428
2/2
✓ Branch 0 taken 11778 times.
✓ Branch 1 taken 9235 times.
21013 for (const auto& change : changed) {
429
1/2
✓ Branch 0 taken 11778 times.
✗ Branch 1 not taken.
11778 auto it = settingsListeners.find(change.first);
430
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 11739 times.
11778 if (it != settingsListeners.end()) {
431
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 39 times.
79 for (const auto& listener : it->second) {
432
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 toCall.emplace_back(listener, &change.first);
433
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 ++listenerCallsInFlight[std::make_pair(change.first, listener.id)];
434 }
435 }
436 }
437 9235 }
438
2/2
✓ Branch 0 taken 9198 times.
✓ Branch 1 taken 37 times.
9235 if (toCall.empty()) {
439 9198 return;
440 }
441 // Each call is retired as it returns; a throw retires the rest.
442 37 size_t next = 0;
443 struct InFlight {
444 SettingsConfig* self;
445 const std::vector<std::pair<SettingListener, const std::string*>>& calls;
446 const size_t& next;
447 37 ~InFlight() {
448 37 --t_inSettingsListener;
449 37 self->retireListenerCalls(calls, next, calls.size());
450 37 }
451 };
452 37 ++t_inSettingsListener;
453 37 InFlight inFlight{ this, toCall, next };
454
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 37 times.
77 while (next < toCall.size()) {
455 40 const auto& [listener, key] = toCall[next];
456
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 const std::string& value = changed.at(*key);
457
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 LogDebug(VB_SETTING, "Notifying listener %s for setting %s with value '%s'\n",
458 listener.id.c_str(), key->c_str(), value.c_str());
459
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 listener.callback(value);
460
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 retireListenerCalls(toCall, next, next + 1);
461 40 ++next;
462 }
463
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 9198 times.
9235 }
464 35864 bool SettingsConfig::setSettingLocked(const std::string& key, const std::string& value, bool logInfo) {
465
2/4
✓ Branch 0 taken 35864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35864 times.
✗ Branch 3 not taken.
35864 std::string val = settings[key].asString();
466
2/2
✓ Branch 0 taken 11856 times.
✓ Branch 1 taken 24008 times.
35864 if (val != value) {
467
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 11766 times.
11856 if (logInfo) {
468
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 std::shared_lock<std::shared_mutex> listenersLock(settingsListenersMutex);
469
3/4
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75 times.
✓ Branch 3 taken 15 times.
90 if (settingsListeners.find(key) != settingsListeners.end()) {
470
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 LogInfo(VB_SETTING, "Setting %s changed from %s to %s\n", key.c_str(), val.c_str(), value.c_str());
471 } else {
472
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 LogDebug(VB_SETTING, "Setting %s: from %s to %s\n", key.c_str(), val.c_str(), value.c_str());
473 }
474 90 } else {
475
1/2
✓ Branch 0 taken 11766 times.
✗ Branch 1 not taken.
11766 LogDebug(VB_SETTING, "Setting %s: from %s to %s\n", key.c_str(), val.c_str(), value.c_str());
476 }
477
2/4
✓ Branch 0 taken 11856 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11856 times.
✗ Branch 3 not taken.
11856 settings[key] = value;
478
3/4
✓ Branch 0 taken 11856 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 11788 times.
11856 if (key == "fppMode") {
479
6/8
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 62 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 62 times.
68 if (value == "player" || value.empty()) {
480 6 fppMode = PLAYER_MODE;
481
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 } else if (value == "bridge") {
482 // legacy, remap to PLAYER
483 ✗ fppMode = PLAYER_MODE;
484
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 } else if (value == "master") {
485 // legacy, remap to player, but turn on MultiSync setting
486 ✗ fppMode = PLAYER_MODE;
487 ✗ settings[key] = "player";
488 ✗ settings["MultiSyncEnabled"] = "1";
489
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
✗ Branch 3 not taken.
62 } else if (value == "remote") {
490 62 fppMode = REMOTE_MODE;
491 } else {
492 // default to playmode if string is invalid
493 ✗ fppMode = PLAYER_MODE;
494 ✗ settings[key] = "player";
495 }
496
4/6
✓ Branch 0 taken 11788 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11788 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1051 times.
✓ Branch 5 taken 10737 times.
23576 } else if (startsWith(key, "LogLevel_")) {
497 // Starts with LogLevel_
498
2/4
✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1051 times.
✗ Branch 3 not taken.
1051 if (value != "") {
499
1/2
✓ Branch 0 taken 1051 times.
✗ Branch 1 not taken.
1051 FPPLogger::INSTANCE.SetLevel(key.c_str(), value.c_str());
500 } else {
501 ✗ FPPLogger::INSTANCE.SetLevel(key.c_str(), "warn");
502 }
503 }
504 11856 return true;
505 }
506 24008 return false;
507 35864 }
508 1492961 std::string SettingsConfig::getSetting(const std::string& setting, const std::string& defaultVal) {
509
1/2
✓ Branch 0 taken 1492961 times.
✗ Branch 1 not taken.
1492961 std::shared_lock<std::shared_mutex> lock(settingsMutex);
510
1/2
✓ Branch 0 taken 1492961 times.
✗ Branch 1 not taken.
1492961 std::string result = defaultVal;
511
3/4
✓ Branch 0 taken 1492961 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1469996 times.
✓ Branch 3 taken 22965 times.
1492961 if (settings.isMember(setting)) {
512
2/4
✓ Branch 0 taken 1469996 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1469996 times.
✗ Branch 3 not taken.
1469996 result = settings[setting].asString();
513 }
514
1/2
✓ Branch 0 taken 1492961 times.
✗ Branch 1 not taken.
1492961 LogExcess(VB_SETTING, "getSetting(%s) returning '%s'\n", setting.c_str(), result.c_str());
515 1492961 return result;
516 1492961 }
517 384623 int SettingsConfig::getSettingInt(const std::string& setting, int defaultVal) {
518
1/2
✓ Branch 0 taken 384623 times.
✗ Branch 1 not taken.
384623 std::shared_lock<std::shared_mutex> lock(settingsMutex);
519 384623 int result = defaultVal;
520
3/4
✓ Branch 0 taken 384623 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 346265 times.
✓ Branch 3 taken 38358 times.
384623 if (settings.isMember(setting)) {
521
2/4
✓ Branch 0 taken 346265 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 346265 times.
✗ Branch 3 not taken.
346265 result = atoi(settings[setting].asString().c_str());
522 }
523
1/2
✓ Branch 0 taken 384623 times.
✗ Branch 1 not taken.
384623 LogExcess(VB_SETTING, "getSettingInt(%s) returning %d\n", setting.c_str(), result);
524 384623 return result;
525 384623 }
526
527 // Returns a string that's the white-space trimmed version
528 // of the input string. Also trim double quotes now that the
529 // settings file will have double quotes in it.
530 23738 char* trimwhitespace(const char* str, int quotesAlso) {
531 const char* end;
532 size_t out_size;
533 char* out;
534
535 // Trim leading space
536
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 23738 times.
✓ Branch 2 taken 23738 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 23738 times.
23738 while ((isspace(*str)) || (quotesAlso && (*str == '"')))
537 ✗ str++;
538
539
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23737 times.
23738 if (*str == 0) // All spaces?
540 1 return strdup("");
541
542 // Trim trailing space
543 23737 end = str + strlen(str) - 1;
544
5/8
✓ Branch 0 taken 47474 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23737 times.
✓ Branch 3 taken 23737 times.
✓ Branch 4 taken 23737 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 23737 times.
47474 while (end > str && ((isspace(*end)) || (quotesAlso && (*end == '"'))))
545 23737 end--;
546 23737 end++;
547
548 // Set output size to minimum of trimmed string length
549 23737 out_size = end - str;
550
551 // All whitespace
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23737 times.
23737 if (out_size == 0)
553 ✗ return strdup("");
554
555 23737 out = (char*)malloc(out_size + 1);
556 // If we didn't allocate anything, die!
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23737 times.
23737 if (!out) {
558 ✗ fprintf(stderr, "Failed to allocate memory for string trimming\n");
559 ✗ exit(EXIT_FAILURE);
560 }
561
562 // Copy trimmed string and add null terminator
563 23737 memcpy(out, str, out_size);
564 23737 out[out_size] = 0;
565
566 23737 return out;
567 }
568
569 // Caller must free the char array we give them
570 196991 char* modeToString(int mode) {
571
2/2
✓ Branch 0 taken 196550 times.
✓ Branch 1 taken 441 times.
196991 if (mode == PLAYER_MODE)
572 196550 return strdup("player");
573
2/2
✓ Branch 0 taken 259 times.
✓ Branch 1 taken 182 times.
441 else if (mode == REMOTE_MODE)
574 259 return strdup("remote");
575
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 30 times.
182 else if (mode == BRIDGE_MODE)
576 152 return strdup("bridge");
577
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 else if (mode == MASTER_MODE)
578 ✗ return strdup("master");
579
580 30 return strdup("unknown");
581 }
582
583 2995 int SettingsConfig::LoadSettings(const std::string& base, bool logChanges) {
584 // A reload between setSetting(persist)'s memory update and its file write
585 // would read the old value back and re-apply it.
586
1/2
✓ Branch 0 taken 2995 times.
✗ Branch 1 not taken.
2995 std::unique_lock<std::mutex> persistLock(persistMutex);
587
1/2
✓ Branch 0 taken 2995 times.
✗ Branch 1 not taken.
2995 std::unique_lock<std::shared_mutex> lock(settingsMutex);
588
1/2
✓ Branch 0 taken 2995 times.
✗ Branch 1 not taken.
2995 Init();
589
590
5/8
✓ Branch 0 taken 2995 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2995 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2995 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✓ Branch 7 taken 2967 times.
5990 if (!FileExists(FPP_FILE_SETTINGS)) {
591
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 LogWarn(VB_SETTING,
592 "Attempted to load settings file %s which does not exist!\n", FPP_FILE_SETTINGS.c_str());
593 28 return -1;
594 }
595
596
3/6
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2967 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2967 times.
✗ Branch 5 not taken.
2967 FILE* file = fopen(FPP_FILE_SETTINGS.c_str(), "r");
597 2967 std::map<std::string, std::string> changed;
598
1/2
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
2967 if (file != NULL) {
599 2967 flock(fileno(file), LOCK_EX); // Lock the file for exclusive access
600 2967 char* line = (char*)calloc(256, 1);
601 2967 size_t len = 256;
602 ssize_t read;
603 2967 int sIndex = 0;
604
605
3/4
✓ Branch 0 taken 12519 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9552 times.
✓ Branch 3 taken 2967 times.
12519 while ((read = getline(&line, &len, file)) != -1) {
606
3/6
✓ Branch 0 taken 9552 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9552 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 9552 times.
9552 if ((!line) || (!read) || (read == 1))
607 1 continue;
608
609 // Split on the first '=' only and drop exactly one pair of
610 // surrounding quotes, the way the PHP side and getRawSetting()
611 // read this file. Splitting on every '=' cut base64 passwords
612 // and JSON values short, and trimming every quote ate quotes
613 // that belong to the value.
614
1/2
✓ Branch 0 taken 9552 times.
✗ Branch 1 not taken.
10140 std::string l(line, read);
615 9552 size_t eq = l.find('=');
616
2/4
✓ Branch 0 taken 9552 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9552 times.
✗ Branch 3 not taken.
9552 char* key = trimwhitespace(l.substr(0, eq).c_str());
617
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9551 times.
9552 if (!strlen(key)) {
618 1 free(key);
619 1 continue;
620 }
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9551 times.
9551 if (eq == std::string::npos) {
622 ✗ fprintf(stderr, "Error tokenizing value for %s setting\n", key);
623 ✗ free(key);
624 ✗ continue;
625 }
626
1/2
✓ Branch 0 taken 9551 times.
✗ Branch 1 not taken.
9551 std::string value = l.substr(eq + 1);
627
1/2
✓ Branch 0 taken 9551 times.
✗ Branch 1 not taken.
9551 TrimWhiteSpace(value);
628
6/8
✓ Branch 0 taken 9551 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9550 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 9550 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9550 times.
✓ Branch 7 taken 1 times.
9551 if (value.size() >= 2 && value.front() == '"' && value.back() == '"') {
629
1/2
✓ Branch 0 taken 9550 times.
✗ Branch 1 not taken.
9550 value = value.substr(1, value.size() - 2);
630 }
631
4/6
✓ Branch 0 taken 9551 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9551 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5588 times.
✓ Branch 5 taken 3963 times.
28065 if (setSettingLocked(key, value, logChanges)) {
632
3/6
✓ Branch 0 taken 5588 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5588 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5588 times.
✗ Branch 5 not taken.
11176 changed[key] = value; // Store the changed setting
633 }
634 9551 free(key);
635
2/2
✓ Branch 0 taken 9551 times.
✓ Branch 1 taken 1 times.
9552 }
636
637
1/2
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
2967 if (line) {
638 2967 free(line);
639 }
640 2967 flock(fileno(file), LOCK_UN); // Lock the file for exclusive access
641
1/2
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
2967 fclose(file);
642 } else {
643 ✗ LogWarn(VB_SETTING, "Warning: couldn't open settings file: '%s'!\n", FPP_FILE_SETTINGS.c_str());
644 ✗ return -1;
645 }
646
1/2
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
2967 lock.unlock();
647 // Before the listeners: one that calls setSetting(persist) takes it.
648
1/2
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
2967 persistLock.unlock();
649
1/2
✓ Branch 0 taken 2967 times.
✗ Branch 1 not taken.
2967 notifyListeners(changed);
650 2967 return 0;
651 2995 }
652
653 2995 int LoadSettings(const char* base, bool logChanges) {
654
2/4
✓ Branch 0 taken 2995 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2995 times.
✗ Branch 3 not taken.
8655 return settings.LoadSettings(base, logChanges);
655 }
656
657 2690 int setSetting(const std::string& key, const int value) {
658
1/2
✓ Branch 0 taken 2690 times.
✗ Branch 1 not taken.
2690 return setSetting(key, std::to_string(value));
659 }
660 26313 int setSetting(const std::string& key, const std::string& value, bool persist) {
661 26313 settings.setSetting(key, value, persist);
662 26313 return 1;
663 }
664 2690 int SetSetting(const std::string& key, const int value) {
665 2690 return setSetting(key, value);
666 }
667 22679 int SetSetting(const std::string& key, const std::string& value) {
668 22679 return setSetting(key, value);
669 }
670 1465789 std::string getSetting(const char* setting, const char* defaultVal) {
671
2/4
✓ Branch 0 taken 1465789 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1465789 times.
1465789 if ((!setting) || (setting[0] == 0x00)) {
672 ✗ LogErr(VB_SETTING, "getSetting() called with NULL or empty value\n");
673 ✗ std::string result = defaultVal;
674 ✗ return result;
675 ✗ }
676
3/6
✓ Branch 0 taken 1465789 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1465789 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1465789 times.
✗ Branch 5 not taken.
5863156 return settings.getSetting(setting, defaultVal);
677 }
678
679 384623 int getSettingInt(const char* setting, int defaultVal) {
680
2/4
✓ Branch 0 taken 384623 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 384623 times.
384623 if ((!setting) || (setting[0] == 0x00)) {
681 ✗ LogErr(VB_SETTING, "getSetting() called with NULL or empty value\n");
682 ✗ return defaultVal;
683 }
684
2/4
✓ Branch 0 taken 384623 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 384623 times.
✗ Branch 3 not taken.
769246 return settings.getSettingInt(setting, defaultVal);
685 }
686
687 27172 bool isPipeWireBackend() {
688 #ifdef PLATFORM_OSX
689 // There is no PipeWire on macOS, whatever MediaBackend says (its default is
690 // a PipeWire mode). Media plays through GStreamer's non-PipeWire path there -
691 // autoaudiosink, i.e. osxaudiosink - so every PipeWire-only feature has to
692 // see "no" from here rather than build pipewiresink/pipewiresrc pipelines
693 // that cannot be created.
694 return false;
695 #endif
696 // Default to the settings.json default rather than "". ALSA is retired --
697 // the UI offers only the two PipeWire modes and FPPINIT migrates a stored
698 // "alsa" away -- so a box that simply has no MediaBackend key must not fall
699 // through to the retired path.
700
3/6
✓ Branch 0 taken 27172 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27172 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27172 times.
✗ Branch 5 not taken.
81516 std::string mb = settings.getSetting("MediaBackend", "pipewire-simple");
701 // Lowercase compare without pulling in toLowerCopy here
702
2/2
✓ Branch 0 taken 402684 times.
✓ Branch 1 taken 27172 times.
429856 for (char& c : mb) {
703
3/4
✓ Branch 0 taken 376180 times.
✓ Branch 1 taken 26504 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 376180 times.
402684 if (c >= 'A' && c <= 'Z')
704 ✗ c = c + ('a' - 'A');
705 }
706
6/8
✓ Branch 0 taken 27172 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26559 times.
✓ Branch 3 taken 613 times.
✓ Branch 4 taken 26559 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 26504 times.
✓ Branch 7 taken 55 times.
54344 return (mb == "pipewire" || mb == "pipewire-simple");
707 27172 }
708
709 2143428 FPPMode getFPPmode(void) {
710 2143428 return settings.fppMode;
711 }
712
713 37 const std::string getFPPmodeStr(FPPMode mode) {
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if (mode == UNKNOWN_MODE)
715 ✗ mode = settings.fppMode;
716
717 37 std::string result;
718
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 char* modePtr = modeToString((int)mode);
719
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 if (modePtr) {
720
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 result = modePtr;
721 37 free(modePtr);
722 } else {
723 ✗ result = "unknown";
724 }
725
726 37 return result;
727 ✗ }
728
729 60989 void registerSettingsListener(const std::string& id, const std::string& key, std::function<void(const std::string&)>&& cb) {
730 60989 settings.registerSettingsListener(id, key, std::move(cb));
731 60989 }
732 44745 void unregisterSettingsListener(const std::string& id, const std::string& key) {
733 44745 settings.unregisterSettingsListener(id, key);
734 44745 }
735