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