gint64 filesize(const gchar *s) { struct stat st; if (!stat_utf8(s, &st)) return 0; return (gint)st.st_size; }
gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted) { struct stat st; if (!insert_ci) return collection_add(cd, fd, sorted); if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode)) { CollectInfo *ci; ci = collection_info_new_if_not_exists(cd, &st, fd); if (!ci) return FALSE; DEBUG_3("insert in collection: %s", fd->path); cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE); cd->changed = TRUE; collection_window_insert(collection_window_find(cd), ci); return TRUE; } return FALSE; }
time_t filetime(const gchar *s) { struct stat st; if (!stat_utf8(s, &st)) return 0; return st.st_mtime; }
static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data) { ThumbValidate *tv = data; GdkPixbuf *pixbuf; gint valid = FALSE; pixbuf = thumb_loader_std_get_pixbuf(tv->tl, FALSE); if (pixbuf) { const gchar *uri; const gchar *mtime_str; uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI); mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME); if (uri && mtime_str) { if (strncmp(uri, "file:", strlen("file:")) == 0) { struct stat st; gchar *target; target = g_filename_from_uri(uri, NULL, NULL); if (stat(target, &st) == 0 && st.st_mtime == strtol(mtime_str, NULL, 10)) { valid = TRUE; } g_free(target); } else { struct stat st; if (debug) printf("thumb uri foreign, doing day check: %s\n", uri); if (stat_utf8(tv->path, &st)) { time_t now; now = time(NULL); if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60) { valid = TRUE; } } } } g_object_unref(pixbuf); } thumb_loader_std_thumb_file_validate_finish(tv, valid); }
gint thumb_loader_std_start(ThumbLoaderStd *tl, const gchar *path) { static gchar *thumb_cache = NULL; struct stat st; if (!tl || !path) return FALSE; thumb_loader_std_reset(tl); if (!stat_utf8(path, &st)) return FALSE; tl->source_path = g_strdup(path); tl->source_mtime = st.st_mtime; tl->source_size = st.st_size; tl->source_mode = st.st_mode; if (!thumb_cache) thumb_cache = g_strconcat(homedir(), "/", THUMB_FOLDER, NULL); if (strncmp(tl->source_path, thumb_cache, strlen(thumb_cache)) != 0) { gchar *pathl; pathl = path_from_utf8(path); tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL); tl->local_uri = filename_from_path(tl->thumb_uri); g_free(pathl); } if (tl->cache_enable) { gint found; tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE); tl->thumb_path_local = FALSE; found = isfile(tl->thumb_path); if (found && thumb_loader_std_setup(tl, tl->thumb_path)) return TRUE; if (thumb_loader_std_fail_check(tl)) return FALSE; return thumb_loader_std_next_source(tl, found); } if (!thumb_loader_std_setup(tl, tl->source_path)) { thumb_loader_std_save(tl, NULL); return FALSE; } return TRUE; }
FileData *file_data_new_simple(const gchar *path) { FileData *fd; struct stat st; fd = g_new0(FileData, 1); fd->path = g_strdup(path); fd->name = filename_from_path(fd->path); if (stat_utf8(fd->path, &st)) { fd->size = st.st_size; fd->date = st.st_mtime; } fd->pixbuf = NULL; return fd; }
static void info_tab_general_sync_perm(InfoTabGeneral *tab, InfoData *id) { struct stat st; if (!stat_utf8(id->path, &st)) { gtk_label_set_text(GTK_LABEL(tab->label_user), ""); gtk_label_set_text(GTK_LABEL(tab->label_group), ""); gtk_label_set_text(GTK_LABEL(tab->label_perms), ""); } else { struct passwd *user; struct group *grp; gchar pbuf[12]; gchar *pmod; gchar *buf; user = getpwuid(st.st_uid); gtk_label_set_text(GTK_LABEL(tab->label_user), (user) ? user->pw_name : ""); grp = getgrgid(st.st_gid); gtk_label_set_text(GTK_LABEL(tab->label_group), (grp) ? grp->gr_name : ""); pbuf[0] = (st.st_mode & S_IRUSR) ? 'r' : '-'; pbuf[1] = (st.st_mode & S_IWUSR) ? 'w' : '-'; pbuf[2] = (st.st_mode & S_IXUSR) ? 'x' : '-'; pbuf[3] = (st.st_mode & S_IRGRP) ? 'r' : '-'; pbuf[4] = (st.st_mode & S_IWGRP) ? 'w' : '-'; pbuf[5] = (st.st_mode & S_IXGRP) ? 'x' : '-'; pbuf[6] = (st.st_mode & S_IROTH) ? 'r' : '-'; pbuf[7] = (st.st_mode & S_IWOTH) ? 'w' : '-'; pbuf[8] = (st.st_mode & S_IXOTH) ? 'x' : '-'; pbuf[9] = '\0'; pmod = mode_number(st.st_mode); buf = g_strdup_printf("%s (%s)", pbuf, pmod); gtk_label_set_text(GTK_LABEL(tab->label_perms), buf); g_free(buf); g_free(pmod); } }
gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist) { struct stat st; gboolean valid; if (must_exist) { valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode)); } else { valid = TRUE; st.st_size = 0; st.st_mtime = 0; } if (valid) { CollectInfo *ci; ci = collection_info_new_if_not_exists(cd, &st, fd); if (!ci) return FALSE; DEBUG_3("add to collection: %s", fd->path); cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE); cd->changed = TRUE; if (!sorted || cd->sort_method == SORT_NONE) { collection_window_add(collection_window_find(cd), ci); } else { collection_window_insert(collection_window_find(cd), ci); } } return valid; }
gint isdir(const gchar *s) { struct stat st; return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode)); }
gint isfile(const gchar *s) { struct stat st; return (stat_utf8(s, &st) && S_ISREG(st.st_mode)); }
gint isname(const gchar *s) { struct stat st; return stat_utf8(s, &st); }
static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf) { gchar *base_path; gchar *tmp_path; gint fail; if (!tl->cache_enable || tl->cache_hit) return; if (tl->thumb_path) return; if (!pixbuf) { /* local failures are not stored */ if (tl->cache_local) return; pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1); fail = TRUE; } else { g_object_ref(G_OBJECT(pixbuf)); fail = FALSE; } tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail); if (!tl->thumb_path) { g_object_unref(G_OBJECT(pixbuf)); return; } tl->thumb_path_local = tl->cache_local; /* create thumbnail dir if needed */ base_path = remove_level_from_path(tl->thumb_path); if (tl->cache_local) { if (!isdir(base_path)) { struct stat st; gchar *source_base; source_base = remove_level_from_path(tl->source_path); if (stat_utf8(source_base, &st)) { cache_ensure_dir_exists(base_path, st.st_mode); } g_free(source_base); } } else { cache_ensure_dir_exists(base_path, THUMB_PERMS_FOLDER); } g_free(base_path); if (debug) { printf("thumb saving: %s\n", tl->source_path); printf(" saved: %s\n", tl->thumb_path); } /* save thumb, using a temp file then renaming into place */ tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2); if (tmp_path) { const gchar *mark_uri; gchar *mark_app; gchar *mark_mtime; gchar *pathl; gint success; mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri; mark_app = g_strdup_printf("GQview %s", VERSION); mark_mtime = g_strdup_printf("%lu", tl->source_mtime); pathl = path_from_utf8(tmp_path); success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL, THUMB_MARKER_URI, mark_uri, THUMB_MARKER_MTIME, mark_mtime, THUMB_MARKER_APP, mark_app, NULL); if (success) { chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB); success = rename_file(tmp_path, tl->thumb_path); } g_free(pathl); g_free(mark_mtime); g_free(mark_app); g_free(tmp_path); if (!success && debug) { printf("thumb save failed: %s\n", tl->source_path); printf(" thumb: %s\n", tl->thumb_path); } } g_object_unref(G_OBJECT(pixbuf)); }
gboolean vdtree_populate_path_by_iter(ViewDir *vd, GtkTreeIter *iter, gboolean force, FileData *target_fd) { GtkTreeModel *store; GList *list; GList *work; GList *old; time_t current_time; GtkTreeIter child; NodeData *nd; store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); gtk_tree_model_get(store, iter, DIR_COLUMN_POINTER, &nd, -1); if (!nd) return FALSE; current_time = time(NULL); if (nd->expanded) { if (!isdir(nd->fd->path)) { if (vd->click_fd == nd->fd) vd->click_fd = NULL; if (vd->drop_fd == nd->fd) vd->drop_fd = NULL; gtk_tree_store_remove(GTK_TREE_STORE(store), iter); vdtree_node_free(nd); return FALSE; } if (!force && current_time - nd->last_update < 2) { DEBUG_1("Too frequent update of %s", nd->fd->path); return TRUE; } if (nd->fd->version == nd->version) return TRUE; } vdtree_busy_push(vd); filelist_read(nd->fd, NULL, &list); /* when hidden files are not enabled, and the user enters a hidden path, * allow the tree to display that path by specifically inserting the hidden entries */ if (!options->file_filter.show_hidden_files && target_fd && strncmp(nd->fd->path, target_fd->path, strlen(nd->fd->path)) == 0) { gint n; n = strlen(nd->fd->path); if (target_fd->path[n] == G_DIR_SEPARATOR && target_fd->path[n+1] == '.') { gchar *name8; struct stat sbuf; n++; while (target_fd->path[n] != '\0' && target_fd->path[n] != G_DIR_SEPARATOR) n++; name8 = g_strndup(target_fd->path, n); if (stat_utf8(name8, &sbuf)) { list = g_list_prepend(list, file_data_new_simple(name8)); } g_free(name8); } } old = NULL; if (gtk_tree_model_iter_children(store, &child, iter)) { do { NodeData *cnd; gtk_tree_model_get(store, &child, DIR_COLUMN_POINTER, &cnd, -1); old = g_list_prepend(old, cnd); } while (gtk_tree_model_iter_next(store, &child)); } work = list; while (work) { FileData *fd; fd = work->data; work = work->next; if (strcmp(fd->name, ".") == 0 || strcmp(fd->name, "..") == 0) { file_data_unref(fd); } else { NodeData *cnd; cnd = vdtree_find_iter_by_fd(vd, iter, fd, &child); if (cnd) { if (cnd->expanded && cnd->version != fd->version) { vdtree_populate_path_by_iter(vd, &child, FALSE, target_fd); } gtk_tree_store_set(GTK_TREE_STORE(store), &child, DIR_COLUMN_NAME, fd->name, -1); cnd->version = fd->version; old = g_list_remove(old, cnd); file_data_unref(fd); } else { vdtree_add_by_data(vd, fd, iter); } } } work = old; while (work) { NodeData *cnd = work->data; work = work->next; if (vd->click_fd == cnd->fd) vd->click_fd = NULL; if (vd->drop_fd == cnd->fd) vd->drop_fd = NULL; if (vdtree_find_iter_by_data(vd, iter, cnd, &child)) { gtk_tree_store_remove(GTK_TREE_STORE(store), &child); vdtree_node_free(cnd); } } g_list_free(old); g_list_free(list); vdtree_busy_pop(vd); nd->expanded = TRUE; nd->last_update = current_time; return TRUE; }