gint move_file(const gchar *s, const gchar *t) { gchar *sl, *tl; gint ret = TRUE; if (!s || !t) return FALSE; sl = path_from_utf8(s); tl = path_from_utf8(t); if (rename(sl, tl) < 0) { /* this may have failed because moving a file across filesystems was attempted, so try copy and delete instead */ if (copy_file(s, t)) { if (unlink(sl) < 0) { /* err, now we can't delete the source file so return FALSE */ ret = FALSE; } } else { ret = FALSE; } } g_free(sl); g_free(tl); return ret; }
static void metadata_legacy_delete(FileData *fd, const gchar *except) { gchar *metadata_path; gchar *metadata_pathl; if (!fd) return; metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path); if (metadata_path && (!except || strcmp(metadata_path, except) != 0)) { metadata_pathl = path_from_utf8(metadata_path); unlink(metadata_pathl); g_free(metadata_pathl); g_free(metadata_path); } #ifdef HAVE_EXIV2 /* without exiv2: do not delete xmp metadata because we are not able to convert it, just ignore it */ metadata_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path); if (metadata_path && (!except || strcmp(metadata_path, except) != 0)) { metadata_pathl = path_from_utf8(metadata_path); unlink(metadata_pathl); g_free(metadata_pathl); g_free(metadata_path); } #endif }
gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime) { struct stat st; gchar *sl, *tl; gint ret = FALSE; if (!s || !t) return FALSE; sl = path_from_utf8(s); tl = path_from_utf8(t); if (stat(sl, &st) == 0) { struct utimbuf tb; ret = TRUE; /* set the dest file attributes to that of source (ignoring errors) */ if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE; if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE; tb.actime = st.st_atime; tb.modtime = st.st_mtime; if (mtime && utime(tl, &tb) < 0) ret = FALSE; } g_free(sl); g_free(tl); return ret; }
gint copy_file(const gchar *s, const gchar *t) { FILE *fi = NULL; FILE *fo = NULL; gchar *sl, *tl; gchar buf[4096]; gint b; sl = path_from_utf8(s); tl = path_from_utf8(t); if (hard_linked(sl, tl)) { g_free(sl); g_free(tl); return TRUE; } fi = fopen(sl, "rb"); if (fi) { fo = fopen(tl, "wb"); if (!fo) { fclose(fi); fi = NULL; } } g_free(sl); g_free(tl); if (!fi || !fo) return FALSE; while((b = fread(buf, sizeof(char), 4096, fi)) && b != 0) { if (fwrite(buf, sizeof(char), b, fo) != b) { fclose(fi); fclose(fo); return FALSE; } } fclose(fi); fclose(fo); copy_file_attributes(s, t, TRUE, TRUE); return TRUE; }
gint rename_file(const gchar *s, const gchar *t) { gchar *sl, *tl; gint ret; if (!s || !t) return FALSE; sl = path_from_utf8(s); tl = path_from_utf8(t); ret = (rename(sl, tl) == 0); g_free(sl); g_free(tl); return ret; }
static GList *editor_add_desktop_dir(GList *list, const gchar *path) { DIR *dp; struct dirent *dir; gchar *pathl; pathl = path_from_utf8(path); dp = opendir(pathl); g_free(pathl); if (!dp) { /* dir not found */ return list; } while ((dir = readdir(dp)) != NULL) { gchar *namel = dir->d_name; if (g_str_has_suffix(namel, ".desktop")) { gchar *name = path_to_utf8(namel); gchar *dpath = g_build_filename(path, name, NULL); list = g_list_prepend(list, dpath); g_free(name); } } closedir(dp); return list; }
gboolean cache_sim_data_save(CacheData *cd) { SecureSaveInfo *ssi; gchar *pathl; if (!cd || !cd->path) return FALSE; pathl = path_from_utf8(cd->path); ssi = secure_open(pathl); g_free(pathl); if (!ssi) { log_printf("Unable to save sim cache data: %s\n", cd->path); return FALSE; } secure_fprintf(ssi, "SIMcache\n#%s %s\n", PACKAGE, VERSION); cache_sim_write_dimensions(ssi, cd); cache_sim_write_date(ssi, cd); cache_sim_write_checksum(ssi, cd); cache_sim_write_md5sum(ssi, cd); cache_sim_write_similarity(ssi, cd); if (secure_close(ssi)) { log_printf(_("error saving sim cache data: %s\nerror: %s\n"), cd->path, secsave_strerror(secsave_errno)); return FALSE; } return TRUE; }
static gboolean isempty(const gchar *path) { DIR *dp; struct dirent *dir; gchar *pathl; pathl = path_from_utf8(path); dp = opendir(pathl); g_free(pathl); if (!dp) return FALSE; while ((dir = readdir(dp)) != NULL) { gchar *name = dir->d_name; if (!(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) ) { closedir(dp); return FALSE; } } closedir(dp); return TRUE; }
gint collection_load_only_geometry(CollectionData *cd, const gchar *path) { gchar s_buf[2048]; FILE *f; gchar *pathl; if (!path && !cd->path) return FALSE; if (!path) path = cd->path; /* load it */ pathl = path_from_utf8(path); f = fopen(pathl, "r"); g_free(pathl); if (!f) return FALSE; while (fgets(s_buf, sizeof(s_buf), f)) { if (s_buf[0]=='#' && strncmp(s_buf, "#geometry:", 10 ) == 0 && scan_geometry(s_buf + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h) ) { cd->window_read = TRUE; fclose(f); return TRUE; } } fclose(f); return FALSE; }
gchar *get_symbolic_link(const gchar *path_utf8) { gchar *sl; struct stat st; gchar *ret = g_strdup(""); sl = path_from_utf8(path_utf8); if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode)) { gchar *buf; gint l; buf = g_malloc(st.st_size + 1); l = readlink(sl, buf, st.st_size); if (l == st.st_size) { buf[l] = '\0'; ret = buf; } else { g_free(buf); } } g_free(sl); return ret; }
gint file_in_path(const gchar *name) { gchar *path; gchar *namel; gint p, l; gint ret = FALSE; if (!name) return FALSE; path = g_strdup(getenv("PATH")); if (!path) return FALSE; namel = path_from_utf8(name); p = 0; l = strlen(path); while (p < l && !ret) { gchar *f; gint e = p; while (path[e] != ':' && path[e] != '\0') e++; path[e] = '\0'; e++; f = g_strconcat(path + p, "/", namel, NULL); if (isfile(f)) ret = TRUE; g_free(f); p = e; } g_free(namel); g_free(path); return ret; }
static void help_window_load_text(GtkWidget *text, const gchar *path) { gchar *pathl; FILE *f; gchar s_buf[1024]; GtkTextBuffer *buffer; GtkTextIter iter; GtkTextIter start, end; if (!text || !path) return; buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)); gtk_text_buffer_get_bounds(buffer, &start, &end); gtk_text_buffer_delete(buffer, &start, &end); gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0); pathl = path_from_utf8(path); f = fopen(pathl, "r"); g_free(pathl); if (!f) { gchar *buf; buf = g_strdup_printf(_("Unable to load:\n%s"), path); gtk_text_buffer_insert(buffer, &iter, buf, -1); g_free(buf); } else { while (fgets(s_buf, sizeof(s_buf), f)) { gchar *buf; gint l; l = strlen(s_buf); if (!g_utf8_validate(s_buf, l, NULL)) { buf = g_locale_to_utf8(s_buf, l, NULL, NULL, NULL); if (!buf) buf = g_strdup("\n"); } else { buf = NULL; } gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, (buf) ? buf : s_buf, -1, "monospace", NULL); g_free(buf); } fclose(f); } gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0); gtk_text_buffer_place_cursor(buffer, &iter); gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0.0, TRUE, 0, 0); }
gint symlink_utf8(const gchar *source, const gchar *target) { gchar *sl; gchar *tl; gint ret; if (!source || !target) return FALSE; sl = path_from_utf8(source); tl = path_from_utf8(target); ret = (symlink(sl, tl) == 0); g_free(sl); g_free(tl); return ret; }
gint mkdir_utf8(const gchar *s, int mode) { gchar *sl; gint ret; if (!s) return FALSE; sl = path_from_utf8(s); ret = (mkdir(sl, mode) == 0); g_free(sl); return ret; }
gint lstat_utf8(const gchar *s, struct stat *st) { gchar *sl; gint ret; if (!s) return FALSE; sl = path_from_utf8(s); ret = (lstat(sl, st) == 0); g_free(sl); return ret; }
static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpointer data) { TMaintMove *tm = data; GdkPixbuf *pixbuf; pixbuf = thumb_loader_std_get_pixbuf(tm->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 && strcmp(uri, tm->source_uri) == 0) { gchar *pathl; /* The validation utility abuses ThumbLoader, and we * abuse the utility just to load the thumbnail, * but the loader needs to look sane for the save to complete. */ tm->tl->cache_enable = TRUE; tm->tl->cache_hit = FALSE; tm->tl->cache_local = FALSE; g_free(tm->tl->source_path); tm->tl->source_path = g_strdup(tm->dest); tm->tl->source_mtime = strtol(mtime_str, NULL, 10); pathl = path_from_utf8(tm->tl->source_path); g_free(tm->tl->thumb_uri); tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL); tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri); g_free(pathl); g_free(tm->tl->thumb_path); tm->tl->thumb_path = NULL; tm->tl->thumb_path_local = FALSE; if (debug) printf("thumb move attempting save:\n"); thumb_loader_std_save(tm->tl, pixbuf); } if (debug) printf("thumb move unlink: %s\n", tm->thumb_path); unlink_file(tm->thumb_path); } thumb_std_maint_move_step(tm); }
gboolean cache_time_valid(const gchar *cache, const gchar *path) { struct stat cache_st; struct stat path_st; gchar *cachel; gchar *pathl; gboolean ret = FALSE; if (!cache || !path) return FALSE; cachel = path_from_utf8(cache); pathl = path_from_utf8(path); if (stat(cachel, &cache_st) == 0 && stat(pathl, &path_st) == 0) { if (cache_st.st_mtime == path_st.st_mtime) { ret = TRUE; } else if (cache_st.st_mtime > path_st.st_mtime) { struct utimbuf ut; ut.actime = ut.modtime = cache_st.st_mtime; if (utime(cachel, &ut) < 0 && errno == EPERM) { DEBUG_1("cache permission workaround: %s", cachel); ret = TRUE; } } } g_free(pathl); g_free(cachel); return ret; }
gint unlink_file(const gchar *s) { gchar *sl; gint ret; if (!s) return FALSE; sl = path_from_utf8(s); ret = (unlink(sl) == 0); g_free(sl); return ret; }
gint access_file(const gchar *s, int mode) { gchar *sl; gint ret; if (!s) return FALSE; sl = path_from_utf8(s); ret = (access(sl, mode) == 0); g_free(sl); return ret; }
gint rmdir_utf8(const gchar *s) { gchar *sl; gint ret; if (!s) return FALSE; sl = path_from_utf8(s); ret = (rmdir(sl) == 0); g_free(sl); return ret; }
gint pan_is_link_loop(const gchar *s) { gchar *sl; struct stat st; gint ret = FALSE; sl = path_from_utf8(s); if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode)) { gchar *buf; gint l; buf = g_malloc(st.st_size + 1); l = readlink(sl, buf, st.st_size); if (l == st.st_size) { buf[l] = '\0'; parse_out_relatives(buf); l = strlen(buf); parse_out_relatives(sl); if (buf[0] == '/') { if (strncmp(sl, buf, l) == 0 && (sl[l] == '\0' || sl[l] == '/' || l == 1)) ret = TRUE; } else { gchar *link_path; link_path = concat_dir_and_file(sl, buf); parse_out_relatives(link_path); if (strncmp(sl, link_path, l) == 0 && (sl[l] == '\0' || sl[l] == '/' || l == 1)) ret = TRUE; g_free(link_path); } } g_free(buf); } g_free(sl); return ret; }
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; }
static gint thumb_loader_std_fail_check(ThumbLoaderStd *tl) { gchar *fail_path; gint result = FALSE; fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE); if (isfile(fail_path)) { GdkPixbuf *pixbuf; if (tl->cache_retry) { pixbuf = NULL; } else { gchar *pathl; pathl = path_from_utf8(fail_path); pixbuf = gdk_pixbuf_new_from_file(pathl, NULL); g_free(pathl); } if (pixbuf) { const gchar *mtime_str; mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME); if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime) { result = TRUE; if (debug) { printf("thumb fail valid: %s\n", tl->source_path); printf(" thumb: %s\n", fail_path); } } g_object_unref(G_OBJECT(pixbuf)); } if (!result) unlink_file(fail_path); } g_free(fail_path); return result; }
gint filetime_set(const gchar *s, time_t tval) { gint ret = FALSE; if (tval > 0) { struct utimbuf ut; gchar *sl; ut.actime = ut.modtime = tval; sl = path_from_utf8(s); ret = (utime(sl, &ut) == 0); g_free(sl); } return ret; }
static void tab_completion_read_dir(TabCompData *td, const gchar *path) { DIR *dp; struct dirent *dir; GList *list = NULL; gchar *pathl; tab_completion_free_list(td); pathl = path_from_utf8(path); dp = opendir(pathl); if (!dp) { /* dir not found */ g_free(pathl); return; } while ((dir = readdir(dp)) != NULL) { gchar *name = dir->d_name; if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0 && (name[0] != '.' || options->file_filter.show_hidden_files)) { gchar *abspath = g_build_filename(pathl, name, NULL); if (g_file_test(abspath, G_FILE_TEST_IS_DIR)) { gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL); list = g_list_prepend(list, path_to_utf8(dname)); g_free(dname); } else { list = g_list_prepend(list, path_to_utf8(name)); } g_free(abspath); } } closedir(dp); td->dir_path = g_strdup(path); td->file_list = list; g_free(pathl); }
/* this also removes local thumbnails (the source is gone so it makes sense) */ void thumb_std_maint_removed(const gchar *source) { gchar *uri; gchar *sourcel; sourcel = path_from_utf8(source); uri = g_filename_to_uri(sourcel, NULL, NULL); g_free(sourcel); /* all this to remove a thumbnail? */ thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL); thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE); thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL); thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL); thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE); g_free(uri); }
static gboolean metadata_legacy_read(FileData *fd, GList **keywords, gchar **comment) { gchar *metadata_path; gchar *metadata_pathl; gboolean success = FALSE; if (!fd) return FALSE; metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path); if (!metadata_path) return FALSE; metadata_pathl = path_from_utf8(metadata_path); success = metadata_file_read(metadata_pathl, keywords, comment); g_free(metadata_pathl); g_free(metadata_path); return success; }
long checksum_simple(const gchar *path) { gchar *path8; FILE *f; long sum = 0; gint c; path8 = path_from_utf8(path); f = fopen(path8, "r"); g_free(path8); if (!f) return -1; while((c = fgetc(f)) != EOF) { sum += c; } fclose(f); return sum; }
static gint thumb_std_maint_move_idle(gpointer data) { TMaintMove *tm; gchar *pathl; if (!thumb_std_maint_move_list) return FALSE; tm = thumb_std_maint_move_list->data; thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm); if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL; pathl = path_from_utf8(tm->source); tm->source_uri = g_filename_to_uri(pathl, NULL, NULL); g_free(pathl); tm->pass = 0; thumb_std_maint_move_step(tm); return FALSE; }
static gboolean metadata_legacy_write(FileData *fd) { gboolean success = FALSE; gchar *metadata_pathl; gpointer keywords; gpointer comment_l; gboolean have_keywords; gboolean have_comment; const gchar *comment; GList *orig_keywords = NULL; gchar *orig_comment = NULL; g_assert(fd->change && fd->change->dest); DEBUG_1("Saving comment: %s", fd->change->dest); if (!fd->modified_xmp) return TRUE; metadata_pathl = path_from_utf8(fd->change->dest); have_keywords = g_hash_table_lookup_extended(fd->modified_xmp, KEYWORD_KEY, NULL, &keywords); have_comment = g_hash_table_lookup_extended(fd->modified_xmp, COMMENT_KEY, NULL, &comment_l); comment = (have_comment && comment_l) ? ((GList *)comment_l)->data : NULL; if (!have_keywords || !have_comment) metadata_file_read(metadata_pathl, &orig_keywords, &orig_comment); success = metadata_file_write(metadata_pathl, have_keywords ? (GList *)keywords : orig_keywords, have_comment ? comment : orig_comment); g_free(metadata_pathl); g_free(orig_comment); string_list_free(orig_keywords); return success; }