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 fputs((text_l) ? text_l : text_utf8, stderr);
61 if(command_line && command_line->ssi)
62 secure_fputs(command_line->ssi, (text_l) ? text_l : text_utf8);
66 static void encoding_dialog(const gchar *path)
68 static gboolean warned_user = FALSE;
74 if (warned_user) return;
78 bf = getenv("G_BROKEN_FILENAMES");
80 string = g_string_new("");
81 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
82 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
83 g_string_append(string, "\n");
84 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
86 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
88 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
89 g_string_append(string, "\n");
90 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
91 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
94 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
95 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
96 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
98 if (g_utf8_validate(path, -1, NULL))
99 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
101 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
102 g_string_append(string, "\n");
106 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
107 "locale warning", NULL, TRUE, NULL, NULL);
108 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
110 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
111 _("Filename encoding locale mismatch"), string->str);
113 gtk_widget_show(gd->dialog);
115 g_string_free(string, TRUE);
118 #if GQ_DEBUG_PATH_UTF8
119 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
121 gchar *path_to_utf8(const gchar *path)
125 GError *error = NULL;
127 if (!path) return NULL;
129 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
132 #if GQ_DEBUG_PATH_UTF8
133 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
135 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
138 encoding_dialog(path);
142 /* just let it through, but bad things may happen */
143 utf8 = g_strdup(path);
149 #if GQ_DEBUG_PATH_UTF8
150 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
152 gchar *path_from_utf8(const gchar *utf8)
156 GError *error = NULL;
158 if (!utf8) return NULL;
160 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
163 #if GQ_DEBUG_PATH_UTF8
164 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
166 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
172 /* if invalid UTF-8, text probaby still in original form, so just copy it */
173 path = g_strdup(utf8);
179 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
180 const gchar *homedir(void)
182 static gchar *home = NULL;
185 home = path_to_utf8(getenv("HOME"));
188 home = path_to_utf8(g_get_home_dir());
193 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
195 gchar *dir = getenv(key);
197 if (!dir || dir[0] == '\0')
199 return g_build_filename(homedir(), fallback, NULL);
202 return path_to_utf8(dir);
205 const gchar *xdg_data_home_get(void)
207 static const gchar *xdg_data_home = NULL;
209 if (xdg_data_home) return xdg_data_home;
211 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
213 return xdg_data_home;
216 const gchar *xdg_config_home_get(void)
218 static const gchar *xdg_config_home = NULL;
220 if (xdg_config_home) return xdg_config_home;
222 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
224 return xdg_config_home;
227 const gchar *xdg_cache_home_get(void)
229 static const gchar *xdg_cache_home = NULL;
231 if (xdg_cache_home) return xdg_cache_home;
233 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
235 return xdg_cache_home;
238 const gchar *get_rc_dir(void)
240 static gchar *rc_dir = NULL;
242 if (rc_dir) return rc_dir;
246 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
250 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
256 const gchar *get_collections_dir(void)
258 static gchar *collections_dir = NULL;
260 if (collections_dir) return collections_dir;
264 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
268 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
271 return collections_dir;
274 const gchar *get_trash_dir(void)
276 static gchar *trash_dir = NULL;
278 if (trash_dir) return trash_dir;
282 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
286 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
292 gboolean stat_utf8(const gchar *s, struct stat *st)
297 if (!s) return FALSE;
298 sl = path_from_utf8(s);
299 ret = (stat(sl, st) == 0);
305 gboolean lstat_utf8(const gchar *s, struct stat *st)
310 if (!s) return FALSE;
311 sl = path_from_utf8(s);
312 ret = (lstat(sl, st) == 0);
318 gboolean isname(const gchar *s)
322 return stat_utf8(s, &st);
325 gboolean isfile(const gchar *s)
329 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
332 gboolean isdir(const gchar *s)
336 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
339 gboolean islink(const gchar *s)
343 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
346 gint64 filesize(const gchar *s)
350 if (!stat_utf8(s, &st)) return 0;
354 time_t filetime(const gchar *s)
358 if (!stat_utf8(s, &st)) return 0;
362 gboolean filetime_set(const gchar *s, time_t tval)
364 gboolean ret = FALSE;
371 ut.actime = ut.modtime = tval;
373 sl = path_from_utf8(s);
374 ret = (utime(sl, &ut) == 0);
381 gboolean is_readable_file(const gchar *s)
383 if (!s || !s[0] || !isfile(s)) return FALSE;
384 return access_file(s, R_OK);
387 gboolean access_file(const gchar *s, gint mode)
392 if (!s || !s[0]) return FALSE;
394 sl = path_from_utf8(s);
395 ret = (access(sl, mode) == 0);
401 gboolean unlink_file(const gchar *s)
406 if (!s) return FALSE;
408 sl = path_from_utf8(s);
409 ret = (unlink(sl) == 0);
415 gboolean symlink_utf8(const gchar *source, const gchar *target)
421 if (!source || !target) return FALSE;
423 sl = path_from_utf8(source);
424 tl = path_from_utf8(target);
426 ret = (symlink(sl, tl) == 0);
434 gboolean mkdir_utf8(const gchar *s, gint mode)
439 if (!s) return FALSE;
441 sl = path_from_utf8(s);
442 ret = (mkdir(sl, mode) == 0);
447 gboolean rmdir_utf8(const gchar *s)
452 if (!s) return FALSE;
454 sl = path_from_utf8(s);
455 ret = (rmdir(sl) == 0);
461 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
465 gboolean ret = FALSE;
467 if (!s || !t) return FALSE;
469 sl = path_from_utf8(s);
470 tl = path_from_utf8(t);
472 if (stat(sl, &st) == 0)
478 /* set the dest file attributes to that of source (ignoring errors) */
482 ret = chown(tl, st.st_uid, st.st_gid);
483 /* Ignores chown errors, while still doing chown
484 (so root still can copy files preserving ownership) */
486 if (chmod(tl, st.st_mode) < 0) ret = FALSE;
489 tb.actime = st.st_atime;
490 tb.modtime = st.st_mtime;
491 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
500 /* paths are in filesystem encoding */
501 static gboolean hard_linked(const gchar *a, const gchar *b)
506 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
508 return (sta.st_dev == stb.st_dev &&
509 sta.st_ino == stb.st_ino);
512 gboolean copy_file(const gchar *s, const gchar *t)
518 gchar *randname = NULL;
524 sl = path_from_utf8(s);
525 tl = path_from_utf8(t);
527 if (hard_linked(sl, tl))
533 fi = fopen(sl, "rb");
536 /* First we write to a temporary file, then we rename it on success,
537 and attributes from original file are copied */
538 randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
539 if (!randname) goto end;
541 fd = g_mkstemp(randname);
542 if (fd == -1) goto end;
544 fo = fdopen(fd, "wb");
550 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
552 if (fwrite(buf, sizeof(gchar), b, fo) != b)
559 fclose(fi); fi = NULL;
560 fclose(fo); fo = NULL;
562 if (rename(randname, tl) < 0) {
567 ret = copy_file_attributes(s, t, TRUE, TRUE);
574 if (randname) g_free(randname);
578 gboolean move_file(const gchar *s, const gchar *t)
583 if (!s || !t) return FALSE;
585 sl = path_from_utf8(s);
586 tl = path_from_utf8(t);
587 if (rename(sl, tl) < 0)
589 /* this may have failed because moving a file across filesystems
590 was attempted, so try copy and delete instead */
595 /* err, now we can't delete the source file so return FALSE */
610 gboolean rename_file(const gchar *s, const gchar *t)
615 if (!s || !t) return FALSE;
617 sl = path_from_utf8(s);
618 tl = path_from_utf8(t);
619 ret = (rename(sl, tl) == 0);
626 gchar *get_current_dir(void)
631 pathl = g_get_current_dir();
632 path8 = path_to_utf8(pathl);
638 void string_list_free(GList *list)
640 g_list_foreach(list, (GFunc)g_free, NULL);
644 GList *string_list_copy(const GList *list)
646 GList *new_list = NULL;
647 GList *work = (GList *) list;
656 new_list = g_list_prepend(new_list, g_strdup(path));
659 return g_list_reverse(new_list);
662 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
668 if (!divider) divider = "";
670 unique = g_strconcat(path, ext, NULL);
671 while (isname(unique))
676 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
680 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
694 gchar *unique_filename_simple(const gchar *path)
700 if (!path) return NULL;
702 name = filename_from_path(path);
703 if (!name) return NULL;
705 ext = registered_extension_from_path(name);
709 unique = unique_filename(path, NULL, "_", TRUE);
715 base = remove_extension_from_path(path);
716 unique = unique_filename(base, ext, "_", TRUE);
723 const gchar *filename_from_path(const gchar *path)
727 if (!path) return NULL;
729 base = strrchr(path, G_DIR_SEPARATOR);
730 if (base) return base + 1;
735 gchar *remove_level_from_path(const gchar *path)
739 if (!path) return NULL;
741 base = strrchr(path, G_DIR_SEPARATOR);
742 if (base) return g_strndup(path, strlen(path)-strlen(base));
747 gboolean file_extension_match(const gchar *path, const gchar *ext)
752 if (!path) return FALSE;
753 if (!ext) return TRUE;
759 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
762 gchar *remove_extension_from_path(const gchar *path)
764 const gchar *reg_ext;
766 if (!path) return NULL;
768 reg_ext = registered_extension_from_path(path);
770 return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
773 void parse_out_relatives(gchar *path)
781 while (path[s] != '\0')
783 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
785 /* /. occurence, let's see more */
788 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
790 /* /./ or /., just skip this part */
794 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
796 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
799 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
804 if (s != t) path[t] = path[s];
809 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
810 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
814 gboolean file_in_path(const gchar *name)
819 gboolean ret = FALSE;
821 if (!name) return FALSE;
822 path = g_strdup(getenv("PATH"));
823 if (!path) return FALSE;
824 namel = path_from_utf8(name);
828 while (p < l && !ret)
832 while (path[e] != ':' && path[e] != '\0') e++;
835 f = g_build_filename(path + p, namel, NULL);
836 if (isfile(f)) ret = TRUE;
846 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
848 if (!path) return FALSE;
852 gchar *npath = g_strdup(path);
858 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
870 DEBUG_1("creating sub dir:%s", npath);
871 if (!mkdir_utf8(npath, mode))
873 log_printf("create dir failed: %s\n", npath);
879 if (!end) p[0] = G_DIR_SEPARATOR;
888 /* does filename utf8 to filesystem encoding first */
889 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
894 pathl = path_from_utf8(path);
895 success = md5_get_digest_from_file(pathl, digest);
902 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
906 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
908 return md5_digest_to_text(digest);
912 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */