Re: [Nautilus-list] Re: Eel patch



On Tue, 18 Sep 2001, Darin Adler wrote:

> on 9/18/01 1:28 PM, Alex Larsson at alexl redhat com wrote:
> 
> > 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)
> 
> I'm thinking that either file_size should be changed to be
> GnomeVFSFileSize*, or max_size should be an int. The idea here is that it's
> not a good idea to read a whole file into a buffer if you are going to read
> more than 2^31 bytes, so the sizes don't have to be expressed as 64-bit or
> even unsigned 32-bit quantities. But we should at least be consistent, I
> think.

I made it int. Although that made it necessary to do some ugly casts.
Making it GnomeVFSFileSize would break binary compat.

> Here on HEAD, I think it would be reasonable to only have the version that
> includes the limit. It doesn't seem necessary to have a convenience cover
> that provides an arbitrary 100 Kb limit. On the gnome 1 branch, we
> definitely need the two calls as you have done here.

Yeah.

> Is there a reason we don't want to return a 1-byte buffer with a 0 byte in
> it for the 0-bytes-read case? I think an empty string is probably better
> than a NULL for that case.

I was just trying to keep the old behaviour.

Index: 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-vfs-extensions.c	2001/05/03 16:41:24	1.3
+++ eel-vfs-extensions.c	2001/09/18 21:59:08
@@ -62,16 +62,19 @@ struct EelReadFileHandle {
 static void read_file_read_chunk (EelReadFileHandle *handle);
 #endif
 
+/* Always returns a zero terminated buffer */
 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,
+				 int 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 +89,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 > (GnomeVFSFileSize)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 +113,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 < (GnomeVFSFileSize)max_size);
 
 	/* Close the file. */
 	result = gnome_vfs_close (handle);
@@ -113,11 +121,38 @@ eel_read_entire_file (const char *uri,
 		g_free (buffer);
 		return result;
 	}
-
+	
+	if (total_bytes_read == (GnomeVFSFileSize)max_size) {
+		g_free (buffer);
+		return GNOME_VFS_ERROR_TOO_BIG;
+	}
+	
+	/* allocate extra byte for zero termination */
+	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;
+
 	return GNOME_VFS_OK;
+}
+
+/* Always returns a zero terminated buffer */
+GnomeVFSResult
+eel_read_entire_file (const char *uri,
+		      int *file_size,
+		      char **file_contents)
+{
+	/* Arbitrary filesize limit of 100Kb.
+	 * This seems large enough that most config-type files
+	 * should fit into it, and small enought to not cause problem
+	 * by allocating far to much memory.
+	 */
+	return eel_read_entire_file_with_limit (uri,
+						file_size,
+						file_contents,
+						100*1024);
 }
 
 #ifndef PTHREAD_ASYNC_READ
Index: 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-vfs-extensions.h	2001/05/03 16:41:24	1.3
+++ eel-vfs-extensions.h	2001/09/18 21:59:08
@@ -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,
+							  int                   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]