Skip to content

Commit

Permalink
Handle redis-check-rdb as a standalone program.
Browse files Browse the repository at this point in the history
This also makes it backward compatible in the usage, but for the command
name. However the old command name was less obvious so it is worth to
break it probably.

With the new setup the program main can perform argument parsing and
everything else useful for an RDB check regardless of the Redis server
itself.
  • Loading branch information
antirez committed Feb 3, 2015
1 parent 45102a6 commit 7d1e158
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dump.rdb
redis-benchmark
redis-check-aof
redis-check-rdb
redis-check-dump
redis-cli
redis-sentinel
redis-server
Expand Down
12 changes: 12 additions & 0 deletions src/redis-check-rdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,15 @@ int redis_check_rdb(char *rdbfilename) {
close(fd);
return 0;
}

/* RDB check main: called form redis.c when Redis is executed with the
* redis-check-rdb alias. */
int redis_check_rdb_main(char **argv, int argc) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <rdb-file-name>\n", argv[0]);
exit(1);
}
redisLog(REDIS_WARNING, "Checking RDB file %s", argv[1]);
exit(redis_check_rdb(argv[1]));
return 0;
}
24 changes: 6 additions & 18 deletions src/redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -3550,17 +3550,6 @@ int checkForSentinelMode(int argc, char **argv) {
return 0;
}

/* Returns 1 if there is --check-rdb among the arguments or if
* argv[0] is exactly "redis-check-rdb". */
int checkForCheckRDBMode(int argc, char **argv) {
int j;

if (strstr(argv[0],"redis-check-rdb") != NULL) return 1;
for (j = 1; j < argc; j++)
if (!strcmp(argv[j],"--check-rdb")) return 1;
return 0;
}

/* Function called at startup to load RDB or AOF file in memory. */
void loadDataFromDisk(void) {
long long start = ustime();
Expand Down Expand Up @@ -3746,6 +3735,12 @@ int main(int argc, char **argv) {
initSentinel();
}

/* Check if we need to start in redis-check-rdb mode. We just execute
* the program main. However the program is part of the Redis executable
* so that we can easily execute an RDB check on loading errors. */
if (strstr(argv[0],"redis-check-rdb") != NULL)
exit(redis_check_rdb_main(argv,argc));

if (argc >= 2) {
int j = 1; /* First option to parse in argv[] */
sds options = sdsempty();
Expand Down Expand Up @@ -3807,13 +3802,6 @@ int main(int argc, char **argv) {
redisLog(REDIS_WARNING, "Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/%s.conf", argv[0], server.sentinel_mode ? "sentinel" : "redis");
}

if (checkForCheckRDBMode(argc, argv)) {
redisLog(REDIS_WARNING, "Checking RDB file %s", server.rdb_filename);
redisLog(REDIS_WARNING, "To check different RDB file: "
"redis-check-rdb --dbfilename <dump.rdb>");
exit(redis_check_rdb(server.rdb_filename));
}

server.supervised = redisIsSupervised(server.supervised_mode);
int background = server.daemonize && !server.supervised;
if (background) daemonize();
Expand Down
1 change: 1 addition & 0 deletions src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ void sentinelIsRunning(void);

/* redis-check-rdb */
int redis_check_rdb(char *rdbfilename);
int redis_check_rdb_main(char **argv, int argc);

/* Scripting */
void scriptingInit(void);
Expand Down

0 comments on commit 7d1e158

Please sign in to comment.