[GRASS-SVN] r48841 - grass/trunk/lib/raster

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Oct 17 14:42:49 EDT 2011


Author: glynn
Date: 2011-10-17 11:42:49 -0700 (Mon, 17 Oct 2011)
New Revision: 48841

Modified:
   grass/trunk/lib/raster/R.h
   grass/trunk/lib/raster/close.c
   grass/trunk/lib/raster/format.c
   grass/trunk/lib/raster/get_row.c
   grass/trunk/lib/raster/open.c
   grass/trunk/lib/raster/put_row.c
Log:
Separate the R__.fileinfo[] index from the raster data file descriptor


Modified: grass/trunk/lib/raster/R.h
===================================================================
--- grass/trunk/lib/raster/R.h	2011-10-17 18:37:16 UTC (rev 48840)
+++ grass/trunk/lib/raster/R.h	2011-10-17 18:42:49 UTC (rev 48841)
@@ -68,6 +68,7 @@
     XDR xdrstream;		/* xdr stream for reading fp    */
     struct Quant quant;
     struct GDAL_link *gdal;
+    int data_fd;		/* Raster data fd               */
 };
 
 struct R__			/*  Structure of library globals */

Modified: grass/trunk/lib/raster/close.c
===================================================================
--- grass/trunk/lib/raster/close.c	2011-10-17 18:37:16 UTC (rev 48840)
+++ grass/trunk/lib/raster/close.c	2011-10-17 18:42:49 UTC (rev 48841)
@@ -170,7 +170,7 @@
 	Rast_quant_free(&fcb->quant);
 	xdr_destroy(&fcb->xdrstream);
     }
-    close(fd);
+    close(fcb->data_fd);
 
     return 1;
 }
@@ -312,8 +312,6 @@
 	Rast_close_gdal_link(fcb->gdal);
     }
 
-    /* NOW CLOSE THE FILE DESCRIPTOR */
-    close(fd);
     fcb->open_mode = -1;
 
     if (fcb->data != NULL)
@@ -387,7 +385,7 @@
 	}			/* null_cur_row > 0 */
 
 	if (fcb->open_mode == OPEN_NEW_COMPRESSED) {	/* auto compression */
-	    fcb->row_ptr[fcb->cellhd.rows] = lseek(fd, 0L, SEEK_CUR);
+	    fcb->row_ptr[fcb->cellhd.rows] = lseek(fcb->data_fd, 0L, SEEK_CUR);
 	    Rast__write_row_ptrs(fd);
 	}
 
@@ -413,12 +411,12 @@
 			      fcb->mapset);
 	    remove(path);
 	    CELL_DIR = "cell";
-	    close(fd);
+	    close(fcb->data_fd);
 	}
     }				/* ok */
     /* NOW CLOSE THE FILE DESCRIPTOR */
 
-    close(fd);
+    close(fcb->data_fd);
     fcb->open_mode = -1;
 
     if (fcb->data != NULL)

Modified: grass/trunk/lib/raster/format.c
===================================================================
--- grass/trunk/lib/raster/format.c	2011-10-17 18:37:16 UTC (rev 48840)
+++ grass/trunk/lib/raster/format.c	2011-10-17 18:42:49 UTC (rev 48841)
@@ -76,13 +76,13 @@
      */
 
     if (fcb->cellhd.compressed < 0) {
-	if (read(fd, compress, 3) != 3
+	if (read(fcb->data_fd, compress, 3) != 3
 	    || compress[0] != 251 || compress[1] != 255 || compress[2] != 251)
 	    fcb->cellhd.compressed = 0;
     }
 
     if (!fcb->cellhd.compressed)
-	return fd;
+	return 1;
 
     /* allocate space to hold the row address array */
     fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
@@ -107,7 +107,7 @@
 
     if (fcb->cellhd.compressed < 0) {
 	n = (nrows + 1) * sizeof(off_t);
-	if (read(fd, fcb->row_ptr, n) != n)
+	if (read(fcb->data_fd, fcb->row_ptr, n) != n)
 	    goto badread;
 	return 1;
     }
@@ -119,14 +119,14 @@
      *  actual values do not exceed the capability of the off_t)
      */
 
-    if (read(fd, &nbytes, 1) != 1)
+    if (read(fcb->data_fd, &nbytes, 1) != 1)
 	goto badread;
     if (nbytes == 0)
 	goto badread;
 
     n = (nrows + 1) * nbytes;
     buf = G_malloc(n);
-    if (read(fd, buf, n) != n)
+    if (read(fcb->data_fd, buf, n) != n)
 	goto badread;
 
     for (row = 0, b = buf; row <= nrows; row++) {
@@ -164,7 +164,7 @@
     unsigned char *buf, *b;
     int len, row, result;
 
-    lseek(fd, 0L, SEEK_SET);
+    lseek(fcb->data_fd, 0L, SEEK_SET);
 
     len = (nrows + 1) * nbytes + 1;
     b = buf = G_malloc(len);
@@ -182,7 +182,7 @@
 	b += nbytes;
     }
 
