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