Mercurial > audlegacy
annotate libaudacious/vfs.c @ 2041:ed1d3dde7130 trunk
[svn] Added to credits :P
author | nazca |
---|---|
date | Mon, 04 Dec 2006 01:01:13 -0800 (2006-12-04) |
parents | c43fb0845b71 |
children | 8aaf0f145578 |
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 | |
1997 | 26 static GList *vfs_transports = NULL; |
27 | |
28 #ifdef VFS_DEBUG | |
29 # define DBG(x, args...) g_print(x, ## args); | |
30 #else | |
31 # define DBG(x, args...) | |
32 #endif | |
33 | |
2034 | 34 /** |
35 * vfs_register_transport: | |
36 * @vtable: The #VFSConstructor vtable to register. | |
2032 | 37 * |
2034 | 38 * Registers a #VFSConstructor vtable with the VFS system. |
2032 | 39 * |
2034 | 40 * Return value: TRUE on success, FALSE on failure. |
41 **/ | |
0 | 42 gboolean |
1997 | 43 vfs_register_transport(VFSConstructor *vtable) |
0 | 44 { |
1997 | 45 vfs_transports = g_list_append(vfs_transports, vtable); |
46 | |
0 | 47 return TRUE; |
48 } | |
49 | |
2034 | 50 /** |
51 * vfs_fopen: | |
52 * @path: The path or URI to open. | |
53 * @mode: The preferred access privileges (not guaranteed). | |
2032 | 54 * |
2034 | 55 * Opens a stream from a VFS transport using a #VFSConstructor. |
2032 | 56 * |
2034 | 57 * Return value: On success, a #VFSFile object representing the stream. |
58 **/ | |
0 | 59 VFSFile * |
60 vfs_fopen(const gchar * path, | |
61 const gchar * mode) | |
62 { | |
63 VFSFile *file; | |
1997 | 64 gchar **vec; |
65 VFSConstructor *vtable = NULL; | |
66 GList *node; | |
0 | 67 |
550 | 68 if (!path || !mode) |
69 return NULL; | |
70 | |
1997 | 71 vec = g_strsplit(path, "://", 2); |
72 | |
73 /* special case: no transport specified, look for the "/" transport */ | |
74 if (vec[1] == NULL) | |
75 { | |
76 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
77 { | |
78 vtable = (VFSConstructor *) node->data; | |
79 | |
80 if (*vtable->uri_id == '/') | |
81 break; | |
82 } | |
83 } | |
84 else | |
85 { | |
86 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
87 { | |
88 vtable = (VFSConstructor *) node->data; | |
0 | 89 |
1997 | 90 if (!g_strcasecmp(vec[0], vtable->uri_id)) |
91 break; | |
92 } | |
93 } | |
94 | |
95 /* no transport vtable has been registered, bail. */ | |
96 if (vtable == NULL) | |
97 { | |
2031
ebce0d5efac1
[svn] - don't leak the split URI vector after doing the vtable lookup
nenolod
parents:
1999
diff
changeset
|
98 g_strfreev(vec); |
1997 | 99 return NULL; |
0 | 100 } |
101 | |
1997 | 102 file = vtable->vfs_fopen_impl(vec[1] ? vec[1] : vec[0], mode); |
103 | |
104 if (file == NULL) | |
105 { | |
2032 | 106 g_strfreev(vec); |
1997 | 107 return NULL; |
108 } | |
109 | |
110 file->uri = g_strdup(path); | |
111 file->base = vtable; | |
112 | |
2031
ebce0d5efac1
[svn] - don't leak the split URI vector after doing the vtable lookup
nenolod
parents:
1999
diff
changeset
|
113 g_strfreev(vec); |
ebce0d5efac1
[svn] - don't leak the split URI vector after doing the vtable lookup
nenolod
parents:
1999
diff
changeset
|
114 |
0 | 115 return file; |
116 } | |
117 | |
2034 | 118 /** |
119 * vfs_fclose: | |
120 * @file: A #VFSFile object to destroy. | |
2032 | 121 * |
2034 | 122 * Closes a VFS stream and destroys a #VFSFile object. |
2032 | 123 * |
2034 | 124 * Return value: -1 on failure, 0 on success. |
125 **/ | |
0 | 126 gint |
127 vfs_fclose(VFSFile * file) | |
128 { | |
129 gint ret = 0; | |
130 | |
821 | 131 if (file == NULL) |
132 return -1; | |
133 | |
1997 | 134 if (file->base->vfs_fclose_impl(file) != 0) |
135 ret = -1; | |
0 | 136 |
1999 | 137 if (file->uri != NULL) |
138 g_free(file->uri); | |
139 | |
0 | 140 g_free(file); |
141 | |
142 return ret; | |
143 } | |
144 | |
2034 | 145 /** |
146 * vfs_fread: | |
147 * @ptr: A pointer to the destination buffer. | |
148 * @size: The size of each element to read. | |
149 * @nmemb: The number of elements to read. | |
150 * @file: #VFSFile object that represents the VFS stream. | |
2032 | 151 * |
152 * Reads from a VFS stream. | |
153 * | |
2034 | 154 * Return value: The amount of elements succesfully read. |
155 **/ | |
0 | 156 size_t |
157 vfs_fread(gpointer ptr, | |
158 size_t size, | |
159 size_t nmemb, | |
160 VFSFile * file) | |
161 { | |
821 | 162 if (file == NULL) |
163 return 0; | |
164 | |
1997 | 165 return file->base->vfs_fread_impl(ptr, size, nmemb, file); |
0 | 166 } |
167 | |
2034 | 168 /** |
169 * vfs_fwrite: | |
170 * @ptr: A const pointer to the source buffer. | |
171 * @size: The size of each element to write. | |
172 * @nmemb: The number of elements to write. | |
173 * @file: #VFSFile object that represents the VFS stream. | |
2032 | 174 * |
175 * Writes to a VFS stream. | |
176 * | |
2034 | 177 * Return value: The amount of elements succesfully written. |
178 **/ | |
0 | 179 size_t |
180 vfs_fwrite(gconstpointer ptr, | |
181 size_t size, | |
182 size_t nmemb, | |
183 VFSFile * file) | |
184 { | |
821 | 185 if (file == NULL) |
186 return 0; | |
187 | |
1997 | 188 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); |
0 | 189 } |
190 | |
2034 | 191 /** |
192 * vfs_getc: | |
193 * @stream: #VFSFile object that represents the VFS stream. | |
2032 | 194 * |
195 * Reads a character from a VFS stream. | |
196 * | |
2034 | 197 * Return value: On success, a character. Otherwise, -1. |
198 **/ | |
0 | 199 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
|
200 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
|
201 { |
1997 | 202 if (stream == NULL) |
203 return -1; | |
204 | |
205 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
|
206 } |
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 |
2034 | 208 /** |
209 * vfs_ungetc: | |
210 * @stream: #VFSFile object that represents the VFS stream. | |
2032 | 211 * |
212 * Pushes a character back to the VFS stream. | |
213 * | |
2034 | 214 * Return value: On success, 0. Otherwise, -1. |
215 **/ | |
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
|
216 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
|
217 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
|
218 { |
1997 | 219 if (stream == NULL) |
220 return -1; | |
221 | |
222 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
|
223 } |
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 |
2034 | 225 /** |
226 * vfs_fseek: | |
227 * @file: #VFSFile object that represents the VFS stream. | |
228 * @offset: The offset to seek to. | |
229 * @whence: Whether or not the seek is absolute or not. | |
2032 | 230 * |
231 * Seeks through a VFS stream. | |
232 * | |
2034 | 233 * Return value: On success, 1. Otherwise, 0. |
234 **/ | |
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
|
235 gint |
0 | 236 vfs_fseek(VFSFile * file, |
237 glong offset, | |
238 gint whence) | |
239 { | |
821 | 240 if (file == NULL) |
241 return 0; | |
242 | |
1997 | 243 return file->base->vfs_fseek_impl(file, offset, whence); |
0 | 244 } |
245 | |
2034 | 246 /** |
247 * vfs_rewind: | |
248 * @file: #VFSFile object that represents the VFS stream. | |
2032 | 249 * |
250 * Rewinds a VFS stream. | |
2034 | 251 **/ |
0 | 252 void |
253 vfs_rewind(VFSFile * file) | |
254 { | |
821 | 255 if (file == NULL) |
256 return; | |
257 | |
1997 | 258 file->base->vfs_rewind_impl(file); |
0 | 259 } |
260 | |
2034 | 261 /** |
262 * vfs_fseek: | |
263 * @file: #VFSFile object that represents the VFS stream. | |
2032 | 264 * |
2034 | 265 * Returns the current position in the VFS stream's buffer. |
2032 | 266 * |
2034 | 267 * Return value: On success, the current position. Otherwise, -1. |
268 **/ | |
0 | 269 glong |
270 vfs_ftell(VFSFile * file) | |
271 { | |
821 | 272 if (file == NULL) |
2032 | 273 return -1; |
821 | 274 |
1997 | 275 return file->base->vfs_ftell_impl(file); |
0 | 276 } |
277 | |
2034 | 278 /** |
279 * vfs_feof: | |
280 * @file: #VFSFile object that represents the VFS stream. | |
2032 | 281 * |
282 * Returns whether or not the VFS stream has reached EOF. | |
283 * | |
2034 | 284 * Return value: On success, whether or not the VFS stream is at EOF. Otherwise, FALSE. |
285 **/ | |
0 | 286 gboolean |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
287 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
|
288 { |
821 | 289 if (file == NULL) |
290 return FALSE; | |
291 | |
1997 | 292 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
|
293 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
294 |
2034 | 295 /** |
296 * vfs_truncate: | |
297 * @file: #VFSFile object that represents the VFS stream. | |
298 * @size: The length to truncate at. | |
2032 | 299 * |
300 * Truncates a VFS stream to a certain size. | |
301 * | |
2034 | 302 * Return value: On success, 0. Otherwise, -1. |
303 **/ | |
2032 | 304 gint |
305 vfs_truncate(VFSFile * file, glong size) | |
306 { | |
307 if (file == NULL) | |
308 return -1; | |
309 | |
310 return file->base->vfs_truncate_impl(file, size); | |
311 } | |
312 | |
2034 | 313 /** |
314 * vfs_file_test: | |
315 * @path: A path to test. | |
316 * @test: A GFileTest to run. | |
2032 | 317 * |
318 * Wrapper for g_file_test(). | |
319 * | |
2034 | 320 * Return value: The result of g_file_test(). |
321 **/ | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
322 gboolean |
0 | 323 vfs_file_test(const gchar * path, GFileTest test) |
324 { | |
325 return g_file_test(path, test); | |
326 } | |
327 | |
2034 | 328 /** |
329 * vfs_is_writable: | |
330 * @path: A path to test. | |
2032 | 331 * |
332 * Tests if a file is writable. | |
333 * | |
2034 | 334 * Return value: TRUE if the file is writable, otherwise FALSE. |
335 **/ | |
0 | 336 gboolean |
337 vfs_is_writeable(const gchar * path) | |
338 { | |
339 struct stat info; | |
340 | |
341 if (stat(path, &info) == -1) | |
342 return FALSE; | |
343 | |
344 return (info.st_mode & S_IWUSR); | |
345 } |