/* This checks all files in ~/GQ_RC_DIR/thumbnails and * removes them if thay have no source counterpart. * (this assumes all cache files have an extension of 4 chars including '.') */ gint cache_maintain_home_dir(const gchar *dir, gint recursive, gint clear) { gchar *base; gint base_length; GList *dlist = NULL; FileData *dir_fd; GList *flist = NULL; gboolean still_have_a_file = FALSE; DEBUG_1("maintainance check: %s", dir); base_length = strlen(homedir()) + strlen("/") + strlen(GQ_CACHE_RC_THUMB); base = g_strconcat(homedir(), "/", GQ_CACHE_RC_THUMB, dir, NULL); dir_fd = file_data_new_simple(base); g_free(base); if (filelist_read(dir_fd, &flist, &dlist)) { GList *work; work = dlist; while (work) { FileData *fd = work->data; if (recursive && strlen(fd->path) > base_length && !cache_maintain_home_dir(fd->path + base_length, recursive, clear)) { DEBUG_1("Deleting thumb dir: %s", fd->path); if (!rmdir_utf8(fd->path)) { log_printf("Unable to delete dir: %s\n", fd->path); } } else { still_have_a_file = TRUE; } work = work->next; } work = flist; while (work) { FileData *fd = work->data; gchar *path = g_strdup(fd->path); gchar *dot; dot = extension_find_dot(path); if (dot) *dot = '\0'; if (clear || (strlen(path) > base_length && !isfile(path + base_length)) ) { if (dot) *dot = '.'; if (!unlink_file(path)) log_printf("failed to delete:%s\n", path); } else { still_have_a_file = TRUE; } g_free(path); work = work->next; } } filelist_free(dlist); filelist_free(flist); file_data_unref(dir_fd); return still_have_a_file; }
int cache_maintain_home_dir (CacheType type, const gchar * dir, gint recursive, gint clear) { gchar *base; gint base_length; GList *dlist = NULL; GList *flist = NULL; gint still_have_a_file = FALSE; if (type == CACHE_THUMBS) { base_length = strlen (home_dir ()) + strlen ("/") + strlen (PORNVIEW_RC_DIR_THUMBS); base = g_strconcat (home_dir (), "/", PORNVIEW_RC_DIR_THUMBS, dir, NULL); } else { base_length = strlen (home_dir ()) + strlen ("/") + strlen (PORNVIEW_RC_DIR_COMMENTS); base = g_strconcat (home_dir (), "/", PORNVIEW_RC_DIR_COMMENTS, dir, NULL); } if (path_list (base, &flist, &dlist)) { GList *work; work = dlist; while (work) { gchar *path = work->data; if (recursive && strlen (path) > base_length && !cache_maintain_home_dir (type, path + base_length, recursive, clear)) { if (rmdir (path) < 0) { printf ("Unable to delete dir: %s\n", path); } } else { still_have_a_file = TRUE; } work = work->next; } work = flist; while (work) { gchar *path = work->data; gchar *dot; dot = extension_find_dot (path); if (dot) *dot = '\0'; if (clear || (strlen (path) > base_length && !isfile (path + base_length))) { if (dot) *dot = '.'; if (unlink (path) < 0) printf ("failed to delete:%s\n", path); } else { still_have_a_file = TRUE; } work = work->next; } } path_list_free (dlist); path_list_free (flist); g_free (base); return still_have_a_file; }
static gboolean cache_maintain_home_cb(gpointer data) { CMData *cm = data; GList *dlist = NULL; GList *list = NULL; FileData *fd; gboolean just_done = FALSE; gboolean still_have_a_file = TRUE; gsize base_length; const gchar *cache_folder; if (cm->metadata) { cache_folder = get_metadata_cache_dir(); } else { cache_folder = get_thumbnails_cache_dir(); } base_length = strlen(cache_folder); if (!cm->list) { DEBUG_1("purge chk done."); cm->idle_id = 0; cache_maintain_home_stop(cm); return FALSE; } fd = cm->list->data; DEBUG_1("purge chk (%d) \"%s\"", (cm->clear && !cm->metadata), fd->path); if (g_list_find(cm->done_list, fd) == NULL) { cm->done_list = g_list_prepend(cm->done_list, fd); if (filelist_read(fd, &list, &dlist)) { GList *work; just_done = TRUE; still_have_a_file = FALSE; work = list; while (work) { FileData *fd_list = work->data; gchar *path_buf = g_strdup(fd_list->path); gchar *dot; dot = extension_find_dot(path_buf); if (dot) *dot = '\0'; if ((!cm->metadata && cm->clear) || (strlen(path_buf) > base_length && !isfile(path_buf + base_length)) ) { if (dot) *dot = '.'; if (!unlink_file(path_buf)) log_printf("failed to delete:%s\n", path_buf); } else { still_have_a_file = TRUE; } g_free(path_buf); work = work->next; } } } filelist_free(list); cm->list = g_list_concat(dlist, cm->list); if (cm->list && g_list_find(cm->done_list, cm->list->data) != NULL) { /* check if the dir is empty */ if (cm->list->data == fd && just_done) { if (!still_have_a_file && !dlist && cm->list->next && !rmdir_utf8(fd->path)) { log_printf("Unable to delete dir: %s\n", fd->path); } } else { /* must re-check for an empty dir */ if (isempty(fd->path) && cm->list->next && !rmdir_utf8(fd->path)) { log_printf("Unable to delete dir: %s\n", fd->path); } } fd = cm->list->data; cm->done_list = g_list_remove(cm->done_list, fd); cm->list = g_list_remove(cm->list, fd); file_data_unref(fd); } if (cm->list) { const gchar *buf; fd = cm->list->data; if (strlen(fd->path) > base_length) { buf = fd->path + base_length; } else { buf = "..."; } gtk_entry_set_text(GTK_ENTRY(cm->entry), buf); } return TRUE; }
static gint cb_cache_maintain_home (gpointer data) { CMData *cm = data; GList *dlist = NULL; GList *list = NULL; gchar *path; gint just_done = FALSE; gint still_have_a_file = TRUE; gint base_length; if (cm->type == CACHE_THUMBS) base_length = strlen (home_dir ()) + strlen ("/") + strlen (PORNVIEW_RC_DIR_THUMBS); else base_length = strlen (home_dir ()) + strlen ("/") + strlen (PORNVIEW_RC_DIR_COMMENTS); if (!cm->list) { cm->idle_id = -1; cache_maintain_home_close (cm); return FALSE; } path = cm->list->data; if (g_list_find (cm->done_list, path) == NULL) { cm->done_list = g_list_prepend (cm->done_list, path); if (path_list (path, &list, &dlist)) { GList *work; just_done = TRUE; still_have_a_file = FALSE; work = list; while (work) { gchar *path_buf = work->data; gchar *dot; dot = extension_find_dot (path_buf); if (dot) *dot = '\0'; if (cm->clear || (strlen (path_buf) > base_length && !isfile (path_buf + base_length))) { if (dot) *dot = '.'; if (unlink (path_buf) < 0) printf ("failed to delete:%s\n", path_buf); } else { still_have_a_file = TRUE; } work = work->next; } } } path_list_free (list); cm->list = g_list_concat (dlist, cm->list); if (cm->list && g_list_find (cm->done_list, cm->list->data) != NULL) { /* * check if the dir is empty */ if (cm->list->data == path && just_done) { if (!still_have_a_file && !dlist && cm->list->next && rmdir (path) < 0) { printf ("Unable to delete dir: %s\n", path); } } else { /* * must re-check for an empty dir */ path = cm->list->data; if (isempty (path) && cm->list->next && rmdir (path) < 0) { printf ("Unable to delete dir: %s\n", path); } } path = cm->list->data; cm->done_list = g_list_remove (cm->done_list, path); cm->list = g_list_remove (cm->list, path); g_free (path); } if (cm->list) { const gchar *buf; path = cm->list->data; if (strlen (path) > base_length) { buf = path + base_length; } else { buf = "..."; } gtk_entry_set_text (GTK_ENTRY (cm->entry), buf); } return TRUE; }