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