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