Mercurial > audlegacy
annotate libaudacious/vfs.c @ 2060:53a3d5db6b58 trunk
[svn] - finish documenting the libaudacious API
author | nenolod |
---|---|
date | Mon, 04 Dec 2006 19:24:14 -0800 |
parents | 8aaf0f145578 |
children | b9c6f1305c99 |
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: | |
2060 | 210 * @c: The character to push back. |
2034 | 211 * @stream: #VFSFile object that represents the VFS stream. |
2032 | 212 * |
213 * Pushes a character back to the VFS stream. | |
214 * | |
2034 | 215 * Return value: On success, 0. Otherwise, -1. |
216 **/ | |
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
|
217 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
|
218 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
|
219 { |
1997 | 220 if (stream == NULL) |
221 return -1; | |
222 | |
223 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
|
224 } |
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 |
2034 | 226 /** |
227 * vfs_fseek: | |
228 * @file: #VFSFile object that represents the VFS stream. | |
229 * @offset: The offset to seek to. | |
230 * @whence: Whether or not the seek is absolute or not. | |
2032 | 231 * |
232 * Seeks through a VFS stream. | |
233 * | |
2034 | 234 * Return value: On success, 1. Otherwise, 0. |
235 **/ | |
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
|
236 gint |
0 | 237 vfs_fseek(VFSFile * file, |
238 glong offset, | |
239 gint whence) | |
240 { | |
821 | 241 if (file == NULL) |
242 return 0; | |
243 | |
1997 | 244 return file->base->vfs_fseek_impl(file, offset, whence); |
0 | 245 } |
246 | |
2034 | 247 /** |
248 * vfs_rewind: | |
249 * @file: #VFSFile object that represents the VFS stream. | |
2032 | 250 * |
251 * Rewinds a VFS stream. | |
2034 | 252 **/ |
0 | 253 void |
254 vfs_rewind(VFSFile * file) | |
255 { | |
821 | 256 if (file == NULL) |
257 return; | |
258 | |
1997 | 259 file->base->vfs_rewind_impl(file); |
0 | 260 } |
261 | |
2034 | 262 /** |
2059 | 263 * vfs_ftell: |
2034 | 264 * @file: #VFSFile object that represents the VFS stream. |
2032 | 265 * |
2034 | 266 * Returns the current position in the VFS stream's buffer. |
2032 | 267 * |
2034 | 268 * Return value: On success, the current position. Otherwise, -1. |
269 **/ | |
0 | 270 glong |
271 vfs_ftell(VFSFile * file) | |
272 { | |
821 | 273 if (file == NULL) |
2032 | 274 return -1; |
821 | 275 |
1997 | 276 return file->base->vfs_ftell_impl(file); |
0 | 277 } |
278 | |
2034 | 279 /** |
280 * vfs_feof: | |
281 * @file: #VFSFile object that represents the VFS stream. | |
2032 | 282 * |
283 * Returns whether or not the VFS stream has reached EOF. | |
284 * | |
2034 | 285 * Return value: On success, whether or not the VFS stream is at EOF. Otherwise, FALSE. |
286 **/ | |
0 | 287 gboolean |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
288 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
|
289 { |
821 | 290 if (file == NULL) |
291 return FALSE; | |
292 | |
1997 | 293 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
|
294 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
295 |
2034 | 296 /** |
297 * vfs_truncate: | |
298 * @file: #VFSFile object that represents the VFS stream. | |
2059 | 299 * @length: The length to truncate at. |
2032 | 300 * |
301 * Truncates a VFS stream to a certain size. | |
302 * | |
2034 | 303 * Return value: On success, 0. Otherwise, -1. |
304 **/ | |
2032 | 305 gint |
2059 | 306 vfs_truncate(VFSFile * file, glong length) |
2032 | 307 { |
308 if (file == NULL) | |
309 return -1; | |
310 | |
2059 | 311 return file->base->vfs_truncate_impl(file, length); |
2032 | 312 } |
313 | |
2034 | 314 /** |
315 * vfs_file_test: | |
316 * @path: A path to test. | |
317 * @test: A GFileTest to run. | |
2032 | 318 * |
319 * Wrapper for g_file_test(). | |
320 * | |
2034 | 321 * Return value: The result of g_file_test(). |
322 **/ | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
323 gboolean |
0 | 324 vfs_file_test(const gchar * path, GFileTest test) |
325 { | |
326 return g_file_test(path, test); | |
327 } | |
328 | |
2034 | 329 /** |
2059 | 330 * vfs_is_writeable: |
2034 | 331 * @path: A path to test. |
2032 | 332 * |
2059 | 333 * Tests if a file is writeable. |
2032 | 334 * |
2059 | 335 * Return value: TRUE if the file is writeable, otherwise FALSE. |
2034 | 336 **/ |
0 | 337 gboolean |
338 vfs_is_writeable(const gchar * path) | |
339 { | |
340 struct stat info; | |
341 | |
342 if (stat(path, &info) == -1) | |
343 return FALSE; | |
344 | |
345 return (info.st_mode & S_IWUSR); | |
346 } |