Преглед на файлове

segment lib: fix for Segment_format_nofill()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@71648 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz преди 7 години
родител
ревизия
825d513351
променени са 2 файла, в които са добавени 24 реда и са изтрити 8 реда
  1. 15 5
      lib/segment/format.c
  2. 9 3
      lib/segment/pagein.c

+ 15 - 5
lib/segment/format.c

@@ -26,6 +26,7 @@ static int seg_format(int, off_t, off_t, int, int, int, int);
 static int write_int(int, int);
 static int write_off_t(int, off_t);
 static int zero_fill(int, off_t);
+static int seek_only(int, off_t);
 
 /* fd must be open for write */
 
@@ -158,13 +159,17 @@ static int seg_format(int fd,
 	|| !write_int(fd, len))
 	return -1;
 
-    if (!fill)
-	return 1;
-
     /* calculate total number of segments */
     nbytes = spr * ((nrows + srows - 1) / srows);
     nbytes *= size;
 
+    if (!fill) {
+	/* only seek and write a zero byte to the end */ 
+	if (seek_only(fd, nbytes) < 0)
+	    return -1;
+	return 1;
+    }
+
     /* fill segment file with zeros */
     /* NOTE: this could be done faster using lseek() by seeking
      * ahead nbytes and then writing a single byte of 0,
@@ -239,10 +244,16 @@ static int zero_fill(int fd, off_t nbytes)
     }
     return 1;
 #else
+    return seek_only(fd, nbytes);
+#endif
+}
+
+static int seek_only(int fd, off_t nbytes)
+{
     /* Using lseek (faster upon initialization).
        NOTE: This version doesn't allocate disk storage for the file; storage will
        be allocated dynamically as blocks are actually written. This could 
-       result in zero_fill() succeeding but a subsequent call to write() failing
+       result in seek_only() succeeding but a subsequent call to write() failing
        with ENOSPC ("No space left on device").
      */
 
@@ -268,5 +279,4 @@ static int zero_fill(int fd, off_t nbytes)
     }
 
     return 1;
-#endif
 }

+ 9 - 3
lib/segment/pagein.c

@@ -93,14 +93,20 @@ int seg_pagein(SEGMENT * SEG, int n)
     SEG->seek(SEG, SEG->scb[cur].n, 0);
 
     read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
-    if (read_result != SEG->size) {
+
+    if (read_result == 0) {
+	/* this can happen if the file was not zero-filled,
+	 * i.e. formatted with Segment_format_nofill() or 
+	 * Segment_format() used lseek for file initialization */
+	G_debug(1, "Segment pagein: zero read");
+	memset(SEG->scb[cur].buf, 0, SEG->size);
+    }
+    else if (read_result != SEG->size) {
 	G_debug(2, "Segment pagein: read_result=%d  SEG->size=%d",
 		read_result, SEG->size);
 
 	if (read_result < 0)
 	    G_warning("Segment pagein: %s", strerror(errno));
-	else if (read_result == 0)
-	    G_warning("Segment pagein: read EOF");
 	else
 	    G_warning
 		("Segment pagein: short count during read(), got %d, expected %d",