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