From realitix at gmail.com  Mon Mar 20 10:24:10 2017
From: realitix at gmail.com (realitix)
Date: Mon, 20 Mar 2017 10:24:10 +0100
Subject: [Rd] IO error when writing to disk
Message-ID: <CAPXDfFnF8iSSoDJHtUy0Wm=0PqwtzysLRG0KU_pqCVAO6AoqcA@mail.gmail.com>

Hello,
Here a small improvement for R.

When you use the function write.table, if the disk is full for example, the
function doesn't return an error and the file is written but truncated.

It can be a source of mistakes because you can then copy the output file
and think everything is ok.

How to reproduce
-------------------------

>> write.csv(1:10000000, 'path')

You must have a path with a small amount of disk available (on linux:
http://souptonuts.sourceforge.net/quota_tutorial.html)

I have joined the patch in this email.
Can you open a bugzilla account for me to keep track of this change.

Thanks,
Jean-S?bastien Bevilacqua
-------------- next part --------------
Index: src/library/utils/src/io.c
===================================================================
--- src/library/utils/src/io.c	(r?vision 72357)
+++ src/library/utils/src/io.c	(copie de travail)
@@ -1120,12 +1120,23 @@
 	for(int i = 0; i < nr; i++) {
 	    if(i % 1000 == 999) R_CheckUserInterrupt();
 	    if(!isNull(rnames))
-		Rconn_printf(con, "%s%s",
-			     EncodeElement2(rnames, i, quote_rn, qmethod,
-					    &strBuf, sdec), csep);
+
+		if(Rconn_printf(con, "%s%s", EncodeElement2(rnames, i, quote_rn, qmethod, &strBuf, sdec), csep) < 0) {
+            error(_("IO error, cannot write table."));
+            break;
+        }
+
 	    for(int j = 0; j < nc; j++) {
 		xj = VECTOR_ELT(x, j);
-		if(j > 0) Rconn_printf(con, "%s", csep);
+
+		if(j > 0) {
+            if(Rconn_printf(con, "%s", csep) < 0) {
+                error(_("IO error, cannot write table."));
+                i = nr;
+                break;
+            }
+        }
+
 		if(isna(xj, i)) tmp = cna;
 		else {
 		    if(!isNull(levels[j])) {
@@ -1148,9 +1159,17 @@
 					     &strBuf, sdec);
 		    }
 		}
-		Rconn_printf(con, "%s", tmp);
+
+        if(Rconn_printf(con, "%s", tmp) < 0) {
+            error(_("IO error, cannot write table."));
+            i = nr;
+            break;
+        }
 	    }
-	    Rconn_printf(con, "%s", ceol);
+	    if(Rconn_printf(con, "%s", ceol) < 0) {
+            error(_("IO error, cannot write table."));
+            break;
+        }
 	}
 
     } else { /* A matrix */
@@ -1163,20 +1182,36 @@
 
 	for(int i = 0; i < nr; i++) {
 	    if(i % 1000 == 999) R_CheckUserInterrupt();
-	    if(!isNull(rnames))
-		Rconn_printf(con, "%s%s",
-			     EncodeElement2(rnames, i, quote_rn, qmethod,
-					    &strBuf, sdec), csep);
+	    if(!isNull(rnames)) {
+            if(Rconn_printf(con, "%s%s", EncodeElement2(rnames, i, quote_rn, qmethod, &strBuf, sdec), csep) < 0) {
+                error(_("IO error, cannot write table."));
+                break;
+            }
+        }
 	    for(int j = 0; j < nc; j++) {
-		if(j > 0) Rconn_printf(con, "%s", csep);
+		if(j > 0) {
+            if(Rconn_printf(con, "%s", csep) < 0) {
+                error(_("IO error, cannot write table."));
+                i = nr;
+                break;
+            }
+        }
 		if(isna(x, i + j*nr)) tmp = cna;
 		else {
 		    tmp = EncodeElement2(x, i + j*nr, quote_col[j], qmethod,
 					&strBuf, sdec);
 		}
-		Rconn_printf(con, "%s", tmp);
+        if(Rconn_printf(con, "%s", tmp) < 0) {
+            error(_("IO error, cannot write table."));
+            i = nr;
+            break;
+        }
 	    }
-	    Rconn_printf(con, "%s", ceol);
+
+	    if(Rconn_printf(con, "%s", ceol) < 0) {
+            error(_("IO error, cannot write table."));
+            break;
+        }
 	}
 
     }


[Rd] IO error when writing to disk
realitix