Mercurial > audlegacy
annotate src/audacious/vfs.c @ 2935:fc8a2ebb2e13 trunk
Fix display of help system.
| author | William Pitcock <nenolod@atheme.org> |
|---|---|
| date | Fri, 29 Jun 2007 08:38:29 -0500 |
| parents | ac22b2cb6013 |
| children | 7d3beedf1db8 6131bf51ee63 |
| 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" | |
| 2579 | 19 #include "strings.h" |
| 2313 | 20 #include <stdio.h> |
| 21 | |
| 22 #include <unistd.h> | |
| 23 #include <sys/stat.h> | |
| 24 #include <sys/types.h> | |
| 25 | |
| 2336 | 26 #include <string.h> |
| 27 | |
|
2416
0fd7f4f969ad
[svn] integrated urldecode.* from libaudacious into audacious directory, made separate ui_fileopener.*
mf0102
parents:
2381
diff
changeset
|
28 #include "urldecode.h" |
| 2313 | 29 |
| 2623 | 30 GList *vfs_transports = NULL; /* temporary. -nenolod */ |
| 2313 | 31 |
| 32 #ifdef VFS_DEBUG | |
| 33 # define DBG(x, args...) g_print(x, ## args); | |
| 34 #else | |
| 35 # define DBG(x, args...) | |
| 36 #endif | |
| 37 | |
| 38 /** | |
| 39 * vfs_register_transport: | |
| 40 * @vtable: The #VFSConstructor vtable to register. | |
| 41 * | |
| 42 * Registers a #VFSConstructor vtable with the VFS system. | |
| 43 * | |
| 44 * Return value: TRUE on success, FALSE on failure. | |
| 45 **/ | |
| 46 gboolean | |
| 47 vfs_register_transport(VFSConstructor *vtable) | |
| 48 { | |
| 49 vfs_transports = g_list_append(vfs_transports, vtable); | |
| 50 | |
| 51 return TRUE; | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * vfs_fopen: | |
| 56 * @path: The path or URI to open. | |
| 57 * @mode: The preferred access privileges (not guaranteed). | |
| 58 * | |
| 59 * Opens a stream from a VFS transport using a #VFSConstructor. | |
| 60 * | |
| 61 * Return value: On success, a #VFSFile object representing the stream. | |
| 62 **/ | |
| 63 VFSFile * | |
| 64 vfs_fopen(const gchar * path, | |
| 65 const gchar * mode) | |
| 66 { | |
| 67 VFSFile *file; | |
| 68 VFSConstructor *vtable = NULL; | |
| 69 GList *node; | |
| 70 gchar *decpath; | |
| 71 | |
| 72 if (!path || !mode) | |
| 73 return NULL; | |
| 74 | |
|
2381
d49913587458
[svn] - changes to the nature of URI handling in the VFS subsystem, will be
nenolod
parents:
2376
diff
changeset
|
75 decpath = g_strdup(path); |
| 2313 | 76 |
|
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
77 for (node = vfs_transports; node != NULL; node = g_list_next(node)) |
| 2313 | 78 { |
| 2534 | 79 VFSConstructor *vtptr = (VFSConstructor *) node->data; |
| 2313 | 80 |
|
2533
ab335d4391df
[svn] - don't return a bogus vtable for an unregistered URI scheme
nenolod
parents:
2416
diff
changeset
|
81 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
|
82 { |
|
ab335d4391df
[svn] - don't return a bogus vtable for an unregistered URI scheme
nenolod
parents:
2416
diff
changeset
|
83 vtable = vtptr; |
|
2335
e80c9dfc93aa
[svn] - g_strsplit() wraps strsplit(3), and thus has different results on
nenolod
parents:
2313
diff
changeset
|
84 break; |
|
2533
ab335d4391df
[svn] - don't return a bogus vtable for an unregistered URI scheme
nenolod
parents:
2416
diff
changeset
|
85 } |
| 2313 | 86 } |
| 87 | |
| 88 /* no transport vtable has been registered, bail. */ | |
| 89 if (vtable == NULL) | |
| 90 return NULL; | |
| 91 | |
|
2381
d49913587458
[svn] - changes to the nature of URI handling in the VFS subsystem, will be
nenolod
parents:
2376
diff
changeset
|
92 file = vtable->vfs_fopen_impl(decpath, mode); |
| 2313 | 93 |
| 94 if (file == NULL) | |
| 95 return NULL; | |
| 96 | |
| 97 file->uri = g_strdup(path); | |
| 98 file->base = vtable; | |
|
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
99 file->ref = 1; |
| 2313 | 100 |
| 101 g_free(decpath); | |
| 102 | |
| 103 return file; | |
| 104 } | |
| 105 | |
| 106 /** | |
| 107 * vfs_fclose: | |
| 108 * @file: A #VFSFile object to destroy. | |
| 109 * | |
| 110 * Closes a VFS stream and destroys a #VFSFile object. | |
| 111 * | |
| 112 * Return value: -1 on failure, 0 on success. | |
| 113 **/ | |
| 114 gint | |
| 115 vfs_fclose(VFSFile * file) | |
| 116 { | |
| 117 gint ret = 0; | |
| 118 | |
| 119 if (file == NULL) | |
| 120 return -1; | |
| 121 | |
|
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
122 if (--file->ref > 0) |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
123 return -1; |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
124 |
| 2313 | 125 if (file->base->vfs_fclose_impl(file) != 0) |
| 126 ret = -1; | |
| 127 | |
| 128 if (file->uri != NULL) | |
| 129 g_free(file->uri); | |
| 130 | |
| 131 g_free(file); | |
| 132 | |
| 133 return ret; | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * vfs_fread: | |
| 138 * @ptr: A pointer to the destination buffer. | |
| 139 * @size: The size of each element to read. | |
| 140 * @nmemb: The number of elements to read. | |
| 141 * @file: #VFSFile object that represents the VFS stream. | |
| 142 * | |
| 143 * Reads from a VFS stream. | |
| 144 * | |
| 145 * Return value: The amount of elements succesfully read. | |
| 146 **/ | |
| 147 size_t | |
| 148 vfs_fread(gpointer ptr, | |
| 149 size_t size, | |
| 150 size_t nmemb, | |
| 151 VFSFile * file) | |
| 152 { | |
| 153 if (file == NULL) | |
| 154 return 0; | |
| 155 | |
| 156 return file->base->vfs_fread_impl(ptr, size, nmemb, file); | |
| 157 } | |
| 158 | |
| 159 /** | |
| 160 * vfs_fwrite: | |
| 161 * @ptr: A const pointer to the source buffer. | |
| 162 * @size: The size of each element to write. | |
| 163 * @nmemb: The number of elements to write. | |
| 164 * @file: #VFSFile object that represents the VFS stream. | |
| 165 * | |
| 166 * Writes to a VFS stream. | |
| 167 * | |
| 168 * Return value: The amount of elements succesfully written. | |
| 169 **/ | |
| 170 size_t | |
| 171 vfs_fwrite(gconstpointer ptr, | |
| 172 size_t size, | |
| 173 size_t nmemb, | |
| 174 VFSFile * file) | |
| 175 { | |
| 176 if (file == NULL) | |
| 177 return 0; | |
| 178 | |
| 179 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); | |
| 180 } | |
| 181 | |
| 182 /** | |
| 183 * vfs_getc: | |
| 184 * @stream: #VFSFile object that represents the VFS stream. | |
| 185 * | |
| 186 * Reads a character from a VFS stream. | |
| 187 * | |
| 188 * Return value: On success, a character. Otherwise, -1. | |
| 189 **/ | |
| 190 gint | |
| 191 vfs_getc(VFSFile *stream) | |
| 192 { | |
| 193 if (stream == NULL) | |
| 194 return -1; | |
| 195 | |
| 196 return stream->base->vfs_getc_impl(stream); | |
| 197 } | |
| 198 | |
| 199 /** | |
| 200 * vfs_ungetc: | |
| 201 * @c: The character to push back. | |
| 202 * @stream: #VFSFile object that represents the VFS stream. | |
| 203 * | |
| 204 * Pushes a character back to the VFS stream. | |
| 205 * | |
| 206 * Return value: On success, 0. Otherwise, -1. | |
| 207 **/ | |
| 208 gint | |
| 209 vfs_ungetc(gint c, VFSFile *stream) | |
| 210 { | |
| 211 if (stream == NULL) | |
| 212 return -1; | |
| 213 | |
| 214 return stream->base->vfs_ungetc_impl(c, stream); | |
| 215 } | |
| 216 | |
| 217 /** | |
| 218 * vfs_fseek: | |
| 219 * @file: #VFSFile object that represents the VFS stream. | |
| 220 * @offset: The offset to seek to. | |
| 221 * @whence: Whether or not the seek is absolute or not. | |
| 222 * | |
| 223 * Seeks through a VFS stream. | |
| 224 * | |
| 225 * Return value: On success, 1. Otherwise, 0. | |
| 226 **/ | |
| 227 gint | |
| 228 vfs_fseek(VFSFile * file, | |
| 229 glong offset, | |
| 230 gint whence) | |
| 231 { | |
| 232 if (file == NULL) | |
| 233 return 0; | |
| 234 | |
| 235 return file->base->vfs_fseek_impl(file, offset, whence); | |
| 236 } | |
| 237 | |
| 238 /** | |
| 239 * vfs_rewind: | |
| 240 * @file: #VFSFile object that represents the VFS stream. | |
| 241 * | |
| 242 * Rewinds a VFS stream. | |
| 243 **/ | |
| 244 void | |
| 245 vfs_rewind(VFSFile * file) | |
| 246 { | |
| 247 if (file == NULL) | |
| 248 return; | |
| 249 | |
| 250 file->base->vfs_rewind_impl(file); | |
| 251 } | |
| 252 | |
| 253 /** | |
| 254 * vfs_ftell: | |
| 255 * @file: #VFSFile object that represents the VFS stream. | |
| 256 * | |
| 257 * Returns the current position in the VFS stream's buffer. | |
| 258 * | |
| 259 * Return value: On success, the current position. Otherwise, -1. | |
| 260 **/ | |
| 261 glong | |
| 262 vfs_ftell(VFSFile * file) | |
| 263 { | |
| 264 if (file == NULL) | |
| 265 return -1; | |
| 266 | |
| 267 return file->base->vfs_ftell_impl(file); | |
| 268 } | |
| 269 | |
| 270 /** | |
| 271 * vfs_feof: | |
| 272 * @file: #VFSFile object that represents the VFS stream. | |
| 273 * | |
| 274 * Returns whether or not the VFS stream has reached EOF. | |
| 275 * | |
| 276 * Return value: On success, whether or not the VFS stream is at EOF. Otherwise, FALSE. | |
| 277 **/ | |
| 278 gboolean | |
| 279 vfs_feof(VFSFile * file) | |
| 280 { | |
| 281 if (file == NULL) | |
| 282 return FALSE; | |
| 283 | |
| 284 return (gboolean) file->base->vfs_feof_impl(file); | |
| 285 } | |
| 286 | |
| 287 /** | |
| 288 * vfs_truncate: | |
| 289 * @file: #VFSFile object that represents the VFS stream. | |
| 290 * @length: The length to truncate at. | |
| 291 * | |
| 292 * Truncates a VFS stream to a certain size. | |
| 293 * | |
| 294 * Return value: On success, 0. Otherwise, -1. | |
| 295 **/ | |
| 296 gint | |
| 297 vfs_truncate(VFSFile * file, glong length) | |
| 298 { | |
| 299 if (file == NULL) | |
| 300 return -1; | |
| 301 | |
| 302 return file->base->vfs_truncate_impl(file, length); | |
| 303 } | |
| 304 | |
| 305 /** | |
| 2688 | 306 * vfs_fsize: |
| 307 * @file: #VFSFile object that represents the VFS stream. | |
| 308 * | |
| 309 * Returns te size of the file | |
| 310 * | |
| 311 * Return value: On success, the size of the file in bytes. | |
| 312 * Otherwise, -1. | |
| 313 */ | |
| 314 off_t | |
| 315 vfs_fsize(VFSFile * file) | |
| 316 { | |
| 317 if (file == NULL) | |
| 318 return -1; | |
| 319 | |
| 320 return file->base->vfs_fsize_impl(file); | |
| 321 } | |
| 322 | |
| 323 /** | |
|
2376
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
324 * vfs_get_metadata: |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
325 * @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
|
326 * @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
|
327 * |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
328 * Returns metadata about the stream. |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
329 * |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
330 * 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
|
331 * field. Otherwise, NULL. |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
332 **/ |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
333 gchar * |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
334 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
|
335 { |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
336 if (file == NULL) |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
337 return NULL; |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
338 |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
339 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
|
340 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
|
341 return NULL; |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
342 } |
|
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
343 |
| 2579 | 344 static gchar * |
| 345 _vfs_urldecode_basic_path(const gchar * encoded_path) | |
| 346 { | |
| 347 const gchar *cur, *ext; | |
| 348 gchar *path, *tmp; | |
| 349 gint realchar; | |
| 350 | |
| 351 if (!encoded_path) | |
| 352 return NULL; | |
| 353 | |
| 354 if (!str_has_prefix_nocase(encoded_path, "file:")) | |
| 355 return NULL; | |
| 356 | |
| 357 cur = encoded_path + 5; | |
| 358 | |
| 359 if (str_has_prefix_nocase(cur, "//localhost")) | |
| 360 cur += 11; | |
| 361 | |
| 362 if (*cur == '/') | |
| 363 while (cur[1] == '/') | |
| 364 cur++; | |
| 365 | |
| 366 tmp = g_malloc0(strlen(cur) + 1); | |
| 367 | |
| 368 while ((ext = strchr(cur, '%')) != NULL) { | |
| 369 strncat(tmp, cur, ext - cur); | |
| 370 ext++; | |
| 371 cur = ext + 2; | |
| 372 if (!sscanf(ext, "%2x", &realchar)) { | |
| 373 /* Assume it is a literal '%'. Several file | |
| 374 * managers send unencoded file: urls on drag | |
| 375 * and drop. */ | |
| 376 realchar = '%'; | |
| 377 cur -= 2; | |
| 378 } | |
| 379 tmp[strlen(tmp)] = realchar; | |
| 380 } | |
| 381 | |
| 382 path = g_strconcat(tmp, cur, NULL); | |
| 383 g_free(tmp); | |
| 384 return path; | |
| 385 } | |
| 386 | |
|
2376
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
387 /** |
| 2313 | 388 * vfs_file_test: |
| 389 * @path: A path to test. | |
| 390 * @test: A GFileTest to run. | |
| 391 * | |
| 392 * Wrapper for g_file_test(). | |
| 393 * | |
| 394 * Return value: The result of g_file_test(). | |
| 395 **/ | |
| 396 gboolean | |
| 397 vfs_file_test(const gchar * path, GFileTest test) | |
| 398 { | |
| 2575 | 399 gchar *path2; |
| 400 gboolean ret; | |
| 401 | |
| 2579 | 402 path2 = _vfs_urldecode_basic_path(path); |
| 2575 | 403 |
|
2580
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
404 if (path2 == NULL) |
|
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
405 path2 = g_strdup(path); |
|
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
406 |
| 2575 | 407 ret = g_file_test(path2, test); |
| 408 | |
| 409 g_free(path2); | |
| 410 | |
| 411 return ret; | |
| 2313 | 412 } |
| 413 | |
| 414 /** | |
| 415 * vfs_is_writeable: | |
| 416 * @path: A path to test. | |
| 417 * | |
| 418 * Tests if a file is writeable. | |
| 419 * | |
| 420 * Return value: TRUE if the file is writeable, otherwise FALSE. | |
| 421 **/ | |
| 422 gboolean | |
| 423 vfs_is_writeable(const gchar * path) | |
| 424 { | |
| 425 struct stat info; | |
| 426 | |
| 427 if (stat(path, &info) == -1) | |
| 428 return FALSE; | |
| 429 | |
| 430 return (info.st_mode & S_IWUSR); | |
| 431 } | |
|
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
432 |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
433 /** |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
434 * vfs_dup: |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
435 * @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
|
436 * |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
437 * 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
|
438 * 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
|
439 * from this function. |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
440 * 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
|
441 * closed. |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
442 **/ |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
443 VFSFile * |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
444 vfs_dup(VFSFile *in) |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
445 { |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
446 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
|
447 |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
448 in->ref++; |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
449 |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
450 return in; |
|
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
451 } |
