Icon cache: open correctly and create when needed

This commit is contained in:
o9000
2016-03-05 11:20:12 +01:00
parent 71abe9a7e1
commit b0f172a8e1

View File

@@ -52,6 +52,8 @@ void load_cache(Cache *cache, const gchar *cache_path)
{ {
init_cache(cache); init_cache(cache);
cache->loaded = TRUE;
int fd = open(cache_path, O_RDONLY); int fd = open(cache_path, O_RDONLY);
if (fd == -1) if (fd == -1)
return; return;
@@ -89,8 +91,6 @@ void load_cache(Cache *cache, const gchar *cache_path)
free(line); free(line);
fclose(f); fclose(f);
cache->loaded = TRUE;
unlock: unlock:
flock(fd, LOCK_UN); flock(fd, LOCK_UN);
close(fd); close(fd);
@@ -107,22 +107,24 @@ void write_cache_line(gpointer key, gpointer value, gpointer user_data)
void save_cache(Cache *cache, const gchar *cache_path) void save_cache(Cache *cache, const gchar *cache_path)
{ {
int fd = open(cache_path, O_RDONLY | O_CREAT); int fd = open(cache_path, O_RDONLY | O_CREAT, 0600);
if (fd == -1) if (fd == -1) {
gchar *dir_path = g_path_get_dirname(cache_path);
g_mkdir_with_parents(dir_path, 0700);
g_free(dir_path);
fd = open(cache_path, O_RDONLY | O_CREAT, 0600);
}
if (fd == -1) {
fprintf(stderr, RED "Could not save icon theme cache!" RESET "\n");
return; return;
}
flock(fd, LOCK_EX); flock(fd, LOCK_EX);
FILE *f = fopen(cache_path, "w"); FILE *f = fopen(cache_path, "w");
if (!f) {
gchar *dir_path = g_path_get_dirname(cache_path);
g_mkdir_with_parents(dir_path, 0700);
g_free(dir_path);
f = fopen(cache_path, "w");
if (!f) { if (!f) {
fprintf(stderr, RED "Could not save icon theme cache!" RESET "\n"); fprintf(stderr, RED "Could not save icon theme cache!" RESET "\n");
goto unlock; goto unlock;
} }
}
g_hash_table_foreach(cache->_table, write_cache_line, f); g_hash_table_foreach(cache->_table, write_cache_line, f);
fclose(f); fclose(f);
cache->dirty = FALSE; cache->dirty = FALSE;