2010-03-01 10:16:32 -08:00
|
|
|
/* vim: set sw=4 ts=4 sts=4 et: */
|
2010-03-01 13:43:59 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
2010-03-01 10:16:32 -08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
2010-03-10 13:33:43 -08:00
|
|
|
#include <sys/mman.h>
|
2010-03-01 10:16:32 -08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2010-03-02 03:26:31 -08:00
|
|
|
#include <errno.h>
|
2010-03-01 10:16:32 -08:00
|
|
|
|
|
|
|
#include <Eina.h>
|
|
|
|
#include <Ecore_File.h>
|
|
|
|
|
|
|
|
#include "Efreet.h"
|
|
|
|
#include "efreet_private.h"
|
|
|
|
|
|
|
|
static Eet_Data_Descriptor *edd = NULL;
|
|
|
|
static Eet_File *ef = NULL;
|
|
|
|
static Eet_File *util_ef = NULL;
|
|
|
|
|
|
|
|
static Eina_Hash *file_ids = NULL;
|
2010-03-01 13:43:37 -08:00
|
|
|
static Eina_Hash *paths = NULL;
|
2010-03-01 10:16:32 -08:00
|
|
|
|
2010-03-10 13:33:43 -08:00
|
|
|
static int
|
|
|
|
strcmplen(const void *data1, const void *data2)
|
|
|
|
{
|
2010-03-29 13:14:43 -07:00
|
|
|
return strncmp(data1, data2, eina_stringshare_strlen(data2));
|
2010-03-10 13:33:43 -08:00
|
|
|
}
|
|
|
|
|
2010-03-01 10:16:32 -08:00
|
|
|
static int
|
2010-03-28 13:46:27 -07:00
|
|
|
cache_add(const char *path, const char *file_id, int priority __UNUSED__, int *changed)
|
2010-03-01 10:16:32 -08:00
|
|
|
{
|
|
|
|
Efreet_Desktop *desk;
|
|
|
|
char *ext;
|
|
|
|
|
|
|
|
ext = strrchr(path, '.');
|
2010-03-13 12:58:17 -08:00
|
|
|
if (!ext || (strcmp(ext, ".desktop") && strcmp(ext, ".directory"))) return 1;
|
2010-03-29 13:11:12 -07:00
|
|
|
desk = efreet_desktop_new(path);
|
2010-04-06 11:36:04 -07:00
|
|
|
if (!desk) return 1;
|
2010-03-30 03:51:35 -07:00
|
|
|
if (!desk->eet)
|
|
|
|
{
|
|
|
|
/* This file isn't in cache */
|
|
|
|
*changed = 1;
|
|
|
|
}
|
|
|
|
else if (ecore_file_mod_time(desk->orig_path) != desk->load_time)
|
2010-03-28 13:46:27 -07:00
|
|
|
{
|
|
|
|
efreet_desktop_free(desk);
|
|
|
|
*changed = 1;
|
|
|
|
desk = efreet_desktop_uncached_new(path);
|
|
|
|
}
|
|
|
|
if (!desk) return 1;
|
2010-03-01 13:43:37 -08:00
|
|
|
if (!eina_hash_find(paths, desk->orig_path))
|
|
|
|
{
|
|
|
|
if (!eet_data_write(ef, edd, desk->orig_path, desk, 0))
|
|
|
|
return 0;
|
|
|
|
eina_hash_add(paths, desk->orig_path, (void *)1);
|
|
|
|
}
|
2010-03-01 13:43:59 -08:00
|
|
|
/* TODO: We should check priority, and not just hope we search in right order */
|
2010-03-13 12:58:17 -08:00
|
|
|
if (desk->type == EFREET_DESKTOP_TYPE_APPLICATION &&
|
|
|
|
file_id && !eina_hash_find(file_ids, file_id))
|
2010-03-01 10:16:32 -08:00
|
|
|
{
|
|
|
|
int id;
|
|
|
|
char key[PATH_MAX];
|
|
|
|
char *data;
|
|
|
|
int i = 0;
|
|
|
|
Eina_List *l;
|
|
|
|
|
|
|
|
id = eina_hash_population(file_ids);
|
|
|
|
i = 0;
|
|
|
|
EINA_LIST_FOREACH(desk->mime_types, l, data)
|
|
|
|
{
|
|
|
|
snprintf(key, sizeof(key), "%d::%d::m", id, i++);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
EINA_LIST_FOREACH(desk->categories, l, data)
|
|
|
|
{
|
|
|
|
snprintf(key, sizeof(key), "%d::%d::ca", id, i++);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
if (desk->startup_wm_class)
|
|
|
|
{
|
|
|
|
data = desk->startup_wm_class;
|
|
|
|
snprintf(key, sizeof(key), "%d::swc", id);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
if (desk->name)
|
|
|
|
{
|
|
|
|
data = desk->name;
|
|
|
|
snprintf(key, sizeof(key), "%d::n", id);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
if (desk->generic_name)
|
|
|
|
{
|
|
|
|
data = desk->generic_name;
|
|
|
|
snprintf(key, sizeof(key), "%d::gn", id);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
if (desk->comment)
|
|
|
|
{
|
|
|
|
data = desk->comment;
|
|
|
|
snprintf(key, sizeof(key), "%d::co", id);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
if (desk->exec)
|
|
|
|
{
|
|
|
|
data = desk->exec;
|
|
|
|
snprintf(key, sizeof(key), "%d::e", id);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
if (desk->orig_path)
|
|
|
|
{
|
|
|
|
data = desk->orig_path;
|
|
|
|
snprintf(key, sizeof(key), "%d::op", id);
|
|
|
|
if (!eet_write(util_ef, key, data, strlen(data) + 1, 0)) return 0;
|
|
|
|
}
|
|
|
|
snprintf(key, sizeof(key), "%d::fi", id);
|
|
|
|
if (!eet_write(util_ef, key, file_id, strlen(file_id) + 1, 0)) return 0;
|
|
|
|
|
|
|
|
eina_hash_add(file_ids, file_id, (void *)1);
|
|
|
|
}
|
|
|
|
efreet_desktop_free(desk);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2010-03-28 13:46:27 -07:00
|
|
|
cache_scan(const char *path, const char *base_id, int priority, int recurse, int *changed)
|
2010-03-01 10:16:32 -08:00
|
|
|
{
|
2010-03-10 13:34:52 -08:00
|
|
|
char *file_id = NULL;
|
|
|
|
char id[PATH_MAX];
|
2010-03-01 10:16:32 -08:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
DIR *files;
|
|
|
|
struct dirent *file;
|
|
|
|
|
|
|
|
if (!ecore_file_is_dir(path)) return 1;
|
|
|
|
|
|
|
|
files = opendir(path);
|
2010-04-17 11:39:13 -07:00
|
|
|
if (!files) return 1;
|
2010-03-10 13:34:52 -08:00
|
|
|
id[0] = '\0';
|
2010-03-01 10:16:32 -08:00
|
|
|
while ((file = readdir(files)))
|
|
|
|
{
|
|
|
|
if (!file) break;
|
|
|
|
if (!strcmp(file->d_name, ".") || !strcmp(file->d_name, "..")) continue;
|
|
|
|
|
2010-03-10 13:34:52 -08:00
|
|
|
if (base_id)
|
|
|
|
{
|
|
|
|
if (*base_id)
|
|
|
|
snprintf(id, sizeof(id), "%s-%s", base_id, file->d_name);
|
|
|
|
else
|
|
|
|
strcpy(id, file->d_name);
|
|
|
|
file_id = id;
|
|
|
|
}
|
2010-03-01 10:16:32 -08:00
|
|
|
|
2010-03-10 13:34:52 -08:00
|
|
|
snprintf(buf, sizeof(buf), "%s/%s", path, file->d_name);
|
2010-03-01 10:16:32 -08:00
|
|
|
if (ecore_file_is_dir(buf))
|
|
|
|
{
|
2010-03-10 13:34:52 -08:00
|
|
|
if (recurse)
|
2010-03-28 13:46:27 -07:00
|
|
|
cache_scan(buf, file_id, priority, recurse, changed);
|
2010-03-01 10:16:32 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-28 13:46:27 -07:00
|
|
|
if (!cache_add(buf, file_id, priority, changed)) return 0;
|
2010-03-01 10:16:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(files);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-03-01 13:43:59 -08:00
|
|
|
main()
|
2010-03-01 10:16:32 -08:00
|
|
|
{
|
|
|
|
/* TODO:
|
|
|
|
* - Add file monitor on files, so that we catch changes on files
|
|
|
|
* during whilst this program runs.
|
2010-04-01 12:40:53 -07:00
|
|
|
* - Maybe linger for a while to reduce number of cache re-creates.
|
2010-03-01 10:16:32 -08:00
|
|
|
*/
|
|
|
|
char file[PATH_MAX];
|
2010-03-01 11:05:49 -08:00
|
|
|
char util_file[PATH_MAX];
|
2010-03-10 13:33:43 -08:00
|
|
|
Eina_List *dirs = NULL, *user_dirs = NULL;
|
2010-03-01 10:16:32 -08:00
|
|
|
int priority = 0;
|
|
|
|
char *dir = NULL;
|
2010-03-10 13:33:43 -08:00
|
|
|
char *map = MAP_FAILED;
|
2010-03-29 13:14:43 -07:00
|
|
|
char *path;
|
2010-03-10 13:33:43 -08:00
|
|
|
int fd = -1, tmpfd, dirsfd = -1;
|
|
|
|
struct stat st;
|
2010-03-28 13:46:27 -07:00
|
|
|
int changed = 0;
|
2010-03-01 10:16:32 -08:00
|
|
|
|
|
|
|
/* init external subsystems */
|
|
|
|
if (!eina_init()) goto eina_error;
|
2010-03-03 17:00:06 -08:00
|
|
|
if (!eet_init()) goto eet_error;
|
2010-03-01 10:16:32 -08:00
|
|
|
|
2010-03-28 11:54:25 -07:00
|
|
|
efreet_cache_update = 0;
|
|
|
|
|
2010-03-01 10:16:32 -08:00
|
|
|
/* create homedir */
|
|
|
|
snprintf(file, sizeof(file), "%s/.efreet", efreet_home_dir_get());
|
|
|
|
if (!ecore_file_mkpath(file)) goto efreet_error;
|
|
|
|
|
2010-03-02 03:26:31 -08:00
|
|
|
/* lock process, so that we only run one copy of this program */
|
2010-04-03 11:45:46 -07:00
|
|
|
snprintf(file, sizeof(file), "%s/.efreet/desktop_data.lock", efreet_home_dir_get());
|
2010-03-28 13:36:26 -07:00
|
|
|
fd = open(file, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
|
2010-03-02 03:26:31 -08:00
|
|
|
if (fd < 0) goto efreet_error;
|
2010-03-03 04:27:32 -08:00
|
|
|
if (flock(fd, LOCK_EX | LOCK_NB) < 0) goto efreet_error;
|
2010-03-02 03:26:31 -08:00
|
|
|
|
2010-03-01 10:16:32 -08:00
|
|
|
/* create dir for desktop cache */
|
|
|
|
dir = ecore_file_dir_get(efreet_desktop_cache_file());
|
|
|
|
if (!ecore_file_mkpath(dir)) goto efreet_error;
|
|
|
|
free(dir);
|
|
|
|
|
|
|
|
/* create dir for util cache */
|
2010-03-03 04:28:40 -08:00
|
|
|
dir = ecore_file_dir_get(efreet_desktop_util_cache_file());
|
2010-03-01 10:16:32 -08:00
|
|
|
if (!ecore_file_mkpath(dir)) goto efreet_error;
|
|
|
|
free(dir);
|
2010-03-01 10:42:24 -08:00
|
|
|
|
2010-03-01 10:16:32 -08:00
|
|
|
/* finish efreet init */
|
|
|
|
if (!efreet_init()) goto efreet_error;
|
|
|
|
edd = efreet_desktop_edd_init();
|
|
|
|
if (!edd) goto edd_error;
|
|
|
|
|
|
|
|
/* create cache */
|
2010-03-01 11:05:49 -08:00
|
|
|
snprintf(file, sizeof(file), "%s.XXXXXX", efreet_desktop_cache_file());
|
|
|
|
tmpfd = mkstemp(file);
|
|
|
|
if (tmpfd < 0) goto error;
|
|
|
|
close(tmpfd);
|
|
|
|
ef = eet_open(file, EET_FILE_MODE_WRITE);
|
2010-03-01 10:16:32 -08:00
|
|
|
if (!ef) goto error;
|
2010-03-01 11:05:49 -08:00
|
|
|
|
2010-03-03 04:28:40 -08:00
|
|
|
snprintf(util_file, sizeof(util_file), "%s.XXXXXX", efreet_desktop_util_cache_file());
|
2010-03-01 11:05:49 -08:00
|
|
|
tmpfd = mkstemp(util_file);
|
|
|
|
if (tmpfd < 0) goto error;
|
|
|
|
close(tmpfd);
|
|
|
|
util_ef = eet_open(util_file, EET_FILE_MODE_WRITE);
|
2010-03-01 10:16:32 -08:00
|
|
|
if (!util_ef) goto error;
|
|
|
|
|
|
|
|
file_ids = eina_hash_string_superfast_new(NULL);
|
|
|
|
if (!file_ids) goto error;
|
2010-03-01 13:43:37 -08:00
|
|
|
paths = eina_hash_string_superfast_new(NULL);
|
|
|
|
if (!paths) goto error;
|
2010-03-01 10:16:32 -08:00
|
|
|
|
|
|
|
dirs = efreet_default_dirs_get(efreet_data_home_get(), efreet_data_dirs_get(),
|
|
|
|
"applications");
|
|
|
|
if (!dirs) goto error;
|
2010-03-10 13:33:43 -08:00
|
|
|
|
|
|
|
dirsfd = open(efreet_desktop_cache_dirs(), O_APPEND | O_RDWR, S_IRUSR | S_IWUSR);
|
|
|
|
if ((dirsfd > 0) && (fstat(dirsfd, &st) == 0) && (st.st_size > 0))
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, dirsfd, 0);
|
|
|
|
if (map == MAP_FAILED) goto error;
|
|
|
|
p = map;
|
|
|
|
while (p < map + st.st_size)
|
|
|
|
{
|
|
|
|
unsigned int size = *(unsigned int *)p;
|
|
|
|
p += sizeof(unsigned int);
|
2010-03-29 13:14:43 -07:00
|
|
|
user_dirs = eina_list_append(user_dirs, eina_stringshare_add(p));
|
2010-03-10 13:33:43 -08:00
|
|
|
p += size;
|
|
|
|
}
|
|
|
|
munmap(map, st.st_size);
|
|
|
|
map = MAP_FAILED;
|
2010-04-06 11:35:35 -07:00
|
|
|
}
|
|
|
|
if (dirsfd > 0)
|
|
|
|
{
|
|
|
|
close(dirsfd);
|
|
|
|
dirsfd = -1;
|
|
|
|
unlink(efreet_desktop_cache_dirs());
|
2010-03-10 13:33:43 -08:00
|
|
|
}
|
|
|
|
|
2010-03-29 13:14:43 -07:00
|
|
|
EINA_LIST_FREE(dirs, path)
|
2010-03-01 10:16:32 -08:00
|
|
|
{
|
|
|
|
char file_id[PATH_MAX] = { '\0' };
|
2010-03-10 13:33:43 -08:00
|
|
|
Eina_List *l;
|
2010-03-01 10:16:32 -08:00
|
|
|
|
2010-03-29 13:14:43 -07:00
|
|
|
if (!cache_scan(path, file_id, priority++, 1, &changed)) goto error;
|
|
|
|
l = eina_list_search_unsorted_list(user_dirs, strcmplen, path);
|
|
|
|
if (l)
|
2010-03-01 10:16:32 -08:00
|
|
|
{
|
2010-03-29 13:14:43 -07:00
|
|
|
eina_stringshare_del(eina_list_data_get(l));
|
|
|
|
user_dirs = eina_list_remove_list(user_dirs, l);
|
2010-03-01 10:16:32 -08:00
|
|
|
}
|
2010-03-29 13:14:43 -07:00
|
|
|
eina_stringshare_del(path);
|
2010-03-01 10:16:32 -08:00
|
|
|
}
|
2010-04-06 11:35:35 -07:00
|
|
|
if (user_dirs)
|
2010-03-10 13:33:43 -08:00
|
|
|
{
|
2010-04-06 11:35:35 -07:00
|
|
|
dirsfd = open(efreet_desktop_cache_dirs(), O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR);
|
|
|
|
if (dirsfd < 0) goto error;
|
|
|
|
EINA_LIST_FREE(user_dirs, dir)
|
2010-03-10 13:33:43 -08:00
|
|
|
{
|
|
|
|
unsigned int size = strlen(dir) + 1;
|
|
|
|
write(dirsfd, &size, sizeof(int));
|
|
|
|
write(dirsfd, dir, size);
|
2010-04-06 11:35:35 -07:00
|
|
|
|
|
|
|
if (!cache_scan(dir, NULL, priority, 0, &changed)) goto error;
|
|
|
|
eina_stringshare_del(dir);
|
2010-03-10 13:33:43 -08:00
|
|
|
}
|
2010-04-06 11:35:35 -07:00
|
|
|
close(dirsfd);
|
|
|
|
dirsfd = -1;
|
2010-03-10 13:33:43 -08:00
|
|
|
}
|
2010-03-01 10:16:32 -08:00
|
|
|
eina_hash_free(file_ids);
|
2010-03-01 13:43:37 -08:00
|
|
|
eina_hash_free(paths);
|
2010-03-30 03:51:45 -07:00
|
|
|
|
|
|
|
/* check if old and new caches contain the same number of entries */
|
|
|
|
if (!changed)
|
|
|
|
{
|
|
|
|
Eet_File *old;
|
|
|
|
|
|
|
|
old = eet_open(efreet_desktop_cache_file(), EET_FILE_MODE_READ);
|
|
|
|
if (!old || eet_num_entries(old) != eet_num_entries(ef)) changed = 1;
|
|
|
|
if (old) eet_close(old);
|
|
|
|
old = eet_open(efreet_desktop_util_cache_file(), EET_FILE_MODE_READ);
|
|
|
|
if (!old || eet_num_entries(old) != eet_num_entries(util_ef)) changed = 1;
|
|
|
|
if (old) eet_close(old);
|
|
|
|
|
|
|
|
}
|
2010-03-01 10:16:32 -08:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
eet_close(util_ef);
|
|
|
|
eet_close(ef);
|
|
|
|
|
2010-03-28 12:31:26 -07:00
|
|
|
/* unlink old cache files */
|
2010-03-28 13:46:27 -07:00
|
|
|
if (changed)
|
2010-03-28 12:31:26 -07:00
|
|
|
{
|
2010-03-28 13:46:27 -07:00
|
|
|
if (unlink(efreet_desktop_cache_file()) < 0)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT) goto error;
|
|
|
|
}
|
|
|
|
if (unlink(efreet_desktop_util_cache_file()) < 0)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT) goto error;
|
|
|
|
}
|
|
|
|
/* rename tmp files to real files */
|
|
|
|
if (rename(util_file, efreet_desktop_util_cache_file()) < 0) goto error;
|
|
|
|
if (rename(file, efreet_desktop_cache_file()) < 0) goto error;
|
2010-03-28 12:31:26 -07:00
|
|
|
}
|
2010-03-28 13:46:27 -07:00
|
|
|
else
|
2010-03-28 12:31:26 -07:00
|
|
|
{
|
2010-03-28 13:46:27 -07:00
|
|
|
unlink(util_file);
|
|
|
|
unlink(file);
|
2010-03-28 12:31:26 -07:00
|
|
|
}
|
2010-03-01 11:05:49 -08:00
|
|
|
|
2010-03-01 10:16:32 -08:00
|
|
|
efreet_desktop_edd_shutdown(edd);
|
|
|
|
efreet_shutdown();
|
|
|
|
eet_shutdown();
|
|
|
|
eina_shutdown();
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
error:
|
2010-03-10 13:33:43 -08:00
|
|
|
if (map != MAP_FAILED) munmap(map, st.st_size);
|
|
|
|
if (dirsfd > 0) close(dirsfd);
|
2010-03-01 10:16:32 -08:00
|
|
|
IF_FREE(dir);
|
|
|
|
efreet_desktop_edd_shutdown(edd);
|
|
|
|
edd_error:
|
|
|
|
efreet_shutdown();
|
|
|
|
efreet_error:
|
|
|
|
eet_shutdown();
|
|
|
|
eet_error:
|
2010-03-03 17:00:06 -08:00
|
|
|
eina_shutdown();
|
|
|
|
eina_error:
|
2010-03-01 10:16:32 -08:00
|
|
|
if (fd > 0) close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|