Save the DB on disk. Return C_ERR on error, C_OK on success. */
| 1480 | |
| 1481 | /* Save the DB on disk. Return C_ERR on error, C_OK on success. */ |
| 1482 | int rdbSaveFile(char *filename, const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi) { |
| 1483 | char tmpfile[256]; |
| 1484 | char cwd[MAXPATHLEN]; /* Current working dir path for error messages. */ |
| 1485 | FILE *fp = NULL; |
| 1486 | rio rdb; |
| 1487 | int error = 0; |
| 1488 | |
| 1489 | getTempFileName(tmpfile, g_pserver->rdbThreadVars.tmpfileNum); |
| 1490 | fp = fopen(tmpfile,"w"); |
| 1491 | if (!fp) { |
| 1492 | char *cwdp = getcwd(cwd,MAXPATHLEN); |
| 1493 | serverLog(LL_WARNING, |
| 1494 | "Failed opening the RDB file %s (in server root dir %s) " |
| 1495 | "for saving: %s", |
| 1496 | filename, |
| 1497 | cwdp ? cwdp : "unknown", |
| 1498 | strerror(errno)); |
| 1499 | return C_ERR; |
| 1500 | } |
| 1501 | |
| 1502 | rioInitWithFile(&rdb,fp); |
| 1503 | startSaving(RDBFLAGS_NONE); |
| 1504 | |
| 1505 | if (g_pserver->rdb_save_incremental_fsync) |
| 1506 | rioSetAutoSync(&rdb,REDIS_AUTOSYNC_BYTES); |
| 1507 | |
| 1508 | if (rdbSaveRio(&rdb,rgpdb,&error,RDBFLAGS_NONE,rsi) == C_ERR) { |
| 1509 | errno = error; |
| 1510 | goto werr; |
| 1511 | } |
| 1512 | |
| 1513 | /* Make sure data will not remain on the OS's output buffers */ |
| 1514 | if (fflush(fp)) goto werr; |
| 1515 | if (fsync(fileno(fp))) goto werr; |
| 1516 | if (fclose(fp)) { fp = NULL; goto werr; } |
| 1517 | fp = NULL; |
| 1518 | |
| 1519 | /* Use RENAME to make sure the DB file is changed atomically only |
| 1520 | * if the generate DB file is ok. */ |
| 1521 | if (rename(tmpfile,filename) == -1) { |
| 1522 | char *cwdp = getcwd(cwd,MAXPATHLEN); |
| 1523 | serverLog(LL_WARNING, |
| 1524 | "Error moving temp DB file %s on the final " |
| 1525 | "destination %s (in server root dir %s): %s", |
| 1526 | tmpfile, |
| 1527 | filename, |
| 1528 | cwdp ? cwdp : "unknown", |
| 1529 | strerror(errno)); |
| 1530 | unlink(tmpfile); |
| 1531 | stopSaving(0); |
| 1532 | return C_ERR; |
| 1533 | } |
| 1534 | |
| 1535 | serverLog(LL_NOTICE,"DB saved on disk"); |
| 1536 | if (!g_pserver->rdbThreadVars.fRdbThreadActive) |
| 1537 | { |
| 1538 | // Do this only in a synchronous save, otherwise our thread controller will update these |
| 1539 | g_pserver->dirty = 0; |
no test coverage detected