main: store .desktop ID in cache, instead of the title
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This ensures we don't store multiple cache entries with the same value (there may be several, different, desktop files with the same title). Closes #339
authored by Daniel Eklöf · committed by Daniel Eklöf · parent 21e5e80 · Browse files · 2 files·+42 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+42 added−20 removed
- other:+42 added−20 removed
Provenance
- Commit
125883e- Tree
eacaa43- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
21e5e80
2 files changed+42 added−20 removed
modified:CHANGELOG.md+8 added
31 unmodified lines32 * Nanosvg updated to 93ce879dc4c04a3ef1758428ec80083c38610b1f33 34 35 ### Changed36+37+* The cache now stores desktop file **IDs** instead of the application38+ titles. This way, we do not store multiple cache entries with the39+ same value (title) ([#339][339]).40+41+[339]: https://codeberg.org/dnkl/fuzzel/issues/33942+43+44 ### Deprecated45 ### Removed46 ### Fixed47 655 unmodified linesmodified:main.c+34 added-20 removed
66 unmodified lines67 int event_fd;68 int dmenu_abort_fd;69 };70 71+struct cache_entry {72+ char *id;73+ size_t count;74+};75+76 static void77 read_cache(struct application_list *apps)78 {79 const char *path = xdg_cache_dir();29 unmodified lines109 LOG_ERRNO("%s/fuzzel: failed to mmap", path);110 return;111 }112 108- size_t app_idx = 0;113+ tll(struct cache_entry) cache_entries = tll_init();114 115 /* Loop lines */116 char *lineptr = NULL;117 for (char *line = strtok_r(text, "\n", &lineptr);1 unmodified line119 line = strtok_r(NULL, "\n", &lineptr))120 {121 /* Parse each line ("<title>|<count>") */122 char *ptr = NULL;118- const char *title = strtok_r(line, "|", &ptr);123+ const char *id = strtok_r(line, "|", &ptr);124 const char *count_str = strtok_r(NULL, "|", &ptr);125 121- if (title == NULL || count_str == NULL) {126+ if (id == NULL || count_str == NULL) {127 LOG_ERR("invalid cache entry (cache corrupt?): %s", line);128 continue;129 }130 131 int count;132 sscanf(count_str, "%u", &count);133 129- size_t wlen = mbstoc32(NULL, title, 0);130- if (wlen == (size_t)-1)131- continue;134+ struct cache_entry entry = {135+ .id = strdup(id),136+ .count = count,137+ };138+ tll_push_back(cache_entries, entry);139+ }140 133- char32_t wtitle[wlen + 1];134- mbstoc32(wtitle, title, wlen + 1);141+ /* Loop all applications, and search for a matching cache entry */142+ for (size_t i = 0; i < apps->count; i++) {143+ struct application *app = &apps->v[i];144 136- for (; app_idx < apps->count; app_idx++) {137- int cmp = c32cmp(apps->v[app_idx].title, wtitle);145+ tll_foreach(cache_entries, it) {146+ const struct cache_entry *e = &it->item;147 139- if (cmp == 0) {140- apps->v[app_idx].count = count;141- app_idx++;148+ if (strcmp(app->id, e->id) == 0) {149+ app->count = e->count;150+151+ free(e->id);152+ tll_remove(cache_entries, it);153 break;143- } else if (cmp > 0)144- break;154+ }155 }156 }157 158+ /* Free all un-matched cache entries */159+ tll_foreach(cache_entries, it) {160+ free(it->item.id);161+ tll_remove(cache_entries, it);162+ }163+164 munmap(text, st.st_size);165 }166 167 static void25 unmodified lines193 194 char count_as_str[11];195 sprintf(count_as_str, "%u", apps->v[i].count);196 const size_t count_len = strlen(count_as_str);197+ const char *id = apps->v[i].id;198+ const size_t id_len = strlen(id);199 182- size_t c32len = c32tombs(NULL, apps->v[i].title, 0);183- char ctitle[c32len + 1];184- c32tombs(ctitle, apps->v[i].title, c32len + 1);185-186- if (write(fd, ctitle, c32len) != c32len ||200+ if (write(fd, id, id_len) != id_len ||201 write(fd, "|", 1) != 1 ||202 write(fd, count_as_str, count_len) != count_len ||203 write(fd, "\n", 1) != 1)204 {1364 unmodified lines