Trim trailing white spaces.
[geeqie.git] / src / ui_fileops.c
index e9c1ac8..f7ee88b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * (SLIK) SimpLIstic sKin functions
  * (C) 2006 John Ellis
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
@@ -30,6 +30,7 @@
 #include "ui_fileops.h"
 
 #include "ui_utildlg.h"        /* for locale warning dialog */
+#include "md5-util.h"
 
 /*
  *-----------------------------------------------------------------------------
@@ -44,45 +45,18 @@ void print_term(const gchar *text_utf8)
        gchar *text_l;
 
        text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
-       fputs((text_l) ? text_l : text_utf8, stdout);
+       fputs((text_l) ? text_l : text_utf8, stderr);
        g_free(text_l);
 }
 
-static void encoding_dialog(const gchar *path);
-
-static gint encoding_dialog_idle(gpointer data)
-{
-       gchar *path = data;
-
-       encoding_dialog(path);
-       g_free(path);
-
-       return FALSE;
-}
-
-static gint encoding_dialog_delay(gpointer data)
-{
-       g_idle_add(encoding_dialog_idle, data);
-
-       return 0;
-}
-
 static void encoding_dialog(const gchar *path)
 {
-       static gint warned_user = FALSE;
+       static gboolean warned_user = FALSE;
        GenericDialog *gd;
        GString *string;
        const gchar *lc;
        const gchar *bf;
 
-       /* check that gtk is initialized (loop is level > 0) */
-       if (gtk_main_level() == 0)
-               {
-               /* gtk not initialized */
-               gtk_init_add(encoding_dialog_delay, g_strdup(path));
-               return;
-               }
-
        if (warned_user) return;
        warned_user = TRUE;
 
@@ -127,7 +101,11 @@ static void encoding_dialog(const gchar *path)
        g_string_free(string, TRUE);
 }
 
+#if GQ_DEBUG_PATH_UTF8
+gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
+#else
 gchar *path_to_utf8(const gchar *path)
+#endif
 {
        gchar *utf8;
        GError *error = NULL;
@@ -137,7 +115,11 @@ gchar *path_to_utf8(const gchar *path)
        utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
        if (error)
                {
+#if GQ_DEBUG_PATH_UTF8
+               log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
+#else
                log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
+#endif
                g_error_free(error);
                encoding_dialog(path);
                }
@@ -150,7 +132,11 @@ gchar *path_to_utf8(const gchar *path)
        return utf8;
 }
 
+#if GQ_DEBUG_PATH_UTF8
+gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
+#else
 gchar *path_from_utf8(const gchar *utf8)
+#endif
 {
        gchar *path;
        GError *error = NULL;
@@ -160,7 +146,11 @@ gchar *path_from_utf8(const gchar *utf8)
        path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
        if (error)
                {
+#if GQ_DEBUG_PATH_UTF8
+               log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
+#else
                log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
+#endif
                g_error_free(error);
                }
        if (!path)
@@ -285,10 +275,10 @@ const gchar *get_trash_dir(void)
        return trash_dir;
 }
 
-gint stat_utf8(const gchar *s, struct stat *st)
+gboolean stat_utf8(const gchar *s, struct stat *st)
 {
        gchar *sl;
-       gint ret;
+       gboolean ret;
 
        if (!s) return FALSE;
        sl = path_from_utf8(s);
@@ -298,10 +288,10 @@ gint stat_utf8(const gchar *s, struct stat *st)
        return ret;
 }
 
-gint lstat_utf8(const gchar *s, struct stat *st)
+gboolean lstat_utf8(const gchar *s, struct stat *st)
 {
        gchar *sl;
-       gint ret;
+       gboolean ret;
 
        if (!s) return FALSE;
        sl = path_from_utf8(s);
@@ -311,73 +301,28 @@ gint lstat_utf8(const gchar *s, struct stat *st)
        return ret;
 }
 
-/* extension must contain only ASCII characters */
-gint stat_utf8_case_insensitive_ext(GString *base, const gchar *ext, struct stat *st)
-{
-       gchar *sl;
-       gchar *extl;
-       gint ret = 0;
-       gint ext_len;
-       gint base_len = strlen(base->str);
-
-       g_string_append(base, ext);
-       sl = path_from_utf8(base->str);
-       
-       extl = strrchr(sl, '.');
-       if (extl)
-               {
-               gint i, j;
-               extl++; /* the first char after . */
-               ext_len = strlen(extl);
-       
-               for (i = 0; i < (1 << ext_len); i++)
-                       {
-                       for (j = 0; j < ext_len; j++)
-                               {
-                               if (i & (1 << j)) 
-                                       extl[j] = g_ascii_toupper(extl[j]);
-                               else
-                                       extl[j] = g_ascii_tolower(extl[j]);
-                               }
-                       ret = (stat(sl, st) == 0);
-                       if (ret) break;
-                       }
-               
-               if (ret)
-                       {
-                       /* append the found extension to base */
-                       base = g_string_truncate(base, base_len);
-                       extl--;
-                       g_string_append(base, extl);
-                       }
-               }
-       g_free(sl);
-
-       return ret;
-}
-
-gint isname(const gchar *s)
+gboolean isname(const gchar *s)
 {
        struct stat st;
 
        return stat_utf8(s, &st);
 }
 
-gint isfile(const gchar *s)
+gboolean isfile(const gchar *s)
 {
        struct stat st;
 
        return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
 }
 
-gint isdir(const gchar *s)
+gboolean isdir(const gchar *s)
 {
        struct stat st;
 
        return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
 }
 
-gint islink(const gchar *s)
+gboolean islink(const gchar *s)
 {
        struct stat st;
 
@@ -400,9 +345,9 @@ time_t filetime(const gchar *s)
        return st.st_mtime;
 }
 
-gint filetime_set(const gchar *s, time_t tval)
+gboolean filetime_set(const gchar *s, time_t tval)
 {
-       gint ret = FALSE;
+       gboolean ret = FALSE;
 
        if (tval > 0)
                {
@@ -419,12 +364,18 @@ gint filetime_set(const gchar *s, time_t tval)
        return ret;
 }
 
-gint access_file(const gchar *s, gint mode)
+gboolean is_readable_file(const gchar *s)
+{
+       if (!s || !s[0] || !isfile(s)) return FALSE;
+       return access_file(s, R_OK);
+}
+
+gboolean access_file(const gchar *s, gint mode)
 {
        gchar *sl;
        gint ret;
 
-       if (!s) return FALSE;
+       if (!s || !s[0]) return FALSE;
 
        sl = path_from_utf8(s);
        ret = (access(sl, mode) == 0);
@@ -433,10 +384,10 @@ gint access_file(const gchar *s, gint mode)
        return ret;
 }
 
-gint unlink_file(const gchar *s)
+gboolean unlink_file(const gchar *s)
 {
        gchar *sl;
-       gint ret;
+       gboolean ret;
 
        if (!s) return FALSE;
 
@@ -447,11 +398,11 @@ gint unlink_file(const gchar *s)
        return ret;
 }
 
-gint symlink_utf8(const gchar *source, const gchar *target)
+gboolean symlink_utf8(const gchar *source, const gchar *target)
 {
        gchar *sl;
        gchar *tl;
-       gint ret;
+       gboolean ret;
 
        if (!source || !target) return FALSE;
 
@@ -466,10 +417,10 @@ gint symlink_utf8(const gchar *source, const gchar *target)
        return ret;
 }
 
-gint mkdir_utf8(const gchar *s, gint mode)
+gboolean mkdir_utf8(const gchar *s, gint mode)
 {
        gchar *sl;
-       gint ret;
+       gboolean ret;
 
        if (!s) return FALSE;
 
@@ -479,10 +430,10 @@ gint mkdir_utf8(const gchar *s, gint mode)
        return ret;
 }
 
-gint rmdir_utf8(const gchar *s)
+gboolean rmdir_utf8(const gchar *s)
 {
        gchar *sl;
-       gint ret;
+       gboolean ret;
 
        if (!s) return FALSE;
 
@@ -493,11 +444,11 @@ gint rmdir_utf8(const gchar *s)
        return ret;
 }
 
-gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
+gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
 {
        struct stat st;
        gchar *sl, *tl;
-       gint ret = FALSE;
+       gboolean ret = FALSE;
 
        if (!s || !t) return FALSE;
 
@@ -512,8 +463,14 @@ gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime
 
                /* set the dest file attributes to that of source (ignoring errors) */
 
-               if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
-               if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
+               if (perms)
+                       {
+                       ret = chown(tl, st.st_uid, st.st_gid);
+                       /* Ignores chown errors, while still doing chown
+                          (so root still can copy files preserving ownership) */
+                       ret = TRUE;
+                       if (chmod(tl, st.st_mode) < 0) ret = FALSE;
+                       }
 
                tb.actime = st.st_atime;
                tb.modtime = st.st_mtime;
@@ -527,7 +484,7 @@ gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime
 }
 
 /* paths are in filesystem encoding */
-static gint hard_linked(const gchar *a, const gchar *b)
+static gboolean hard_linked(const gchar *a, const gchar *b)
 {
        struct stat sta;
        struct stat stb;
@@ -538,62 +495,76 @@ static gint hard_linked(const gchar *a, const gchar *b)
                sta.st_ino == stb.st_ino);
 }
 
-gint copy_file(const gchar *s, const gchar *t)
+gboolean copy_file(const gchar *s, const gchar *t)
 {
        FILE *fi = NULL;
        FILE *fo = NULL;
-       gchar *sl, *tl;
-       gchar buf[4096];
+       gchar *sl = NULL;
+       gchar *tl = NULL;
+       gchar *randname = NULL;
+       gchar buf[16384];
        size_t b;
+       gint ret = FALSE;
+       gint fd = -1;
 
        sl = path_from_utf8(s);
        tl = path_from_utf8(t);
 
        if (hard_linked(sl, tl))
                {
-               g_free(sl);
-               g_free(tl);
-               return TRUE;
+               ret = TRUE;
+               goto end;
                }
 
        fi = fopen(sl, "rb");
-       if (fi)
-               {
-               fo = fopen(tl, "wb");
-               if (!fo)
-                       {
-                       fclose(fi);
-                       fi = NULL;
-                       }
-               }
-
-       g_free(sl);
-       g_free(tl);
-
-       if (!fi || !fo) return FALSE;
+       if (!fi) goto end;
+       
+       /* First we write to a temporary file, then we rename it on success,
+          and attributes from original file are copied */
+       randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
+       if (!randname) goto end;
+       
+       fd = g_mkstemp(randname);
+       if (fd == -1) goto end;
+       
+       fo = fdopen(fd, "wb");
+       if (!fo) {
+               close(fd);
+               goto end;
+       }
 
        while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
                {
                if (fwrite(buf, sizeof(gchar), b, fo) != b)
                        {
-                       fclose(fi);
-                       fclose(fo);
-                       return FALSE;
+                       unlink(randname);
+                       goto end;
                        }
                }
 
-       fclose(fi);
-       fclose(fo);
+       fclose(fi); fi = NULL;
+       fclose(fo); fo = NULL;
 
-       copy_file_attributes(s, t, TRUE, TRUE);
+       if (rename(randname, tl) < 0) {
+               unlink(randname);
+               goto end;
+       }
 
-       return TRUE;
+       ret = copy_file_attributes(s, t, TRUE, TRUE);
+
+end:
+       if (fi) fclose(fi);
+       if (fo) fclose(fo);
+       if (sl) g_free(sl);
+       if (tl) g_free(tl);
+       if (randname) g_free(randname);
+       return ret;
 }
 
-gint move_file(const gchar *s, const gchar *t)
+gboolean move_file(const gchar *s, const gchar *t)
 {
        gchar *sl, *tl;
-       gint ret = TRUE;
+       gboolean ret = TRUE;
 
        if (!s || !t) return FALSE;
 
@@ -622,10 +593,10 @@ gint move_file(const gchar *s, const gchar *t)
        return ret;
 }
 
-gint rename_file(const gchar *s, const gchar *t)
+gboolean rename_file(const gchar *s, const gchar *t)
 {
        gchar *sl, *tl;
-       gint ret;
+       gboolean ret;
 
        if (!s || !t) return FALSE;
 
@@ -656,12 +627,11 @@ void string_list_free(GList *list)
        g_list_free(list);
 }
 
-GList *string_list_copy(GList *list)
+GList *string_list_copy(const GList *list)
 {
        GList *new_list = NULL;
-       GList *work;
+       GList *work = (GList *) list;
 
-       work = list;
        while (work)
                {
                gchar *path;
@@ -675,7 +645,7 @@ GList *string_list_copy(GList *list)
        return g_list_reverse(new_list);
 }
 
-gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
+gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
 {
        gchar *unique;
        gint n = 1;
@@ -770,7 +740,7 @@ const gchar *extension_from_path(const gchar *path)
        return strrchr(path, '.');
 }
 
-gint file_extension_match(const gchar *path, const gchar *ext)
+gboolean file_extension_match(const gchar *path, const gchar *ext)
 {
        gint p;
        gint e;
@@ -782,7 +752,7 @@ gint file_extension_match(const gchar *path, const gchar *ext)
        e = strlen(ext);
 
        /* FIXME: utf8 */
-       return (p > e && strncasecmp(path + p - e, ext, e) == 0);
+       return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
 }
 
 gchar *remove_extension_from_path(const gchar *path)
@@ -842,12 +812,12 @@ void parse_out_relatives(gchar *path)
        path[t] = '\0';
 }
 
-gint file_in_path(const gchar *name)
+gboolean file_in_path(const gchar *name)
 {
        gchar *path;
        gchar *namel;
        gint p, l;
-       gint ret = FALSE;
+       gboolean ret = FALSE;
 
        if (!name) return FALSE;
        path = g_strdup(getenv("PATH"));
@@ -888,7 +858,7 @@ gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
                        p++;
                        if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
                                {
-                               gint end = TRUE;
+                               gboolean end = TRUE;
 
                                if (p[0] != '\0')
                                        {
@@ -916,6 +886,28 @@ gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
        return TRUE;
 }
 
+/* does filename utf8 to filesystem encoding first */
+gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
+{
+       gboolean success;
+       gchar *pathl;
+
+       pathl = path_from_utf8(path);
+       success = md5_get_digest_from_file(pathl, digest);
+       g_free(pathl);
+
+       return success;
+}
+
+
+gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
+{
+       guchar digest[16];
+
+       if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
+
+       return md5_digest_to_text(digest);
+}
 
 
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */