Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

Launcher: .desktop file names with spaces or other characters get a sanitized id (listed and launched from their real file; app scope names stay valid); app list cache compares folder mtimes in ns; apps-actions-test 27/27

Federico Pereira3e736897d8d3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 20bf537..839822f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -120,6 +120,11 @@ of the matching [GitHub release](https://github.com/lordbasex/LordOS/releases),
### Fixed
+- **Apps whose launcher file has a space in its name didn't appear in Apps.** A package may name its
+ `.desktop` file freely (the LordOS Imager's is `LordOS Imager.desktop`), but the launcher skipped any
+ name with a space or another unusual character. Such a name now gets an id with those characters
+ turned into `-`, and the app is listed and launched from its real file. Also, an app installed in the
+ same second the list was last read now shows up (the folders' times are compared in nanoseconds).
- **A see-through line around the Terminal.** The Terminal's window has a transparent background, so
its 1-pixel border and the line under its title bar let what was behind the window show through (a
colored line when it was over another window). Both are now drawn opaque for transparent apps, in
diff --git a/server/src/apps.c b/server/src/apps.c
index b987c78..99ba5e2 100644
--- a/server/src/apps.c
+++ b/server/src/apps.c
@@ -46,7 +46,7 @@ extern char **environ;
static DesktopApp *apps;
static int napps;
static char cached_lang[32];
-static time_t cached_mtime[2];
+static long long cached_mtime[2]; /* the folders' mtimes in ns: a file added in the same second still shows */
/* ---- reading a .desktop file ---- */
@@ -331,8 +331,8 @@ static int seen(const char *id, char (*ids)[160], int n) {
int apps_list(const char *lang, const DesktopApp **out) {
char dirs[2][PATH_MAX];
app_dirs(dirs);
- time_t mt[2];
- for (int d = 0; d < 2; d++) { struct stat st; mt[d] = stat(dirs[d], &st) ? 0 : st.st_mtime; }
+ long long mt[2];
+ for (int d = 0; d < 2; d++) { struct stat st; mt[d] = stat(dirs[d], &st) ? 0 : st.st_mtim.tv_sec * 1000000000LL + st.st_mtim.tv_nsec; }
if (apps && !strcmp(cached_lang, lang) && mt[0] == cached_mtime[0] && mt[1] == cached_mtime[1]) { *out = apps; return napps; }
DesktopApp *list = calloc(MAX_APPS, sizeof *list);
@@ -346,8 +346,12 @@ int apps_list(const char *lang, const DesktopApp **out) {
while ((de = readdir(dir)) && n < MAX_APPS) {
size_t len = strlen(de->d_name);
if (len < 9 || strcmp(de->d_name + len - 8, ".desktop")) continue;
+ /* the id is the file name without .desktop, with any character outside [A-Za-z0-9._-] made
+ * '-' ("LordOS Imager.desktop" → "LordOS-Imager"): packages name their files freely, but the
+ * id also names the app's scope (app-lordos-<id>.scope); the real file is kept in a->file */
char id[160];
snprintf(id, sizeof id, "%.*s", (int)(len - 8), de->d_name);
+ for (char *q = id; *q; q++) if (!isalnum((unsigned char)*q) && !strchr("._-", *q)) *q = '-';
if (!valid_id(id) || seen(id, ids, nids)) continue;
if (nids < MAX_APPS * 2) snprintf(ids[nids++], sizeof ids[0], "%s", id); /* it also shadows if hidden */
char file[PATH_MAX * 2];
diff --git a/server/tests/apps_actions_test.c b/server/tests/apps_actions_test.c
index 56e8c4e..cf929c3 100644
--- a/server/tests/apps_actions_test.c
+++ b/server/tests/apps_actions_test.c
@@ -91,6 +91,20 @@ int main(void) {
snprintf(path, sizeof path, "%s/main", home);
CHECK("main Exec ran", appeared(path));
+ /* a package that names its file freely ("Lord Spaced App.desktop"): listed as Lord-Spaced-App,
+ * launched from its real file (0.8.5) */
+ snprintf(path, sizeof path, "%s/.local/share/applications/Lord Spaced App.desktop", home);
+ f = fopen(path, "w");
+ if (f) { fprintf(f, "[Desktop Entry]\nType=Application\nName=Spaced\nExec=/usr/bin/touch %s/spaced\n", home); fclose(f); }
+ const DesktopApp *list;
+ int na = apps_list("en", &list), found = 0;
+ for (int i = 0; i < na; i++) if (!strcmp(list[i].id, "Lord-Spaced-App")) found = 1;
+ CHECK("a file name with spaces is listed, its id sanitized", found);
+ CHECK("it launches", apps_launch("Lord-Spaced-App") == 0);
+ snprintf(path, sizeof path, "%s/spaced", home);
+ CHECK("its Exec ran", appeared(path));
+ CHECK("the raw name with spaces is not an id", apps_launch("Lord Spaced App") != 0);
+
char cmd[600];
snprintf(cmd, sizeof cmd, "rm -rf '%s'", home);
if (system(cmd)) {}