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