Mercurial > audlegacy
annotate libaudacious/vfs_gnome.c @ 1621:fae6e0cfcc77 trunk
[svn] - Reapply r2161
author | nhjm449 |
---|---|
date | Tue, 05 Sep 2006 19:57:18 -0700 |
parents | 411b4aaf928b |
children | e9c24e35bd76 |
rev | line source |
---|---|
0 | 1 /* This program is free software; you can redistribute it and/or modify |
1460 | 2 * it under the terms of the GNU General Public License as published by |
0 | 3 * the Free Software Foundation; either version 2 of the License, or |
4 * (at your option) any later version. | |
5 * | |
6 * This program is distributed in the hope that it will be useful, | |
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 * GNU General Public License for more details. | |
10 * | |
11 * You should have received a copy of the GNU General Public License | |
12 * along with this program; if not, write to the Free Software | |
1459 | 13 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
0 | 14 */ |
15 | |
16 #include "vfs.h" | |
17 #include <string.h> | |
18 #include <libgnomevfs/gnome-vfs.h> | |
19 | |
20 | |
21 struct _VFSFile | |
22 { | |
23 GnomeVFSHandle *handle; | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
24 gboolean eof; |
0 | 25 }; |
26 | |
27 | |
28 static void mode_to_gnome_vfs(const gchar * mode, | |
29 GnomeVFSOpenMode * g_mode, | |
30 gboolean * truncate, | |
31 gboolean * append); | |
32 | |
33 gboolean | |
34 vfs_init(void) | |
35 { | |
36 if (!gnome_vfs_init()) | |
37 return FALSE; | |
38 | |
39 g_atexit(gnome_vfs_shutdown); | |
40 return TRUE; | |
41 } | |
42 | |
43 VFSFile * | |
44 vfs_fopen(const gchar * path, | |
45 const gchar * mode) | |
46 { | |
47 VFSFile *file; | |
48 GnomeVFSResult g_result; | |
49 GnomeVFSOpenMode g_mode; | |
50 gboolean truncate, append; | |
51 | |
550 | 52 if (!path || !mode) |
53 return NULL; | |
54 | |
0 | 55 file = g_new(VFSFile, 1); |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
56 file->eof = FALSE; |
0 | 57 |
58 mode_to_gnome_vfs(mode, &g_mode, &truncate, &append); | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
59 gchar *escaped_file = gnome_vfs_escape_path_string(path); |
0 | 60 |
61 if (!truncate) { | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
62 g_result = gnome_vfs_open(&(file->handle), escaped_file, g_mode); |
0 | 63 if (append && g_result == GNOME_VFS_ERROR_NOT_FOUND) { |
64 g_result = gnome_vfs_create(&(file->handle), | |
65 path, g_mode, TRUE, | |
66 S_IRUSR | S_IWUSR | | |
67 S_IRGRP | S_IWGRP | | |
68 S_IROTH | S_IWOTH); | |
69 } | |
70 | |
71 if (append && g_result == GNOME_VFS_OK) { | |
72 g_result = gnome_vfs_seek(file->handle, GNOME_VFS_SEEK_END, 0); | |
73 if (g_result != GNOME_VFS_OK) | |
74 gnome_vfs_close(file->handle); | |
75 } | |
76 } | |
77 else { | |
78 g_result = gnome_vfs_create(&(file->handle), | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
79 escaped_file, g_mode, FALSE, |
0 | 80 S_IRUSR | S_IWUSR | |
81 S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
82 } | |
83 | |
84 if (g_result != GNOME_VFS_OK) | |
85 file->handle = NULL; | |
86 | |
87 if (file->handle == NULL) { | |
88 g_free(file); | |
89 file = NULL; | |
90 } | |
91 | |
78
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
92 g_free(escaped_file); |
ae0af0523844
[svn] Call gnome_vfs_escape_path_string() before attempting to open the file.
msameer
parents:
0
diff
changeset
|
93 |
0 | 94 return file; |
95 } | |
96 | |
97 gint | |
98 vfs_fclose(VFSFile * file) | |
99 { | |
100 gint ret = 0; | |
101 | |
821 | 102 if (file == NULL) |
103 return 0; | |
104 | |
0 | 105 if (file->handle) { |
106 if (gnome_vfs_close(file->handle) != GNOME_VFS_OK) | |
107 ret = -1; | |
108 } | |
109 | |
110 g_free(file); | |
111 | |
112 return ret; | |
113 } | |
114 | |
115 size_t | |
116 vfs_fread(gpointer ptr, | |
117 size_t size, | |
118 size_t nmemb, | |
119 VFSFile * file) | |
120 { | |
121 GnomeVFSResult result; | |
122 GnomeVFSFileSize bytes_read; | |
123 | |
821 | 124 if (file == NULL) |
125 return 0; | |
126 | |
0 | 127 result = gnome_vfs_read(file->handle, ptr, size * nmemb, &bytes_read); |
128 if (result == GNOME_VFS_OK) | |
129 return bytes_read; | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
130 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
131 if (result == GNOME_VFS_ERROR_EOF) { |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
132 file->eof = TRUE; |
0 | 133 return 0; |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
134 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
135 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
136 return -1; |
0 | 137 } |
138 | |
139 size_t | |
140 vfs_fwrite(gconstpointer ptr, | |
141 size_t size, | |
142 size_t nmemb, | |
143 VFSFile * file) | |
144 { | |
145 GnomeVFSResult result; | |
146 GnomeVFSFileSize bytes_written; | |
147 | |
821 | 148 if (file == NULL) |
149 return 0; | |
150 | |
0 | 151 result = gnome_vfs_write(file->handle, ptr, size * nmemb, &bytes_written); |
152 if (result == GNOME_VFS_OK) | |
153 return bytes_written; | |
154 else | |
155 return -1; | |
156 } | |
157 | |
158 gint | |
159 vfs_fseek(VFSFile * file, | |
160 glong offset, | |
161 gint whence) | |
162 { | |
163 GnomeVFSResult result; | |
164 GnomeVFSSeekPosition g_whence; | |
165 | |
821 | 166 if (file == NULL) |
167 return 0; | |
168 | |
0 | 169 switch (whence) { |
170 case SEEK_SET: | |
171 g_whence = GNOME_VFS_SEEK_START; | |
172 break; | |
173 case SEEK_CUR: | |
174 g_whence = GNOME_VFS_SEEK_CURRENT; | |
175 break; | |
176 case SEEK_END: | |
177 g_whence = GNOME_VFS_SEEK_END; | |
178 break; | |
179 default: | |
180 g_warning("vfs_fseek: invalid whence value"); | |
181 return -1; | |
182 } | |
183 | |
184 result = gnome_vfs_seek(file->handle, g_whence, offset); | |
185 | |
186 if (result == GNOME_VFS_OK) | |
187 return 0; | |
188 else | |
189 return -1; | |
190 } | |
191 | |
192 void | |
193 vfs_rewind(VFSFile * file) | |
194 { | |
821 | 195 if (file == NULL) |
196 return; | |
197 | |
0 | 198 vfs_fseek(file, 0L, SEEK_SET); |
199 } | |
200 | |
201 glong | |
202 vfs_ftell(VFSFile * file) | |
203 { | |
204 GnomeVFSResult result; | |
205 GnomeVFSFileSize position; | |
206 | |
821 | 207 if (file == NULL) |
208 return 0; | |
209 | |
0 | 210 result = gnome_vfs_tell(file->handle, &position); |
211 | |
212 if (result == GNOME_VFS_OK) | |
213 return position; | |
214 else | |
215 return -1; | |
216 } | |
217 | |
218 gboolean | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
219 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
|
220 { |
821 | 221 if (file == NULL) |
222 return FALSE; | |
223 | |
811
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
224 return file->eof; |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
225 } |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
226 |
86ca43d8a845
[svn] - implement vfs_feof() and vfs_ftell() and update the scrobbler plugin to reflect that,
nenolod
parents:
550
diff
changeset
|
227 gboolean |
0 | 228 vfs_file_test(const gchar * path, |
229 GFileTest test) | |
230 { | |
231 GnomeVFSResult result; | |
232 GnomeVFSFileInfo info; | |
233 GFileTest file_test; | |
234 | |
235 result = gnome_vfs_get_file_info(path, &info, | |
236 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS); | |
237 | |
238 if (test == G_FILE_TEST_EXISTS) | |
239 return (result == GNOME_VFS_OK) ? TRUE : FALSE; | |
240 else if (test == G_FILE_TEST_IS_EXECUTABLE) | |
241 return (info.permissions & GNOME_VFS_PERM_ACCESS_EXECUTABLE) | |
242 ? TRUE : FALSE; | |
243 | |
244 switch (info.type) { | |
245 case GNOME_VFS_FILE_TYPE_REGULAR: | |
246 file_test = G_FILE_TEST_IS_REGULAR; | |
247 break; | |
248 case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK: | |
249 file_test = G_FILE_TEST_IS_SYMLINK; | |
250 break; | |
251 case GNOME_VFS_FILE_TYPE_DIRECTORY: | |
252 file_test = G_FILE_TEST_IS_DIR; | |
253 default: | |
254 return FALSE; | |
255 } | |
256 | |
257 if (test == file_test) | |
258 return TRUE; | |
259 else | |
260 return FALSE; | |
261 } | |
262 | |
263 gboolean | |
264 vfs_is_writeable(const gchar * path) | |
265 { | |
266 GnomeVFSFileInfo info; | |
267 | |
268 if (gnome_vfs_get_file_info(path, &info, GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS) | |
269 != GNOME_VFS_OK) { | |
270 return FALSE; | |
271 } | |
272 | |
273 return (info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE ? TRUE : FALSE); | |
274 } | |
275 | |
276 gint | |
277 vfs_truncate(VFSFile * file, | |
278 glong length) | |
279 { | |
280 GnomeVFSResult result; | |
281 | |
821 | 282 if (file == NULL) |
283 return -1; | |
284 | |
0 | 285 result = gnome_vfs_truncate_handle(file->handle, (GnomeVFSFileSize) length); |
286 | |
287 if (result == GNOME_VFS_OK) | |
288 return 0; | |
289 else | |
290 return -1; | |
291 } | |
292 | |
293 static gchar *strdup_exclude_chars(const gchar * s, | |
294 const gchar * c); | |
295 | |
296 static void | |
297 mode_to_gnome_vfs(const gchar * mode, | |
298 GnomeVFSOpenMode * g_mode, | |
299 gboolean * truncate, | |
300 gboolean * append) | |
301 { | |
302 gchar *s; | |
303 | |
304 *g_mode = GNOME_VFS_OPEN_RANDOM; | |
305 *truncate = *append = FALSE; | |
306 | |
307 s = strdup_exclude_chars(mode, "bt"); | |
308 switch (s[0]) { | |
309 case 'r': | |
310 *g_mode |= GNOME_VFS_OPEN_READ; | |
311 | |
312 if (s[1] == '+') | |
313 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
314 | |
315 break; | |
316 case 'w': | |
317 case 'a': | |
318 *g_mode |= GNOME_VFS_OPEN_WRITE; | |
319 | |
320 if (s[0] == 'w') | |
321 *truncate = TRUE; | |
322 else | |
323 *append = TRUE; | |
324 | |
325 if (s[1] == '+') | |
326 *g_mode |= GNOME_VFS_OPEN_READ; | |
327 | |
328 break; | |
329 default: | |
330 g_warning("mode_to_gnome_vfs: unhandled mode character"); | |
331 } | |
332 g_free(s); | |
333 } | |
334 | |
335 static gchar * | |
336 strdup_exclude_chars(const gchar * s, | |
337 const gchar * c) | |
338 { | |
339 gint i, j, k; | |
340 gint newlen = 0; | |
341 gchar *newstr; | |
342 gboolean found; | |
343 | |
344 /* Calculate number of chars in new string */ | |
345 for (i = 0; s[i] != '\0'; i++) { | |
346 found = FALSE; | |
347 | |
348 for (j = 0; j < strlen(c) && !found; j++) | |
349 if (s[i] == c[j]) | |
350 found = TRUE; | |
351 | |
352 if (!found) | |
353 newlen++; | |
354 } | |
355 | |
356 newstr = g_malloc(newlen + 1); | |
357 | |
358 /* Copy valid chars to new string */ | |
359 for (i = k = 0; s[i] != '\0'; i++) { | |
360 found = FALSE; | |
361 | |
362 for (j = 0; j < strlen(c) && !found; j++) | |
363 if (s[i] == c[j]) | |
364 found = TRUE; | |
365 | |
366 if (!found) | |
367 newstr[k++] = s[i]; | |
368 } | |
369 | |
370 newstr[k] = '\0'; | |
371 | |
372 return newstr; | |
373 } |