From a2c4706fc7b3f9478adf7eb077d419177e89c62f Mon Sep 17 00:00:00 2001 From: Matt Stancliff Date: Thu, 13 Mar 2014 14:40:25 -0400 Subject: [PATCH] Fix data loss when save AOF/RDB with no free space Previously, the (!fp) would only catch lack of free space under OS X. Linux waits to discover it can't write until it actually writes contents to disk. (fwrite() returns success even if the underlying file has no free space to write into. All the errors only show up at flush/sync/close time.) Fixes antirez/redis#1604 --- src/aof.c | 6 +++--- src/rdb.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/aof.c b/src/aof.c index dbe7bfa6d..236a98c48 100644 --- a/src/aof.c +++ b/src/aof.c @@ -969,9 +969,9 @@ int rewriteAppendOnlyFile(char *filename) { } /* Make sure data will not remain on the OS's output buffers */ - fflush(fp); - aof_fsync(fileno(fp)); - fclose(fp); + if (fflush(fp) == EOF) goto werr; + if (aof_fsync(fileno(fp)) == -1) goto werr; + if (fclose(fp) == EOF) goto werr; /* Use RENAME to make sure the DB file is changed atomically only * if the generate DB file is ok. */ diff --git a/src/rdb.c b/src/rdb.c index 1a472dbc7..eb27bfa7a 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -690,9 +690,9 @@ int rdbSave(char *filename) { rioWrite(&rdb,&cksum,8); /* Make sure data will not remain on the OS's output buffers */ - fflush(fp); - fsync(fileno(fp)); - fclose(fp); + if (fflush(fp) == EOF) goto werr; + if (fsync(fileno(fp)) == -1) goto werr; + if (fclose(fp) == EOF) goto werr; /* Use RENAME to make sure the DB file is changed atomically only * if the generate DB file is ok. */