Index: src/rrd_dump.c =================================================================== --- src/rrd_dump.c (revision 1746) +++ src/rrd_dump.c (working copy) @@ -355,13 +355,14 @@ fprintf(out_file, "\n"); rrd_free(&rrd); fclose(in_file); + if (fflush(out_file) || ferror(out_file)) { + if (out_file != stdout) fclose(out_file); + return(-1); + } if (out_file != stdout) { - fclose(out_file); + if(fclose(out_file)) return(-1); } return(0); } - - - Index: src/rrd_create.c =================================================================== --- src/rrd_create.c (revision 1746) +++ src/rrd_create.c (working copy) @@ -679,16 +679,28 @@ unkn_cnt -= 512; } free(unknown); - + /* lets see if we had an error */ - if(ferror(rrd_file)){ - rrd_set_error("a file error occurred while creating '%s'",file_name); + if(fflush(rrd_file) || ferror(rrd_file)){ + rrd_set_error("a file error occurred while writing to '%s'",file_name); fclose(rrd_file); rrd_free(rrd); return(-1); } - - fclose(rrd_file); + + if (fsync(fileno(rrd_file)) == -1) { + rrd_set_error("a file error occurred while fsync('%s'): %s", + file_name, rrd_strerror(errno)); + rrd_free(rrd); + return (-1); + } + + if (fclose(rrd_file)) { + rrd_set_error("a file error occurred while closing '%s'", file_name); + rrd_free(rrd); + return(-1); + } + rrd_free(rrd); return (0); } Index: src/rrd_graph_helper.c =================================================================== --- src/rrd_graph_helper.c (revision 1746) +++ src/rrd_graph_helper.c (working copy) @@ -858,7 +858,7 @@ break; } if (gdp->debug) { - dprintf("used %i out of %i chars\n",eaten,strlen(argv[i])); + dprintf("used %i out of %zu chars\n",eaten,strlen(argv[i])); dprintf("parsed line: '%s'\n",argv[i]); dprintf("remaining: '%s'\n",&argv[i][eaten]); if (eaten >= strlen(argv[i])) Index: src/rrd_graph.c =================================================================== --- src/rrd_graph.c (revision 1746) +++ src/rrd_graph.c (working copy) @@ -2669,6 +2669,8 @@ graph_paint(image_desc_t *im, char ***calcpr) { int i,ii; + int ret; + char graphtmp[MAXPATH]; int lazy = lazy_check(im); #ifdef WITH_PIECHART int piechart = 0; @@ -3047,16 +3049,41 @@ _setmode( _fileno( fo ), O_BINARY ); #endif } else { - if ((fo = fopen(im->graphfile,"wb")) == NULL) { - rrd_set_error("Opening '%s' for write: %s",im->graphfile, + graphtmp[0] = 0; + if (snprintf(graphtmp, MAXPATH, "%s.%lu.%u.tmp", + im->graphfile, (unsigned long)time(NULL), getpid()) >= MAXPATH) { + rrd_set_error("Filename too long: '%s'", graphtmp); + return (-1); + } + (void)unlink(graphtmp); + if ((fo = fopen(graphtmp, "wbex")) == NULL) { + rrd_set_error("Opening '%s' for write: %s",graphtmp, rrd_strerror(errno)); return (-1); } } - gfx_render (im->canvas,im->ximg,im->yimg,0x00000000,fo); - if (strcmp(im->graphfile,"-") != 0) - fclose(fo); - return 0; + ret = gfx_render (im->canvas,im->ximg,im->yimg,0x00000000,fo); + if (fflush(fo) == EOF) { + rrd_set_error("fflush '%s': ", graphtmp, rrd_strerror(errno)); + ret = -1; + } else if (strcmp(im->graphfile,"-") != 0) { +#if 0 + if (fsync(fileno(fo)) == -1) { + rrd_set_error("fsync '%s': ", graphtmp, rrd_strerror(errno)); + ret = -1; + } +#endif + if (fclose(fo) == EOF) { + rrd_set_error("fclose '%s': ", graphtmp, rrd_strerror(errno)); + ret = -1; + } else if (rename(graphtmp, im->graphfile) == -1) { + unlink(graphtmp); + rrd_set_error("rename '%s' to '%s': ", graphtmp, im->graphfile, + rrd_strerror(errno)); + ret = -1; + } + } + return ret; }