c653fb04443589040cd94e578e89b6f5f3d540e5
[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                 ssize_t i;
559
560                 link_target = g_malloc(st.st_size + 1);
561                 i = readlink(sl, link_target, st.st_size);
562                 if (i<0)
563                         {
564                         g_free(link_target);
565                         goto orig_copy;  // try a "normal" copy
566                         }
567                 link_target[st.st_size] = '\0';
568
569                 if (link_target[0] != G_DIR_SEPARATOR) // if it is a relative symlink
570                         {
571                         gchar *absolute;
572
573                         char *lastslash = strrchr(sl, G_DIR_SEPARATOR);
574                         int len = lastslash - sl + 1;
575
576                         int path_max;
577 #ifdef PATH_MAX
578                         path_max = PATH_MAX;
579 #else
580                         path_max = pathconf(sl, _PC_PATH_MAX);
581                         if (path_max <= 0)
582                                 path_max = 4096;
583 #endif
584
585                         absolute = g_malloc(path_max + 1);
586
587                         strncpy(absolute, sl, len);
588                         strcpy(absolute + len, link_target);
589                         strcpy(link_target, absolute);
590
591                         char *realPath;
592                         realPath = realpath(link_target, absolute);
593
594                         if (realPath != NULL) // successfully resolved into an absolute path
595                                 {
596                                 g_free(link_target);
597                                 link_target = absolute;
598                                 }
599                         else                 // could not get absolute path, got some error instead
600                                 {
601                                 g_free(link_target);
602                                 g_free(absolute);
603                                 goto orig_copy;  // so try a "normal" copy
604                                 }
605                         }
606
607                 if (stat_utf8(tl, &st)) unlink(tl); // first try to remove directory entry in destination directory if such entry exists
608
609                 gint success = (symlink(link_target, tl) == 0);
610                 g_free(link_target);
611
612                 if (success)
613                         {
614                         ret = TRUE;
615                         goto end;
616                         }
617                 } // if symlink did not succeed, continue on to try a copy procedure
618         orig_copy:
619
620         fi = fopen(sl, "rb");
621         if (!fi) goto end;
622
623         /* First we write to a temporary file, then we rename it on success,
624            and attributes from original file are copied */
625         randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
626         if (!randname) goto end;
627
628         fd = g_mkstemp(randname);
629         if (fd == -1) goto end;
630
631         fo = fdopen(fd, "wb");
632         if (!fo) {
633                 close(fd);
634                 goto end;
635         }
636
637         while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
638                 {
639                 if (fwrite(buf, sizeof(gchar), b, fo) != b)
640                         {
641                         unlink(randname);
642                         goto end;
643                         }
644                 }
645
646         fclose(fi); fi = NULL;
647         fclose(fo); fo = NULL;
648
649         if (rename(randname, tl) < 0) {
650                 unlink(randname);
651                 goto end;
652         }
653
654         ret = copy_file_attributes(s, t, TRUE, TRUE);
655
656 end:
657         if (fi) fclose(fi);
658         if (fo) fclose(fo);
659         if (sl) g_free(sl);
660         if (tl) g_free(tl);
661         if (randname) g_free(randname);
662         return ret;
663 }
664
665 gboolean move_file(const gchar *s, const gchar *t)
666 {
667         gchar *sl, *tl;
668         gboolean ret = TRUE;
669
670         if (!s || !t) return FALSE;
671
672         sl = path_from_utf8(s);
673         tl = path_from_utf8(t);
674         if (rename(sl, tl) < 0)
675                 {
676                 /* this may have failed because moving a file across filesystems
677                 was attempted, so try copy and delete instead */
678                 if (copy_file(s, t))
679                         {
680                         if (unlink(sl) < 0)
681                                 {
682                                 /* err, now we can't delete the source file so return FALSE */
683                                 ret = FALSE;
684                                 }
685                         }
686                 else
687                         {
688                         ret = FALSE;
689                         }
690                 }
691         g_free(sl);
692         g_free(tl);
693
694         return ret;
695 }
696
697 gboolean rename_file(const gchar *s, const gchar *t)
698 {
699         gchar *sl, *tl;
700         gboolean ret;
701
702         if (!s || !t) return FALSE;
703
704         sl = path_from_utf8(s);
705         tl = path_from_utf8(t);
706         ret = (rename(sl, tl) == 0);
707         g_free(sl);
708         g_free(tl);
709
710         return ret;
711 }
712
713 gchar *get_current_dir(void)
714 {
715         gchar *pathl;
716         gchar *path8;
717
718         pathl = g_get_current_dir();
719         path8 = path_to_utf8(pathl);
720         g_free(pathl);
721
722         return path8;
723 }
724
725 void string_list_free(GList *list)
726 {
727         g_list_foreach(list, (GFunc)g_free, NULL);
728         g_list_free(list);
729 }
730
731 GList *string_list_copy(const GList *list)
732 {
733         GList *new_list = NULL;
734         GList *work = (GList *) list;
735
736         while (work)
737                 {
738                 gchar *path;
739
740                 path = work->data;
741                 work = work->next;
742
743                 new_list = g_list_prepend(new_list, g_strdup(path));
744                 }
745
746         return g_list_reverse(new_list);
747 }
748
749 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
750 {
751         gchar *unique;
752         gint n = 1;
753
754         if (!ext) ext = "";
755         if (!divider) divider = "";
756
757         unique = g_strconcat(path, ext, NULL);
758         while (isname(unique))
759                 {
760                 g_free(unique);
761                 if (pad)
762                         {
763                         unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
764                         }
765                 else
766                         {
767                         unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
768                         }
769                 n++;
770                 if (n > 999)
771                         {
772                         /* well, we tried */
773                         g_free(unique);
774                         return NULL;
775                         }
776                 }
777
778         return unique;
779 }
780
781 gchar *unique_filename_simple(const gchar *path)
782 {
783         gchar *unique;
784         const gchar *name;
785         const gchar *ext;
786
787         if (!path) return NULL;
788
789         name = filename_from_path(path);
790         if (!name) return NULL;
791
792         ext = registered_extension_from_path(name);
793
794         if (!ext)
795                 {
796                 unique = unique_filename(path, NULL, "_", TRUE);
797                 }
798         else
799                 {
800                 gchar *base;
801
802                 base = remove_extension_from_path(path);
803                 unique = unique_filename(base, ext, "_", TRUE);
804                 g_free(base);
805                 }
806
807         return unique;
808 }
809
810 const gchar *filename_from_path(const gchar *path)
811 {
812         const gchar *base;
813
814         if (!path) return NULL;
815
816         base = strrchr(path, G_DIR_SEPARATOR);
817         if (base) return base + 1;
818
819         return path;
820 }
821
822 gchar *remove_level_from_path(const gchar *path)
823 {
824         const gchar *base;
825
826         if (!path) return g_strdup("");
827
828         base = strrchr(path, G_DIR_SEPARATOR);
829         /* Take account of a file being in the root ( / ) folder - ensure the returned value
830          * is at least one character long */
831         if (base) return g_strndup(path, (strlen(path)-strlen(base)) == 0 ? 1 : (strlen(path)-strlen(base)));
832
833         return g_strdup("");
834 }
835
836 gboolean file_extension_match(const gchar *path, const gchar *ext)
837 {
838         gint p;
839         gint e;
840
841         if (!path) return FALSE;
842         if (!ext) return TRUE;
843
844         p = strlen(path);
845         e = strlen(ext);
846
847         /* FIXME: utf8 */
848         return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
849 }
850
851 gchar *remove_extension_from_path(const gchar *path)
852 {
853         const gchar *reg_ext;
854
855         if (!path) return NULL;
856
857         reg_ext = registered_extension_from_path(path);
858
859         return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
860 }
861
862 void parse_out_relatives(gchar *path)
863 {
864         gint s, t;
865
866         if (!path) return;
867
868         s = t = 0;
869
870         while (path[s] != '\0')
871                 {
872                 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
873                         {
874                         /* /. occurence, let's see more */
875                         gint p = s + 2;
876
877                         if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
878                                 {
879                                 /* /./ or /., just skip this part */
880                                 s = p;
881                                 continue;
882                                 }
883                         else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
884                                 {
885                                 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
886                                 s = p + 1;
887                                 if (t > 0) t--;
888                                 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
889                                 continue;
890                                 }
891                         }
892
893                 if (s != t) path[t] = path[s];
894                 t++;
895                 s++;
896                 }
897
898         if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
899         if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
900         path[t] = '\0';
901 }
902
903 gboolean file_in_path(const gchar *name)
904 {
905         gchar *path;
906         gchar *namel;
907         gint p, l;
908         gboolean ret = FALSE;
909
910         if (!name) return FALSE;
911         path = g_strdup(getenv("PATH"));
912         if (!path) return FALSE;
913         namel = path_from_utf8(name);
914
915         p = 0;
916         l = strlen(path);
917         while (p < l && !ret)
918                 {
919                 gchar *f;
920                 gint e = p;
921                 while (path[e] != ':' && path[e] != '\0') e++;
922                 path[e] = '\0';
923                 e++;
924                 f = g_build_filename(path + p, namel, NULL);
925                 if (isfile(f)) ret = TRUE;
926                 g_free(f);
927                 p = e;
928                 }
929         g_free(namel);
930         g_free(path);
931
932         return ret;
933 }
934
935 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
936 {
937         if (!path) return FALSE;
938
939         if (!isdir(path))
940                 {
941                 gchar *npath = g_strdup(path);
942                 gchar *p = npath;
943
944                 while (p[0] != '\0')
945                         {
946                         p++;
947                         if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
948                                 {
949                                 gboolean end = TRUE;
950
951                                 if (p[0] != '\0')
952                                         {
953                                         p[0] = '\0';
954                                         end = FALSE;
955                                         }
956
957                                 if (!isdir(npath))
958                                         {
959                                         DEBUG_1("creating sub dir:%s", npath);
960                                         if (!mkdir_utf8(npath, mode))
961                                                 {
962                                                 log_printf("create dir failed: %s\n", npath);
963                                                 g_free(npath);
964                                                 return FALSE;
965                                                 }
966                                         }
967
968                                 if (!end) p[0] = G_DIR_SEPARATOR;
969                                 }
970                         }
971                 g_free(npath);
972                 }
973
974         return TRUE;
975 }
976
977 /* does filename utf8 to filesystem encoding first */
978 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
979 {
980         gboolean success;
981         gchar *pathl;
982
983         pathl = path_from_utf8(path);
984         success = md5_get_digest_from_file(pathl, digest);
985         g_free(pathl);
986
987         return success;
988 }
989
990
991 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
992 {
993         guchar digest[16];
994
995         if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
996
997         return md5_digest_to_text(digest);
998 }
999
1000
1001 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */