Skip to content
Myr

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

2 files+42 added−20 removed

modified:CHANGELOG.md+8 added
31 unmodified lines
32* Nanosvg updated to 93ce879dc4c04a3ef1758428ec80083c38610b1f333435### Changed3637* 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]).4041[339]: https://codeberg.org/dnkl/fuzzel/issues/339424344### Deprecated45### Removed46### Fixed47
655 unmodified lines
modified:main.c+34 added-20 removed
66 unmodified lines
67 int event_fd;68 int dmenu_abort_fd;69};7071struct cache_entry {72 char *id;73 size_t count;74};7576static void77read_cache(struct application_list *apps)78{79 const char *path = xdg_cache_dir();
29 unmodified lines
109 LOG_ERRNO("%s/fuzzel: failed to mmap", path);110 return;111 }112108 size_t app_idx = 0;113 tll(struct cache_entry) cache_entries = tll_init();114115 /* Loop lines */116 char *lineptr = NULL;117 for (char *line = strtok_r(text, "\n", &lineptr);
1 unmodified line
119 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);125121 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 }130131 int count;132 sscanf(count_str, "%u", &count);133129 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 }140133 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];144136 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;147139 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;150151 free(e->id);152 tll_remove(cache_entries, it);153 break;143 } else if (cmp > 0)144 break;154 }155 }156 }157158 /* Free all un-matched cache entries */159 tll_foreach(cache_entries, it) {160 free(it->item.id);161 tll_remove(cache_entries, it);162 }163164 munmap(text, st.st_size);165}166167static void
25 unmodified lines
193194 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);199182 size_t c32len = c32tombs(NULL, apps->v[i].title, 0);183 char ctitle[c32len + 1];184 c32tombs(ctitle, apps->v[i].title, c32len + 1);185186 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