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 */
44 #include "filefilter.h"
46 *-----------------------------------------------------------------------------
47 * generic file information and manipulation routines (public)
48 *-----------------------------------------------------------------------------
53 void print_term(const gchar *text_utf8)
57 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
58 fputs((text_l) ? text_l : text_utf8, stderr);
62 static void encoding_dialog(const gchar *path)
64 static gboolean warned_user = FALSE;
70 if (warned_user) return;
74 bf = getenv("G_BROKEN_FILENAMES");
76 string = g_string_new("");
77 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
78 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
79 g_string_append(string, "\n");
80 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
82 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
84 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
85 g_string_append(string, "\n");
86 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
87 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
90 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
91 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
92 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
94 if (g_utf8_validate(path, -1, NULL))
95 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
97 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
98 g_string_append(string, "\n");
102 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
103 "locale warning", NULL, TRUE, NULL, NULL);
104 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
106 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
107 _("Filename encoding locale mismatch"), string->str);
109 gtk_widget_show(gd->dialog);
111 g_string_free(string, TRUE);
114 #if GQ_DEBUG_PATH_UTF8
115 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
117 gchar *path_to_utf8(const gchar *path)
121 GError *error = NULL;
123 if (!path) return NULL;
125 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
128 #if GQ_DEBUG_PATH_UTF8
129 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
131 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
134 encoding_dialog(path);
138 /* just let it through, but bad things may happen */
139 utf8 = g_strdup(path);
145 #if GQ_DEBUG_PATH_UTF8
146 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
148 gchar *path_from_utf8(const gchar *utf8)
152 GError *error = NULL;
154 if (!utf8) return NULL;
156 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
159 #if GQ_DEBUG_PATH_UTF8
160 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
162 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
168 /* if invalid UTF-8, text probaby still in original form, so just copy it */
169 path = g_strdup(utf8);
175 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
176 const gchar *homedir(void)
178 static gchar *home = NULL;
181 home = path_to_utf8(getenv("HOME"));
184 home = path_to_utf8(g_get_home_dir());
189 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
191 gchar *dir = getenv(key);
193 if (!dir || dir[0] == '\0')
195 return g_build_filename(homedir(), fallback, NULL);
198 return path_to_utf8(dir);
201 const gchar *xdg_data_home_get(void)
203 static const gchar *xdg_data_home = NULL;
205 if (xdg_data_home) return xdg_data_home;
207 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
209 return xdg_data_home;
212 const gchar *xdg_config_home_get(void)
214 static const gchar *xdg_config_home = NULL;
216 if (xdg_config_home) return xdg_config_home;
218 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
220 return xdg_config_home;
223 const gchar *xdg_cache_home_get(void)
225 static const gchar *xdg_cache_home = NULL;
227 if (xdg_cache_home) return xdg_cache_home;
229 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
231 return xdg_cache_home;
234 const gchar *get_rc_dir(void)
236 static gchar *rc_dir = NULL;
238 if (rc_dir) return rc_dir;
242 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
246 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
252 const gchar *get_collections_dir(void)
254 static gchar *collections_dir = NULL;
256 if (collections_dir) return collections_dir;
260 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
264 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
267 return collections_dir;
270 const gchar *get_trash_dir(void)
272 static gchar *trash_dir = NULL;
274 if (trash_dir) return trash_dir;
278 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
282 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
288 gboolean stat_utf8(const gchar *s, struct stat *st)
293 if (!s) return FALSE;
294 sl = path_from_utf8(s);
295 ret = (stat(sl, st) == 0);
301 gboolean lstat_utf8(const gchar *s, struct stat *st)
306 if (!s) return FALSE;
307 sl = path_from_utf8(s);
308 ret = (lstat(sl, st) == 0);
314 gboolean isname(const gchar *s)
318 return stat_utf8(s, &st);
321 gboolean isfile(const gchar *s)
325 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
328 gboolean isdir(const gchar *s)
332 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
335 gboolean islink(const gchar *s)
339 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
342 gint64 filesize(const gchar *s)
346 if (!stat_utf8(s, &st)) return 0;
350 time_t filetime(const gchar *s)
354 if (!stat_utf8(s, &st)) return 0;
358 gboolean filetime_set(const gchar *s, time_t tval)
360 gboolean ret = FALSE;
367 ut.actime = ut.modtime = tval;
369 sl = path_from_utf8(s);
370 ret = (utime(sl, &ut) == 0);
377 gboolean is_readable_file(const gchar *s)
379 if (!s || !s[0] || !isfile(s)) return FALSE;
380 return access_file(s, R_OK);
383 gboolean access_file(const gchar *s, gint mode)
388 if (!s || !s[0]) return FALSE;
390 sl = path_from_utf8(s);
391 ret = (access(sl, mode) == 0);
397 gboolean unlink_file(const gchar *s)
402 if (!s) return FALSE;
404 sl = path_from_utf8(s);
405 ret = (unlink(sl) == 0);
411 gboolean symlink_utf8(const gchar *source, const gchar *target)
417 if (!source || !target) return FALSE;
419 sl = path_from_utf8(source);
420 tl = path_from_utf8(target);
422 ret = (symlink(sl, tl) == 0);
430 gboolean mkdir_utf8(const gchar *s, gint mode)
435 if (!s) return FALSE;
437 sl = path_from_utf8(s);
438 ret = (mkdir(sl, mode) == 0);
443 gboolean rmdir_utf8(const gchar *s)
448 if (!s) return FALSE;
450 sl = path_from_utf8(s);
451 ret = (rmdir(sl) == 0);
457 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
461 gboolean ret = FALSE;
463 if (!s || !t) return FALSE;
465 sl = path_from_utf8(s);
466 tl = path_from_utf8(t);
468 if (stat(sl, &st) == 0)
474 /* set the dest file attributes to that of source (ignoring errors) */
478 ret = chown(tl, st.st_uid, st.st_gid);
479 /* Ignores chown errors, while still doing chown
480 (so root still can copy files preserving ownership) */
482 if (chmod(tl, st.st_mode) < 0) ret = FALSE;
485 tb.actime = st.st_atime;
486 tb.modtime = st.st_mtime;
487 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
496 /* paths are in filesystem encoding */
497 static gboolean hard_linked(const gchar *a, const gchar *b)
502 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
504 return (sta.st_dev == stb.st_dev &&
505 sta.st_ino == stb.st_ino);
508 gboolean copy_file(const gchar *s, const gchar *t)
514 gchar *randname = NULL;
520 sl = path_from_utf8(s);
521 tl = path_from_utf8(t);
523 if (hard_linked(sl, tl))
529 fi = fopen(sl, "rb");
532 /* First we write to a temporary file, then we rename it on success,
533 and attributes from original file are copied */
534 randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
535 if (!randname) goto end;
537 fd = g_mkstemp(randname);
538 if (fd == -1) goto end;
540 fo = fdopen(fd, "wb");
546 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
548 if (fwrite(buf, sizeof(gchar), b, fo) != b)
555 fclose(fi); fi = NULL;
556 fclose(fo); fo = NULL;
558 if (rename(randname, tl) < 0) {
563 ret = copy_file_attributes(s, t, TRUE, TRUE);
570 if (randname) g_free(randname);
574 gboolean move_file(const gchar *s, const gchar *t)
579 if (!s || !t) return FALSE;
581 sl = path_from_utf8(s);
582 tl = path_from_utf8(t);
583 if (rename(sl, tl) < 0)
585 /* this may have failed because moving a file across filesystems
586 was attempted, so try copy and delete instead */
591 /* err, now we can't delete the source file so return FALSE */
606 gboolean rename_file(const gchar *s, const gchar *t)
611 if (!s || !t) return FALSE;
613 sl = path_from_utf8(s);
614 tl = path_from_utf8(t);
615 ret = (rename(sl, tl) == 0);
622 gchar *get_current_dir(void)
627 pathl = g_get_current_dir();
628 path8 = path_to_utf8(pathl);
634 void string_list_free(GList *list)
636 g_list_foreach(list, (GFunc)g_free, NULL);
640 GList *string_list_copy(const GList *list)
642 GList *new_list = NULL;
643 GList *work = (GList *) list;
652 new_list = g_list_prepend(new_list, g_strdup(path));
655 return g_list_reverse(new_list);
658 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
664 if (!divider) divider = "";
666 unique = g_strconcat(path, ext, NULL);
667 while (isname(unique))
672 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
676 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
690 gchar *unique_filename_simple(const gchar *path)
696 if (!path) return NULL;
698 name = filename_from_path(path);
699 if (!name) return NULL;
701 ext = registered_extension_from_path(name);
705 unique = unique_filename(path, NULL, "_", TRUE);
711 base = remove_extension_from_path(path);
712 unique = unique_filename(base, ext, "_", TRUE);
719 const gchar *filename_from_path(const gchar *path)
723 if (!path) return NULL;
725 base = strrchr(path, G_DIR_SEPARATOR);
726 if (base) return base + 1;
731 gchar *remove_level_from_path(const gchar *path)
735 if (!path) return NULL;
737 base = strrchr(path, G_DIR_SEPARATOR);
738 if (base) return g_strndup(path, strlen(path)-strlen(base));
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)
760 const gchar *reg_ext;
762 if (!path) return NULL;
764 reg_ext = registered_extension_from_path(path);
766 return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
769 void parse_out_relatives(gchar *path)
777 while (path[s] != '\0')
779 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
781 /* /. occurence, let's see more */
784 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
786 /* /./ or /., just skip this part */
790 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
792 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
795 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
800 if (s != t) path[t] = path[s];
805 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
806 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
810 gboolean file_in_path(const gchar *name)
815 gboolean ret = FALSE;
817 if (!name) return FALSE;
818 path = g_strdup(getenv("PATH"));
819 if (!path) return FALSE;
820 namel = path_from_utf8(name);
824 while (p < l && !ret)
828 while (path[e] != ':' && path[e] != '\0') e++;
831 f = g_build_filename(path + p, namel, NULL);
832 if (isfile(f)) ret = TRUE;
842 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
844 if (!path) return FALSE;
848 gchar *npath = g_strdup(path);
854 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
866 DEBUG_1("creating sub dir:%s", npath);
867 if (!mkdir_utf8(npath, mode))
869 log_printf("create dir failed: %s\n", npath);
875 if (!end) p[0] = G_DIR_SEPARATOR;
884 /* does filename utf8 to filesystem encoding first */
885 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
890 pathl = path_from_utf8(path);
891 success = md5_get_digest_from_file(pathl, digest);
898 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
902 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
904 return md5_digest_to_text(digest);
908 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */