Save the DB on disk. Return C_ERR on error, C_OK on success. */
| 1353 | |
| 1354 | /* Save the DB on disk. Return C_ERR on error, C_OK on success. */ |
| 1355 | int rdbSave(char *filename, rdbSaveInfo *rsi) { |
| 1356 | char tmpfile[256]; |
| 1357 | char cwd[MAXPATHLEN]; /* Current working dir path for error messages. */ |
| 1358 | FILE *fp = NULL; |
| 1359 | rio rdb; |
| 1360 | int error = 0; |
| 1361 | |
| 1362 | snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid()); |
| 1363 | fp = fopen(tmpfile,"w"); |
| 1364 | if (!fp) { |
| 1365 | char *cwdp = getcwd(cwd,MAXPATHLEN); |
| 1366 | serverLog(LL_WARNING, |
| 1367 | "Failed opening the RDB file %s (in server root dir %s) " |
| 1368 | "for saving: %s", |
| 1369 | filename, |
| 1370 | cwdp ? cwdp : "unknown", |
| 1371 | strerror(errno)); |
| 1372 | return C_ERR; |
| 1373 | } |
| 1374 | |
| 1375 | rioInitWithFile(&rdb,fp); |
| 1376 | startSaving(RDBFLAGS_NONE); |
| 1377 | |
| 1378 | if (server.rdb_save_incremental_fsync) |
| 1379 | rioSetAutoSync(&rdb,REDIS_AUTOSYNC_BYTES); |
| 1380 | |
| 1381 | if (rdbSaveRio(&rdb,&error,RDBFLAGS_NONE,rsi) == C_ERR) { |
| 1382 | errno = error; |
| 1383 | goto werr; |
| 1384 | } |
| 1385 | |
| 1386 | /* Make sure data will not remain on the OS's output buffers */ |
| 1387 | if (fflush(fp)) goto werr; |
| 1388 | if (fsync(fileno(fp))) goto werr; |
| 1389 | if (fclose(fp)) { fp = NULL; goto werr; } |
| 1390 | fp = NULL; |
| 1391 | |
| 1392 | /* Use RENAME to make sure the DB file is changed atomically only |
| 1393 | * if the generate DB file is ok. */ |
| 1394 | if (rename(tmpfile,filename) == -1) { |
| 1395 | char *cwdp = getcwd(cwd,MAXPATHLEN); |
| 1396 | serverLog(LL_WARNING, |
| 1397 | "Error moving temp DB file %s on the final " |
| 1398 | "destination %s (in server root dir %s): %s", |
| 1399 | tmpfile, |
| 1400 | filename, |
| 1401 | cwdp ? cwdp : "unknown", |
| 1402 | strerror(errno)); |
| 1403 | unlink(tmpfile); |
| 1404 | stopSaving(0); |
| 1405 | return C_ERR; |
| 1406 | } |
| 1407 | |
| 1408 | serverLog(LL_NOTICE,"DB saved on disk"); |
| 1409 | server.dirty = 0; |
| 1410 | server.lastsave = time(NULL); |
| 1411 | server.lastbgsave_status = C_OK; |
| 1412 | stopSaving(1); |
no test coverage detected