Mercurial > audlegacy
annotate src/audacious/vfs.c @ 2574:40407b7363f3 trunk
[svn] - enforce playback stop when clearing the playlist to load new files in. closes #808.
| author | nenolod |
|---|---|
| date | Sun, 25 Feb 2007 00:53:19 -0800 |
| parents | 07b990906823 |
| children | 9713b5a67cba |
| 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 | |
| 2336 | 25 #include <string.h> |
| 26 | |
|
2416
0fd7f4f969ad
[svn] integrated urldecode.* from libaudacious into audacious directory, made separate ui_fileopener.*
mf0102
parents:
2381
diff
changeset
|
27 #include "urldecode.h" |
| 2313 | 28 |
| 29 static GList *vfs_transports = NULL; | |
| 30 | |
| 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 | |
|
2381
d49913587458
[svn] - changes to the nature of URI handling in the VFS subsystem, will be
nenolod
parents:
2376
diff
changeset
|
74 decpath = g_strdup(path); |
| 2313 | 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 { |
| 2534 | 78 VFSConstructor *vtptr = (VFSConstructor *) node->data; |
| 2313 | 79 |
|
2533
ab335d4391df
[svn] - don't return a bogus vtable for an unregistered URI scheme
nenolod
parents:
2416
diff
changeset
|
80 if (!strncasecmp(decpath, vtptr->uri_id, strlen(vtptr->uri_id))) |
|
ab335d4391df
[svn] - don't return a bogus vtable for an unregistered URI scheme
nenolod
parents:
2416
diff
changeset
|
81 { |
|
ab335d4391df
[svn] - don't return a bogus vtable for an unregistered URI scheme
nenolod
parents:
2416
diff
changeset
|
82 vtable = vtptr; |
|
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
83 break; |
|
2533
ab335d4391df
[svn] - don't return a bogus vtable for an unregistered URI scheme
nenolod
parents:
2416
diff
changeset
|
84 } |
| 2313 | 85 } |
| 86 | |
| 87 /* no transport vtable has been registered, bail. */ | |
| 88 if (vtable == NULL) | |
| 89 return NULL; | |
| 90 | |
|
2381
d49913587458
[svn] - changes to the nature of URI handling in the VFS subsystem, will be
nenolod
parents:
2376
diff
changeset
|
91 file = vtable->vfs_fopen_impl(decpath, mode); |
| 2313 | 92 |
| 93 if (file == NULL) | |
| 94 return NULL; | |
| 95 | |
| 96 file->uri = g_strdup(path); | |
| 97 file->base = vtable; | |
|
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
98 file->ref = 1; |
| 2313 | 99 |
| 100 g_free(decpath); | |
| 101 | |
| 102 return file; | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * vfs_fclose: | |
| 107 * @file: A #VFSFile object to destroy. | |
| 108 * | |
| 109 * Closes a VFS stream and destroys a #VFSFile object. | |
| 110 * | |
| 111 * Return value: -1 on failure, 0 on success. | |
| 112 **/ | |
| 113 gint | |
| 114 vfs_fclose(VFSFile * file) | |
| 115 { | |
| 116 gint ret = 0; | |
| 117 | |
| 118 if (file == NULL) | |
| 119 return -1; | |
| 120 | |
|
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
121 if (--file->ref > 0) |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
122 return -1; |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
123 |
| 2313 | 124 if (file->base->vfs_fclose_impl(file) != 0) |
| 125 ret = -1; | |
| 126 | |
| 127 if (file->uri != NULL) | |
| 128 g_free(file->uri); | |
| 129 | |
| 130 g_free(file); | |
| 131 | |
| 132 return ret; | |
| 133 } | |
| 134 | |
| 135 /** | |
| 136 * vfs_fread: | |
| 137 * @ptr: A pointer to the destination buffer. | |
| 138 * @size: The size of each element to read. | |
| 139 * @nmemb: The number of elements to read. | |
| 140 * @file: #VFSFile object that represents the VFS stream. | |
| 141 * | |
| 142 * Reads from a VFS stream. | |
| 143 * | |
| 144 * Return value: The amount of elements succesfully read. | |
| 145 **/ | |
| 146 size_t | |
| 147 vfs_fread(gpointer ptr, | |
| 148 size_t size, | |
| 149 size_t nmemb, | |
| 150 VFSFile * file) | |
| 151 { | |
| 152 if (file == NULL) | |
| 153 return 0; | |
| 154 | |
| 155 return file->base->vfs_fread_impl(ptr, size, nmemb, file); | |
| 156 } | |
| 157 | |
| 158 /** | |
| 159 * vfs_fwrite: | |
| 160 * @ptr: A const pointer to the source buffer. | |
| 161 * @size: The size of each element to write. | |
| 162 * @nmemb: The number of elements to write. | |
| 163 * @file: #VFSFile object that represents the VFS stream. | |
| 164 * | |
| 165 * Writes to a VFS stream. | |
| 166 * | |
| 167 * Return value: The amount of elements succesfully written. | |
| 168 **/ | |
| 169 size_t | |
| 170 vfs_fwrite(gconstpointer ptr, | |
| 171 size_t size, | |
| 172 size_t nmemb, | |
| 173 VFSFile * file) | |
| 174 { | |
| 175 if (file == NULL) | |
| 176 return 0; | |
| 177 | |
| 178 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); | |
| 179 } | |
| 180 | |
| 181 /** | |
| 182 * vfs_getc: | |
| 183 * @stream: #VFSFile object that represents the VFS stream. | |
| 184 * | |
| 185 * Reads a character from a VFS stream. | |
| 186 * | |
| 187 * Return value: On success, a character. Otherwise, -1. | |
| 188 **/ | |
| 189 gint | |
| 190 vfs_getc(VFSFile *stream) | |
| 191 { | |
| 192 if (stream == NULL) | |
| 193 return -1; | |
| 194 | |
| 195 return stream->base->vfs_getc_impl(stream); | |
| 196 } | |
| 197 | |
| 198 /** | |
| 199 * vfs_ungetc: | |
| 200 * @c: The character to push back. | |
| 201 * @stream: #VFSFile object that represents the VFS stream. | |
| 202 * | |
| 203 * Pushes a character back to the VFS stream. | |
| 204 * | |
| 205 * Return value: On success, 0. Otherwise, -1. | |
| 206 **/ | |
| 207 gint | |
| 208 vfs_ungetc(gint c, VFSFile *stream) | |
| 209 { | |
| 210 if (stream == NULL) | |
| 211 return -1; | |
| 212 | |
| 213 return stream->base->vfs_ungetc_impl(c, stream); | |
| 214 } | |
| 215 | |
| 216 /** | |
| 217 * vfs_fseek: | |
| 218 * @file: #VFSFile object that represents the VFS stream. | |
| 219 * @offset: The offset to seek to. | |
| 220 * @whence: Whether or not the seek is absolute or not. | |
| 221 * | |
| 222 * Seeks through a VFS stream. | |
| 223 * | |
| 224 * Return value: On success, 1. Otherwise, 0. | |
| 225 **/ | |
| 226 gint | |
| 227 vfs_fseek(VFSFile * file, | |
| 228 glong offset, | |
| 229 gint whence) | |
| 230 { | |
| 231 if (file == NULL) | |
| 232 return 0; | |
| 233 | |
| 234 return file->base->vfs_fseek_impl(file, offset, whence); | |
| 235 } | |
| 236 | |
| 237 /** | |
| 238 * vfs_rewind: | |
| 239 * @file: #VFSFile object that represents the VFS stream. | |
| 240 * | |
| 241 * Rewinds a VFS stream. | |
| 242 **/ | |
| 243 void | |
| 244 vfs_rewind(VFSFile * file) | |
| 245 { | |
| 246 if (file == NULL) | |
| 247 return; | |
| 248 | |
| 249 file->base->vfs_rewind_impl(file); | |
| 250 } | |
| 251 | |
| 252 /** | |
| 253 * vfs_ftell: | |
| 254 * @file: #VFSFile object that represents the VFS stream. | |
| 255 * | |
| 256 * Returns the current position in the VFS stream's buffer. | |
| 257 * | |
| 258 * Return value: On success, the current position. Otherwise, -1. | |
| 259 **/ | |
| 260 glong | |
| 261 vfs_ftell(VFSFile * file) | |
| 262 { | |
| 263 if (file == NULL) | |
| 264 return -1; | |
| 265 | |
| 266 return file->base->vfs_ftell_impl(file); | |
| 267 } | |
| 268 | |
| 269 /** | |
| 270 * vfs_feof: | |
| 271 * @file: #VFSFile object that represents the VFS stream. | |
| 272 * | |
| 273 * Returns whether or not the VFS stream has reached EOF. | |
| 274 * | |
| 275 * Return value: On success, whether or not the VFS stream is at EOF. Otherwise, FALSE. | |
| 276 **/ | |
| 277 gboolean | |
| 278 vfs_feof(VFSFile * file) | |
| 279 { | |
| 280 if (file == NULL) | |
| 281 return FALSE; | |
| 282 | |
| 283 return (gboolean) file->base->vfs_feof_impl(file); | |
| 284 } | |
| 285 | |
| 286 /** | |
| 287 * vfs_truncate: | |
| 288 * @file: #VFSFile object that represents the VFS stream. | |
| 289 * @length: The length to truncate at. | |
| 290 * | |
| 291 * Truncates a VFS stream to a certain size. | |
| 292 * | |
| 293 * Return value: On success, 0. Otherwise, -1. | |
| 294 **/ | |
| 295 gint | |
| 296 vfs_truncate(VFSFile * file, glong length) | |
| 297 { | |
| 298 if (file == NULL) | |
| 299 return -1; | |
| 300 | |
| 301 return file->base->vfs_truncate_impl(file, length); | |
| 302 } | |
| 303 | |
| 304 /** | |
|
2376
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
305 * vfs_get_metadata: |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
306 * @file: #VFSFile object that represents the VFS stream. |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
307 * @field: The string constant field name to get. |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
308 * |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
309 * Returns metadata about the stream. |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
310 * |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
311 * Return value: On success, a copy of the value of the |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
312 * field. Otherwise, NULL. |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
313 **/ |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
314 gchar * |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
315 vfs_get_metadata(VFSFile * file, const gchar * field) |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
316 { |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
317 if (file == NULL) |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
318 return NULL; |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
319 |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
320 if (file->base->vfs_get_metadata_impl) |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
321 return file->base->vfs_get_metadata_impl(file, field); |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
322 return NULL; |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
323 } |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
324 |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
325 /** |
| 2313 | 326 * vfs_file_test: |
| 327 * @path: A path to test. | |
| 328 * @test: A GFileTest to run. | |
| 329 * | |
| 330 * Wrapper for g_file_test(). | |
| 331 * | |
| 332 * Return value: The result of g_file_test(). | |
| 333 **/ | |
| 334 gboolean | |
| 335 vfs_file_test(const gchar * path, GFileTest test) | |
| 336 { | |
| 337 return g_file_test(path, test); | |
| 338 } | |
| 339 | |
| 340 /** | |
| 341 * vfs_is_writeable: | |
| 342 * @path: A path to test. | |
| 343 * | |
| 344 * Tests if a file is writeable. | |
| 345 * | |
| 346 * Return value: TRUE if the file is writeable, otherwise FALSE. | |
| 347 **/ | |
| 348 gboolean | |
| 349 vfs_is_writeable(const gchar * path) | |
| 350 { | |
| 351 struct stat info; | |
| 352 | |
| 353 if (stat(path, &info) == -1) | |
| 354 return FALSE; | |
| 355 | |
| 356 return (info.st_mode & S_IWUSR); | |
| 357 } | |
|
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
358 |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
359 /** |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
360 * vfs_dup: |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
361 * @in: The VFSFile handle to mark as duplicated. |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
362 * |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
363 * Increments the amount of references that are using this FD. |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
364 * References are removed by calling vfs_fclose on the handle returned |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
365 * from this function. |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
366 * If the amount of references reaches zero, then the file will be |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
367 * closed. |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
368 **/ |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
369 VFSFile * |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
370 vfs_dup(VFSFile *in) |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
371 { |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
372 g_return_val_if_fail(in != NULL, NULL); |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
373 |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
374 in->ref++; |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
375 |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
376 return in; |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
377 } |
