From 86a62c8e93f652e77a7d6f7dac8a9ce676d7ff12 Mon Sep 17 00:00:00 2001
From: Slava Pestov <slava@slava-pestovs-macbook-pro.local>
Date: Mon, 30 Jun 2008 18:33:31 -0500
Subject: [PATCH 1/2] Better error message

---
 vm/image.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/vm/image.c b/vm/image.c
index 141594f01f..09ff035d7e 100755
--- a/vm/image.c
+++ b/vm/image.c
@@ -28,8 +28,14 @@ INLINE void load_data_heap(FILE *file, F_HEADER *h, F_PARAMETERS *p)
 
 	F_ZONE *tenured = &data_heap->generations[TENURED];
 
-	if(fread((void*)tenured->start,h->data_size,1,file) != 1)
+	long int bytes_read = fread((void*)tenured->start,1,h->data_size,file);
+
+	if(bytes_read != h->data_size)
+	{
+		fprintf(stderr,"truncated image: %ld bytes read, %ld bytes expected\n",
+			bytes_read,h->data_size);
 		fatal_error("load_data_heap failed",0);
+	}
 
 	tenured->here = tenured->start + h->data_size;
 	data_relocation_base = h->data_relocation_base;
@@ -44,9 +50,16 @@ INLINE void load_code_heap(FILE *file, F_HEADER *h, F_PARAMETERS *p)
 
 	init_code_heap(p->code_size);
 
-	if(h->code_size != 0
-		&& fread(first_block(&code_heap),h->code_size,1,file) != 1)
-		fatal_error("load_code_heap failed",0);
+	if(h->code_size != 0)
+	{
+		long int bytes_read = fread(first_block(&code_heap),1,h->code_size,file);
+		if(bytes_read != h->code_size)
+		{
+			fprintf(stderr,"truncated image: %ld bytes read, %ld bytes expected\n",
+				bytes_read,h->code_size);
+			fatal_error("load_code_heap failed",0);
+		}
+	}
 
 	code_relocation_base = h->code_relocation_base;
 	build_free_list(&code_heap,h->code_size);

From e9cd70619443308bf1693a8ff43f396388a2a56b Mon Sep 17 00:00:00 2001
From: Slava Pestov <slava@slava-pestovs-macbook-pro.local>
Date: Mon, 30 Jun 2008 19:16:33 -0500
Subject: [PATCH 2/2] Add sanity check to bootstrp.image.download

---
 extra/bootstrap/image/download/download.factor | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/extra/bootstrap/image/download/download.factor b/extra/bootstrap/image/download/download.factor
index 701a784ea4..71aa2e8adc 100644
--- a/extra/bootstrap/image/download/download.factor
+++ b/extra/bootstrap/image/download/download.factor
@@ -1,13 +1,13 @@
 ! Copyright (C) 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-IN: bootstrap.image.download
 USING: http.client checksums checksums.openssl splitting assocs
-kernel io.files bootstrap.image sequences io ;
+kernel io.files bootstrap.image sequences io urls ;
+IN: bootstrap.image.download
 
-: url "http://factorcode.org/images/latest/" ;
+: url URL" http://factorcode.org/images/latest/" ;
 
 : download-checksums ( -- alist )
-    url "checksums.txt" append http-get nip
+    url "checksums.txt" >url derive-url http-get nip
     string-lines [ " " split1 ] { } map>assoc ;
 
 : need-new-image? ( image -- ? )
@@ -21,7 +21,10 @@ kernel io.files bootstrap.image sequences io ;
 : download-image ( arch -- )
     boot-image-name dup need-new-image? [
         "Downloading " write dup write "..." print
-        url prepend download
+         url over >url derive-url download
+         need-new-image? [
+             "Boot image corrupt, or checksums.txt on server out of date" throw
+         ] when
     ] [
         "Boot image up to date" print
         drop