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