changeset 4129:d9870d3e9550

Fix downloading of dynamically generated content with vfs_get_file_contents(). (Bugzilla #46)
author Maciej Grela <thermal@o2.pl>
date Fri, 28 Dec 2007 04:31:02 -0600
parents 1d6bb27fac21
children c2039c3004d6 3aafc46605b4
files src/audacious/vfs_common.c
diffstat 1 files changed, 43 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/audacious/vfs_common.c	Thu Dec 27 20:55:05 2007 +0100
+++ b/src/audacious/vfs_common.c	Fri Dec 28 04:31:02 2007 -0600
@@ -142,22 +142,57 @@
 vfs_file_get_contents(const gchar *filename, gchar **buf, gsize *size)
 {
     VFSFile *fd;
-
+    size_t filled_size = 0;
+    size_t buf_size = 4096;
+    gchar *ptr;
+    
     fd = vfs_fopen(filename, "rb");
 
     if (fd == NULL)
         return;
 
-    vfs_fseek(fd, 0, SEEK_END);
-    *size = vfs_ftell(fd);
+    if ( vfs_fseek(fd, 0, SEEK_END) == 0) { // seeking supported by VFS backend
+	    *size = vfs_ftell(fd);
+
+	    *buf = g_new(gchar, *size);
+    
+	    if (*buf == NULL)
+    		goto close_handle;
 
-    *buf = g_new(gchar, *size);
+	    vfs_fseek(fd, 0, SEEK_SET);
+	    vfs_fread(*buf, 1, *size, fd);
 
+	    goto close_handle;
+    }
+
+
+    *buf = g_new(gchar, buf_size);
+    
     if (*buf == NULL)
-        return;
+    	goto close_handle;
 
-    vfs_fseek(fd, 0, SEEK_SET);
-    vfs_fread(*buf, 1, *size, fd);
+    ptr=*buf;
+    while ( 1 ) {
+	    size_t read_size = vfs_fread(ptr, 1, buf_size - filled_size, fd);
+	    if ( read_size == 0 ) break;
+	    
+	    filled_size+=read_size;
+	    ptr+=read_size;
+	    
+	    if ( filled_size == buf_size ) {
+		    buf_size+=4096;
+		    
+		    *buf = g_realloc(*buf, buf_size);
+		    
+		    if ( *buf == NULL )
+			goto close_handle;
+			
+		    ptr=*buf + filled_size;
+	    }
+    }
 
-    vfs_fclose(fd);
+    *size = filled_size;
+    
+    close_handle:
+    vfs_fclose(fd);    
 }