added 2010 to copyright text
[geeqie.git] / src / ui_fileops.c
1 /*
2  * (SLIK) SimpLIstic sKin functions
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2010 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #  include "config.h"
15 #endif
16
17 #include <pwd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/param.h>
23 #include <dirent.h>
24 #include <utime.h>
25
26 #include <glib.h>
27 #include <gtk/gtk.h>    /* for locale warning dialog */
28
29 #include "main.h"
30 #include "ui_fileops.h"
31
32 #include "ui_utildlg.h" /* for locale warning dialog */
33 #include "md5-util.h"
34
35 /*
36  *-----------------------------------------------------------------------------
37  * generic file information and manipulation routines (public)
38  *-----------------------------------------------------------------------------
39  */
40
41
42
43 void print_term(const gchar *text_utf8)
44 {
45         gchar *text_l;
46
47         text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
48         fputs((text_l) ? text_l : text_utf8, stderr);
49         g_free(text_l);
50 }
51
52 static void encoding_dialog(const gchar *path);
53
54 static gboolean encoding_dialog_idle(gpointer data)
55 {
56         gchar *path = data;
57
58         encoding_dialog(path);
59         g_free(path);
60
61         return FALSE;
62 }
63
64 static gint encoding_dialog_delay(gpointer data)
65 {
66         g_idle_add(encoding_dialog_idle, data);
67
68         return 0;
69 }
70
71 static void encoding_dialog(const gchar *path)
72 {
73         static gboolean warned_user = FALSE;
74         GenericDialog *gd;
75         GString *string;
76         const gchar *lc;
77         const gchar *bf;
78
79         /* check that gtk is initialized (loop is level > 0) */
80         if (gtk_main_level() == 0)
81                 {
82                 /* gtk not initialized */
83                 gtk_init_add(encoding_dialog_delay, g_strdup(path));
84                 return;
85                 }
86
87         if (warned_user) return;
88         warned_user = TRUE;
89
90         lc = getenv("LANG");
91         bf = getenv("G_BROKEN_FILENAMES");
92
93         string = g_string_new("");
94         g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
95         g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
96         g_string_append(string, "\n");
97         g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
98         if (bf)
99                 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
100         else
101                 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
102         g_string_append(string, "\n");
103         g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
104         if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
105                 {
106                 gchar *name;
107                 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
108                 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
109                 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
110                 
111                 if (g_utf8_validate(path, -1, NULL))
112                         g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
113                 else
114                         g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
115                 g_string_append(string, "\n");
116                 g_free(name);
117                 }
118
119         gd = generic_dialog_new(_("Filename encoding locale mismatch"),
120                                 "locale warning", NULL, TRUE, NULL, NULL);
121         generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
122
123         generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
124                                    _("Filename encoding locale mismatch"), string->str);
125
126         gtk_widget_show(gd->dialog);
127
128         g_string_free(string, TRUE);
129 }
130
131 #if GQ_DEBUG_PATH_UTF8
132 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
133 #else
134 gchar *path_to_utf8(const gchar *path)
135 #endif
136 {
137         gchar *utf8;
138         GError *error = NULL;
139
140         if (!path) return NULL;
141
142         utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
143         if (error)
144                 {
145 #if GQ_DEBUG_PATH_UTF8
146                 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
147 #else
148                 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
149 #endif
150                 g_error_free(error);
151                 encoding_dialog(path);
152                 }
153         if (!utf8)
154                 {
155                 /* just let it through, but bad things may happen */
156                 utf8 = g_strdup(path);
157                 }
158
159         return utf8;
160 }
161
162 #if GQ_DEBUG_PATH_UTF8
163 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
164 #else
165 gchar *path_from_utf8(const gchar *utf8)
166 #endif
167 {
168         gchar *path;
169         GError *error = NULL;
170
171         if (!utf8) return NULL;
172
173         path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
174         if (error)
175                 {
176 #if GQ_DEBUG_PATH_UTF8
177                 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
178 #else
179                 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
180 #endif
181                 g_error_free(error);
182                 }
183         if (!path)
184                 {
185                 /* if invalid UTF-8, text probaby still in original form, so just copy it */
186                 path = g_strdup(utf8);
187                 }
188
189         return path;
190 }
191
192 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
193 const gchar *homedir(void)
194 {
195         static gchar *home = NULL;
196
197         if (!home)
198                 home = path_to_utf8(getenv("HOME"));
199         
200         if (!home)
201                 home = path_to_utf8(g_get_home_dir());
202
203         return home;
204 }
205
206 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
207 {
208         gchar *dir = getenv(key);
209
210         if (!dir || dir[0] == '\0')
211                 {
212                 return g_build_filename(homedir(), fallback, NULL);
213                 }
214         
215         return path_to_utf8(dir);
216 }
217
218 const gchar *xdg_data_home_get(void)
219 {
220         static const gchar *xdg_data_home = NULL;
221
222         if (xdg_data_home) return xdg_data_home;
223         
224         xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
225
226         return xdg_data_home;
227 }
228
229 const gchar *xdg_config_home_get(void)
230 {
231         static const gchar *xdg_config_home = NULL;
232
233         if (xdg_config_home) return xdg_config_home;
234         
235         xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
236
237         return xdg_config_home;
238 }
239
240 const gchar *xdg_cache_home_get(void)
241 {
242         static const gchar *xdg_cache_home = NULL;
243
244         if (xdg_cache_home) return xdg_cache_home;
245         
246         xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
247
248         return xdg_cache_home;
249 }
250
251 const gchar *get_rc_dir(void)
252 {
253         static gchar *rc_dir = NULL;
254         
255         if (rc_dir) return rc_dir;
256
257         if (USE_XDG)
258                 {
259                 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
260                 }
261         else
262                 {
263                 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
264                 }
265
266         return rc_dir;
267 }
268
269 const gchar *get_collections_dir(void)
270 {
271         static gchar *collections_dir = NULL;
272
273         if (collections_dir) return collections_dir;
274
275         if (USE_XDG)
276                 {
277                 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
278                 }
279         else
280                 {
281                 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
282                 }
283
284         return collections_dir;
285 }
286
287 const gchar *get_trash_dir(void)
288 {
289         static gchar *trash_dir = NULL;
290
291         if (trash_dir) return trash_dir;
292         
293         if (USE_XDG)
294                 {
295                 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
296                 }
297         else
298                 {
299                 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
300         }
301
302         return trash_dir;
303 }
304
305 gboolean stat_utf8(const gchar *s, struct stat *st)
306 {
307         gchar *sl;
308         gboolean ret;
309
310         if (!s) return FALSE;
311         sl = path_from_utf8(s);
312         ret = (stat(sl, st) == 0);
313         g_free(sl);
314
315         return ret;
316 }
317
318 gboolean lstat_utf8(const gchar *s, struct stat *st)
319 {
320         gchar *sl;
321         gboolean ret;
322
323         if (!s) return FALSE;
324         sl = path_from_utf8(s);
325         ret = (lstat(sl, st) == 0);
326         g_free(sl);
327
328         return ret;
329 }
330
331 gboolean isname(const gchar *s)
332 {
333         struct stat st;
334
335         return stat_utf8(s, &st);
336 }
337
338 gboolean isfile(const gchar *s)
339 {
340         struct stat st;
341
342         return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
343 }
344
345 gboolean isdir(const gchar *s)
346 {
347         struct stat st;
348
349         return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
350 }
351
352 gboolean islink(const gchar *s)
353 {
354         struct stat st;
355
356         return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
357 }
358
359 gint64 filesize(const gchar *s)
360 {
361         struct stat st;
362
363         if (!stat_utf8(s, &st)) return 0;
364         return st.st_size;
365 }
366
367 time_t filetime(const gchar *s)
368 {
369         struct stat st;
370
371         if (!stat_utf8(s, &st)) return 0;
372         return st.st_mtime;
373 }
374
375 gboolean filetime_set(const gchar *s, time_t tval)
376 {
377         gboolean ret = FALSE;
378
379         if (tval > 0)
380                 {
381                 struct utimbuf ut;
382                 gchar *sl;
383
384                 ut.actime = ut.modtime = tval;
385
386                 sl = path_from_utf8(s);
387                 ret = (utime(sl, &ut) == 0);
388                 g_free(sl);
389                 }
390
391         return ret;
392 }
393
394 gboolean is_readable_file(const gchar *s)
395 {
396         if (!s || !s[0] || !isfile(s)) return FALSE;
397         return access_file(s, R_OK);
398 }
399
400 gboolean access_file(const gchar *s, gint mode)
401 {
402         gchar *sl;
403         gint ret;
404
405         if (!s || !s[0]) return FALSE;
406
407         sl = path_from_utf8(s);
408         ret = (access(sl, mode) == 0);
409         g_free(sl);
410
411         return ret;
412 }
413
414 gboolean unlink_file(const gchar *s)
415 {
416         gchar *sl;
417         gboolean ret;
418
419         if (!s) return FALSE;
420
421         sl = path_from_utf8(s);
422         ret = (unlink(sl) == 0);
423         g_free(sl);
424
425         return ret;
426 }
427
428 gboolean symlink_utf8(const gchar *source, const gchar *target)
429 {
430         gchar *sl;
431         gchar *tl;
432         gboolean ret;
433
434         if (!source || !target) return FALSE;
435
436         sl = path_from_utf8(source);
437         tl = path_from_utf8(target);
438
439         ret = (symlink(sl, tl) == 0);
440
441         g_free(sl);
442         g_free(tl);
443
444         return ret;
445 }
446
447 gboolean mkdir_utf8(const gchar *s, gint mode)
448 {
449         gchar *sl;
450         gboolean ret;
451
452         if (!s) return FALSE;
453
454         sl = path_from_utf8(s);
455         ret = (mkdir(sl, mode) == 0);
456         g_free(sl);
457         return ret;
458 }
459
460 gboolean rmdir_utf8(const gchar *s)
461 {
462         gchar *sl;
463         gboolean ret;
464
465         if (!s) return FALSE;
466
467         sl = path_from_utf8(s);
468         ret = (rmdir(sl) == 0);
469         g_free(sl);
470
471         return ret;
472 }
473
474 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
475 {
476         struct stat st;
477         gchar *sl, *tl;
478         gboolean ret = FALSE;
479
480         if (!s || !t) return FALSE;
481
482         sl = path_from_utf8(s);
483         tl = path_from_utf8(t);
484
485         if (stat(sl, &st) == 0)
486                 {
487                 struct utimbuf tb;
488
489                 ret = TRUE;
490
491                 /* set the dest file attributes to that of source (ignoring errors) */
492
493                 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
494                 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
495
496                 tb.actime = st.st_atime;
497                 tb.modtime = st.st_mtime;
498                 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
499                 }
500
501         g_free(sl);
502         g_free(tl);
503
504         return ret;
505 }
506
507 /* paths are in filesystem encoding */
508 static gboolean hard_linked(const gchar *a, const gchar *b)
509 {
510         struct stat sta;
511         struct stat stb;
512
513         if (stat(a, &sta) !=  0 || stat(b, &stb) != 0) return FALSE;
514
515         return (sta.st_dev == stb.st_dev &&
516                 sta.st_ino == stb.st_ino);
517 }
518
519 gboolean copy_file(const gchar *s, const gchar *t)
520 {
521         FILE *fi = NULL;
522         FILE *fo = NULL;
523         gchar *sl = NULL;
524         gchar *tl = NULL;
525         gchar *randname = NULL;
526         gchar buf[16384];
527         size_t b;
528         gint ret = FALSE;
529         gint fd = -1;
530
531         sl = path_from_utf8(s);
532         tl = path_from_utf8(t);
533
534         if (hard_linked(sl, tl))
535                 {
536                 ret = TRUE;
537                 goto end;
538                 }
539
540         fi = fopen(sl, "rb");
541         if (!fi) goto end;
542         
543         /* First we write to a temporary file, then we rename it on success,
544            and attributes from original file are copied */
545         randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
546         if (!randname) goto end;
547         
548         fd = g_mkstemp(randname);
549         if (fd == -1) goto end;
550         
551         fo = fdopen(fd, "wb");
552         if (!fo) {
553                 close(fd);
554                 goto end;
555         }
556
557         while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
558                 {
559                 if (fwrite(buf, sizeof(gchar), b, fo) != b)
560                         {
561                         unlink(randname);
562                         goto end;
563                         }
564                 }
565
566         fclose(fi); fi = NULL;
567         fclose(fo); fo = NULL;
568
569         if (rename(randname, tl) < 0) {
570                 unlink(randname);
571                 goto end;       
572         }
573
574         ret = copy_file_attributes(s, t, TRUE, TRUE);
575
576 end:
577         if (fi) fclose(fi);
578         if (fo) fclose(fo);
579         if (sl) g_free(sl);
580         if (tl) g_free(tl);
581         if (randname) g_free(randname);
582         return ret;
583 }
584
585 gboolean move_file(const gchar *s, const gchar *t)
586 {
587         gchar *sl, *tl;
588         gboolean ret = TRUE;
589
590         if (!s || !t) return FALSE;
591
592         sl = path_from_utf8(s);
593         tl = path_from_utf8(t);
594         if (rename(sl, tl) < 0)
595                 {
596                 /* this may have failed because moving a file across filesystems
597                 was attempted, so try copy and delete instead */
598                 if (copy_file(s, t))
599                         {
600                         if (unlink(sl) < 0)
601                                 {
602                                 /* err, now we can't delete the source file so return FALSE */
603                                 ret = FALSE;
604                                 }
605                         }
606                 else
607                         {
608                         ret = FALSE;
609                         }
610                 }
611         g_free(sl);
612         g_free(tl);
613
614         return ret;
615 }
616
617 gboolean rename_file(const gchar *s, const gchar *t)
618 {
619         gchar *sl, *tl;
620         gboolean ret;
621
622         if (!s || !t) return FALSE;
623
624         sl = path_from_utf8(s);
625         tl = path_from_utf8(t);
626         ret = (rename(sl, tl) == 0);
627         g_free(sl);
628         g_free(tl);
629
630         return ret;
631 }
632
633 gchar *get_current_dir(void)
634 {
635         gchar *pathl;
636         gchar *path8;
637
638         pathl = g_get_current_dir();
639         path8 = path_to_utf8(pathl);
640         g_free(pathl);
641
642         return path8;
643 }
644
645 void string_list_free(GList *list)
646 {
647         g_list_foreach(list, (GFunc)g_free, NULL);
648         g_list_free(list);
649 }
650
651 GList *string_list_copy(const GList *list)
652 {
653         GList *new_list = NULL;
654         GList *work = (GList *) list;
655
656         while (work)
657                 {
658                 gchar *path;
659
660                 path = work->data;
661                 work = work->next;
662
663                 new_list = g_list_prepend(new_list, g_strdup(path));
664                 }
665
666         return g_list_reverse(new_list);
667 }
668
669 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
670 {
671         gchar *unique;
672         gint n = 1;
673
674         if (!ext) ext = "";
675         if (!divider) divider = "";
676
677         unique = g_strconcat(path, ext, NULL);
678         while (isname(unique))
679                 {
680                 g_free(unique);
681                 if (pad)
682                         {
683                         unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
684                         }
685                 else
686                         {
687                         unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
688                         }
689                 n++;
690                 if (n > 999)
691                         {
692                         /* well, we tried */
693                         g_free(unique);
694                         return NULL;
695                         }
696                 }
697
698         return unique;
699 }
700
701 gchar *unique_filename_simple(const gchar *path)
702 {
703         gchar *unique;
704         const gchar *name;
705         const gchar *ext;
706
707         if (!path) return NULL;
708
709         name = filename_from_path(path);
710         if (!name) return NULL;
711
712         ext = extension_from_path(name);
713
714         if (!ext)
715                 {
716                 unique = unique_filename(path, NULL, "_", TRUE);
717                 }
718         else
719                 {
720                 gchar *base;
721
722                 base = remove_extension_from_path(path);
723                 unique = unique_filename(base, ext, "_", TRUE);
724                 g_free(base);
725                 }
726
727         return unique;
728 }
729
730 const gchar *filename_from_path(const gchar *path)
731 {
732         const gchar *base;
733
734         if (!path) return NULL;
735
736         base = strrchr(path, G_DIR_SEPARATOR);
737         if (base) return base + 1;
738
739         return path;
740 }
741
742 gchar *remove_level_from_path(const gchar *path)
743 {
744         gint p = 0, n = -1;
745
746         if (!path) return NULL;
747
748         while (path[p])
749                 {
750                 if (path[p] == G_DIR_SEPARATOR) n = p;
751                 p++;
752                 }
753         if (n <= 0) n++;
754
755         return g_strndup(path, (gsize) n);
756 }
757
758 const gchar *extension_from_path(const gchar *path)
759 {
760         if (!path) return NULL;
761         return strrchr(path, '.');
762 }
763
764 gboolean file_extension_match(const gchar *path, const gchar *ext)
765 {
766         gint p;
767         gint e;
768
769         if (!path) return FALSE;
770         if (!ext) return TRUE;
771
772         p = strlen(path);
773         e = strlen(ext);
774
775         /* FIXME: utf8 */
776         return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
777 }
778
779 gchar *remove_extension_from_path(const gchar *path)
780 {
781         gint p = 0, n = -1;
782
783         if (!path) return NULL;
784
785         while (path[p])
786                 {
787                 if (path[p] == '.') n = p;
788                 p++;
789                 }
790         if (n < 0) n = p;
791
792         return g_strndup(path, (gsize) n);
793 }
794
795 void parse_out_relatives(gchar *path)
796 {
797         gint s, t;
798
799         if (!path) return;
800
801         s = t = 0;
802
803         while (path[s] != '\0')
804                 {
805                 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
806                         {
807                         /* /. occurence, let's see more */
808                         gint p = s + 2;
809
810                         if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
811                                 {
812                                 /* /./ or /., just skip this part */
813                                 s = p;
814                                 continue;
815                                 }
816                         else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
817                                 {
818                                 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
819                                 s = p + 1;
820                                 if (t > 0) t--;
821                                 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
822                                 continue;
823                                 }
824                         }
825         
826                 if (s != t) path[t] = path[s];
827                 t++;
828                 s++;
829                 }
830
831         if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
832         if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
833         path[t] = '\0';
834 }
835
836 gboolean file_in_path(const gchar *name)
837 {
838         gchar *path;
839         gchar *namel;
840         gint p, l;
841         gboolean ret = FALSE;
842
843         if (!name) return FALSE;
844         path = g_strdup(getenv("PATH"));
845         if (!path) return FALSE;
846         namel = path_from_utf8(name);
847
848         p = 0;
849         l = strlen(path);
850         while (p < l && !ret)
851                 {
852                 gchar *f;
853                 gint e = p;
854                 while (path[e] != ':' && path[e] != '\0') e++;
855                 path[e] = '\0';
856                 e++;
857                 f = g_build_filename(path + p, namel, NULL);
858                 if (isfile(f)) ret = TRUE;
859                 g_free(f);
860                 p = e;
861                 }
862         g_free(namel);
863         g_free(path);
864
865         return ret;
866 }
867
868 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
869 {
870         if (!path) return FALSE;
871
872         if (!isdir(path))
873                 {
874                 gchar *npath = g_strdup(path);
875                 gchar *p = npath;
876
877                 while (p[0] != '\0')
878                         {
879                         p++;
880                         if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
881                                 {
882                                 gboolean end = TRUE;
883
884                                 if (p[0] != '\0')
885                                         {
886                                         p[0] = '\0';
887                                         end = FALSE;
888                                         }
889                                 
890                                 if (!isdir(npath))
891                                         {
892                                         DEBUG_1("creating sub dir:%s", npath);
893                                         if (!mkdir_utf8(npath, mode))
894                                                 {
895                                                 log_printf("create dir failed: %s\n", npath);
896                                                 g_free(npath);
897                                                 return FALSE;
898                                                 }
899                                         }
900                                 
901                                 if (!end) p[0] = G_DIR_SEPARATOR;
902                                 }
903                         }
904                 g_free(npath);
905                 }
906
907         return TRUE;
908 }
909
910 /* does filename utf8 to filesystem encoding first */
911 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
912 {
913         gboolean success;
914         gchar *pathl;
915
916         pathl = path_from_utf8(path);
917         success = md5_get_digest_from_file(pathl, digest);
918         g_free(pathl);
919
920         return success;
921 }
922
923
924 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
925 {
926         guchar digest[16];
927
928         if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
929
930         return md5_digest_to_text(digest);
931 }
932
933
934 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */