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