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