--- rrdtool-1.2.18/src/rrd_graph.c.bak 2007-01-23 22:54:49.000000000 +0200 +++ rrdtool-1.2.18/src/rrd_graph.c 2007-02-03 16:22:25.668458169 +0200 @@ -2550,6 +2550,8 @@ int 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; @@ -2928,16 +2930,38 @@ graph_paint(image_desc_t *im, char ***ca _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; + snprintf(graphtmp, MAXPATH - 16, "%s.%u.tmp", im->graphfile, getpid()); + if ((fo = fopen(graphtmp, "wb")) == 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; + } + 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; + } + 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; }