Mercurial > audlegacy
annotate libaudacious/vfs.c @ 2031:ebce0d5efac1 trunk
[svn] - don't leak the split URI vector after doing the vtable lookup
author | nenolod |
---|---|
date | Fri, 01 Dec 2006 23:29:36 -0800 |
parents | dcea7c329ad6 |
children | 7aed5cf10141 |
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 |
0 | 6 * the Free Software Foundation; either version 2 of the License, or |
7 * (at your option) any later version. | |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with this program; if not, write to the Free Software | |
1459 | 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 17 */ |
18 | |
19 #include "vfs.h" | |
20 #include <stdio.h> | |
21 | |
22 #include <unistd.h> | |
23 #include <sys/stat.h> | |
24 #include <sys/types.h> | |
25 | |
1997 | 26 static GList *vfs_transports = NULL; |
27 | |
28 #ifdef VFS_DEBUG | |
29 # define DBG(x, args...) g_print(x, ## args); | |
30 #else | |
31 # define DBG(x, args...) | |
32 #endif | |
33 | |
0 | 34 gboolean |
1997 | 35 vfs_register_transport(VFSConstructor *vtable) |
0 | 36 { |
1997 | 37 vfs_transports = g_list_append(vfs_transports, vtable); |
38 | |
0 | 39 return TRUE; |
40 } | |
41 | |
42 VFSFile * | |
43 vfs_fopen(const gchar * path, | |
44 const gchar * mode) | |
45 { | |
46 VFSFile *file; | |
1997 | 47 gchar **vec; |
48 VFSConstructor *vtable = NULL; | |
49 GList *node; | |
0 | 50 |
550 | 51 if (!path || !mode) |
52 return NULL; | |
53 | |
1997 | 54 vec = g_strsplit(path, "://", 2); |
55 | |
56 /* special case: no transport specified, look for the "/" transport */ | |
57 if (vec[1] == NULL) | |
58 { | |
59 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
60 { | |
61 vtable = (VFSConstructor *) node->data; | |
62 | |
63 if (*vtable->uri_id == '/') | |
64 break; | |
65 } | |
66 } | |
67 else | |
68 { | |
69 for (node = vfs_transports; node != NULL; node = g_list_next(node)) | |
70 { | |
71 vtable = (VFSConstructor *) node->data; | |
0 | 72 |
1997 | 73 if (!g_strcasecmp(vec[0], vtable->uri_id)) |
74 break; | |
75 } | |
76 } | |
77 | |
78 /* no transport vtable has been registered, bail. */ | |
79 if (vtable == NULL) | |
80 { | |
2031
ebce0d5efac1
[svn] - don't leak the split URI vector after doing the vtable lookup
nenolod
parents:
1999
diff
changeset
|
81 g_strfreev(vec); |
1997 | 82 return NULL; |
0 | 83 } |
84 | |
1997 | 85 file = vtable->vfs_fopen_impl(vec[1] ? vec[1] : vec[0], mode); |
86 | |
87 if (file == NULL) | |
88 { | |
89 return NULL; | |
90 } | |
91 | |
92 file->uri = g_strdup(path); | |
93 file->base = vtable; | |
94 | |
2031
ebce0d5efac1
[svn] - don't leak the split URI vector after doing the vtable lookup
nenolod
parents:
1999
diff
changeset
|
95 g_strfreev(vec); |
ebce0d5efac1
[svn] - don't leak the split URI vector after doing the vtable lookup
nenolod
parents:
1999
diff
changeset
|
96 |
0 | 97 return file; |
98 } | |
99 | |
100 gint | |
101 vfs_fclose(VFSFile * file) | |
102 { | |
103 gint ret = 0; | |
104 | |
821 | 105 if (file == NULL) |
106 return -1; | |
107 | |
1997 | 108 if (file->base->vfs_fclose_impl(file) != 0) |
109 ret = -1; | |
0 | 110 |
1999 | 111 if (file->uri != NULL) |
112 g_free(file->uri); | |
113 | |
0 | 114 g_free(file); |
115 | |
116 return ret; | |
117 } | |
118 | |
119 size_t | |
120 vfs_fread(gpointer ptr, | |
121 size_t size, | |
122 size_t nmemb, | |
123 VFSFile * file) | |
124 { | |
821 | 125 if (file == NULL) |
126 return 0; | |
127 | |
1997 | 128 return file->base->vfs_fread_impl(ptr, size, nmemb, file); |
0 | 129 } |
130 | |
131 size_t | |
132 vfs_fwrite(gconstpointer ptr, | |
133 size_t size, | |
134 size_t nmemb, | |
135 VFSFile * file) | |
136 { | |
821 | 137 if (file == NULL) |
138 return 0; | |
139 | |
1997 | 140 return file->base->vfs_fwrite_impl(ptr, size, nmemb, file); |
0 | 141 } |
142 | |
143 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
|
144 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
|
145 { |
1997 | 146 if (stream == NULL) |
147 return -1; | |
148 | |
149 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
|
150 } |
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
|
151 |
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
|
152 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
|
153 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
|
154 { |
1997 | 155 if (stream == NULL) |
156 return -1; | |
157 | |
158 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
|
159 } |
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
|
160 |
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
|
161 gint |
0 | 162 vfs_fseek(VFSFile * file, |
163 glong offset, | |
164 gint whence) | |
165 { | |
821 | 166 if (file == NULL) |
167 return 0; | |
168 | |
1997 | 169 return file->base->vfs_fseek_impl(file, offset, whence); |
0 | 170 } |
171 | |
172 void | |
173 vfs_rewind(VFSFile * file) | |
174 { | |
821 | 175 if (file == NULL) |
176 return; | |
177 | |
1997 | 178 file->base->vfs_rewind_impl(file); |
0 | 179 } |
180 | |
181 glong | |
182 vfs_ftell(VFSFile * file) | |
183 { | |
821 | 184 if (file == NULL) |
185 return 0; | |
186 | |
1997 | 187 return file->base->vfs_ftell_impl(file); |
0 | 188 } |
189 | |
190 gboolean | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
191 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
|
192 { |
821 | 193 if (file == NULL) |
194 return FALSE; | |
195 | |
1997 | 196 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
|
197 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
198 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
199 gboolean |
0 | 200 vfs_file_test(const gchar * path, GFileTest test) |
201 { | |
202 return g_file_test(path, test); | |
203 } | |
204 | |
205 /* NOTE: stat() is not part of stdio */ | |
206 gboolean | |
207 vfs_is_writeable(const gchar * path) | |
208 { | |
209 struct stat info; | |
210 | |
211 if (stat(path, &info) == -1) | |
212 return FALSE; | |
213 | |
214 return (info.st_mode & S_IWUSR); | |
215 } | |
216 | |
217 gint | |
218 vfs_truncate(VFSFile * file, glong size) | |
219 { | |
821 | 220 if (file == NULL) |
221 return -1; | |
222 | |
1997 | 223 return file->base->vfs_truncate_impl(file, size); |
0 | 224 } |