Mercurial > audlegacy
annotate src/audacious/vfs.c @ 2335:e80c9dfc93aa trunk
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
different systems (strsplit nul-terminates on uclibc). process URIs
in a different way, as a result.
author | nenolod |
---|---|
date | Mon, 15 Jan 2007 08:44:39 -0800 |
parents | 3149d4b1a9a9 |
children | b332cdd2ea43 |
rev | line source |
---|---|
2313 | 1 /* Audacious |
2 * Copyright (c) 2006-2007 William Pitcock | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; under version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * You should have received a copy of the GNU General Public License | |
14 * along with this program; if not, write to the Free Software | |
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
16 */ | |
17 | |
18 #include "vfs.h" | |
19 #include <stdio.h> | |
20 | |
21 #include <unistd.h> | |
22 #include <sys/stat.h> | |
23 #include <sys/types.h> | |
24 | |
25 #include "libaudacious/urldecode.h" | |
26 | |
27 static GList *vfs_transports = NULL; | |
28 | |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
29 #define VFS_DEBUG |
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
30 |
2313 | 31 #ifdef VFS_DEBUG |
32 # define DBG(x, args...) g_print(x, ## args); | |
33 #else | |
34 # define DBG(x, args...) | |
35 #endif | |
36 | |
37 /** | |
38 * vfs_register_transport: | |
39 * @vtable: The #VFSConstructor vtable to register. | |
40 * | |
41 * Registers a #VFSConstructor vtable with the VFS system. | |
42 * | |
43 * Return value: TRUE on success, FALSE on failure. | |
44 **/ | |
45 gboolean | |
46 vfs_register_transport(VFSConstructor *vtable) | |
47 { | |
48 vfs_transports = g_list_append(vfs_transports, vtable); | |
49 | |
50 return TRUE; | |
51 } | |
52 | |
53 /** | |
54 * vfs_fopen: | |
55 * @path: The path or URI to open. | |
56 * @mode: The preferred access privileges (not guaranteed). | |
57 * | |
58 * Opens a stream from a VFS transport using a #VFSConstructor. | |
59 * | |
60 * Return value: On success, a #VFSFile object representing the stream. | |
61 **/ | |
62 VFSFile * | |
63 vfs_fopen(const gchar * path, | |
64 const gchar * mode) | |
65 { | |
66 VFSFile *file; | |
67 VFSConstructor *vtable = NULL; | |
68 GList *node; | |
69 gchar *decpath; | |
70 | |
71 if (!path || !mode) | |
72 return NULL; | |
73 | |
74 decpath = xmms_urldecode_plain(path); | |
75 | |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
76 for (node = vfs_transports; node != NULL; node = g_list_next(node)) |
2313 | 77 { |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
78 vtable = (VFSConstructor *) node->data; |
2313 | 79 |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
80 if (!strncasecmp(decpath, vtable->uri_id, strlen(vtable->uri_id))) |
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
81 break; |
2313 | 82 } |
83 | |
84 /* no transport vtable has been registered, bail. */ | |
85 if (vtable == NULL) | |
86 return NULL; | |
87 | |
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
88 file = vtable->vfs_fopen_impl(decpath + strlen(vtable->uri_id), mode); |
2313 | 89 |
90 if (file == NULL) | |
91 return NULL; | |
92 | |
93 file->uri = g_strdup(path); | |
94 file->base = vtable; | |
95 | |
96 g_free(decpath); | |
97 | |
98 return file; | |
99 } | |
100 | |
101 /** | |
102 * vfs_fclose: | |
103 * @file: A #VFSFile object to destroy. | |
104 * | |
105 * Closes a VFS stream and destroys a #VFSFile object. | |
106 * | |
107 * Return value: -1 on failure, 0 on success. | |
108 **/ | |
109 gint | |
110 vfs_fclose(VFSFile * file) | |
111 { | |
112 gint ret = 0; | |
113 | |
114 if (file == NULL) | |
115 return -1; | |
116 | |
117 if (file->base->vfs_fclose_impl(file) != 0) | |
118 ret = -1; | |
119 | |
120 if (file->uri != NULL) | |
121 g_free(file->uri); | |
122 | |
123 g_free(file); | |
124 | |
125 return ret; | |
126 } | |
127 | |
128 /** | |
129 * vfs_fread: | |
130 * @ptr: A pointer to the destination buffer. | |
131 * @size: The size of each element to read. | |
132 * @nmemb: The number of elements to read. | |
133 * @file: #VFSFile object that represents the VFS stream. | |
134 * | |
135 * Reads from a VFS stream. | |
136 * | |
137 * Return value: The amount of elements succesfully read. | |
138 **/ | |
139 size_t | |
140 vfs_fread(gpointer ptr, | |
141 size_t size, | |
142 size_t nmemb, | |
143 VFSFile * file) | |
144 { | |
145 if (file == NULL) | |
146 return 0; | |
147 | |
148 return file->base->vfs_fread_impl(ptr, size, nmemb, file); | |
149 } | |
150 | |
151 /** | |
152 * vfs_fwrite: | |
153 * @ptr: A const pointer to the source buffer. | |
154 * @size: The size of each element to write. | |
155 * @nmemb: The number of elements to write. | |
156 * @file: #VFSFile object that represents the VFS stream. | |
157 * | |
158 * Writes to a VFS stream. | |
159 * | |
160 * Return value: The amount of elements succesfully written. | |
161 **/ | |
162 size_t | |
163 vfs_fwrite(gconstpointer ptr, | |
164 size_t size, | |
165 size_t nmemb, | |
166 VFSFile * file) | |
167 { | |
168 if (file == NULL) | |
169 return 0; | |
170 | |
171 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); | |
172 } | |
173 | |
174 /** | |
175 * vfs_getc: | |
176 * @stream: #VFSFile object that represents the VFS stream. | |
177 * | |
178 * Reads a character from a VFS stream. | |
179 * | |
180 * Return value: On success, a character. Otherwise, -1. | |
181 **/ | |
182 gint | |
183 vfs_getc(VFSFile *stream) | |
184 { | |
185 if (stream == NULL) | |
186 return -1; | |
187 | |
188 return stream->base->vfs_getc_impl(stream); | |
189 } | |
190 | |
191 /** | |
192 * vfs_ungetc: | |
193 * @c: The character to push back. | |
194 * @stream: #VFSFile object that represents the VFS stream. | |
195 * | |
196 * Pushes a character back to the VFS stream. | |
197 * | |
198 * Return value: On success, 0. Otherwise, -1. | |
199 **/ | |
200 gint | |
201 vfs_ungetc(gint c, VFSFile *stream) | |
202 { | |
203 if (stream == NULL) | |
204 return -1; | |
205 | |
206 return stream->base->vfs_ungetc_impl(c, stream); | |
207 } | |
208 | |
209 /** | |
210 * vfs_fseek: | |
211 * @file: #VFSFile object that represents the VFS stream. | |
212 * @offset: The offset to seek to. | |
213 * @whence: Whether or not the seek is absolute or not. | |
214 * | |
215 * Seeks through a VFS stream. | |
216 * | |
217 * Return value: On success, 1. Otherwise, 0. | |
218 **/ | |
219 gint | |
220 vfs_fseek(VFSFile * file, | |
221 glong offset, | |
222 gint whence) | |
223 { | |
224 if (file == NULL) | |
225 return 0; | |
226 | |
227 return file->base->vfs_fseek_impl(file, offset, whence); | |
228 } | |
229 | |
230 /** | |
231 * vfs_rewind: | |
232 * @file: #VFSFile object that represents the VFS stream. | |
233 * | |
234 * Rewinds a VFS stream. | |
235 **/ | |
236 void | |
237 vfs_rewind(VFSFile * file) | |
238 { | |
239 if (file == NULL) | |
240 return; | |
241 | |
242 file->base->vfs_rewind_impl(file); | |
243 } | |
244 | |
245 /** | |
246 * vfs_ftell: | |
247 * @file: #VFSFile object that represents the VFS stream. | |
248 * | |
249 * Returns the current position in the VFS stream's buffer. | |
250 * | |
251 * Return value: On success, the current position. Otherwise, -1. | |
252 **/ | |
253 glong | |
254 vfs_ftell(VFSFile * file) | |
255 { | |
256 if (file == NULL) | |
257 return -1; | |
258 | |
259 return file->base->vfs_ftell_impl(file); | |
260 } | |
261 | |
262 /** | |
263 * vfs_feof: | |
264 * @file: #VFSFile object that represents the VFS stream. | |
265 * | |
266 * Returns whether or not the VFS stream has reached EOF. | |
267 * | |
268 * Return value: On success, whether or not the VFS stream is at EOF. Otherwise, FALSE. | |
269 **/ | |
270 gboolean | |
271 vfs_feof(VFSFile * file) | |
272 { | |
273 if (file == NULL) | |
274 return FALSE; | |
275 | |
276 return (gboolean) file->base->vfs_feof_impl(file); | |
277 } | |
278 | |
279 /** | |
280 * vfs_truncate: | |
281 * @file: #VFSFile object that represents the VFS stream. | |
282 * @length: The length to truncate at. | |
283 * | |
284 * Truncates a VFS stream to a certain size. | |
285 * | |
286 * Return value: On success, 0. Otherwise, -1. | |
287 **/ | |
288 gint | |
289 vfs_truncate(VFSFile * file, glong length) | |
290 { | |
291 if (file == NULL) | |
292 return -1; | |
293 | |
294 return file->base->vfs_truncate_impl(file, length); | |
295 } | |
296 | |
297 /** | |
298 * vfs_file_test: | |
299 * @path: A path to test. | |
300 * @test: A GFileTest to run. | |
301 * | |
302 * Wrapper for g_file_test(). | |
303 * | |
304 * Return value: The result of g_file_test(). | |
305 **/ | |
306 gboolean | |
307 vfs_file_test(const gchar * path, GFileTest test) | |
308 { | |
309 return g_file_test(path, test); | |
310 } | |
311 | |
312 /** | |
313 * vfs_is_writeable: | |
314 * @path: A path to test. | |
315 * | |
316 * Tests if a file is writeable. | |
317 * | |
318 * Return value: TRUE if the file is writeable, otherwise FALSE. | |
319 **/ | |
320 gboolean | |
321 vfs_is_writeable(const gchar * path) | |
322 { | |
323 struct stat info; | |
324 | |
325 if (stat(path, &info) == -1) | |
326 return FALSE; | |
327 | |
328 return (info.st_mode & S_IWUSR); | |
329 } |