Mercurial > geeqie.yaz
annotate src/ui_fileops.c @ 1145:3a7af6a8cd5f
Use functions to return directories instead of constants.
Following functions were added:
get_collections_dir()
get_metadata_cache_dir()
get_rc_dir()
get_thumbnails_cache_dir()
get_trash_dir()
They return the full directory path.
author | zas_ |
---|---|
date | Sat, 15 Nov 2008 17:30:45 +0000 |
parents | 1646720364cf |
children | 95860439070b |
rev | line source |
---|---|
9 | 1 /* |
2 * (SLIK) SimpLIstic sKin functions | |
69
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
40
diff
changeset
|
3 * (C) 2006 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
9 | 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 | |
281 | 29 #include "main.h" |
9 | 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 *----------------------------------------------------------------------------- | |
442 | 38 */ |
9 | 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); | |
759 | 47 fputs((text_l) ? text_l : text_utf8, stdout); |
9 | 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(""); | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
93 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
94 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
95 g_string_append(string, "\n"); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
96 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
97 if (bf) |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
98 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
99 else |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
100 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
101 g_string_append(string, "\n"); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
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"); |
9 | 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); | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
107 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
108 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
109 |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
110 if (g_utf8_validate(path, -1, NULL)) |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
111 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
112 else |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
113 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
114 g_string_append(string, "\n"); |
9 | 115 g_free(name); |
116 } | |
117 | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
118 gd = generic_dialog_new(_("Filename encoding locale mismatch"), |
254
9faf34f047b1
Make the wmclass value unique among the code by defining
zas_
parents:
138
diff
changeset
|
119 GQ_WMCLASS, "locale warning", NULL, TRUE, NULL, NULL); |
9 | 120 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE); |
121 | |
122 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING, | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
123 _("Filename encoding locale mismatch"), string->str); |
9 | 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 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
140 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message); |
9 | 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 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
163 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message); |
9 | 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 | |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
175 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */ |
9 | 176 const gchar *homedir(void) |
177 { | |
178 static gchar *home = NULL; | |
179 | |
180 if (!home) | |
181 home = path_to_utf8(getenv("HOME")); | |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
182 |
9 | 183 if (!home) |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
184 home = path_to_utf8(g_get_home_dir()); |
9 | 185 |
186 return home; | |
187 } | |
188 | |
1145
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
189 const gchar *get_rc_dir(void) |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
190 { |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
191 static gchar *rc_dir = NULL; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
192 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
193 if (rc_dir) return rc_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
194 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
195 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL); |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
196 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
197 return rc_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
198 } |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
199 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
200 const gchar *get_collections_dir(void) |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
201 { |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
202 static gchar *collections_dir = NULL; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
203 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
204 if (collections_dir) return collections_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
205 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
206 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL); |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
207 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
208 return collections_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
209 } |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
210 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
211 const gchar *get_trash_dir(void) |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
212 { |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
213 static gchar *trash_dir = NULL; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
214 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
215 if (trash_dir) return trash_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
216 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
217 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL); |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
218 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
219 return trash_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
220 } |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
221 |
9 | 222 gint stat_utf8(const gchar *s, struct stat *st) |
223 { | |
224 gchar *sl; | |
225 gint ret; | |
226 | |
227 if (!s) return FALSE; | |
228 sl = path_from_utf8(s); | |
229 ret = (stat(sl, st) == 0); | |
230 g_free(sl); | |
231 | |
232 return ret; | |
233 } | |
234 | |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
235 gint lstat_utf8(const gchar *s, struct stat *st) |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
236 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
237 gchar *sl; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
238 gint ret; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
239 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
240 if (!s) return FALSE; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
241 sl = path_from_utf8(s); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
242 ret = (lstat(sl, st) == 0); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
243 g_free(sl); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
244 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
245 return ret; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
246 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
247 |
9 | 248 gint isname(const gchar *s) |
249 { | |
250 struct stat st; | |
251 | |
252 return stat_utf8(s, &st); | |
253 } | |
254 | |
255 gint isfile(const gchar *s) | |
256 { | |
257 struct stat st; | |
258 | |
259 return (stat_utf8(s, &st) && S_ISREG(st.st_mode)); | |
260 } | |
261 | |
262 gint isdir(const gchar *s) | |
263 { | |
264 struct stat st; | |
442 | 265 |
9 | 266 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode)); |
267 } | |
268 | |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
269 gint islink(const gchar *s) |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
270 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
271 struct stat st; |
442 | 272 |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
273 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode)); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
274 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
275 |
9 | 276 gint64 filesize(const gchar *s) |
277 { | |
278 struct stat st; | |
442 | 279 |
9 | 280 if (!stat_utf8(s, &st)) return 0; |
983 | 281 return st.st_size; |
9 | 282 } |
283 | |
284 time_t filetime(const gchar *s) | |
285 { | |
442 | 286 struct stat st; |
9 | 287 |
288 if (!stat_utf8(s, &st)) return 0; | |
289 return st.st_mtime; | |
290 } | |
291 | |
292 gint filetime_set(const gchar *s, time_t tval) | |
293 { | |
294 gint ret = FALSE; | |
295 | |
296 if (tval > 0) | |
297 { | |
298 struct utimbuf ut; | |
299 gchar *sl; | |
300 | |
301 ut.actime = ut.modtime = tval; | |
302 | |
303 sl = path_from_utf8(s); | |
304 ret = (utime(sl, &ut) == 0); | |
305 g_free(sl); | |
306 } | |
307 | |
308 return ret; | |
309 } | |
310 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
311 gint access_file(const gchar *s, gint mode) |
9 | 312 { |
313 gchar *sl; | |
314 gint ret; | |
315 | |
316 if (!s) return FALSE; | |
317 | |
318 sl = path_from_utf8(s); | |
319 ret = (access(sl, mode) == 0); | |
320 g_free(sl); | |
321 | |
322 return ret; | |
323 } | |
324 | |
325 gint unlink_file(const gchar *s) | |
326 { | |
327 gchar *sl; | |
328 gint ret; | |
329 | |
330 if (!s) return FALSE; | |
331 | |
332 sl = path_from_utf8(s); | |
333 ret = (unlink(sl) == 0); | |
334 g_free(sl); | |
335 | |
336 return ret; | |
337 } | |
338 | |
339 gint symlink_utf8(const gchar *source, const gchar *target) | |
340 { | |
341 gchar *sl; | |
342 gchar *tl; | |
343 gint ret; | |
344 | |
345 if (!source || !target) return FALSE; | |
346 | |
347 sl = path_from_utf8(source); | |
348 tl = path_from_utf8(target); | |
349 | |
350 ret = (symlink(sl, tl) == 0); | |
351 | |
352 g_free(sl); | |
353 g_free(tl); | |
354 | |
355 return ret; | |
356 } | |
357 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
358 gint mkdir_utf8(const gchar *s, gint mode) |
9 | 359 { |
360 gchar *sl; | |
361 gint ret; | |
362 | |
363 if (!s) return FALSE; | |
364 | |
365 sl = path_from_utf8(s); | |
366 ret = (mkdir(sl, mode) == 0); | |
367 g_free(sl); | |
368 return ret; | |
369 } | |
370 | |
371 gint rmdir_utf8(const gchar *s) | |
372 { | |
373 gchar *sl; | |
374 gint ret; | |
375 | |
376 if (!s) return FALSE; | |
377 | |
378 sl = path_from_utf8(s); | |
379 ret = (rmdir(sl) == 0); | |
380 g_free(sl); | |
381 | |
382 return ret; | |
383 } | |
384 | |
385 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime) | |
386 { | |
387 struct stat st; | |
388 gchar *sl, *tl; | |
389 gint ret = FALSE; | |
390 | |
391 if (!s || !t) return FALSE; | |
392 | |
393 sl = path_from_utf8(s); | |
394 tl = path_from_utf8(t); | |
395 | |
396 if (stat(sl, &st) == 0) | |
397 { | |
398 struct utimbuf tb; | |
399 | |
400 ret = TRUE; | |
401 | |
402 /* set the dest file attributes to that of source (ignoring errors) */ | |
403 | |
404 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE; | |
405 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE; | |
406 | |
407 tb.actime = st.st_atime; | |
408 tb.modtime = st.st_mtime; | |
409 if (mtime && utime(tl, &tb) < 0) ret = FALSE; | |
410 } | |
411 | |
412 g_free(sl); | |
413 g_free(tl); | |
414 | |
415 return ret; | |
416 } | |
417 | |
418 /* paths are in filesystem encoding */ | |
419 static gint hard_linked(const gchar *a, const gchar *b) | |
420 { | |
421 struct stat sta; | |
422 struct stat stb; | |
423 | |
424 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE; | |
425 | |
426 return (sta.st_dev == stb.st_dev && | |
427 sta.st_ino == stb.st_ino); | |
428 } | |
429 | |
430 gint copy_file(const gchar *s, const gchar *t) | |
431 { | |
432 FILE *fi = NULL; | |
433 FILE *fo = NULL; | |
434 gchar *sl, *tl; | |
435 gchar buf[4096]; | |
704
839525c4e85f
Use size_t instead of gint, it silents a signed vs unsigned warning.
zas_
parents:
703
diff
changeset
|
436 size_t b; |
9 | 437 |
438 sl = path_from_utf8(s); | |
439 tl = path_from_utf8(t); | |
440 | |
441 if (hard_linked(sl, tl)) | |
442 { | |
443 g_free(sl); | |
444 g_free(tl); | |
445 return TRUE; | |
446 } | |
447 | |
448 fi = fopen(sl, "rb"); | |
449 if (fi) | |
450 { | |
451 fo = fopen(tl, "wb"); | |
452 if (!fo) | |
453 { | |
454 fclose(fi); | |
455 fi = NULL; | |
456 } | |
457 } | |
458 | |
459 g_free(sl); | |
460 g_free(tl); | |
461 | |
462 if (!fi || !fo) return FALSE; | |
463 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
464 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0) |
9 | 465 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
466 if (fwrite(buf, sizeof(gchar), b, fo) != b) |
9 | 467 { |
468 fclose(fi); | |
469 fclose(fo); | |
470 return FALSE; | |
471 } | |
472 } | |
473 | |
474 fclose(fi); | |
475 fclose(fo); | |
476 | |
477 copy_file_attributes(s, t, TRUE, TRUE); | |
478 | |
479 return TRUE; | |
480 } | |
481 | |
482 gint move_file(const gchar *s, const gchar *t) | |
483 { | |
484 gchar *sl, *tl; | |
485 gint ret = TRUE; | |
486 | |
487 if (!s || !t) return FALSE; | |
488 | |
489 sl = path_from_utf8(s); | |
490 tl = path_from_utf8(t); | |
491 if (rename(sl, tl) < 0) | |
492 { | |
493 /* this may have failed because moving a file across filesystems | |
494 was attempted, so try copy and delete instead */ | |
495 if (copy_file(s, t)) | |
496 { | |
497 if (unlink(sl) < 0) | |
498 { | |
499 /* err, now we can't delete the source file so return FALSE */ | |
500 ret = FALSE; | |
501 } | |
502 } | |
503 else | |
504 { | |
505 ret = FALSE; | |
506 } | |
507 } | |
508 g_free(sl); | |
509 g_free(tl); | |
510 | |
511 return ret; | |
512 } | |
513 | |
514 gint rename_file(const gchar *s, const gchar *t) | |
515 { | |
516 gchar *sl, *tl; | |
517 gint ret; | |
518 | |
519 if (!s || !t) return FALSE; | |
520 | |
521 sl = path_from_utf8(s); | |
522 tl = path_from_utf8(t); | |
523 ret = (rename(sl, tl) == 0); | |
524 g_free(sl); | |
525 g_free(tl); | |
526 | |
527 return ret; | |
528 } | |
529 | |
530 gchar *get_current_dir(void) | |
531 { | |
532 gchar *pathl; | |
533 gchar *path8; | |
534 | |
535 pathl = g_get_current_dir(); | |
536 path8 = path_to_utf8(pathl); | |
537 g_free(pathl); | |
538 | |
539 return path8; | |
540 } | |
541 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
545
diff
changeset
|
542 void string_list_free(GList *list) |
9 | 543 { |
544 g_list_foreach(list, (GFunc)g_free, NULL); | |
545 g_list_free(list); | |
546 } | |
547 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
545
diff
changeset
|
548 GList *string_list_copy(GList *list) |
9 | 549 { |
550 GList *new_list = NULL; | |
551 GList *work; | |
552 | |
553 work = list; | |
554 while (work) | |
555 { | |
556 gchar *path; | |
442 | 557 |
9 | 558 path = work->data; |
559 work = work->next; | |
442 | 560 |
9 | 561 new_list = g_list_prepend(new_list, g_strdup(path)); |
562 } | |
442 | 563 |
9 | 564 return g_list_reverse(new_list); |
565 } | |
566 | |
567 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad) | |
568 { | |
569 gchar *unique; | |
570 gint n = 1; | |
571 | |
572 if (!ext) ext = ""; | |
573 if (!divider) divider = ""; | |
574 | |
575 unique = g_strconcat(path, ext, NULL); | |
576 while (isname(unique)) | |
577 { | |
578 g_free(unique); | |
579 if (pad) | |
580 { | |
581 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext); | |
582 } | |
583 else | |
584 { | |
585 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext); | |
586 } | |
587 n++; | |
588 if (n > 999) | |
589 { | |
590 /* well, we tried */ | |
591 g_free(unique); | |
592 return NULL; | |
593 } | |
594 } | |
595 | |
596 return unique; | |
597 } | |
598 | |
599 gchar *unique_filename_simple(const gchar *path) | |
600 { | |
601 gchar *unique; | |
602 const gchar *name; | |
603 const gchar *ext; | |
604 | |
605 if (!path) return NULL; | |
606 | |
607 name = filename_from_path(path); | |
608 if (!name) return NULL; | |
609 | |
610 ext = extension_from_path(name); | |
611 | |
612 if (!ext) | |
613 { | |
614 unique = unique_filename(path, NULL, "_", TRUE); | |
615 } | |
616 else | |
617 { | |
618 gchar *base; | |
619 | |
620 base = remove_extension_from_path(path); | |
621 unique = unique_filename(base, ext, "_", TRUE); | |
622 g_free(base); | |
623 } | |
624 | |
625 return unique; | |
626 } | |
627 | |
628 const gchar *filename_from_path(const gchar *path) | |
629 { | |
630 const gchar *base; | |
631 | |
632 if (!path) return NULL; | |
633 | |
701 | 634 base = strrchr(path, G_DIR_SEPARATOR); |
9 | 635 if (base) return base + 1; |
636 | |
637 return path; | |
638 } | |
639 | |
640 gchar *remove_level_from_path(const gchar *path) | |
641 { | |
543 | 642 gint p = 0, n = -1; |
9 | 643 |
644 if (!path) return NULL; | |
645 | |
543 | 646 while (path[p]) |
647 { | |
701 | 648 if (path[p] == G_DIR_SEPARATOR) n = p; |
543 | 649 p++; |
650 } | |
651 if (n <= 0) n++; | |
652 | |
653 return g_strndup(path, (gsize) n); | |
9 | 654 } |
655 | |
656 const gchar *extension_from_path(const gchar *path) | |
657 { | |
658 if (!path) return NULL; | |
659 return strrchr(path, '.'); | |
660 } | |
661 | |
662 gint file_extension_match(const gchar *path, const gchar *ext) | |
663 { | |
664 gint p; | |
665 gint e; | |
666 | |
667 if (!path) return FALSE; | |
668 if (!ext) return TRUE; | |
669 | |
670 p = strlen(path); | |
671 e = strlen(ext); | |
672 | |
605
651ae2be1031
Use g_ascii_strncasecmp() instead of strncasecmp() where applicable.
zas_
parents:
576
diff
changeset
|
673 /* FIXME: utf8 */ |
9 | 674 return (p > e && strncasecmp(path + p - e, ext, e) == 0); |
675 } | |
676 | |
677 gchar *remove_extension_from_path(const gchar *path) | |
678 { | |
544 | 679 gint p = 0, n = -1; |
9 | 680 |
681 if (!path) return NULL; | |
682 | |
544 | 683 while (path[p]) |
684 { | |
685 if (path[p] == '.') n = p; | |
686 p++; | |
687 } | |
688 if (n < 0) n = p; | |
689 | |
690 return g_strndup(path, (gsize) n); | |
9 | 691 } |
692 | |
693 void parse_out_relatives(gchar *path) | |
694 { | |
695 gint s, t; | |
696 | |
697 if (!path) return; | |
698 | |
699 s = t = 0; | |
700 | |
701 while (path[s] != '\0') | |
702 { | |
919 | 703 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.') |
9 | 704 { |
919 | 705 /* /. occurence, let's see more */ |
706 gint p = s + 2; | |
707 | |
708 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0') | |
709 { | |
710 /* /./ or /., just skip this part */ | |
711 s = p; | |
712 continue; | |
713 } | |
714 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0')) | |
715 { | |
716 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */ | |
717 s = p + 1; | |
718 if (t > 0) t--; | |
719 while (path[t] != G_DIR_SEPARATOR && t > 0) t--; | |
720 continue; | |
721 } | |
9 | 722 } |
919 | 723 |
724 if (s != t) path[t] = path[s]; | |
725 t++; | |
726 s++; | |
9 | 727 } |
919 | 728 |
701 | 729 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++; |
730 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--; | |
9 | 731 path[t] = '\0'; |
732 } | |
733 | |
734 gint file_in_path(const gchar *name) | |
735 { | |
736 gchar *path; | |
737 gchar *namel; | |
738 gint p, l; | |
739 gint ret = FALSE; | |
740 | |
741 if (!name) return FALSE; | |
742 path = g_strdup(getenv("PATH")); | |
743 if (!path) return FALSE; | |
744 namel = path_from_utf8(name); | |
745 | |
746 p = 0; | |
747 l = strlen(path); | |
748 while (p < l && !ret) | |
749 { | |
750 gchar *f; | |
751 gint e = p; | |
752 while (path[e] != ':' && path[e] != '\0') e++; | |
753 path[e] = '\0'; | |
754 e++; | |
703 | 755 f = g_build_filename(path + p, namel, NULL); |
9 | 756 if (isfile(f)) ret = TRUE; |
757 g_free(f); | |
758 p = e; | |
759 } | |
760 g_free(namel); | |
761 g_free(path); | |
762 | |
763 return ret; | |
764 } | |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1000
diff
changeset
|
765 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |