Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
[geeqie.git] / src / ui_fileops.c
1 /*
2  * (SLIK) SimpLIstic sKin functions
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2009 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 /* extension must contain only ASCII characters */
332 gboolean stat_utf8_case_insensitive_ext(GString *base, const gchar *ext, struct stat *st)
333 {
334         gchar *sl;
335         gchar *extl;
336         gboolean ret = FALSE;
337         gint ext_len;
338         gint base_len = strlen(base->str);
339
340         g_string_append(base, ext);
341         sl = path_from_utf8(base->str);
342         
343         extl = strrchr(sl, '.');
344         if (extl)
345                 {
346                 gint i, j;
347                 extl++; /* the first char after . */
348                 ext_len = strlen(extl);
349         
350                 for (i = 0; i < (1 << ext_len); i++)
351                         {
352                         for (j = 0; j < ext_len; j++)
353                                 {
354                                 if (i & (1 << j)) 
355                                         extl[j] = g_ascii_toupper(extl[j]);
356                                 else
357                                         extl[j] = g_ascii_tolower(extl[j]);
358                                 }
359                         ret = (stat(sl, st) == 0);
360                         if (ret) break;
361                         }
362                 
363                 if (ret)
364                         {
365                         /* append the found extension to base */
366                         base = g_string_truncate(base, base_len);
367                         extl--;
368                         g_string_append(base, extl);
369                         }
370                 }
371         g_free(sl);
372
373         return ret;
374 }
375
376 gboolean isname(const gchar *s)
377 {
378         struct stat st;
379
380         return stat_utf8(s, &st);
381 }
382
383 gboolean isfile(const gchar *s)
384 {
385         struct stat st;
386
387         return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
388 }
389
390 gboolean isdir(const gchar *s)
391 {
392         struct stat st;
393
394         return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
395 }
396
397 gboolean islink(const gchar *s)
398 {
399         struct stat st;
400
401         return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
402 }
403
404 gint64 filesize(const gchar *s)
405 {
406         struct stat st;
407
408         if (!stat_utf8(s, &st)) return 0;
409         return st.st_size;
410 }
411
412 time_t filetime(const gchar *s)
413 {
414         struct stat st;
415
416         if (!stat_utf8(s, &st)) return 0;
417         return st.st_mtime;
418 }
419
420 gboolean filetime_set(const gchar *s, time_t tval)
421 {
422         gboolean ret = FALSE;
423
424         if (tval > 0)
425                 {
426                 struct utimbuf ut;
427                 gchar *sl;
428
429                 ut.actime = ut.modtime = tval;
430
431                 sl = path_from_utf8(s);
432                 ret = (utime(sl, &ut) == 0);
433                 g_free(sl);
434                 }
435
436         return ret;
437 }
438
439 gboolean is_readable_file(const gchar *s)
440 {
441         if (!s || !s[0] || !isfile(s)) return FALSE;
442         return access_file(s, R_OK);
443 }
444
445 gboolean access_file(const gchar *s, gint mode)
446 {
447         gchar *sl;
448         gint ret;
449
450         if (!s || !s[0]) return FALSE;
451
452         sl = path_from_utf8(s);
453         ret = (access(sl, mode) == 0);
454         g_free(sl);
455
456         return ret;
457 }
458
459 gboolean unlink_file(const gchar *s)
460 {
461         gchar *sl;
462         gboolean ret;
463
464         if (!s) return FALSE;
465
466         sl = path_from_utf8(s);
467         ret = (unlink(sl) == 0);
468         g_free(sl);
469
470         return ret;
471 }
472
473 gboolean symlink_utf8(const gchar *source, const gchar *target)
474 {
475         gchar *sl;
476         gchar *tl;
477         gboolean ret;
478
479         if (!source || !target) return FALSE;
480
481         sl = path_from_utf8(source);
482         tl = path_from_utf8(target);
483
484         ret = (symlink(sl, tl) == 0);
485
486         g_free(sl);
487         g_free(tl);
488
489         return ret;
490 }
491
492 gboolean mkdir_utf8(const gchar *s, gint mode)
493 {
494         gchar *sl;
495         gboolean ret;
496
497         if (!s) return FALSE;
498
499         sl = path_from_utf8(s);
500         ret = (mkdir(sl, mode) == 0);
501         g_free(sl);
502         return ret;
503 }
504
505 gboolean rmdir_utf8(const gchar *s)
506 {
507         gchar *sl;
508         gboolean ret;
509
510         if (!s) return FALSE;
511
512         sl = path_from_utf8(s);
513         ret = (rmdir(sl) == 0);
514         g_free(sl);
515
516         return ret;
517 }
518
519 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
520 {
521         struct stat st;
522         gchar *sl, *tl;
523         gboolean ret = FALSE;
524
525         if (!s || !t) return FALSE;
526
527         sl = path_from_utf8(s);
528         tl = path_from_utf8(t);
529
530         if (stat(sl, &st) == 0)
531                 {
532                 struct utimbuf tb;
533
534                 ret = TRUE;
535
536                 /* set the dest file attributes to that of source (ignoring errors) */
537
538                 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
539                 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
540
541                 tb.actime = st.st_atime;
542                 tb.modtime = st.st_mtime;
543                 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
544                 }
545
546         g_free(sl);
547         g_free(tl);
548
549         return ret;
550 }
551
552 /* paths are in filesystem encoding */
553 static gboolean hard_linked(const gchar *a, const gchar *b)
554 {
555         struct stat sta;
556         struct stat stb;
557
558         if (stat(a, &sta) !=  0 || stat(b, &stb) != 0) return FALSE;
559
560         return (sta.st_dev == stb.st_dev &&
561                 sta.st_ino == stb.st_ino);
562 }
563
564 gboolean copy_file(const gchar *s, const gchar *t)
565 {
566         FILE *fi = NULL;
567         FILE *fo = NULL;
568         gchar *sl, *tl;
569         gchar buf[4096];
570         size_t b;
571
572         sl = path_from_utf8(s);
573         tl = path_from_utf8(t);
574
575         if (hard_linked(sl, tl))
576                 {
577                 g_free(sl);
578                 g_free(tl);
579                 return TRUE;
580                 }
581
582         fi = fopen(sl, "rb");
583         if (fi)
584                 {
585                 fo = fopen(tl, "wb");
586                 if (!fo)
587                         {
588                         fclose(fi);
589                         fi = NULL;
590                         }
591                 }
592
593         g_free(sl);
594         g_free(tl);
595
596         if (!fi || !fo) return FALSE;
597
598         while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
599                 {
600                 if (fwrite(buf, sizeof(gchar), b, fo) != b)
601                         {
602                         fclose(fi);
603                         fclose(fo);
604                         return FALSE;
605                         }
606                 }
607
608         fclose(fi);
609         fclose(fo);
610
611         copy_file_attributes(s, t, TRUE, TRUE);
612
613         return TRUE;
614 }
615
616 gboolean move_file(const gchar *s, const gchar *t)
617 {
618         gchar *sl, *tl;
619         gboolean ret = TRUE;
620
621         if (!s || !t) return FALSE;
622
623         sl = path_from_utf8(s);
624         tl = path_from_utf8(t);
625         if (rename(sl, tl) < 0)
626                 {
627                 /* this may have failed because moving a file across filesystems
628                 was attempted, so try copy and delete instead */
629                 if (copy_file(s, t))
630                         {
631                         if (unlink(sl) < 0)
632                                 {
633                                 /* err, now we can't delete the source file so return FALSE */
634                                 ret = FALSE;
635                                 }
636                         }
637                 else
638                         {
639                         ret = FALSE;
640                         }
641                 }
642         g_free(sl);
643         g_free(tl);
644
645         return ret;
646 }
647
648 gboolean rename_file(const gchar *s, const gchar *t)
649 {
650         gchar *sl, *tl;
651         gboolean ret;
652
653         if (!s || !t) return FALSE;
654
655         sl = path_from_utf8(s);
656         tl = path_from_utf8(t);
657         ret = (rename(sl, tl) == 0);
658         g_free(sl);
659         g_free(tl);
660
661         return ret;
662 }
663
664 gchar *get_current_dir(void)
665 {
666         gchar *pathl;
667         gchar *path8;
668
669         pathl = g_get_current_dir();
670         path8 = path_to_utf8(pathl);
671         g_free(pathl);
672
673         return path8;
674 }
675
676 void string_list_free(GList *list)
677 {
678         g_list_foreach(list, (GFunc)g_free, NULL);
679         g_list_free(list);
680 }
681
682 GList *string_list_copy(const GList *list)
683 {
684         GList *new_list = NULL;
685         GList *work = (GList *) list;
686
687         while (work)
688                 {
689                 gchar *path;
690
691                 path = work->data;
692                 work = work->next;
693
694                 new_list = g_list_prepend(new_list, g_strdup(path));
695                 }
696
697         return g_list_reverse(new_list);
698 }
699
700 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
701 {
702         gchar *unique;
703         gint n = 1;
704
705         if (!ext) ext = "";
706         if (!divider) divider = "";
707
708         unique = g_strconcat(path, ext, NULL);
709         while (isname(unique))
710                 {
711                 g_free(unique);
712                 if (pad)
713                         {
714                         unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
715                         }
716                 else
717                         {
718                         unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
719                         }
720                 n++;
721                 if (n > 999)
722                         {
723                         /* well, we tried */
724                         g_free(unique);
725                         return NULL;
726                         }
727                 }
728
729         return unique;
730 }
731
732 gchar *unique_filename_simple(const gchar *path)
733 {
734         gchar *unique;
735         const gchar *name;
736         const gchar *ext;
737
738         if (!path) return NULL;
739
740         name = filename_from_path(path);
741         if (!name) return NULL;
742
743         ext = extension_from_path(name);
744
745         if (!ext)
746                 {
747                 unique = unique_filename(path, NULL, "_", TRUE);
748                 }
749         else
750                 {
751                 gchar *base;
752
753                 base = remove_extension_from_path(path);
754                 unique = unique_filename(base, ext, "_", TRUE);
755                 g_free(base);
756                 }
757
758         return unique;
759 }
760
761 const gchar *filename_from_path(const gchar *path)
762 {
763         const gchar *base;
764
765         if (!path) return NULL;
766
767         base = strrchr(path, G_DIR_SEPARATOR);
768         if (base) return base + 1;
769
770         return path;
771 }
772
773 gchar *remove_level_from_path(const gchar *path)
774 {
775         gint p = 0, n = -1;
776
777         if (!path) return NULL;
778
779         while (path[p])
780                 {
781                 if (path[p] == G_DIR_SEPARATOR) n = p;
782                 p++;
783                 }
784         if (n <= 0) n++;
785
786         return g_strndup(path, (gsize) n);
787 }
788
789 const gchar *extension_from_path(const gchar *path)
790 {
791         if (!path) return NULL;
792         return strrchr(path, '.');
793 }
794
795 gboolean file_extension_match(const gchar *path, const gchar *ext)
796 {
797         gint p;
798         gint e;
799
800         if (!path) return FALSE;
801         if (!ext) return TRUE;
802
803         p = strlen(path);
804         e = strlen(ext);
805
806         /* FIXME: utf8 */
807         return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
808 }
809
810 gchar *remove_extension_from_path(const gchar *path)
811 {
812         gint p = 0, n = -1;
813
814         if (!path) return NULL;
815
816         while (path[p])
817                 {
818                 if (path[p] == '.') n = p;
819                 p++;
820                 }
821         if (n < 0) n = p;
822
823         return g_strndup(path, (gsize) n);
824 }
825
826 void parse_out_relatives(gchar *path)
827 {
828         gint s, t;
829
830         if (!path) return;
831
832         s = t = 0;
833
834         while (path[s] != '\0')
835                 {
836                 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
837                         {
838                         /* /. occurence, let's see more */
839                         gint p = s + 2;
840
841                         if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
842                                 {
843                                 /* /./ or /., just skip this part */
844                                 s = p;
845                                 continue;
846                                 }
847                         else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
848                                 {
849                                 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
850                                 s = p + 1;
851                                 if (t > 0) t--;
852                                 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
853                                 continue;
854                                 }
855                         }
856         
857                 if (s != t) path[t] = path[s];
858                 t++;
859                 s++;
860                 }
861
862         if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
863         if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
864         path[t] = '\0';
865 }
866
867 gboolean file_in_path(const gchar *name)
868 {
869         gchar *path;
870         gchar *namel;
871         gint p, l;
872         gboolean ret = FALSE;
873
874         if (!name) return FALSE;
875         path = g_strdup(getenv("PATH"));
876         if (!path) return FALSE;
877         namel = path_from_utf8(name);
878
879         p = 0;
880         l = strlen(path);
881         while (p < l && !ret)
882                 {
883                 gchar *f;
884                 gint e = p;
885                 while (path[e] != ':' && path[e] != '\0') e++;
886                 path[e] = '\0';
887                 e++;
888                 f = g_build_filename(path + p, namel, NULL);
889                 if (isfile(f)) ret = TRUE;
890                 g_free(f);
891                 p = e;
892                 }
893         g_free(namel);
894         g_free(path);
895
896         return ret;
897 }
898
899 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
900 {
901         if (!path) return FALSE;
902
903         if (!isdir(path))
904                 {
905                 gchar *npath = g_strdup(path);
906                 gchar *p = npath;
907
908                 while (p[0] != '\0')
909                         {
910                         p++;
911                         if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
912                                 {
913                                 gboolean end = TRUE;
914
915                                 if (p[0] != '\0')
916                                         {
917                                         p[0] = '\0';
918                                         end = FALSE;
919                                         }
920                                 
921                                 if (!isdir(npath))
922                                         {
923                                         DEBUG_1("creating sub dir:%s", npath);
924                                         if (!mkdir_utf8(npath, mode))
925                                                 {
926                                                 log_printf("create dir failed: %s\n", npath);
927                                                 g_free(npath);
928                                                 return FALSE;
929                                                 }
930                                         }
931                                 
932                                 if (!end) p[0] = G_DIR_SEPARATOR;
933                                 }
934                         }
935                 g_free(npath);
936                 }
937
938         return TRUE;
939 }
940
941 /* does filename utf8 to filesystem encoding first */
942 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
943 {
944         gboolean success;
945         gchar *pathl;
946
947         pathl = path_from_utf8(path);
948         success = md5_get_digest_from_file(pathl, digest);
949         g_free(pathl);
950
951         return success;
952 }
953
954
955 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
956 {
957         guchar digest[16];
958
959         if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
960
961         return md5_digest_to_text(digest);
962 }
963
964
965 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */