Mercurial > audlegacy
annotate src/audacious/vfs.c @ 2623:0ad10a95ed10 trunk
[svn] - plugin reloading.
author | nenolod |
---|---|
date | Thu, 15 Mar 2007 19:25:27 -0700 |
parents | 48288757d7c7 |
children | ac22b2cb6013 |
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 /** | |
2376
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
306 * vfs_get_metadata: |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
307 * @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
|
308 * @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
|
309 * |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
310 * Returns metadata about the stream. |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
311 * |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
312 * 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
|
313 * field. Otherwise, NULL. |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
314 **/ |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
315 gchar * |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
316 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
|
317 { |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
318 if (file == NULL) |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
319 return NULL; |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
320 |
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
321 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
|
322 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
|
323 return NULL; |
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 |
2579 | 326 static gchar * |
327 _vfs_urldecode_basic_path(const gchar * encoded_path) | |
328 { | |
329 const gchar *cur, *ext; | |
330 gchar *path, *tmp; | |
331 gint realchar; | |
332 | |
333 if (!encoded_path) | |
334 return NULL; | |
335 | |
336 if (!str_has_prefix_nocase(encoded_path, "file:")) | |
337 return NULL; | |
338 | |
339 cur = encoded_path + 5; | |
340 | |
341 if (str_has_prefix_nocase(cur, "//localhost")) | |
342 cur += 11; | |
343 | |
344 if (*cur == '/') | |
345 while (cur[1] == '/') | |
346 cur++; | |
347 | |
348 tmp = g_malloc0(strlen(cur) + 1); | |
349 | |
350 while ((ext = strchr(cur, '%')) != NULL) { | |
351 strncat(tmp, cur, ext - cur); | |
352 ext++; | |
353 cur = ext + 2; | |
354 if (!sscanf(ext, "%2x", &realchar)) { | |
355 /* Assume it is a literal '%'. Several file | |
356 * managers send unencoded file: urls on drag | |
357 * and drop. */ | |
358 realchar = '%'; | |
359 cur -= 2; | |
360 } | |
361 tmp[strlen(tmp)] = realchar; | |
362 } | |
363 | |
364 path = g_strconcat(tmp, cur, NULL); | |
365 g_free(tmp); | |
366 return path; | |
367 } | |
368 | |
2376
e1513290ee3c
[svn] Add a VFSFile method for getting metadata associated with the file.
iabervon
parents:
2370
diff
changeset
|
369 /** |
2313 | 370 * vfs_file_test: |
371 * @path: A path to test. | |
372 * @test: A GFileTest to run. | |
373 * | |
374 * Wrapper for g_file_test(). | |
375 * | |
376 * Return value: The result of g_file_test(). | |
377 **/ | |
378 gboolean | |
379 vfs_file_test(const gchar * path, GFileTest test) | |
380 { | |
2575 | 381 gchar *path2; |
382 gboolean ret; | |
383 | |
2579 | 384 path2 = _vfs_urldecode_basic_path(path); |
2575 | 385 |
2580
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
386 if (path2 == NULL) |
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
387 path2 = g_strdup(path); |
48288757d7c7
[svn] - fix a regression introduced by the DnD fix.
nenolod
parents:
2579
diff
changeset
|
388 |
2575 | 389 ret = g_file_test(path2, test); |
390 | |
391 g_free(path2); | |
392 | |
393 return ret; | |
2313 | 394 } |
395 | |
396 /** | |
397 * vfs_is_writeable: | |
398 * @path: A path to test. | |
399 * | |
400 * Tests if a file is writeable. | |
401 * | |
402 * Return value: TRUE if the file is writeable, otherwise FALSE. | |
403 **/ | |
404 gboolean | |
405 vfs_is_writeable(const gchar * path) | |
406 { | |
407 struct stat info; | |
408 | |
409 if (stat(path, &info) == -1) | |
410 return FALSE; | |
411 | |
412 return (info.st_mode & S_IWUSR); | |
413 } | |
2562
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
414 |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
415 /** |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
416 * vfs_dup: |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
417 * @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
|
418 * |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
419 * 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
|
420 * 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
|
421 * from this function. |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
422 * 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
|
423 * closed. |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
424 **/ |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
425 VFSFile * |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
426 vfs_dup(VFSFile *in) |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
427 { |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
428 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
|
429 |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
430 in->ref++; |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
431 |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
432 return in; |
07b990906823
[svn] - add reference-counting to VFS and add new function, vfs_dup() to
nenolod
parents:
2534
diff
changeset
|
433 } |