# HG changeset patch # User nenolod # Date 1163704807 28800 # Node ID 976da06332df1f408c49800e03930034b3d4efdf # Parent 9f6c17f1cc93c53d1432f329162ee4e40697189c [svn] - newvfs code, part 1 diff -r 9f6c17f1cc93 -r 976da06332df ChangeLog --- a/ChangeLog Thu Nov 16 10:53:21 2006 -0800 +++ b/ChangeLog Thu Nov 16 11:20:07 2006 -0800 @@ -1,3 +1,11 @@ +2006-11-16 18:53:21 +0000 William Pitcock + revision [2925] + - define VFSConstructor + + trunk/libaudacious/vfs.h | 36 ++++++++++++++++++++++++++++++++++++ + 1 file changed, 36 insertions(+) + + 2006-11-14 20:37:14 +0000 William Pitcock revision [2923] - reseek to 0 on a newvfs FD for each probe pass diff -r 9f6c17f1cc93 -r 976da06332df libaudacious/Makefile --- a/libaudacious/Makefile Thu Nov 16 10:53:21 2006 -0800 +++ b/libaudacious/Makefile Thu Nov 16 11:20:07 2006 -0800 @@ -20,7 +20,7 @@ -I../intl CONF_SRC = configdb_$(CONFIGDB_BACKEND).c -VFS_SRC = vfs_stdio.c vfs_common.c +VFS_SRC = vfs.c vfs_stdio.c vfs_common.c SOURCES = \ $(CONF_SRC) \ diff -r 9f6c17f1cc93 -r 976da06332df libaudacious/vfs.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libaudacious/vfs.c Thu Nov 16 11:20:07 2006 -0800 @@ -0,0 +1,173 @@ +/* This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "vfs.h" +#include + +#include +#include +#include + + +struct _VFSFile { + FILE *handle; +}; + + +gboolean +vfs_init(void) +{ + return TRUE; +} + +VFSFile * +vfs_fopen(const gchar * path, + const gchar * mode) +{ + VFSFile *file; + + if (!path || !mode) + return NULL; + + file = g_new(VFSFile, 1); + + file->handle = fopen(path, mode); + + if (file->handle == NULL) { + g_free(file); + file = NULL; + } + + return file; +} + +gint +vfs_fclose(VFSFile * file) +{ + gint ret = 0; + + if (file == NULL) + return -1; + + if (file->handle) { + if (fclose(file->handle) != 0) + ret = -1; + } + + g_free(file); + + return ret; +} + +size_t +vfs_fread(gpointer ptr, + size_t size, + size_t nmemb, + VFSFile * file) +{ + if (file == NULL) + return 0; + + return fread(ptr, size, nmemb, file->handle); +} + +size_t +vfs_fwrite(gconstpointer ptr, + size_t size, + size_t nmemb, + VFSFile * file) +{ + if (file == NULL) + return 0; + + return fwrite(ptr, size, nmemb, file->handle); +} + +gint +vfs_getc(VFSFile *stream) +{ + return getc( stream->handle ); +} + +gint +vfs_ungetc(gint c, VFSFile *stream) +{ + return ungetc( c , stream->handle ); +} + +gint +vfs_fseek(VFSFile * file, + glong offset, + gint whence) +{ + if (file == NULL) + return 0; + + return fseek(file->handle, offset, whence); +} + +void +vfs_rewind(VFSFile * file) +{ + if (file == NULL) + return; + + rewind(file->handle); +} + +glong +vfs_ftell(VFSFile * file) +{ + if (file == NULL) + return 0; + + return ftell(file->handle); +} + +gboolean +vfs_feof(VFSFile * file) +{ + if (file == NULL) + return FALSE; + + return (gboolean) feof(file->handle); +} + +gboolean +vfs_file_test(const gchar * path, GFileTest test) +{ + return g_file_test(path, test); +} + +/* NOTE: stat() is not part of stdio */ +gboolean +vfs_is_writeable(const gchar * path) +{ + struct stat info; + + if (stat(path, &info) == -1) + return FALSE; + + return (info.st_mode & S_IWUSR); +} + +gint +vfs_truncate(VFSFile * file, glong size) +{ + if (file == NULL) + return -1; + + return ftruncate(fileno(file->handle), size); +} diff -r 9f6c17f1cc93 -r 976da06332df libaudacious/vfs.h --- a/libaudacious/vfs.h Thu Nov 16 10:53:21 2006 -0800 +++ b/libaudacious/vfs.h Thu Nov 16 11:20:07 2006 -0800 @@ -24,6 +24,13 @@ #include typedef struct _VFSFile VFSFile; +typedef struct _VFSConstructor VFSConstructor; + +struct _VFSFile { + gchar *uri; + gpointer handle; + VFSConstructor *base; +}; struct _VFSConstructor { gchar *uri_id; @@ -32,6 +39,9 @@ gint (*vfs_fclose_impl)(VFSFile * file); size_t (*vfs_fread_impl)(gpointer ptr, size_t size, size_t nmemb, VFSFile *file); + size_t (*vfs_fwrite_impl)(gconstpointer ptr, size_t size, + size_t nmemb, VFSFile *file); + gint (*vfs_getc_impl)(VFSFile *stream); gint (*vfs_ungetc_impl)(gint c, VFSFile *stream); gint (*vfs_fseek_impl)(VFSFile *file, glong offset, gint whence); void (*vfs_rewind_impl)(VFSFile *file); @@ -40,8 +50,6 @@ gboolean (*vfs_truncate_impl)(VFSFile *file, glong length); }; -typedef struct _VFSConstructor VFSConstructor; - G_BEGIN_DECLS /* Reserved for private use by BMP */ diff -r 9f6c17f1cc93 -r 976da06332df libaudacious/vfs_stdio.c --- a/libaudacious/vfs_stdio.c Thu Nov 16 10:53:21 2006 -0800 +++ b/libaudacious/vfs_stdio.c Thu Nov 16 11:20:07 2006 -0800 @@ -20,20 +20,8 @@ #include #include - -struct _VFSFile { - FILE *handle; -}; - - -gboolean -vfs_init(void) -{ - return TRUE; -} - VFSFile * -vfs_fopen(const gchar * path, +stdio_vfs_fopen_impl(const gchar * path, const gchar * mode) { VFSFile *file; @@ -54,7 +42,7 @@ } gint -vfs_fclose(VFSFile * file) +stdio_vfs_fclose_impl(VFSFile * file) { gint ret = 0; @@ -72,7 +60,7 @@ } size_t -vfs_fread(gpointer ptr, +stdio_vfs_fread_impl(gpointer ptr, size_t size, size_t nmemb, VFSFile * file) @@ -84,7 +72,7 @@ } size_t -vfs_fwrite(gconstpointer ptr, +stdio_vfs_fwrite_impl(gconstpointer ptr, size_t size, size_t nmemb, VFSFile * file) @@ -96,19 +84,19 @@ } gint -vfs_getc(VFSFile *stream) +stdio_vfs_getc_impl(VFSFile *stream) { return getc( stream->handle ); } gint -vfs_ungetc(gint c, VFSFile *stream) +stdio_vfs_ungetc_impl(gint c, VFSFile *stream) { return ungetc( c , stream->handle ); } gint -vfs_fseek(VFSFile * file, +stdio_vfs_fseek_impl(VFSFile * file, glong offset, gint whence) { @@ -119,7 +107,7 @@ } void -vfs_rewind(VFSFile * file) +stdio_vfs_rewind_impl(VFSFile * file) { if (file == NULL) return; @@ -128,7 +116,7 @@ } glong -vfs_ftell(VFSFile * file) +stdio_vfs_ftell_impl(VFSFile * file) { if (file == NULL) return 0; @@ -137,7 +125,7 @@ } gboolean -vfs_feof(VFSFile * file) +stdio_vfs_feof_impl(VFSFile * file) { if (file == NULL) return FALSE; @@ -145,29 +133,51 @@ return (gboolean) feof(file->handle); } -gboolean -vfs_file_test(const gchar * path, GFileTest test) -{ - return g_file_test(path, test); -} - -/* NOTE: stat() is not part of stdio */ -gboolean -vfs_is_writeable(const gchar * path) -{ - struct stat info; - - if (stat(path, &info) == -1) - return FALSE; - - return (info.st_mode & S_IWUSR); -} - gint -vfs_truncate(VFSFile * file, glong size) +stdio_vfs_truncate_impl(VFSFile * file, glong size) { if (file == NULL) return -1; return ftruncate(fileno(file->handle), size); } + +VFSConstructor file_const = { + "file://", + stdio_vfs_fopen_impl, + stdio_vfs_fclose_impl, + stdio_vfs_fread_impl, + stdio_vfs_fwrite_impl, + stdio_vfs_getc_impl, + stdio_vfs_ungetc_impl, + stdio_vfs_fseek_impl, + stdio_vfs_rewind_impl, + stdio_vfs_ftell_impl, + stdio_vfs_feof_impl, + stdio_vfs_truncate_impl +}; + +VFSConstructor default_const = { + "/", + stdio_vfs_fopen_impl, + stdio_vfs_fclose_impl, + stdio_vfs_fread_impl, + stdio_vfs_fwrite_impl, + stdio_vfs_getc_impl, + stdio_vfs_ungetc_impl, + stdio_vfs_fseek_impl, + stdio_vfs_rewind_impl, + stdio_vfs_ftell_impl, + stdio_vfs_feof_impl, + stdio_vfs_truncate_impl +}; + +#if 0 +gboolean +vfs_init(void) +{ + vfs_register_transport(&default_const); + vfs_register_transport(&file_const); + return TRUE; +} +#endif