-    result = (write(fd, buf, len) == len);
+    result = (write(fcb->data_fd, buf, len) == len);
     G_free(buf);
 
     return result;

Modified: grass/trunk/lib/raster/get_row.c
===================================================================
--- grass/trunk/lib/raster/get_row.c	2011-10-17 18:37:16 UTC (rev 48840)
+++ grass/trunk/lib/raster/get_row.c	2011-10-17 18:42:49 UTC (rev 48841)
@@ -92,12 +92,12 @@
     size_t readamount = t2 - t1;
     size_t bufsize = fcb->cellhd.cols * fcb->nbytes;
 
-    if (lseek(fd, t1, SEEK_SET) < 0)
+    if (lseek(fcb->data_fd, t1, SEEK_SET) < 0)
 	G_fatal_error(_("Error reading raster data"));
 
     *nbytes = fcb->nbytes;
 
-    if ((size_t) G_zlib_read(fd, readamount, data_buf, bufsize) != bufsize)
+    if ((size_t) G_zlib_read(fcb->data_fd, readamount, data_buf, bufsize) != bufsize)
 	G_fatal_error(_("Error reading raster data"));
 }
 
@@ -130,12 +130,12 @@
     unsigned char *cmp;
     int n;
 
-    if (lseek(fd, t1, SEEK_SET) < 0)
+    if (lseek(fcb->data_fd, t1, SEEK_SET) < 0)
 	G_fatal_error(_("Error reading raster data"));
 
     cmp = G__alloca(readamount);
 
-    if (read(fd, cmp, readamount) != readamount) {
+    if (read(fcb->data_fd, cmp, readamount) != readamount) {
 	G__freea(cmp);
 	G_fatal_error(_("Error reading raster data"));
     }
@@ -170,10 +170,10 @@
 
     *nbytes = fcb->nbytes;
 
-    if (lseek(fd, (off_t) row * bufsize, SEEK_SET) == -1)
+    if (lseek(fcb->data_fd, (off_t) row * bufsize, SEEK_SET) == -1)
 	G_fatal_error(_("Error reading raster data"));
 
-    if (read(fd, data_buf, bufsize) != bufsize)
+    if (read(fcb->data_fd, data_buf, bufsize) != bufsize)
 	G_fatal_error(_("Error reading raster data"));
 }
 

Modified: grass/trunk/lib/raster/open.c
===================================================================
--- grass/trunk/lib/raster/open.c	2011-10-17 18:37:16 UTC (rev 48840)
+++ grass/trunk/lib/raster/open.c	2011-10-17 18:42:49 UTC (rev 48841)
@@ -29,18 +29,23 @@
 #define FORMAT_FILE "f_format"
 #define NULL_FILE   "null"
 
-static struct fileinfo *new_fileinfo(int fd)
+static int new_fileinfo(void)
 {
     int oldsize = R__.fileinfo_count;
     int newsize = oldsize;
     int i;
 
-    if (fd < oldsize)
-	return &R__.fileinfo[fd];
+    for (i = 0; i < oldsize; i++)
+	if (R__.fileinfo[i].open_mode <= 0) {
+	    memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo));
+	    R__.fileinfo[i].open_mode = -1;
+	    return i;
+	}
 
-    newsize *= 2;
-    if (newsize <= fd)
-	newsize = fd + 20;
+    if (newsize < 20)
+	newsize += 20;
+    else
+	newsize *= 2;
 
     R__.fileinfo = G_realloc(R__.fileinfo, newsize * sizeof(struct fileinfo));
 
@@ -52,7 +57,7 @@
 
     R__.fileinfo_count = newsize;
 
-    return &R__.fileinfo[fd];
+    return oldsize;
 }
 
 /*!
@@ -146,7 +151,7 @@
 int Rast__open_old(const char *name, const char *mapset)
 {
     struct fileinfo *fcb;
-    int fd;
+    int cell_fd, fd;
     char *cell_dir;
     const char *r_name;
     const char *r_mapset;
@@ -246,23 +251,24 @@
     gdal = Rast_get_gdal_link(r_name, r_mapset);
     if (gdal) {
 #ifdef HAVE_GDAL
-	/* dummy descriptor to reserve the fileinfo slot */
-	fd = open(G_DEV_NULL, O_RDONLY);
+	cell_fd = -1;
 #else
 	G_fatal_error(_("Raster map <%s@%s> is a GDAL link but GRASS is compiled without GDAL support"),
 		      r_name, r_mapset);
 #endif
     }
-    else
+    else {
 	/* now actually open file for reading */
-	fd = G_open_old(cell_dir, r_name, r_mapset);
+	cell_fd = G_open_old(cell_dir, r_name, r_mapset);
+	if (cell_fd < 0)
+	    G_fatal_error(_("Unable to open %s file for raster map <%s@%s>"),
+			  cell_dir, r_name, r_mapset);
+    }
 
-    if (fd < 0)
-	G_fatal_error(_("Unable to open %s file for raster map <%s@%s>"),
-		      cell_dir, r_name, r_mapset);
+    fd = new_fileinfo();
+    fcb = &R__.fileinfo[fd];
+    fcb->data_fd = cell_fd;
 
-    fcb = new_fileinfo(fd);
-
     fcb->map_type = MAP_TYPE;
 
     /* Save cell header */
@@ -291,7 +297,7 @@
     if (!gdal)
 	/* check for compressed data format, making initial reads if necessary */
 	if (Rast__check_format(fd) < 0) {
-	    close(fd);		/* warning issued by check_format() */
+	    close(cell_fd);	/* warning issued by check_format() */
 	    G_fatal_error(_("Error reading format for <%s@%s>"),
 			  r_name, r_mapset);
 	}
@@ -483,13 +489,10 @@
     int fd;
     struct fileinfo *fcb;
 
-    /* dummy descriptor to reserve the fileinfo slot */
-    fd = open(G_DEV_NULL, O_RDONLY);
-    if (fd < 0)
-	G_fatal_error(_("Unable to open null device"));
+    fd = new_fileinfo();
+    fcb = &R__.fileinfo[fd];
+    fcb->data_fd = -1;
 
-    fcb = new_fileinfo(fd);
-
     /* mark closed */
     fcb->map_type = map_type;
     fcb->open_mode = -1;
@@ -544,7 +547,7 @@
 {
     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
     struct fileinfo *fcb;
-    int fd;
+    int fd, cell_fd;
     char *tempname;
     char *map;
     char *mapset;
@@ -588,15 +591,18 @@
 
     /* open a tempfile name */
     tempname = G_tempfile();
-    fd = creat(tempname, 0666);
-    if (fd < 0) {
+    cell_fd = creat(tempname, 0666);
+    if (cell_fd < 0) {
 	G_free(mapset);
 	G_free(tempname);
 	G_free(map);
 	G_fatal_error(_("No temp files available"));
     }
 
-    fcb = new_fileinfo(fd);
+    fd = new_fileinfo();
+    fcb = &R__.fileinfo[fd];
+    fcb->data_fd = cell_fd;
+
     /*
      * since we are bypassing the normal open logic
      * must create the cell element 
@@ -661,7 +667,7 @@
 	G_free(fcb->name);
 	G_free(fcb->mapset);
 	G_free(fcb->temp_name);
-	close(fd);
+	close(cell_fd);
 	G_fatal_error(_("no temp files available"));
     }
 

Modified: grass/trunk/lib/raster/put_row.c
===================================================================
--- grass/trunk/lib/raster/put_row.c	2011-10-17 18:37:16 UTC (rev 48840)
+++ grass/trunk/lib/raster/put_row.c	2011-10-17 18:42:49 UTC (rev 48841)
@@ -116,7 +116,7 @@
     struct fileinfo *fcb = &R__.fileinfo[fd];
     ssize_t nwrite = fcb->nbytes * n;
 
-    if (write(fd, buf, nwrite) != nwrite)
+    if (write(fcb->data_fd, buf, nwrite) != nwrite)
 	G_fatal_error(_("Error writing uncompressed FP data for row %d of <%s>"),
 		      fcb->name, row);
 }
@@ -126,7 +126,7 @@
     struct fileinfo *fcb = &R__.fileinfo[fd];
     int nwrite = fcb->nbytes * n;
 
-    if (G_zlib_write(fd, buf, nwrite) < 0)
+    if (G_zlib_write(fcb->data_fd, buf, nwrite) < 0)
 	G_fatal_error(_("Error writing compressed FP data for row %d of <%s>"),
 		      fcb->name, row);
 }
@@ -135,7 +135,7 @@
 {
     struct fileinfo *fcb = &R__.fileinfo[fd];
 
-    fcb->row_ptr[row] = lseek(fd, 0L, SEEK_CUR);
+    fcb->row_ptr[row] = lseek(fcb->data_fd, 0L, SEEK_CUR);
 }
 
 static void convert_float(XDR * xdrs, char *null_buf, const FCELL * rast,
@@ -393,13 +393,13 @@
 	if (nwrite > 0) {
 	    nwrite++;
 
-	    if (write(fd, compressed_buf, nwrite) != nwrite)
+	    if (write(fcb->data_fd, compressed_buf, nwrite) != nwrite)
 		G_fatal_error(_("Error writing compressed data for row %d of <%s>"),
 			      row, fcb->name);
 	}
 	else {
 	    nwrite = nbytes * n + 1;
-	    if (write(fd, work_buf, nwrite) != nwrite)
+	    if (write(fcb->data_fd, work_buf, nwrite) != nwrite)
 		G_fatal_error(_("Error writing compressed data for row %d of <%s>"),
 			      row, fcb->name);
 	}
@@ -409,7 +409,7 @@
     else {
 	nwrite = fcb->nbytes * n;
 
-	if (write(fd, work_buf, nwrite) != nwrite)
+	if (write(fcb->data_fd, work_buf, nwrite) != nwrite)
 	    G_fatal_error(_("Error writing uncompressed data for row %d of <%s>"),
 			  row, fcb->name);
     }



More information about the grass-commit mailing list