[Nautilus-list] Eel patch



maximum size for eel_read_entire_file()

Index: eel/eel-vfs-extensions.c
===================================================================
RCS file: /cvs/gnome/eel/eel/eel-vfs-extensions.c,v
retrieving revision 1.3
diff -u -p -r1.3 eel-vfs-extensions.c
--- eel/eel-vfs-extensions.c	2001/05/03 16:41:24	1.3
+++ eel/eel-vfs-extensions.c	2001/09/18 20:16:50
@@ -63,15 +63,17 @@ static void read_file_read_chunk (EelRea
 #endif
 
 GnomeVFSResult
-eel_read_entire_file (const char *uri,
-			   int *file_size,
-			   char **file_contents)
+eel_read_entire_file_with_limit (const char *uri,
+				 int *file_size,
+				 char **file_contents,
+				 GnomeVFSFileSize max_size)
 {
 	GnomeVFSResult result;
 	GnomeVFSHandle *handle;
 	char *buffer;
 	GnomeVFSFileSize total_bytes_read;
 	GnomeVFSFileSize bytes_read;
+	GnomeVFSFileSize chunk_size;
 
 	*file_size = 0;
 	*file_contents = NULL;
@@ -86,10 +88,15 @@ eel_read_entire_file (const char *uri,
 	buffer = NULL;
 	total_bytes_read = 0;
 	do {
-		buffer = g_realloc (buffer, total_bytes_read + READ_CHUNK_SIZE);
+		chunk_size = READ_CHUNK_SIZE;
+		if (total_bytes_read + chunk_size > max_size) {
+			chunk_size = max_size - total_bytes_read;
+		}
+
+		buffer = g_realloc (buffer, total_bytes_read + chunk_size);
 		result = gnome_vfs_read (handle,
 					 buffer + total_bytes_read,
-					 READ_CHUNK_SIZE,
+					 chunk_size,
 					 &bytes_read);
 		if (result != GNOME_VFS_OK && result != GNOME_VFS_ERROR_EOF) {
 			g_free (buffer);
@@ -105,7 +112,7 @@ eel_read_entire_file (const char *uri,
 		}
 
 		total_bytes_read += bytes_read;
-	} while (result == GNOME_VFS_OK);
+	} while (result == GNOME_VFS_OK && total_bytes_read < max_size);
 
 	/* Close the file. */
 	result = gnome_vfs_close (handle);
@@ -114,10 +121,34 @@ eel_read_entire_file (const char *uri,
 		return result;
 	}
 
+	/* Possibly allocate extra byte for zero termination */
+	if (total_bytes_read == 0) {
+		g_free (buffer);
+		buffer = NULL;
+	} else {
+		buffer = g_realloc (buffer, total_bytes_read + 1);
+		buffer[total_bytes_read] = 0;
+	}
+	
 	/* Return the file. */
 	*file_size = total_bytes_read;
-	*file_contents = g_realloc (buffer, total_bytes_read);
+	*file_contents = buffer;
+
+	/* Zero terminate file */
+
 	return GNOME_VFS_OK;
+}
+
+GnomeVFSResult
+eel_read_entire_file (const char *uri,
+		      int *file_size,
+		      char **file_contents)
+{
+	/* Pick random filesize limit of 100Kb */
+	return eel_read_entire_file_with_limit (uri,
+						file_size,
+						file_contents,
+						100*1024);
 }
 
 #ifndef PTHREAD_ASYNC_READ
Index: eel/eel-vfs-extensions.h
===================================================================
RCS file: /cvs/gnome/eel/eel/eel-vfs-extensions.h,v
retrieving revision 1.3
diff -u -p -r1.3 eel-vfs-extensions.h
--- eel/eel-vfs-extensions.h	2001/05/03 16:41:24	1.3
+++ eel/eel-vfs-extensions.h	2001/09/18 20:16:50
@@ -50,6 +50,10 @@ typedef struct EelReadFileHandle EelRead
 GnomeVFSResult     eel_read_entire_file                  (const char           *uri,
 							  int                  *file_size,
 							  char                **file_contents);
+GnomeVFSResult     eel_read_entire_file_with_limit       (const char           *uri,
+							  int                  *file_size,
+							  char                **file_contents,
+							  GnomeVFSFileSize      max_size);
 EelReadFileHandle *eel_read_entire_file_async            (const char           *uri,
 							  EelReadFileCallback   callback,
 							  gpointer              callback_data);





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]