Mercurial > audlegacy
annotate libaudacious/vfs_gnome.c @ 625:0a73d1faeb4e trunk
[svn] GCC 4.1 warning fixes by Diego 'Flameeyes' Petteno from Gentoo.
author | chainsaw |
---|---|
date | Mon, 06 Feb 2006 17:10:47 -0800 |
parents | adee87ebe853 |
children | 86ca43d8a845 |
rev | line source |
---|---|
0 | 1 /* This program is free software; you can redistribute it and/or modify |
2 * it under the terms of the GNU General Public Licensse as published by | |
3 * the Free Software Foundation; either version 2 of the License, or | |
4 * (at your option) any later version. | |
5 * | |
6 * This program is distributed in the hope that it will be useful, | |
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 * GNU General Public License for more details. | |
10 * | |
11 * You should have received a copy of the GNU General Public License | |
12 * along with this program; if not, write to the Free Software | |
13 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
14 */ | |
15 | |
16 #include "vfs.h" | |
17 #include <string.h> | |
18 #include <libgnomevfs/gnome-vfs.h> | |
19 | |
20 | |
21 struct _VFSFile | |
22 { | |
23 GnomeVFSHandle *handle; | |
24 }; | |
25 | |
26 | |
27 static void mode_to_gnome_vfs(const gchar * mode, | |
28 GnomeVFSOpenMode * g_mode, | |
29 gboolean * truncate, | |
30 gboolean * append); | |
31 | |
32 gboolean | |
33 vfs_init(void) | |
34 { | |
35 if (!gnome_vfs_init()) | |
36 return FALSE; | |
37 | |
38 g_atexit(gnome_vfs_shutdown); | |
39 return TRUE; | |
40 } | |
41 | |
42 VFSFile * | |
43 vfs_fopen(const gchar * path, | |
44 const gchar * mode) | |
45 { | |
46 VFSFile *file; | |
47 GnomeVFSResult g_result; | |
48 GnomeVFSOpenMode g_mode; | |
49 gboolean truncate, append; | |
50 | |
550 | 51 if (!path || !mode) |
52 return NULL; | |
53 | |
0 | 54 file = g_new(VFSFile, 1); |
55 | |
56 mode_to_gnome_vfs(mode, &g_mode, &truncate, &append); | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
57 gchar *escaped_file = gnome_vfs_escape_path_string(path); |
0 | 58 |
59 if (!truncate) { | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
60 g_result = gnome_vfs_open(&(file->handle), escaped_file, g_mode); |
0 | 61 if (append && g_result == GNOME_VFS_ERROR_NOT_FOUND) { |
62 g_result = gnome_vfs_create(&(file->handle), | |
63 path, g_mode, TRUE, | |
64 S_IRUSR | S_IWUSR | | |
65 S_IRGRP | S_IWGRP | | |
66 S_IROTH | S_IWOTH); | |
67 } | |
68 | |
69 if (append && g_result == GNOME_VFS_OK) { | |
70 g_result = gnome_vfs_seek(file->handle, GNOME_VFS_SEEK_END, 0); | |
71 if (g_result != GNOME_VFS_OK) | |
72 gnome_vfs_close(file->handle); | |
73 } | |
74 } | |
75 else { | |
76 g_result = gnome_vfs_create(&(file->handle), | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
77 escaped_file, g_mode, FALSE, |
0 | 78 S_IRUSR | S_IWUSR | |
79 S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
80 } | |
81 | |
82 if (g_result != GNOME_VFS_OK) | |
83 file->handle = NULL; | |
84 | |
85 if (file->handle == NULL) { | |
86 g_free(file); | |
87 file = NULL; | |
88 } | |
89 | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
90 g_free(escaped_file); |
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
91 |
0 | 92 return file; |
93 } | |
94 | |
95 gint | |
96 vfs_fclose(VFSFile * file) | |
97 { | |
98 gint ret = 0; | |
99 | |
100 if (file->handle) { | |
101 if (gnome_vfs_close(file->handle) != GNOME_VFS_OK) | |
102 ret = -1; | |
103 } | |
104 | |
105 g_free(file); | |
106 | |
107 return ret; | |
108 } | |
109 | |
110 size_t | |
111 vfs_fread(gpointer ptr, | |
112 size_t size, | |
113 size_t nmemb, | |
114 VFSFile * file) | |
115 { | |
116 GnomeVFSResult result; | |
117 GnomeVFSFileSize bytes_read; | |
118 | |
119 result = gnome_vfs_read(file->handle, ptr, size * nmemb, &bytes_read); | |
120 if (result == GNOME_VFS_OK) | |
121 return bytes_read; | |
122 if (result == GNOME_VFS_ERROR_EOF) | |
123 return 0; | |
124 else | |
125 return -1; | |
126 } | |
127 | |
128 size_t | |
129 vfs_fwrite(gconstpointer ptr, | |
130 size_t size, | |
131 size_t nmemb, | |
132 VFSFile * file) | |
133 { | |
134 GnomeVFSResult result; | |
135 GnomeVFSFileSize bytes_written; | |
136 | |
137 result = gnome_vfs_write(file->handle, ptr, size * nmemb, &bytes_written); | |
138 if (result == GNOME_VFS_OK) | |
139 return bytes_written; | |
140 else | |
141 return -1; | |
142 } | |
143 | |
144 gint | |
145 vfs_fseek(VFSFile * file, | |
146 glong offset, | |
147 gint whence) | |
148 { | |
149 GnomeVFSResult result; | |
150 GnomeVFSSeekPosition g_whence; | |
151 | |
152 switch (whence) { | |
153 case SEEK_SET: | |
154 g_whence = GNOME_VFS_SEEK_START; | |
155 break; | |
156 case SEEK_CUR: | |
157 g_whence = GNOME_VFS_SEEK_CURRENT; | |
158 break; | |
159 case SEEK_END: | |
160 g_whence = GNOME_VFS_SEEK_END; | |
161 break; | |
162 default: | |
163 g_warning("vfs_fseek: invalid whence value"); | |
164 return -1; | |
165 } | |
166 | |
167 result = gnome_vfs_seek(file->handle, g_whence, offset); | |
168 | |
169 if (result == GNOME_VFS_OK) | |
170 return 0; | |
171 else | |
172 return -1; | |
173 } | |
174 | |
175 void | |
176 vfs_rewind(VFSFile * file) | |
177 { | |
178 vfs_fseek(file, 0L, SEEK_SET); | |
179 } | |
180 | |
181 glong | |
182 vfs_ftell(VFSFile * file) | |
183 { | |
184 GnomeVFSResult result; | |
185 GnomeVFSFileSize position; | |
186 | |
187 result = gnome_vfs_tell(file->handle, &position); | |
188 | |
189 if (result == GNOME_VFS_OK) | |
190 return position; | |
191 else | |
192 return -1; | |
193 } | |
194 | |
195 gboolean | |
196 vfs_file_test(const gchar * path, | |
197 GFileTest test) | |
198 { | |
199 GnomeVFSResult result; | |
200 GnomeVFSFileInfo info; | |
201 GFileTest file_test; | |
202 | |
203 result = gnome_vfs_get_file_info(path, &info, | |
204 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS); | |
205 | |
206 if (test == G_FILE_TEST_EXISTS) | |
207 return (result == GNOME_VFS_OK) ? TRUE : FALSE; | |
208 else if (test == G_FILE_TEST_IS_EXECUTABLE) | |
209 return (info.permissions & GNOME_VFS_PERM_ACCESS_EXECUTABLE) | |
210 ? TRUE : FALSE; | |
211 | |
212 switch (info.type) { | |
213 case GNOME_VFS_FILE_TYPE_REGULAR: | |
214 file_test = G_FILE_TEST_IS_REGULAR; | |
215 break; | |
216 case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: | |
217 file_test = G_FILE_TEST_IS_SYMLINK; | |
218 break; | |
219 case GNOME_VFS_FILE_TYPE_DIRECTORY: | |
220 file_test = G_FILE_TEST_IS_DIR; | |
221 default: | |
222 return FALSE; | |
223 } | |
224 | |
225 if (test == file_test) | |
226 return TRUE; | |
227 else | |
228 return FALSE; | |
229 } | |
230 | |
231 gboolean | |
232 vfs_is_writeable(const gchar * path) | |
233 { | |
234 GnomeVFSFileInfo info; | |
235 | |
236 if (gnome_vfs_get_file_info(path, &info, GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS) | |
237 != GNOME_VFS_OK) { | |
238 return FALSE; | |
239 } | |
240 | |
241 return (info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE ? TRUE : FALSE); | |
242 } | |
243 | |
244 gint | |
245 vfs_truncate(VFSFile * file, | |
246 glong length) | |
247 { | |
248 GnomeVFSResult result; | |
249 | |
250 result = gnome_vfs_truncate_handle(file->handle, (GnomeVFSFileSize) length); | |
251 | |
252 if (result == GNOME_VFS_OK) | |
253 return 0; | |
254 else | |
255 return -1; | |
256 } | |
257 | |
258 static gchar *strdup_exclude_chars(const gchar * s, | |
259 const gchar * c); | |
260 | |
261 static void | |
262 mode_to_gnome_vfs(const gchar * mode, | |
263 GnomeVFSOpenMode * g_mode, | |
264 gboolean * truncate, | |
265 gboolean * append) | |
266 { | |
267 gchar *s; | |
268 | |
269 *g_mode = GNOME_VFS_OPEN_RANDOM; | |
270 *truncate = *append = FALSE; | |
271 | |
272 s = strdup_exclude_chars(mode, "bt"); | |
273 switch (s[0]) { | |
274 case 'r': | |
275 *g_mode |= GNOME_VFS_OPEN_READ; | |
276 | |
277 if (s[1] == '+') | |
278 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
279 | |
280 break; | |
281 case 'w': | |
282 case 'a': | |
283 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
284 | |
285 if (s[0] == 'w') | |
286 *truncate = TRUE; | |
287 else | |
288 *append = TRUE; | |
289 | |
290 if (s[1] == '+') | |
291 *g_mode |= GNOME_VFS_OPEN_READ; | |
292 | |
293 break; | |
294 default: | |
295 g_warning("mode_to_gnome_vfs: unhandled mode character"); | |
296 } | |
297 g_free(s); | |
298 } | |
299 | |
300 static gchar * | |
301 strdup_exclude_chars(const gchar * s, | |
302 const gchar * c) | |
303 { | |
304 gint i, j, k; | |
305 gint newlen = 0; | |
306 gchar *newstr; | |
307 gboolean found; | |
308 | |
309 /* Calculate number of chars in new string */ | |
310 for (i = 0; s[i] != '\0'; i++) { | |
311 found = FALSE; | |
312 | |
313 for (j = 0; j < strlen(c) && !found; j++) | |
314 if (s[i] == c[j]) | |
315 found = TRUE; | |
316 | |
317 if (!found) | |
318 newlen++; | |
319 } | |
320 | |
321 newstr = g_malloc(newlen + 1); | |
322 | |
323 /* Copy valid chars to new string */ | |
324 for (i = k = 0; s[i] != '\0'; i++) { | |
325 found = FALSE; | |
326 | |
327 for (j = 0; j < strlen(c) && !found; j++) | |
328 if (s[i] == c[j]) | |
329 found = TRUE; | |
330 | |
331 if (!found) | |
332 newstr[k++] = s[i]; | |
333 } | |
334 | |
335 newstr[k] = '\0'; | |
336 | |
337 return newstr; | |
338 } |