2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/param.h>
36 #include <gtk/gtk.h> /* for locale warning dialog */
39 #include "ui_fileops.h"
41 #include "ui_utildlg.h" /* for locale warning dialog */
45 *-----------------------------------------------------------------------------
46 * generic file information and manipulation routines (public)
47 *-----------------------------------------------------------------------------
52 void print_term(const gchar *text_utf8)
56 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
57 fputs((text_l) ? text_l : text_utf8, stderr);
61 static void encoding_dialog(const gchar *path)
63 static gboolean warned_user = FALSE;
69 if (warned_user) return;
73 bf = getenv("G_BROKEN_FILENAMES");
75 string = g_string_new("");
76 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
77 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
78 g_string_append(string, "\n");
79 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
81 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
83 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
84 g_string_append(string, "\n");
85 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
86 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
89 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
90 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
91 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
93 if (g_utf8_validate(path, -1, NULL))
94 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
96 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
97 g_string_append(string, "\n");
101 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
102 "locale warning", NULL, TRUE, NULL, NULL);
103 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
105 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
106 _("Filename encoding locale mismatch"), string->str);
108 gtk_widget_show(gd->dialog);
110 g_string_free(string, TRUE);
113 #if GQ_DEBUG_PATH_UTF8
114 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
116 gchar *path_to_utf8(const gchar *path)
120 GError *error = NULL;
122 if (!path) return NULL;
124 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
127 #if GQ_DEBUG_PATH_UTF8
128 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
130 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
133 encoding_dialog(path);
137 /* just let it through, but bad things may happen */
138 utf8 = g_strdup(path);
144 #if GQ_DEBUG_PATH_UTF8
145 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
147 gchar *path_from_utf8(const gchar *utf8)
151 GError *error = NULL;
153 if (!utf8) return NULL;
155 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
158 #if GQ_DEBUG_PATH_UTF8
159 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
161 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
167 /* if invalid UTF-8, text probaby still in original form, so just copy it */
168 path = g_strdup(utf8);
174 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
175 const gchar *homedir(void)
177 static gchar *home = NULL;
180 home = path_to_utf8(getenv("HOME"));
183 home = path_to_utf8(g_get_home_dir());
188 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
190 gchar *dir = getenv(key);
192 if (!dir || dir[0] == '\0')
194 return g_build_filename(homedir(), fallback, NULL);
197 return path_to_utf8(dir);
200 const gchar *xdg_data_home_get(void)
202 static const gchar *xdg_data_home = NULL;
204 if (xdg_data_home) return xdg_data_home;
206 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
208 return xdg_data_home;
211 const gchar *xdg_config_home_get(void)
213 static const gchar *xdg_config_home = NULL;
215 if (xdg_config_home) return xdg_config_home;
217 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
219 return xdg_config_home;
222 const gchar *xdg_cache_home_get(void)
224 static const gchar *xdg_cache_home = NULL;
226 if (xdg_cache_home) return xdg_cache_home;
228 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
230 return xdg_cache_home;
233 const gchar *get_rc_dir(void)
235 static gchar *rc_dir = NULL;
237 if (rc_dir) return rc_dir;
241 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
245 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
251 const gchar *get_collections_dir(void)
253 static gchar *collections_dir = NULL;
255 if (collections_dir) return collections_dir;
259 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
263 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
266 return collections_dir;
269 const gchar *get_trash_dir(void)
271 static gchar *trash_dir = NULL;
273 if (trash_dir) return trash_dir;
277 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
281 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
287 gboolean stat_utf8(const gchar *s, struct stat *st)
292 if (!s) return FALSE;
293 sl = path_from_utf8(s);
294 ret = (stat(sl, st) == 0);
300 gboolean lstat_utf8(const gchar *s, struct stat *st)
305 if (!s) return FALSE;
306 sl = path_from_utf8(s);
307 ret = (lstat(sl, st) == 0);
313 gboolean isname(const gchar *s)
317 return stat_utf8(s, &st);
320 gboolean isfile(const gchar *s)
324 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
327 gboolean isdir(const gchar *s)
331 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
334 gboolean islink(const gchar *s)
338 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
341 gint64 filesize(const gchar *s)
345 if (!stat_utf8(s, &st)) return 0;
349 time_t filetime(const gchar *s)
353 if (!stat_utf8(s, &st)) return 0;
357 gboolean filetime_set(const gchar *s, time_t tval)
359 gboolean ret = FALSE;
366 ut.actime = ut.modtime = tval;
368 sl = path_from_utf8(s);
369 ret = (utime(sl, &ut) == 0);
376 gboolean is_readable_file(const gchar *s)
378 if (!s || !s[0] || !isfile(s)) return FALSE;
379 return access_file(s, R_OK);
382 gboolean access_file(const gchar *s, gint mode)
387 if (!s || !s[0]) return FALSE;
389 sl = path_from_utf8(s);
390 ret = (access(sl, mode) == 0);
396 gboolean unlink_file(const gchar *s)
401 if (!s) return FALSE;
403 sl = path_from_utf8(s);
404 ret = (unlink(sl) == 0);
410 gboolean symlink_utf8(const gchar *source, const gchar *target)
416 if (!source || !target) return FALSE;
418 sl = path_from_utf8(source);
419 tl = path_from_utf8(target);
421 ret = (symlink(sl, tl) == 0);
429 gboolean mkdir_utf8(const gchar *s, gint mode)
434 if (!s) return FALSE;
436 sl = path_from_utf8(s);
437 ret = (mkdir(sl, mode) == 0);
442 gboolean rmdir_utf8(const gchar *s)
447 if (!s) return FALSE;
449 sl = path_from_utf8(s);
450 ret = (rmdir(sl) == 0);
456 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
460 gboolean ret = FALSE;
462 if (!s || !t) return FALSE;
464 sl = path_from_utf8(s);
465 tl = path_from_utf8(t);
467 if (stat(sl, &st) == 0)
473 /* set the dest file attributes to that of source (ignoring errors) */
477 ret = chown(tl, st.st_uid, st.st_gid);
478 /* Ignores chown errors, while still doing chown
479 (so root still can copy files preserving ownership) */
481 if (chmod(tl, st.st_mode) < 0) ret = FALSE;
484 tb.actime = st.st_atime;
485 tb.modtime = st.st_mtime;
486 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
495 /* paths are in filesystem encoding */
496 static gboolean hard_linked(const gchar *a, const gchar *b)
501 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
503 return (sta.st_dev == stb.st_dev &&
504 sta.st_ino == stb.st_ino);
507 gboolean copy_file(const gchar *s, const gchar *t)
513 gchar *randname = NULL;
519 sl = path_from_utf8(s);
520 tl = path_from_utf8(t);
522 if (hard_linked(sl, tl))
528 fi = fopen(sl, "rb");
531 /* First we write to a temporary file, then we rename it on success,
532 and attributes from original file are copied */
533 randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
534 if (!randname) goto end;
536 fd = g_mkstemp(randname);
537 if (fd == -1) goto end;
539 fo = fdopen(fd, "wb");
545 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
547 if (fwrite(buf, sizeof(gchar), b, fo) != b)
554 fclose(fi); fi = NULL;
555 fclose(fo); fo = NULL;
557 if (rename(randname, tl) < 0) {
562 ret = copy_file_attributes(s, t, TRUE, TRUE);
569 if (randname) g_free(randname);
573 gboolean move_file(const gchar *s, const gchar *t)
578 if (!s || !t) return FALSE;
580 sl = path_from_utf8(s);
581 tl = path_from_utf8(t);
582 if (rename(sl, tl) < 0)
584 /* this may have failed because moving a file across filesystems
585 was attempted, so try copy and delete instead */
590 /* err, now we can't delete the source file so return FALSE */
605 gboolean rename_file(const gchar *s, const gchar *t)
610 if (!s || !t) return FALSE;
612 sl = path_from_utf8(s);
613 tl = path_from_utf8(t);
614 ret = (rename(sl, tl) == 0);
621 gchar *get_current_dir(void)
626 pathl = g_get_current_dir();
627 path8 = path_to_utf8(pathl);
633 void string_list_free(GList *list)
635 g_list_foreach(list, (GFunc)g_free, NULL);
639 GList *string_list_copy(const GList *list)
641 GList *new_list = NULL;
642 GList *work = (GList *) list;
651 new_list = g_list_prepend(new_list, g_strdup(path));
654 return g_list_reverse(new_list);
657 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
663 if (!divider) divider = "";
665 unique = g_strconcat(path, ext, NULL);
666 while (isname(unique))
671 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
675 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
689 gchar *unique_filename_simple(const gchar *path)
695 if (!path) return NULL;
697 name = filename_from_path(path);
698 if (!name) return NULL;
700 ext = extension_from_path(name);
704 unique = unique_filename(path, NULL, "_", TRUE);
710 base = remove_extension_from_path(path);
711 unique = unique_filename(base, ext, "_", TRUE);
718 const gchar *filename_from_path(const gchar *path)
722 if (!path) return NULL;
724 base = strrchr(path, G_DIR_SEPARATOR);
725 if (base) return base + 1;
730 gchar *remove_level_from_path(const gchar *path)
734 if (!path) return NULL;
738 if (path[p] == G_DIR_SEPARATOR) n = p;
743 return g_strndup(path, (gsize) n);
746 const gchar *extension_from_path(const gchar *path)
748 if (!path) return NULL;
749 return strrchr(path, '.');
752 gboolean file_extension_match(const gchar *path, const gchar *ext)
757 if (!path) return FALSE;
758 if (!ext) return TRUE;
764 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
767 gchar *remove_extension_from_path(const gchar *path)
771 if (!path) return NULL;
775 if (path[p] == '.') n = p;
780 return g_strndup(path, (gsize) n);
783 void parse_out_relatives(gchar *path)
791 while (path[s] != '\0')
793 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
795 /* /. occurence, let's see more */
798 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
800 /* /./ or /., just skip this part */
804 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
806 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
809 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
814 if (s != t) path[t] = path[s];
819 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
820 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
824 gboolean file_in_path(const gchar *name)
829 gboolean ret = FALSE;
831 if (!name) return FALSE;
832 path = g_strdup(getenv("PATH"));
833 if (!path) return FALSE;
834 namel = path_from_utf8(name);
838 while (p < l && !ret)
842 while (path[e] != ':' && path[e] != '\0') e++;
845 f = g_build_filename(path + p, namel, NULL);
846 if (isfile(f)) ret = TRUE;
856 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
858 if (!path) return FALSE;
862 gchar *npath = g_strdup(path);
868 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
880 DEBUG_1("creating sub dir:%s", npath);
881 if (!mkdir_utf8(npath, mode))
883 log_printf("create dir failed: %s\n", npath);
889 if (!end) p[0] = G_DIR_SEPARATOR;
898 /* does filename utf8 to filesystem encoding first */
899 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
904 pathl = path_from_utf8(path);
905 success = md5_get_digest_from_file(pathl, digest);
912 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
916 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
918 return md5_digest_to_text(digest);
922 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */