Merge branch 'master' into ke
[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)
494                         {
495                         ret = chown(tl, st.st_uid, st.st_gid);
496                         /* Ignores chown errors, while still doing chown
497                            (so root still can copy files preserving ownership) */
498                         ret = TRUE;
499                         if (chmod(tl, st.st_mode) < 0) ret = FALSE;
500                         }
501
502                 tb.actime = st.st_atime;
503                 tb.modtime = st.st_mtime;
504                 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
505                 }
506
507         g_free(sl);
508         g_free(tl);
509
510         return ret;
511 }
512
513 /* paths are in filesystem encoding */
514 static gboolean hard_linked(const gchar *a, const gchar *b)
515 {
516         struct stat sta;
517         struct stat stb;
518
519         if (stat(a, &sta) !=  0 || stat(b, &stb) != 0) return FALSE;
520
521         return (sta.st_dev == stb.st_dev &&
522                 sta.st_ino == stb.st_ino);
523 }
524
525 gboolean copy_file(const gchar *s, const gchar *t)
526 {
527         FILE *fi = NULL;
528         FILE *fo = NULL;
529         gchar *sl = NULL;
530         gchar *tl = NULL;
531         gchar *randname = NULL;
532         gchar buf[16384];
533         size_t b;
534         gint ret = FALSE;
535         gint fd = -1;
536
537         sl = path_from_utf8(s);
538         tl = path_from_utf8(t);
539
540         if (hard_linked(sl, tl))
541                 {
542                 ret = TRUE;
543                 goto end;
544                 }
545
546         fi = fopen(sl, "rb");
547         if (!fi) goto end;
548         
549         /* First we write to a temporary file, then we rename it on success,
550            and attributes from original file are copied */
551         randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
552         if (!randname) goto end;
553         
554         fd = g_mkstemp(randname);
555         if (fd == -1) goto end;
556         
557         fo = fdopen(fd, "wb");
558         if (!fo) {
559                 close(fd);
560                 goto end;
561         }
562
563         while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
564                 {
565                 if (fwrite(buf, sizeof(gchar), b, fo) != b)
566                         {
567                         unlink(randname);
568                         goto end;
569                         }
570                 }
571
572         fclose(fi); fi = NULL;
573         fclose(fo); fo = NULL;
574
575         if (rename(randname, tl) < 0) {
576                 unlink(randname);
577                 goto end;       
578         }
579
580         ret = copy_file_attributes(s, t, TRUE, TRUE);
581
582 end:
583         if (fi) fclose(fi);
584         if (fo) fclose(fo);
585         if (sl) g_free(sl);
586         if (tl) g_free(tl);
587         if (randname) g_free(randname);
588         return ret;
589 }
590
591 gboolean move_file(const gchar *s, const gchar *t)
592 {
593         gchar *sl, *tl;
594         gboolean ret = TRUE;
595
596         if (!s || !t) return FALSE;
597
598         sl = path_from_utf8(s);
599         tl = path_from_utf8(t);
600         if (rename(sl, tl) < 0)
601                 {
602                 /* this may have failed because moving a file across filesystems
603                 was attempted, so try copy and delete instead */
604                 if (copy_file(s, t))
605                         {
606                         if (unlink(sl) < 0)
607                                 {
608                                 /* err, now we can't delete the source file so return FALSE */
609                                 ret = FALSE;
610                                 }
611                         }
612                 else
613                         {
614                         ret = FALSE;
615                         }
616                 }
617         g_free(sl);
618         g_free(tl);
619
620         return ret;
621 }
622
623 gboolean rename_file(const gchar *s, const gchar *t)
624 {
625         gchar *sl, *tl;
626         gboolean ret;
627
628         if (!s || !t) return FALSE;
629
630         sl = path_from_utf8(s);
631         tl = path_from_utf8(t);
632         ret = (rename(sl, tl) == 0);
633         g_free(sl);
634         g_free(tl);
635
636         return ret;
637 }
638
639 gchar *get_current_dir(void)
640 {
641         gchar *pathl;
642         gchar *path8;
643
644         pathl = g_get_current_dir();
645         path8 = path_to_utf8(pathl);
646         g_free(pathl);
647
648         return path8;
649 }
650
651 void string_list_free(GList *list)
652 {
653         g_list_foreach(list, (GFunc)g_free, NULL);
654         g_list_free(list);
655 }
656
657 GList *string_list_copy(const GList *list)
658 {
659         GList *new_list = NULL;
660         GList *work = (GList *) list;
661
662         while (work)
663                 {
664                 gchar *path;
665
666                 path = work->data;
667                 work = work->next;
668
669                 new_list = g_list_prepend(new_list, g_strdup(path));
670                 }
671
672         return g_list_reverse(new_list);
673 }
674
675 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
676 {
677         gchar *unique;
678         gint n = 1;
679
680         if (!ext) ext = "";
681         if (!divider) divider = "";
682
683         unique = g_strconcat(path, ext, NULL);
684         while (isname(unique))
685                 {
686                 g_free(unique);
687                 if (pad)
688                         {
689                         unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
690                         }
691                 else
692                         {
693                         unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
694                         }
695                 n++;
696                 if (n > 999)
697                         {
698                         /* well, we tried */
699                         g_free(unique);
700                         return NULL;
701                         }
702                 }
703
704         return unique;
705 }
706
707 gchar *unique_filename_simple(const gchar *path)
708 {
709         gchar *unique;
710         const gchar *name;
711         const gchar *ext;
712
713         if (!path) return NULL;
714
715         name = filename_from_path(path);
716         if (!name) return NULL;
717
718         ext = extension_from_path(name);
719
720         if (!ext)
721                 {
722                 unique = unique_filename(path, NULL, "_", TRUE);
723                 }
724         else
725                 {
726                 gchar *base;
727
728                 base = remove_extension_from_path(path);
729                 unique = unique_filename(base, ext, "_", TRUE);
730                 g_free(base);
731                 }
732
733         return unique;
734 }
735
736 const gchar *filename_from_path(const gchar *path)
737 {
738         const gchar *base;
739
740         if (!path) return NULL;
741
742         base = strrchr(path, G_DIR_SEPARATOR);
743         if (base) return base + 1;
744
745         return path;
746 }
747
748 gchar *remove_level_from_path(const gchar *path)
749 {
750         gint p = 0, n = -1;
751
752         if (!path) return NULL;
753
754         while (path[p])
755                 {
756                 if (path[p] == G_DIR_SEPARATOR) n = p;
757                 p++;
758                 }
759         if (n <= 0) n++;
760
761         return g_strndup(path, (gsize) n);
762 }
763
764 const gchar *extension_from_path(const gchar *path)
765 {
766         if (!path) return NULL;
767         return strrchr(path, '.');
768 }
769
770 gboolean file_extension_match(const gchar *path, const gchar *ext)
771 {
772         gint p;
773         gint e;
774
775         if (!path) return FALSE;
776         if (!ext) return TRUE;
777
778         p = strlen(path);
779         e = strlen(ext);
780
781         /* FIXME: utf8 */
782         return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
783 }
784
785 gchar *remove_extension_from_path(const gchar *path)
786 {
787         gint p = 0, n = -1;
788
789         if (!path) return NULL;
790
791         while (path[p])
792                 {
793                 if (path[p] == '.') n = p;
794                 p++;
795                 }
796         if (n < 0) n = p;
797
798         return g_strndup(path, (gsize) n);
799 }
800
801 void parse_out_relatives(gchar *path)
802 {
803         gint s, t;
804
805         if (!path) return;
806
807         s = t = 0;
808
809         while (path[s] != '\0')
810                 {
811                 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
812                         {
813                         /* /. occurence, let's see more */
814                         gint p = s + 2;
815
816                         if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
817                                 {
818                                 /* /./ or /., just skip this part */
819                                 s = p;
820                                 continue;
821                                 }
822                         else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
823                                 {
824                                 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
825                                 s = p + 1;
826                                 if (t > 0) t--;
827                                 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
828                                 continue;
829                                 }
830                         }
831         
832                 if (s != t) path[t] = path[s];
833                 t++;
834                 s++;
835                 }
836
837         if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
838         if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
839         path[t] = '\0';
840 }
841
842 gboolean file_in_path(const gchar *name)
843 {
844         gchar *path;
845         gchar *namel;
846         gint p, l;
847         gboolean ret = FALSE;
848
849         if (!name) return FALSE;
850         path = g_strdup(getenv("PATH"));
851         if (!path) return FALSE;
852         namel = path_from_utf8(name);
853
854         p = 0;
855         l = strlen(path);
856         while (p < l && !ret)
857                 {
858                 gchar *f;
859                 gint e = p;
860                 while (path[e] != ':' && path[e] != '\0') e++;
861                 path[e] = '\0';
862                 e++;
863                 f = g_build_filename(path + p, namel, NULL);
864                 if (isfile(f)) ret = TRUE;
865                 g_free(f);
866                 p = e;
867                 }
868         g_free(namel);
869         g_free(path);
870
871         return ret;
872 }
873
874 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
875 {
876         if (!path) return FALSE;
877
878         if (!isdir(path))
879                 {
880                 gchar *npath = g_strdup(path);
881                 gchar *p = npath;
882
883                 while (p[0] != '\0')
884                         {
885                         p++;
886                         if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
887                                 {
888                                 gboolean end = TRUE;
889
890                                 if (p[0] != '\0')
891                                         {
892                                         p[0] = '\0';
893                                         end = FALSE;
894                                         }
895                                 
896                                 if (!isdir(npath))
897                                         {
898                                         DEBUG_1("creating sub dir:%s", npath);
899                                         if (!mkdir_utf8(npath, mode))
900                                                 {
901                                                 log_printf("create dir failed: %s\n", npath);
902                                                 g_free(npath);
903                                                 return FALSE;
904                                                 }
905                                         }
906                                 
907                                 if (!end) p[0] = G_DIR_SEPARATOR;
908                                 }
909                         }
910                 g_free(npath);
911                 }
912
913         return TRUE;
914 }
915
916 /* does filename utf8 to filesystem encoding first */
917 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
918 {
919         gboolean success;
920         gchar *pathl;
921
922         pathl = path_from_utf8(path);
923         success = md5_get_digest_from_file(pathl, digest);
924         g_free(pathl);
925
926         return success;
927 }
928
929
930 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
931 {
932         guchar digest[16];
933
934         if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
935
936         return md5_digest_to_text(digest);
937 }
938
939
940 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */