Bug #1851 mysqldump does not return an error code if the output device is filled
Submitted: 16 Nov 2003 9:26 Modified: 24 Jun 2004 21:03
Reporter: Sergey Beduev Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: mysqldump Command-line Client Severity:S2 (Serious)
Version:3.23.58 OS:Linux (Linux)
Assigned to: Victor Vagin CPU Architecture:Any

[16 Nov 2003 9:26] Sergey Beduev
Description:
mysqldump does not return an error code if the output device is filled.

How to repeat:
The dump of the big DB on section where is not present a place for all disk looks so.
$ /usr/bin/mysqldump -p -u root -r ~/tmp/1.sql 2>&1 ; echo $?
Enter password: 
0

after apply this patch, a dump of the big DB looks so.
$ ./mysqldump -p -u root -r /home/shaman/tmp/1.sql 2>&1; echo $?
Enter password: 
lt-mysqldump: write error: No space left on device
5

Suggested fix:
--- mysqldump.c.orig	Thu Sep 11 07:49:19 2003
+++ mysqldump.c	Sun Nov 16 11:50:11 2003
@@ -54,6 +54,7 @@
 #define EX_MYSQLERR 2
 #define EX_CONSCHECK 3
 #define EX_EOM 4
+#define EX_EOF 5
 
 /* index into 'show fields from table' */
 
@@ -1043,17 +1044,26 @@
   else
   {
     if (!opt_xml)
-      fprintf(md_result_file,"\n--\n-- Dumping data for table %s\n--\n",
-	      result_table);
+      if (fprintf(md_result_file,"\n--\n-- Dumping data for table %s\n--\n",
+	      result_table) == EOF) {
+	fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	safe_exit(EX_EOF);
+      }
     sprintf(query, "SELECT * FROM %s", result_table);
     if (where)
     {
       if (!opt_xml)
-	fprintf(md_result_file,"-- WHERE:  %s\n",where);
+	if (fprintf(md_result_file,"-- WHERE:  %s\n",where) == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	}
       strxmov(strend(query), " WHERE ",where,NullS);
     }
     if (!opt_xml)
-      fputs("\n\n", md_result_file);
+      if (fputs("\n\n", md_result_file) == EOF) {
+        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+        safe_exit(EX_EOF);
+      }
     if (mysql_query(sock, query))
     {
       DBerror(sock, "when retrieving data from server");
@@ -1079,17 +1089,25 @@
     }
 
     if (opt_lock)
-      fprintf(md_result_file,"LOCK TABLES %s WRITE;\n", opt_quoted_table);
-
+      if (fprintf(md_result_file,"LOCK TABLES %s WRITE;\n", opt_quoted_table) == EOF) {
+        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	safe_exit(EX_EOF);
+      }
     total_length=net_buffer_length;		/* Force row break */
     row_break=0;
     rownr=0;
     init_length=(uint) strlen(insert_pat)+4;
     if (opt_xml)
-      fprintf(md_result_file, "\t<%s>\n", table);
+      if (fprintf(md_result_file, "\t<%s>\n", table) == EOF) {
+        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+        safe_exit(EX_EOF);
+      }
 
     if (opt_autocommit)
-      fprintf(md_result_file, "set autocommit=0;\n");
+      if (fprintf(md_result_file, "set autocommit=0;\n") == EOF) {
+        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+        safe_exit(EX_EOF);
+      }
 
     while ((row=mysql_fetch_row(res)))
     {
@@ -1097,11 +1115,17 @@
       ulong *lengths=mysql_fetch_lengths(res);
       rownr++;
       if (!extended_insert && !opt_xml)
-	fputs(insert_pat,md_result_file);
+	if (fputs(insert_pat,md_result_file) == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	}
       mysql_field_seek(res,0);
 
       if (opt_xml)
-        fprintf(md_result_file, "\t<row>\n");
+        if (fprintf(md_result_file, "\t<row>\n") == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	}
 
       for (i = 0; i < mysql_num_fields(res); i++)
       {
@@ -1159,7 +1183,10 @@
 	else
 	{
 	  if (i && !opt_xml)
-	    fputc(',', md_result_file);
+	    if (fputc(',', md_result_file) == EOF) {
+		fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+		safe_exit(EX_EOF);
+	    }
 	  if (row[i])
 	  {
 	    if (!IS_NUM_FIELD(field))
@@ -1175,25 +1202,40 @@
 	      /* change any strings ("inf","nan",..) into NULL */
 	      char *ptr = row[i];
 	      if (opt_xml)
-		fprintf(md_result_file, "\t\t<%s>%s</%s>\n",
-			field->name,!isalpha(*ptr) ?ptr: "NULL",field->name);
+		if (fprintf(md_result_file, "\t\t<%s>%s</%s>\n",
+			field->name,!isalpha(*ptr) ?ptr: "NULL",field->name) == EOF) {
+		    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+		    safe_exit(EX_EOF);
+		}
 	      else
-		fputs((!isalpha(*ptr)) ? ptr : "NULL", md_result_file);
+		if (fputs((!isalpha(*ptr)) ? ptr : "NULL", md_result_file) == EOF) {
+		    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+		    safe_exit(EX_EOF);
+		}
 	    }
 	  }
 	  else
 	  {
 	    if (opt_xml)
-	      fprintf(md_result_file, "\t\t<%s>%s</%s>\n",
-		      field->name, "NULL", field->name);
+	      if (fprintf(md_result_file, "\t\t<%s>%s</%s>\n",
+		      field->name, "NULL", field->name) == EOF) {
+		fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+		safe_exit(EX_EOF);
+	      }
 	    else
-	      fputs("NULL", md_result_file);
+	      if (fputs("NULL", md_result_file) == EOF) {
+	        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	        safe_exit(EX_EOF);
+	      }
 	  }
 	}
       }
 
       if (opt_xml)
-        fprintf(md_result_file, "\t</row>\n");
+        if (fprintf(md_result_file, "\t</row>\n") == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	}
 
       if (extended_insert)
       {
@@ -1203,32 +1245,56 @@
         if (total_length + row_length < net_buffer_length)
         {
 	  total_length += row_length;
-	  fputc(',',md_result_file);		/* Always row break */
-	  fputs(extended_row.str,md_result_file);
+	  if (fputc(',',md_result_file) == EOF)	{	/* Always row break */
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	  }
+	  if (fputs(extended_row.str,md_result_file) == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	  }
 	}
         else
         {
 	  if (row_break && !opt_xml)
-	    fputs(";\n", md_result_file);
+	    if (fputs(";\n", md_result_file) == EOF) {
+		fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+		safe_exit(EX_EOF);
+	    }
 	  row_break=1;				/* This is first row */
 
 	  if (!opt_xml)
 	  {
-	    fputs(insert_pat,md_result_file);
-	    fputs(extended_row.str,md_result_file);
+	    if (fputs(insert_pat,md_result_file) == EOF) {
+		fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+		safe_exit(EX_EOF);
+	    }
+	    if (fputs(extended_row.str,md_result_file) == EOF) {
+		fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+		safe_exit(EX_EOF);
+	    }
 	  }
 	  total_length = row_length+init_length;
         }
       }
       else if (!opt_xml)
-	fputs(");\n", md_result_file);
+	if (fputs(");\n", md_result_file) == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	}
     }
 
     /*XML - close table tag and supress regular output*/
     if (opt_xml)
-	fprintf(md_result_file, "\t</%s>\n", table);
+	if (fprintf(md_result_file, "\t</%s>\n", table) == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	}
     else if (extended_insert && row_break)
-      fputs(";\n", md_result_file);		/* If not empty table */
+      if (fputs(";\n", md_result_file) == EOF)	{	/* If not empty table */
+        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+        safe_exit(EX_EOF);
+      }
     fflush(md_result_file);
     if (mysql_errno(sock))
     {
@@ -1243,12 +1309,19 @@
       return;
     }
     if (opt_disable_keys)
-      fprintf(md_result_file, "\n/*!40000 ALTER TABLE %s ENABLE KEYS */;\n",
-	      result_table);
+      if (fprintf(md_result_file, "\n/*!40000 ALTER TABLE %s ENABLE KEYS */;\n",
+	      result_table) == EOF)
+	safe_exit(EX_EOF);
     if (opt_lock)
-      fputs("UNLOCK TABLES;\n", md_result_file);
+      if (fputs("UNLOCK TABLES;\n", md_result_file) == EOF) {
+        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+        safe_exit(EX_EOF);
+      }
     if (opt_autocommit)
-      fprintf(md_result_file, "commit;\n");
+      if (fprintf(md_result_file, "commit;\n") == EOF) {
+        fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+        safe_exit(EX_EOF);
+      }
     mysql_free_result(res);
   }
 } /* dumpTable */
@@ -1329,11 +1402,13 @@
   {
     /*XML edit - add database element*/
     if (opt_xml)
-      fprintf(md_result_file, "<%s>\n", *db_names);
+      if (fprintf(md_result_file, "<%s>\n", *db_names) == EOF)
+        safe_exit(EX_CONSCHECK);
     if (dump_all_tables_in_db(*db_names))
       result=1;
     if (opt_xml)
-      fprintf(md_result_file, "</%s>\n", *db_names);
+      if (fprintf(md_result_file, "</%s>\n", *db_names) == EOF)
+       safe_exit(EX_CONSCHECK);
   }
   return result;
 } /* dump_databases */
@@ -1350,11 +1425,17 @@
   {
     if (opt_databases || opt_alldbs)
     {
-      fprintf(md_result_file,"\n--\n-- Current Database: %s\n--\n", database);
+      if (fprintf(md_result_file,"\n--\n-- Current Database: %s\n--\n", 
+    	  database) == EOF)
+	safe_exit(EX_CONSCHECK);
       if (!opt_create_db)
-	fprintf(md_result_file,"\nCREATE DATABASE /*!32312 IF NOT EXISTS*/ %s;\n",
-		database);
-      fprintf(md_result_file,"\nUSE %s;\n", database);
+	if (fprintf(md_result_file,"\nCREATE DATABASE /*!32312 IF NOT EXISTS*/ %s;\n",
+		database) == EOF)
+	    safe_exit(EX_EOF);
+      if (fprintf(md_result_file,"\nUSE %s;\n", database) == EOF) {
+	fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+        safe_exit(EX_EOF);
+      }
     }
   }
   if (extended_insert)
@@ -1532,12 +1613,21 @@
       {
 	row = mysql_fetch_row(master);
 	if(row && row[0] && row[1]) {
-	  fprintf(md_result_file,
-		  "\n--\n-- Position to start replication from\n--\n\n");
-	  fprintf(md_result_file,
-		  "CHANGE MASTER TO MASTER_LOG_FILE='%s' ;\n", row[0]);
-	  fprintf(md_result_file, "CHANGE MASTER TO MASTER_LOG_POS=%s ;\n",
-		  row[1]);
+	  if (fprintf(md_result_file,
+		  "\n--\n-- Position to start replication from\n--\n\n") == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	  }
+	  if (fprintf(md_result_file,
+		  "CHANGE MASTER TO MASTER_LOG_FILE='%s' ;\n", row[0]) == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	  }
+	  if (fprintf(md_result_file, "CHANGE MASTER TO MASTER_LOG_POS=%s ;\n",
+		  row[1]) == EOF) {
+	    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+	    safe_exit(EX_EOF);
+	  }
 	}
 	mysql_free_result(master);
       }
@@ -1549,7 +1639,10 @@
    }
   }
   dbDisconnect(current_host);
-  fputs("\n", md_result_file);
+  if (fputs("\n", md_result_file) == EOF) {
+    fprintf(stderr, "%s: write error: %s\n",my_progname,strerror(errno));
+    safe_exit(EX_EOF);
+  }
   if (md_result_file != stdout)
     my_fclose(md_result_file, MYF(0));
   my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
[18 Dec 2003 20:32] MySQL Verification Team
Victor could you please take a look in the fix suggested.

Thanks you in advance.
[24 Jun 2004 21:03] Victor Vagin
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Additional info:

bk commit - 4.1 tree (vva:1.1920) BUG#1851

ChangeSet
  1.1920 04/06/25 01:14:12 vva@eagle.mysql.r18.ru +1 -0
  fixed bug #1851 "mysqldump does not return 
  an error code if the output device is filled"

The filx will be in mysql-4.1.3