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(gboolean err, const gchar *text_utf8)
59 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
62 fputs((text_l) ? text_l : text_utf8, stderr);
66 fputs((text_l) ? text_l : text_utf8, stdout);
68 if(command_line && command_line->ssi)
69 secure_fputs(command_line->ssi, (text_l) ? text_l : text_utf8);
73 static void encoding_dialog(const gchar *path)
75 static gboolean warned_user = FALSE;
81 if (warned_user) return;
85 bf = getenv("G_BROKEN_FILENAMES");
87 string = g_string_new("");
88 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
89 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
90 g_string_append(string, "\n");
91 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
93 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
95 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
96 g_string_append(string, "\n");
97 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
98 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
101 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
102 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
103 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
105 if (g_utf8_validate(path, -1, NULL))
106 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
108 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
109 g_string_append(string, "\n");
113 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
114 "locale warning", NULL, TRUE, NULL, NULL);
115 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
117 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
118 _("Filename encoding locale mismatch"), string->str, TRUE);
120 gtk_widget_show(gd->dialog);
122 g_string_free(string, TRUE);
125 #if GQ_DEBUG_PATH_UTF8
126 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
128 gchar *path_to_utf8(const gchar *path)
132 GError *error = NULL;
134 if (!path) return NULL;
136 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
139 #if GQ_DEBUG_PATH_UTF8
140 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
142 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
145 encoding_dialog(path);
149 /* just let it through, but bad things may happen */
150 utf8 = g_strdup(path);
156 #if GQ_DEBUG_PATH_UTF8
157 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
159 gchar *path_from_utf8(const gchar *utf8)
163 GError *error = NULL;
165 if (!utf8) return NULL;
167 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
170 #if GQ_DEBUG_PATH_UTF8
171 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
173 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
179 /* if invalid UTF-8, text probaby still in original form, so just copy it */
180 path = g_strdup(utf8);
186 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
187 const gchar *homedir(void)
189 static gchar *home = NULL;
192 home = path_to_utf8(getenv("HOME"));
195 home = path_to_utf8(g_get_home_dir());
197 DEBUG_1("Home directory: %s", home);
202 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
204 gchar *dir = getenv(key);
206 if (!dir || dir[0] == '\0')
208 return g_build_filename(homedir(), fallback, NULL);
211 DEBUG_1("Got xdg %s: %s", key, dir);
213 return path_to_utf8(dir);
216 const gchar *xdg_data_home_get(void)
218 static const gchar *xdg_data_home = NULL;
220 if (xdg_data_home) return xdg_data_home;
222 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
224 return xdg_data_home;
227 const gchar *xdg_config_home_get(void)
229 static const gchar *xdg_config_home = NULL;
231 if (xdg_config_home) return xdg_config_home;
233 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
235 return xdg_config_home;
238 const gchar *xdg_cache_home_get(void)
240 static const gchar *xdg_cache_home = NULL;
242 if (xdg_cache_home) return xdg_cache_home;
244 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
246 return xdg_cache_home;
249 const gchar *get_rc_dir(void)
251 static gchar *rc_dir = NULL;
253 if (rc_dir) return rc_dir;
257 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
261 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
267 const gchar *get_collections_dir(void)
269 static gchar *collections_dir = NULL;
271 if (collections_dir) return collections_dir;
275 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
279 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
282 return collections_dir;
285 const gchar *get_trash_dir(void)
287 static gchar *trash_dir = NULL;
289 if (trash_dir) return trash_dir;
293 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
297 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
303 gboolean stat_utf8(const gchar *s, struct stat *st)
308 if (!s) return FALSE;
309 sl = path_from_utf8(s);
310 ret = (stat(sl, st) == 0);
316 gboolean lstat_utf8(const gchar *s, struct stat *st)
321 if (!s) return FALSE;
322 sl = path_from_utf8(s);
323 ret = (lstat(sl, st) == 0);
329 gboolean isname(const gchar *s)
333 return stat_utf8(s, &st);
336 gboolean isfile(const gchar *s)
340 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
343 gboolean isdir(const gchar *s)
347 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
350 gboolean islink(const gchar *s)
354 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
357 gint64 filesize(const gchar *s)
361 if (!stat_utf8(s, &st)) return 0;
365 time_t filetime(const gchar *s)
369 if (!stat_utf8(s, &st)) return 0;
373 gboolean filetime_set(const gchar *s, time_t tval)
375 gboolean ret = FALSE;
382 ut.actime = ut.modtime = tval;
384 sl = path_from_utf8(s);
385 ret = (utime(sl, &ut) == 0);
392 gboolean is_readable_file(const gchar *s)
394 if (!s || !s[0] || !isfile(s)) return FALSE;
395 return access_file(s, R_OK);
398 gboolean access_file(const gchar *s, gint mode)
403 if (!s || !s[0]) return FALSE;
405 sl = path_from_utf8(s);
406 ret = (access(sl, mode) == 0);
412 gboolean unlink_file(const gchar *s)
417 if (!s) return FALSE;
419 sl = path_from_utf8(s);
420 ret = (unlink(sl) == 0);
426 gboolean symlink_utf8(const gchar *source, const gchar *target)
432 if (!source || !target) return FALSE;
434 sl = path_from_utf8(source);
435 tl = path_from_utf8(target);
437 ret = (symlink(sl, tl) == 0);
445 gboolean mkdir_utf8(const gchar *s, gint mode)
450 if (!s) return FALSE;
452 sl = path_from_utf8(s);
453 ret = (mkdir(sl, mode) == 0);
458 gboolean rmdir_utf8(const gchar *s)
463 if (!s) return FALSE;
465 sl = path_from_utf8(s);
466 ret = (rmdir(sl) == 0);
472 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
476 gboolean ret = FALSE;
478 if (!s || !t) return FALSE;
480 sl = path_from_utf8(s);
481 tl = path_from_utf8(t);
483 if (stat(sl, &st) == 0)
489 /* set the dest file attributes to that of source (ignoring errors) */
493 ret = chown(tl, st.st_uid, st.st_gid);
494 /* Ignores chown errors, while still doing chown
495 (so root still can copy files preserving ownership) */
497 if (chmod(tl, st.st_mode) < 0) {
499 if (stat(tl, &st2) != 0 || st2.st_mode != st.st_mode) {
505 tb.actime = st.st_atime;
506 tb.modtime = st.st_mtime;
507 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
516 /* paths are in filesystem encoding */
517 static gboolean hard_linked(const gchar *a, const gchar *b)
522 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
524 return (sta.st_dev == stb.st_dev &&
525 sta.st_ino == stb.st_ino);
528 gboolean copy_file(const gchar *s, const gchar *t)
534 gchar *randname = NULL;
540 sl = path_from_utf8(s);
541 tl = path_from_utf8(t);
543 if (hard_linked(sl, tl))
549 /* Do not dereference absolute symlinks, but copy them "as is".
550 * For a relative symlink, we don't know how to properly change it when
551 * copied/moved to another dir to keep pointing it to same target as
552 * a relative symlink, so we turn it into absolute symlink using
553 * realpath() instead. */
555 if (lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
560 link_target = g_malloc(st.st_size + 1);
561 i = readlink(sl, link_target, st.st_size);
565 goto orig_copy; // try a "normal" copy
567 link_target[st.st_size] = '\0';
569 if (link_target[0] != G_DIR_SEPARATOR) // if it is a relative symlink
573 gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
574 gint len = lastslash - sl + 1;
576 absolute = g_malloc(len + st.st_size + 1);
577 strncpy(absolute, sl, len);
578 strcpy(absolute + len, link_target);
580 link_target = absolute;
583 realPath = realpath(link_target, NULL);
585 if (realPath != NULL) // successfully resolved into an absolute path
588 link_target = g_strdup(realPath);
591 else // could not get absolute path, got some error instead
594 goto orig_copy; // so try a "normal" copy
598 if (stat_utf8(tl, &st)) unlink(tl); // first try to remove directory entry in destination directory if such entry exists
600 gint success = (symlink(link_target, tl) == 0);
608 } // if symlink did not succeed, continue on to try a copy procedure
611 fi = fopen(sl, "rb");
614 /* First we write to a temporary file, then we rename it on success,
615 and attributes from original file are copied */
616 randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
617 if (!randname) goto end;
619 fd = g_mkstemp(randname);
620 if (fd == -1) goto end;
622 fo = fdopen(fd, "wb");
628 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
630 if (fwrite(buf, sizeof(gchar), b, fo) != b)
637 fclose(fi); fi = NULL;
638 fclose(fo); fo = NULL;
640 if (rename(randname, tl) < 0) {
645 ret = copy_file_attributes(s, t, TRUE, TRUE);
652 if (randname) g_free(randname);
656 gboolean move_file(const gchar *s, const gchar *t)
661 if (!s || !t) return FALSE;
663 sl = path_from_utf8(s);
664 tl = path_from_utf8(t);
665 if (rename(sl, tl) < 0)
667 /* this may have failed because moving a file across filesystems
668 was attempted, so try copy and delete instead */
673 /* err, now we can't delete the source file so return FALSE */
688 gboolean rename_file(const gchar *s, const gchar *t)
693 if (!s || !t) return FALSE;
695 sl = path_from_utf8(s);
696 tl = path_from_utf8(t);
697 ret = (rename(sl, tl) == 0);
704 gchar *get_current_dir(void)
709 pathl = g_get_current_dir();
710 path8 = path_to_utf8(pathl);
716 void string_list_free(GList *list)
718 g_list_foreach(list, (GFunc)g_free, NULL);
722 GList *string_list_copy(const GList *list)
724 GList *new_list = NULL;
725 GList *work = (GList *) list;
734 new_list = g_list_prepend(new_list, g_strdup(path));
737 return g_list_reverse(new_list);
740 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
746 if (!divider) divider = "";
748 unique = g_strconcat(path, ext, NULL);
749 while (isname(unique))
754 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
758 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
772 gchar *unique_filename_simple(const gchar *path)
778 if (!path) return NULL;
780 name = filename_from_path(path);
781 if (!name) return NULL;
783 ext = registered_extension_from_path(name);
787 unique = unique_filename(path, NULL, "_", TRUE);
793 base = remove_extension_from_path(path);
794 unique = unique_filename(base, ext, "_", TRUE);
801 const gchar *filename_from_path(const gchar *path)
805 if (!path) return NULL;
807 base = strrchr(path, G_DIR_SEPARATOR);
808 if (base) return base + 1;
813 gchar *remove_level_from_path(const gchar *path)
817 if (!path) return g_strdup("");
819 base = strrchr(path, G_DIR_SEPARATOR);
820 /* Take account of a file being in the root ( / ) folder - ensure the returned value
821 * is at least one character long */
822 if (base) return g_strndup(path, (strlen(path)-strlen(base)) == 0 ? 1 : (strlen(path)-strlen(base)));
827 gboolean file_extension_match(const gchar *path, const gchar *ext)
832 if (!path) return FALSE;
833 if (!ext) return TRUE;
839 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
842 gchar *remove_extension_from_path(const gchar *path)
844 const gchar *reg_ext;
846 if (!path) return NULL;
848 reg_ext = registered_extension_from_path(path);
850 return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
853 void parse_out_relatives(gchar *path)
861 while (path[s] != '\0')
863 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
865 /* /. occurence, let's see more */
868 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
870 /* /./ or /., just skip this part */
874 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
876 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
879 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
884 if (s != t) path[t] = path[s];
889 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
890 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
894 gboolean file_in_path(const gchar *name)
899 gboolean ret = FALSE;
901 if (!name) return FALSE;
902 path = g_strdup(getenv("PATH"));
903 if (!path) return FALSE;
904 namel = path_from_utf8(name);
908 while (p < l && !ret)
912 while (path[e] != ':' && path[e] != '\0') e++;
915 f = g_build_filename(path + p, namel, NULL);
916 if (isfile(f)) ret = TRUE;
926 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
928 if (!path) return FALSE;
932 gchar *npath = g_strdup(path);
938 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
950 DEBUG_1("creating sub dir:%s", npath);
951 if (!mkdir_utf8(npath, mode))
953 log_printf("create dir failed: %s\n", npath);
959 if (!end) p[0] = G_DIR_SEPARATOR;
968 /* does filename utf8 to filesystem encoding first */
969 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
974 pathl = path_from_utf8(path);
975 success = md5_get_digest_from_file(pathl, digest);
982 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
986 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
988 return md5_digest_to_text(digest);
992 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */