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"
45 #include "secure_save.h"
48 *-----------------------------------------------------------------------------
49 * generic file information and manipulation routines (public)
50 *-----------------------------------------------------------------------------
55 void print_term(const gchar *text_utf8)
59 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
60 if(command_line && command_line->ssi)
61 secure_fputs(command_line->ssi, (text_l) ? text_l : text_utf8);
65 static void encoding_dialog(const gchar *path)
67 static gboolean warned_user = FALSE;
73 if (warned_user) return;
77 bf = getenv("G_BROKEN_FILENAMES");
79 string = g_string_new("");
80 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
81 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
82 g_string_append(string, "\n");
83 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
85 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
87 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
88 g_string_append(string, "\n");
89 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
90 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
93 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
94 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
95 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
97 if (g_utf8_validate(path, -1, NULL))
98 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
100 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
101 g_string_append(string, "\n");
105 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
106 "locale warning", NULL, TRUE, NULL, NULL);
107 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
109 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
110 _("Filename encoding locale mismatch"), string->str, TRUE);
112 gtk_widget_show(gd->dialog);
114 g_string_free(string, TRUE);
117 #if GQ_DEBUG_PATH_UTF8
118 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
120 gchar *path_to_utf8(const gchar *path)
124 GError *error = NULL;
126 if (!path) return NULL;
128 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
131 #if GQ_DEBUG_PATH_UTF8
132 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
134 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
137 encoding_dialog(path);
141 /* just let it through, but bad things may happen */
142 utf8 = g_strdup(path);
148 #if GQ_DEBUG_PATH_UTF8
149 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
151 gchar *path_from_utf8(const gchar *utf8)
155 GError *error = NULL;
157 if (!utf8) return NULL;
159 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
162 #if GQ_DEBUG_PATH_UTF8
163 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
165 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
171 /* if invalid UTF-8, text probaby still in original form, so just copy it */
172 path = g_strdup(utf8);
178 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
179 const gchar *homedir(void)
181 static gchar *home = NULL;
184 home = path_to_utf8(getenv("HOME"));
187 home = path_to_utf8(g_get_home_dir());
192 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
194 gchar *dir = getenv(key);
196 if (!dir || dir[0] == '\0')
198 return g_build_filename(homedir(), fallback, NULL);
201 return path_to_utf8(dir);
204 const gchar *xdg_data_home_get(void)
206 static const gchar *xdg_data_home = NULL;
208 if (xdg_data_home) return xdg_data_home;
210 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
212 return xdg_data_home;
215 const gchar *xdg_config_home_get(void)
217 static const gchar *xdg_config_home = NULL;
219 if (xdg_config_home) return xdg_config_home;
221 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
223 return xdg_config_home;
226 const gchar *xdg_cache_home_get(void)
228 static const gchar *xdg_cache_home = NULL;
230 if (xdg_cache_home) return xdg_cache_home;
232 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
234 return xdg_cache_home;
237 const gchar *get_rc_dir(void)
239 static gchar *rc_dir = NULL;
241 if (rc_dir) return rc_dir;
245 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
249 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
255 const gchar *get_collections_dir(void)
257 static gchar *collections_dir = NULL;
259 if (collections_dir) return collections_dir;
263 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
267 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
270 return collections_dir;
273 const gchar *get_trash_dir(void)
275 static gchar *trash_dir = NULL;
277 if (trash_dir) return trash_dir;
281 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
285 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
291 gboolean stat_utf8(const gchar *s, struct stat *st)
296 if (!s) return FALSE;
297 sl = path_from_utf8(s);
298 ret = (stat(sl, st) == 0);
304 gboolean lstat_utf8(const gchar *s, struct stat *st)
309 if (!s) return FALSE;
310 sl = path_from_utf8(s);
311 ret = (lstat(sl, st) == 0);
317 gboolean isname(const gchar *s)
321 return stat_utf8(s, &st);
324 gboolean isfile(const gchar *s)
328 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
331 gboolean isdir(const gchar *s)
335 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
338 gboolean islink(const gchar *s)
342 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
345 gint64 filesize(const gchar *s)
349 if (!stat_utf8(s, &st)) return 0;
353 time_t filetime(const gchar *s)
357 if (!stat_utf8(s, &st)) return 0;
361 gboolean filetime_set(const gchar *s, time_t tval)
363 gboolean ret = FALSE;
370 ut.actime = ut.modtime = tval;
372 sl = path_from_utf8(s);
373 ret = (utime(sl, &ut) == 0);
380 gboolean is_readable_file(const gchar *s)
382 if (!s || !s[0] || !isfile(s)) return FALSE;
383 return access_file(s, R_OK);
386 gboolean access_file(const gchar *s, gint mode)
391 if (!s || !s[0]) return FALSE;
393 sl = path_from_utf8(s);
394 ret = (access(sl, mode) == 0);
400 gboolean unlink_file(const gchar *s)
405 if (!s) return FALSE;
407 sl = path_from_utf8(s);
408 ret = (unlink(sl) == 0);
414 gboolean symlink_utf8(const gchar *source, const gchar *target)
420 if (!source || !target) return FALSE;
422 sl = path_from_utf8(source);
423 tl = path_from_utf8(target);
425 ret = (symlink(sl, tl) == 0);
433 gboolean mkdir_utf8(const gchar *s, gint mode)
438 if (!s) return FALSE;
440 sl = path_from_utf8(s);
441 ret = (mkdir(sl, mode) == 0);
446 gboolean rmdir_utf8(const gchar *s)
451 if (!s) return FALSE;
453 sl = path_from_utf8(s);
454 ret = (rmdir(sl) == 0);
460 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
464 gboolean ret = FALSE;
466 if (!s || !t) return FALSE;
468 sl = path_from_utf8(s);
469 tl = path_from_utf8(t);
471 if (stat(sl, &st) == 0)
477 /* set the dest file attributes to that of source (ignoring errors) */
481 ret = chown(tl, st.st_uid, st.st_gid);
482 /* Ignores chown errors, while still doing chown
483 (so root still can copy files preserving ownership) */
485 if (chmod(tl, st.st_mode) < 0) {
487 if (stat(tl, &st2) != 0 || st2.st_mode != st.st_mode) {
493 tb.actime = st.st_atime;
494 tb.modtime = st.st_mtime;
495 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
504 /* paths are in filesystem encoding */
505 static gboolean hard_linked(const gchar *a, const gchar *b)
510 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
512 return (sta.st_dev == stb.st_dev &&
513 sta.st_ino == stb.st_ino);
516 gboolean copy_file(const gchar *s, const gchar *t)
522 gchar *randname = NULL;
528 sl = path_from_utf8(s);
529 tl = path_from_utf8(t);
531 if (hard_linked(sl, tl))
537 fi = fopen(sl, "rb");
540 /* First we write to a temporary file, then we rename it on success,
541 and attributes from original file are copied */
542 randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
543 if (!randname) goto end;
545 fd = g_mkstemp(randname);
546 if (fd == -1) goto end;
548 fo = fdopen(fd, "wb");
554 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
556 if (fwrite(buf, sizeof(gchar), b, fo) != b)
563 fclose(fi); fi = NULL;
564 fclose(fo); fo = NULL;
566 if (rename(randname, tl) < 0) {
571 ret = copy_file_attributes(s, t, TRUE, TRUE);
578 if (randname) g_free(randname);
582 gboolean move_file(const gchar *s, const gchar *t)
587 if (!s || !t) return FALSE;
589 sl = path_from_utf8(s);
590 tl = path_from_utf8(t);
591 if (rename(sl, tl) < 0)
593 /* this may have failed because moving a file across filesystems
594 was attempted, so try copy and delete instead */
599 /* err, now we can't delete the source file so return FALSE */
614 gboolean rename_file(const gchar *s, const gchar *t)
619 if (!s || !t) return FALSE;
621 sl = path_from_utf8(s);
622 tl = path_from_utf8(t);
623 ret = (rename(sl, tl) == 0);
630 gchar *get_current_dir(void)
635 pathl = g_get_current_dir();
636 path8 = path_to_utf8(pathl);
642 void string_list_free(GList *list)
644 g_list_foreach(list, (GFunc)g_free, NULL);
648 GList *string_list_copy(const GList *list)
650 GList *new_list = NULL;
651 GList *work = (GList *) list;
660 new_list = g_list_prepend(new_list, g_strdup(path));
663 return g_list_reverse(new_list);
666 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
672 if (!divider) divider = "";
674 unique = g_strconcat(path, ext, NULL);
675 while (isname(unique))
680 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
684 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
698 gchar *unique_filename_simple(const gchar *path)
704 if (!path) return NULL;
706 name = filename_from_path(path);
707 if (!name) return NULL;
709 ext = registered_extension_from_path(name);
713 unique = unique_filename(path, NULL, "_", TRUE);
719 base = remove_extension_from_path(path);
720 unique = unique_filename(base, ext, "_", TRUE);
727 const gchar *filename_from_path(const gchar *path)
731 if (!path) return NULL;
733 base = strrchr(path, G_DIR_SEPARATOR);
734 if (base) return base + 1;
739 gchar *remove_level_from_path(const gchar *path)
743 if (!path) return NULL;
745 base = strrchr(path, G_DIR_SEPARATOR);
746 if (base) return g_strndup(path, strlen(path)-strlen(base));
751 gboolean file_extension_match(const gchar *path, const gchar *ext)
756 if (!path) return FALSE;
757 if (!ext) return TRUE;
763 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
766 gchar *remove_extension_from_path(const gchar *path)
768 const gchar *reg_ext;
770 if (!path) return NULL;
772 reg_ext = registered_extension_from_path(path);
774 return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
777 void parse_out_relatives(gchar *path)
785 while (path[s] != '\0')
787 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
789 /* /. occurence, let's see more */
792 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
794 /* /./ or /., just skip this part */
798 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
800 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
803 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
808 if (s != t) path[t] = path[s];
813 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
814 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
818 gboolean file_in_path(const gchar *name)
823 gboolean ret = FALSE;
825 if (!name) return FALSE;
826 path = g_strdup(getenv("PATH"));
827 if (!path) return FALSE;
828 namel = path_from_utf8(name);
832 while (p < l && !ret)
836 while (path[e] != ':' && path[e] != '\0') e++;
839 f = g_build_filename(path + p, namel, NULL);
840 if (isfile(f)) ret = TRUE;
850 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
852 if (!path) return FALSE;
856 gchar *npath = g_strdup(path);
862 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
874 DEBUG_1("creating sub dir:%s", npath);
875 if (!mkdir_utf8(npath, mode))
877 log_printf("create dir failed: %s\n", npath);
883 if (!end) p[0] = G_DIR_SEPARATOR;
892 /* does filename utf8 to filesystem encoding first */
893 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
898 pathl = path_from_utf8(path);
899 success = md5_get_digest_from_file(pathl, digest);
906 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
910 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
912 return md5_digest_to_text(digest);
916 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */