Trim trailing white spaces.
[geeqie.git] / src / ui_fileops.c
index 3564cfd..f7ee88b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * (SLIK) SimpLIstic sKin functions
  * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2009 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,29 +45,10 @@ 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 gboolean 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 gboolean warned_user = FALSE;
@@ -75,14 +57,6 @@ static void encoding_dialog(const gchar *path)
        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;
 
@@ -327,51 +301,6 @@ gboolean lstat_utf8(const gchar *s, struct stat *st)
        return ret;
 }
 
-/* extension must contain only ASCII characters */
-gboolean stat_utf8_case_insensitive_ext(GString *base, const gchar *ext, struct stat *st)
-{
-       gchar *sl;
-       gchar *extl;
-       gboolean ret = FALSE;
-       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;
-}
-
 gboolean isname(const gchar *s)
 {
        struct stat st;
@@ -534,8 +463,14 @@ gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint m
 
                /* 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;
@@ -564,52 +499,66 @@ 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;
 }
 
 gboolean move_file(const gchar *s, const gchar *t)
@@ -937,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: */