2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 - 2012 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
22 #include <sys/param.h>
27 #include <gtk/gtk.h> /* for locale warning dialog */
30 #include "ui_fileops.h"
32 #include "ui_utildlg.h" /* for locale warning dialog */
36 *-----------------------------------------------------------------------------
37 * generic file information and manipulation routines (public)
38 *-----------------------------------------------------------------------------
43 void print_term(const gchar *text_utf8)
47 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
48 fputs((text_l) ? text_l : text_utf8, stderr);
52 static void encoding_dialog(const gchar *path)
54 static gboolean warned_user = FALSE;
60 if (warned_user) return;
64 bf = getenv("G_BROKEN_FILENAMES");
66 string = g_string_new("");
67 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
68 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
69 g_string_append(string, "\n");
70 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
72 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
74 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
75 g_string_append(string, "\n");
76 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
77 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
80 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
81 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
82 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
84 if (g_utf8_validate(path, -1, NULL))
85 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
87 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
88 g_string_append(string, "\n");
92 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
93 "locale warning", NULL, TRUE, NULL, NULL);
94 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
96 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
97 _("Filename encoding locale mismatch"), string->str);
99 gtk_widget_show(gd->dialog);
101 g_string_free(string, TRUE);
104 #if GQ_DEBUG_PATH_UTF8
105 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
107 gchar *path_to_utf8(const gchar *path)
111 GError *error = NULL;
113 if (!path) return NULL;
115 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
118 #if GQ_DEBUG_PATH_UTF8
119 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
121 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
124 encoding_dialog(path);
128 /* just let it through, but bad things may happen */
129 utf8 = g_strdup(path);
135 #if GQ_DEBUG_PATH_UTF8
136 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
138 gchar *path_from_utf8(const gchar *utf8)
142 GError *error = NULL;
144 if (!utf8) return NULL;
146 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
149 #if GQ_DEBUG_PATH_UTF8
150 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
152 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
158 /* if invalid UTF-8, text probaby still in original form, so just copy it */
159 path = g_strdup(utf8);
165 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
166 const gchar *homedir(void)
168 static gchar *home = NULL;
171 home = path_to_utf8(getenv("HOME"));
174 home = path_to_utf8(g_get_home_dir());
179 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
181 gchar *dir = getenv(key);
183 if (!dir || dir[0] == '\0')
185 return g_build_filename(homedir(), fallback, NULL);
188 return path_to_utf8(dir);
191 const gchar *xdg_data_home_get(void)
193 static const gchar *xdg_data_home = NULL;
195 if (xdg_data_home) return xdg_data_home;
197 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
199 return xdg_data_home;
202 const gchar *xdg_config_home_get(void)
204 static const gchar *xdg_config_home = NULL;
206 if (xdg_config_home) return xdg_config_home;
208 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
210 return xdg_config_home;
213 const gchar *xdg_cache_home_get(void)
215 static const gchar *xdg_cache_home = NULL;
217 if (xdg_cache_home) return xdg_cache_home;
219 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
221 return xdg_cache_home;
224 const gchar *get_rc_dir(void)
226 static gchar *rc_dir = NULL;
228 if (rc_dir) return rc_dir;
232 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
236 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
242 const gchar *get_collections_dir(void)
244 static gchar *collections_dir = NULL;
246 if (collections_dir) return collections_dir;
250 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
254 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
257 return collections_dir;
260 const gchar *get_trash_dir(void)
262 static gchar *trash_dir = NULL;
264 if (trash_dir) return trash_dir;
268 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
272 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
278 gboolean stat_utf8(const gchar *s, struct stat *st)
283 if (!s) return FALSE;
284 sl = path_from_utf8(s);
285 ret = (stat(sl, st) == 0);
291 gboolean lstat_utf8(const gchar *s, struct stat *st)
296 if (!s) return FALSE;
297 sl = path_from_utf8(s);
298 ret = (lstat(sl, st) == 0);
304 gboolean isname(const gchar *s)
308 return stat_utf8(s, &st);
311 gboolean isfile(const gchar *s)
315 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
318 gboolean isdir(const gchar *s)
322 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
325 gboolean islink(const gchar *s)
329 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
332 gint64 filesize(const gchar *s)
336 if (!stat_utf8(s, &st)) return 0;
340 time_t filetime(const gchar *s)
344 if (!stat_utf8(s, &st)) return 0;
348 gboolean filetime_set(const gchar *s, time_t tval)
350 gboolean ret = FALSE;
357 ut.actime = ut.modtime = tval;
359 sl = path_from_utf8(s);
360 ret = (utime(sl, &ut) == 0);
367 gboolean is_readable_file(const gchar *s)
369 if (!s || !s[0] || !isfile(s)) return FALSE;
370 return access_file(s, R_OK);
373 gboolean access_file(const gchar *s, gint mode)
378 if (!s || !s[0]) return FALSE;
380 sl = path_from_utf8(s);
381 ret = (access(sl, mode) == 0);
387 gboolean unlink_file(const gchar *s)
392 if (!s) return FALSE;
394 sl = path_from_utf8(s);
395 ret = (unlink(sl) == 0);
401 gboolean symlink_utf8(const gchar *source, const gchar *target)
407 if (!source || !target) return FALSE;
409 sl = path_from_utf8(source);
410 tl = path_from_utf8(target);
412 ret = (symlink(sl, tl) == 0);
420 gboolean mkdir_utf8(const gchar *s, gint mode)
425 if (!s) return FALSE;
427 sl = path_from_utf8(s);
428 ret = (mkdir(sl, mode) == 0);
433 gboolean rmdir_utf8(const gchar *s)
438 if (!s) return FALSE;
440 sl = path_from_utf8(s);
441 ret = (rmdir(sl) == 0);
447 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
451 gboolean ret = FALSE;
453 if (!s || !t) return FALSE;
455 sl = path_from_utf8(s);
456 tl = path_from_utf8(t);
458 if (stat(sl, &st) == 0)
464 /* set the dest file attributes to that of source (ignoring errors) */
468 ret = chown(tl, st.st_uid, st.st_gid);
469 /* Ignores chown errors, while still doing chown
470 (so root still can copy files preserving ownership) */
472 if (chmod(tl, st.st_mode) < 0) ret = FALSE;
475 tb.actime = st.st_atime;
476 tb.modtime = st.st_mtime;
477 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
486 /* paths are in filesystem encoding */
487 static gboolean hard_linked(const gchar *a, const gchar *b)
492 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
494 return (sta.st_dev == stb.st_dev &&
495 sta.st_ino == stb.st_ino);
498 gboolean copy_file(const gchar *s, const gchar *t)
504 gchar *randname = NULL;
510 sl = path_from_utf8(s);
511 tl = path_from_utf8(t);
513 if (hard_linked(sl, tl))
519 fi = fopen(sl, "rb");
522 /* First we write to a temporary file, then we rename it on success,
523 and attributes from original file are copied */
524 randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
525 if (!randname) goto end;
527 fd = g_mkstemp(randname);
528 if (fd == -1) goto end;
530 fo = fdopen(fd, "wb");
536 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
538 if (fwrite(buf, sizeof(gchar), b, fo) != b)
545 fclose(fi); fi = NULL;
546 fclose(fo); fo = NULL;
548 if (rename(randname, tl) < 0) {
553 ret = copy_file_attributes(s, t, TRUE, TRUE);
560 if (randname) g_free(randname);
564 gboolean move_file(const gchar *s, const gchar *t)
569 if (!s || !t) return FALSE;
571 sl = path_from_utf8(s);
572 tl = path_from_utf8(t);
573 if (rename(sl, tl) < 0)
575 /* this may have failed because moving a file across filesystems
576 was attempted, so try copy and delete instead */
581 /* err, now we can't delete the source file so return FALSE */
596 gboolean rename_file(const gchar *s, const gchar *t)
601 if (!s || !t) return FALSE;
603 sl = path_from_utf8(s);
604 tl = path_from_utf8(t);
605 ret = (rename(sl, tl) == 0);
612 gchar *get_current_dir(void)
617 pathl = g_get_current_dir();
618 path8 = path_to_utf8(pathl);
624 void string_list_free(GList *list)
626 g_list_foreach(list, (GFunc)g_free, NULL);
630 GList *string_list_copy(const GList *list)
632 GList *new_list = NULL;
633 GList *work = (GList *) list;
642 new_list = g_list_prepend(new_list, g_strdup(path));
645 return g_list_reverse(new_list);
648 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
654 if (!divider) divider = "";
656 unique = g_strconcat(path, ext, NULL);
657 while (isname(unique))
662 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
666 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
680 gchar *unique_filename_simple(const gchar *path)
686 if (!path) return NULL;
688 name = filename_from_path(path);
689 if (!name) return NULL;
691 ext = extension_from_path(name);
695 unique = unique_filename(path, NULL, "_", TRUE);
701 base = remove_extension_from_path(path);
702 unique = unique_filename(base, ext, "_", TRUE);
709 const gchar *filename_from_path(const gchar *path)
713 if (!path) return NULL;
715 base = strrchr(path, G_DIR_SEPARATOR);
716 if (base) return base + 1;
721 gchar *remove_level_from_path(const gchar *path)
725 if (!path) return NULL;
729 if (path[p] == G_DIR_SEPARATOR) n = p;
734 return g_strndup(path, (gsize) n);
737 const gchar *extension_from_path(const gchar *path)
739 if (!path) return NULL;
740 return strrchr(path, '.');
743 gboolean file_extension_match(const gchar *path, const gchar *ext)
748 if (!path) return FALSE;
749 if (!ext) return TRUE;
755 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
758 gchar *remove_extension_from_path(const gchar *path)
762 if (!path) return NULL;
766 if (path[p] == '.') n = p;
771 return g_strndup(path, (gsize) n);
774 void parse_out_relatives(gchar *path)
782 while (path[s] != '\0')
784 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
786 /* /. occurence, let's see more */
789 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
791 /* /./ or /., just skip this part */
795 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
797 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
800 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
805 if (s != t) path[t] = path[s];
810 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
811 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
815 gboolean file_in_path(const gchar *name)
820 gboolean ret = FALSE;
822 if (!name) return FALSE;
823 path = g_strdup(getenv("PATH"));
824 if (!path) return FALSE;
825 namel = path_from_utf8(name);
829 while (p < l && !ret)
833 while (path[e] != ':' && path[e] != '\0') e++;
836 f = g_build_filename(path + p, namel, NULL);
837 if (isfile(f)) ret = TRUE;
847 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
849 if (!path) return FALSE;
853 gchar *npath = g_strdup(path);
859 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
871 DEBUG_1("creating sub dir:%s", npath);
872 if (!mkdir_utf8(npath, mode))
874 log_printf("create dir failed: %s\n", npath);
880 if (!end) p[0] = G_DIR_SEPARATOR;
889 /* does filename utf8 to filesystem encoding first */
890 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
895 pathl = path_from_utf8(path);
896 success = md5_get_digest_from_file(pathl, digest);
903 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
907 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
909 return md5_digest_to_text(digest);
913 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */