12 #include <Ecore_File.h>
14 #define EFREET_MODULE_LOG_DOM _efreet_desktop_cache_log_dom
15 static int _efreet_desktop_cache_log_dom = -1;
18 #include "efreet_private.h"
19 #include "efreet_cache_private.h"
21 static Eet_Data_Descriptor *edd = NULL;
22 static Eet_File *ef = NULL;
23 static Eet_File *util_ef = NULL;
25 static Eina_Hash *desktops = NULL;
27 static Eina_Hash *file_ids = NULL;
28 static Efreet_Cache_Hash *old_file_ids = NULL;
29 static Eina_Hash *paths = NULL;
31 static Eina_Hash *mime_types = NULL;
32 static Eina_Hash *categories = NULL;
33 static Eina_Hash *startup_wm_class = NULL;
34 static Eina_Hash *name = NULL;
35 static Eina_Hash *generic_name = NULL;
36 static Eina_Hash *comment = NULL;
37 static Eina_Hash *exec = NULL;
40 strcmplen(const void *data1, const void *data2)
42 return strncmp(data1, data2, eina_stringshare_strlen(data1));
46 cache_add(const char *path, const char *file_id, int priority __UNUSED__, int *changed)
51 INF("FOUND: %s", path);
52 if (file_id) INF(" (id): %s", file_id);
53 ext = strrchr(path, '.');
54 if (!ext || (strcmp(ext, ".desktop") && strcmp(ext, ".directory"))) return 1;
55 desk = efreet_desktop_new(path);
61 /* This file isn't in cache */
65 else if (ecore_file_mod_time(desk->orig_path) != desk->load_time)
67 efreet_desktop_free(desk);
69 desk = efreet_desktop_uncached_new(path);
70 if (desk) INF(" CHANGED");
71 else INF(" NO UNCACHED");
74 if (file_id && old_file_ids && !eina_hash_find(old_file_ids->hash, file_id))
79 if (!eina_hash_find(paths, desk->orig_path))
81 if (!eet_data_write(ef, edd, desk->orig_path, desk, 0))
83 eina_hash_add(paths, desk->orig_path, (void *)1);
85 /* TODO: We should check priority, and not just hope we search in right order */
86 /* TODO: We need to find out if prioritized file id has changed because of
87 * changed search order. */
88 if (!desk->hidden && desk->type == EFREET_DESKTOP_TYPE_APPLICATION &&
89 file_id && !eina_hash_find(file_ids, file_id))
93 Efreet_Cache_Array_String *array;
95 #define ADD_LIST(list, hash) \
96 EINA_LIST_FOREACH((list), l, data) \
98 array = eina_hash_find((hash), data); \
100 array = NEW(Efreet_Cache_Array_String, 1); \
101 array->array = realloc(array->array, sizeof (char *) * (array->array_count + 1)); \
102 array->array[array->array_count++] = desk->orig_path; \
103 eina_hash_set((hash), data, array); \
105 #define ADD_ELEM(elem, hash) \
109 array = eina_hash_find((hash), data); \
111 array = NEW(Efreet_Cache_Array_String, 1); \
112 array->array = realloc(array->array, sizeof (char *) * (array->array_count + 1)); \
113 array->array[array->array_count++] = desk->orig_path; \
114 eina_hash_set((hash), data, array); \
116 ADD_LIST(desk->mime_types, mime_types);
117 ADD_LIST(desk->categories, categories);
118 ADD_ELEM(desk->startup_wm_class, startup_wm_class);
119 ADD_ELEM(desk->name, name);
120 ADD_ELEM(desk->generic_name, generic_name);
121 ADD_ELEM(desk->comment, comment);
122 ADD_ELEM(desk->exec, exec);
123 eina_hash_add(file_ids, file_id, desk->orig_path);
124 eina_hash_add(desktops, desk->orig_path, desk);
127 efreet_desktop_free(desk);
133 cache_scan(const char *path, const char *base_id, int priority, int recurse, int *changed)
135 char *file_id = NULL;
139 Eina_File_Direct_Info *info;
141 if (!ecore_file_is_dir(path)) return 1;
143 it = eina_file_direct_ls(path);
147 EINA_ITERATOR_FOREACH(it, info)
151 fname = info->path + info->name_start;
155 snprintf(id, sizeof(id), "%s-%s", base_id, fname);
161 snprintf(buf, sizeof(buf), "%s/%s", path, fname);
162 if (ecore_file_is_dir(buf))
165 cache_scan(buf, file_id, priority, recurse, changed);
169 if (!cache_add(buf, file_id, priority, changed))
171 eina_iterator_free(it);
176 eina_iterator_free(it);
181 cache_lock_file(void)
187 snprintf(file, sizeof(file), "%s/efreet/desktop_data.lock", efreet_cache_home_get());
188 lockfd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
189 if (lockfd < 0) return -1;
190 efreet_fsetowner(lockfd);
192 memset(&fl, 0, sizeof(struct flock));
194 fl.l_whence = SEEK_SET;
195 if (fcntl(lockfd, F_SETLK, &fl) < 0)
197 INF("LOCKED! You may want to delete %s if this persists", file);
206 main(int argc, char **argv)
209 * - Add file monitor on files, so that we catch changes on files
210 * during whilst this program runs.
211 * - Maybe linger for a while to reduce number of cache re-creates.
213 Efreet_Cache_Hash hash;
214 Efreet_Cache_Version version;
215 Eina_List *dirs = NULL;
216 Eina_List *systemdirs = NULL;
217 Efreet_Cache_Array_String *user_dirs = NULL;
218 Eina_List *extra_dirs = NULL;
219 Eina_List *store_dirs = NULL;
223 int lockfd = -1, tmpfd;
226 char file[PATH_MAX] = { '\0' };
227 char util_file[PATH_MAX] = { '\0' };
229 if (!eina_init()) goto eina_error;
230 _efreet_desktop_cache_log_dom =
231 eina_log_domain_register("efreet_desktop_cache", EFREET_DEFAULT_LOG_COLOR);
232 if (_efreet_desktop_cache_log_dom < 0)
234 EINA_LOG_ERR("Efreet: Could not create a log domain for efreet_desktop_cache.");
238 for (i = 1; i < argc; i++)
240 if (!strcmp(argv[i], "-v"))
241 eina_log_domain_level_set("efreet_desktop_cache", EINA_LOG_LEVEL_DBG);
242 else if ((!strcmp(argv[i], "-h")) ||
243 (!strcmp(argv[i], "-help")) ||
244 (!strcmp(argv[i], "--h")) ||
245 (!strcmp(argv[i], "--help")))
247 printf("Options:\n");
248 printf(" -v Verbose mode\n");
249 printf(" -d dir1 dir2 Extra dirs\n");
252 else if (!strcmp(argv[i], "-d"))
254 while ((i < (argc - 1)) && (argv[(i + 1)][0] != '-'))
255 extra_dirs = eina_list_append(extra_dirs, argv[++i]);
258 extra_dirs = eina_list_sort(extra_dirs, -1, EINA_COMPARE_CB(strcmp));
260 /* init external subsystems */
261 if (!eet_init()) goto eet_error;
262 if (!ecore_init()) goto ecore_error;
264 efreet_cache_update = 0;
265 /* finish efreet init */
266 if (!efreet_init()) goto efreet_error;
269 snprintf(file, sizeof(file), "%s/efreet", efreet_cache_home_get());
270 if (!ecore_file_exists(file))
272 if (!ecore_file_mkpath(file)) goto efreet_error;
273 efreet_setowner(file);
276 /* lock process, so that we only run one copy of this program */
277 lockfd = cache_lock_file();
278 if (lockfd == -1) goto efreet_error;
280 edd = efreet_desktop_edd();
281 if (!edd) goto edd_error;
283 /* read user dirs from old cache */
284 ef = eet_open(efreet_desktop_cache_file(), EET_FILE_MODE_READ);
287 user_dirs = eet_data_read(ef, efreet_array_string_edd(), EFREET_CACHE_DESKTOP_DIRS);
291 ef = eet_open(efreet_desktop_util_cache_file(), EET_FILE_MODE_READ);
294 old_file_ids = eet_data_read(ef, efreet_hash_string_edd(), "file_id");
299 snprintf(file, sizeof(file), "%s.XXXXXX", efreet_desktop_cache_file());
300 tmpfd = mkstemp(file);
301 if (tmpfd < 0) goto error;
303 ef = eet_open(file, EET_FILE_MODE_READ_WRITE);
306 snprintf(util_file, sizeof(util_file), "%s.XXXXXX", efreet_desktop_util_cache_file());
307 tmpfd = mkstemp(util_file);
308 if (tmpfd < 0) goto error;
310 util_ef = eet_open(util_file, EET_FILE_MODE_READ_WRITE);
311 if (!util_ef) goto error;
313 /* write cache version */
314 version.major = EFREET_DESKTOP_UTILS_CACHE_MAJOR;
315 version.minor = EFREET_DESKTOP_UTILS_CACHE_MINOR;
316 eet_data_write(util_ef, efreet_version_edd(), EFREET_CACHE_VERSION, &version, 1);
317 version.major = EFREET_DESKTOP_CACHE_MAJOR;
318 version.minor = EFREET_DESKTOP_CACHE_MINOR;
319 eet_data_write(ef, efreet_version_edd(), EFREET_CACHE_VERSION, &version, 1);
321 desktops = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_desktop_free));
323 file_ids = eina_hash_string_superfast_new(NULL);
324 paths = eina_hash_string_superfast_new(NULL);
326 mime_types = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
327 categories = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
328 startup_wm_class = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
329 name = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
330 generic_name = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
331 comment = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
332 exec = eina_hash_string_superfast_new(EINA_FREE_CB(efreet_cache_array_string_free));
334 dirs = efreet_default_dirs_get(efreet_data_home_get(), efreet_data_dirs_get(),
336 if (!dirs) goto error;
338 EINA_LIST_FREE(dirs, path)
340 char file_id[PATH_MAX] = { '\0' };
342 if (!cache_scan(path, file_id, priority++, 1, &changed)) goto error;
343 systemdirs = eina_list_append(systemdirs, path);
350 for (j = 0; j < user_dirs->array_count; j++)
352 if (eina_list_search_unsorted_list(systemdirs, strcmplen, user_dirs->array[j]))
354 if (!ecore_file_is_dir(user_dirs->array[j])) continue;
355 if (!cache_scan(user_dirs->array[j], NULL, priority, 0, &changed)) goto error;
357 store_dirs = eina_list_append(store_dirs, user_dirs->array[j]);
359 store_dirs = eina_list_sort(store_dirs, -1, EINA_COMPARE_CB(strcmp));
366 EINA_LIST_FOREACH(extra_dirs, l, path)
368 if (eina_list_search_unsorted_list(systemdirs, strcmplen, path))
370 if (eina_list_search_unsorted_list(store_dirs, EINA_COMPARE_CB(strcmp), path))
372 if (!ecore_file_is_dir(path)) continue;
374 /* If we scan a passed dir, we must have changed */
376 if (!cache_scan(path, NULL, priority, 0, &changed)) goto error;
378 store_dirs = eina_list_append(store_dirs, path);
380 store_dirs = eina_list_sort(store_dirs, -1, EINA_COMPARE_CB(strcmp));
384 efreet_cache_array_string_free(user_dirs);
386 /* store user dirs */
391 user_dirs = NEW(Efreet_Cache_Array_String, 1);
392 user_dirs->array = NEW(char *, eina_list_count(store_dirs));
393 user_dirs->array_count = 0;
394 EINA_LIST_FOREACH(store_dirs, l, path)
395 user_dirs->array[user_dirs->array_count++] = path;
397 eet_data_write(ef, efreet_array_string_edd(), EFREET_CACHE_DESKTOP_DIRS, user_dirs, 1);
398 IF_FREE(user_dirs->array);
403 #define STORE_HASH_ARRAY(_hash) \
404 if (eina_hash_population((_hash)) > 0) \
407 Efreet_Cache_Array_String array; \
410 hash.hash = (_hash); \
411 eet_data_write(util_ef, efreet_hash_array_string_edd(), #_hash "_hash", &hash, 1); \
412 array.array_count = 0; \
413 array.array = malloc(eina_hash_population(hash.hash) * sizeof(char *)); \
414 it = eina_hash_iterator_key_new(hash.hash); \
415 EINA_ITERATOR_FOREACH(it, str) \
416 array.array[array.array_count++] = str; \
417 eina_iterator_free(it); \
418 eet_data_write(util_ef, efreet_array_string_edd(), #_hash "_list", &array, 1); \
421 STORE_HASH_ARRAY(mime_types);
422 STORE_HASH_ARRAY(categories);
423 STORE_HASH_ARRAY(startup_wm_class);
424 STORE_HASH_ARRAY(name);
425 STORE_HASH_ARRAY(generic_name);
426 STORE_HASH_ARRAY(comment);
427 STORE_HASH_ARRAY(exec);
428 if (eina_hash_population(file_ids) > 0)
430 hash.hash = file_ids;
431 eet_data_write(util_ef, efreet_hash_string_edd(), "file_id", &hash, 1);
434 eina_hash_free(mime_types);
435 eina_hash_free(categories);
436 eina_hash_free(startup_wm_class);
437 eina_hash_free(name);
438 eina_hash_free(generic_name);
439 eina_hash_free(comment);
440 eina_hash_free(exec);
444 eina_hash_free(old_file_ids->hash);
448 eina_hash_free(file_ids);
449 eina_hash_free(paths);
451 eina_hash_free(desktops);
453 /* check if old and new caches contain the same number of entries */
458 old = eet_open(efreet_desktop_cache_file(), EET_FILE_MODE_READ);
459 if (!old || eet_num_entries(old) != eet_num_entries(ef)) changed = 1;
460 if (old) eet_close(old);
461 old = eet_open(efreet_desktop_util_cache_file(), EET_FILE_MODE_READ);
462 if (!old || eet_num_entries(old) != eet_num_entries(util_ef)) changed = 1;
463 if (old) eet_close(old);
470 /* unlink old cache files */
473 if (unlink(efreet_desktop_cache_file()) < 0)
475 if (errno != ENOENT) goto error;
477 if (unlink(efreet_desktop_util_cache_file()) < 0)
479 if (errno != ENOENT) goto error;
481 /* rename tmp files to real files */
482 if (rename(util_file, efreet_desktop_util_cache_file()) < 0) goto error;
483 efreet_setowner(efreet_desktop_util_cache_file());
484 if (rename(file, efreet_desktop_cache_file()) < 0) goto error;
485 efreet_setowner(efreet_desktop_cache_file());
493 /* touch update file */
494 snprintf(file, sizeof(file), "%s/efreet/desktop_data.update", efreet_cache_home_get());
495 tmpfd = open(file, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
500 efreet_fsetowner(tmpfd);
501 if (changed) c = 'c';
502 if (write(tmpfd, &c, 1) != 1) perror("write");
506 EINA_LIST_FREE(systemdirs, dir)
507 eina_stringshare_del(dir);
508 eina_list_free(extra_dirs);
509 eina_list_free(store_dirs);
513 eina_log_domain_unregister(_efreet_desktop_cache_log_dom);
520 if (user_dirs) efreet_cache_array_string_free(user_dirs);
523 eina_hash_free(old_file_ids->hash);
532 EINA_LIST_FREE(systemdirs, dir)
533 eina_stringshare_del(dir);
534 eina_list_free(extra_dirs);
535 eina_list_free(store_dirs);
536 eina_log_domain_unregister(_efreet_desktop_cache_log_dom);
539 if (lockfd >= 0) close(lockfd);