X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/cb8ae3c88908a8a4b9c88d56713ae2aea472cbd8..4d16bb253c8807e45abcd776fcf6521c525a3f2b:/redis-check-aof.c?ds=sidebyside diff --git a/redis-check-aof.c b/redis-check-aof.c index 31abe1be..ff0d1f82 100644 --- a/redis-check-aof.c +++ b/redis-check-aof.c @@ -1,3 +1,4 @@ +#include "fmacros.h" #include #include #include @@ -58,13 +59,9 @@ int readString(FILE *fp, char** target) { len += 2; *target = (char*)malloc(len); if (!readBytes(fp,*target,len)) { - free(*target); - *target = NULL; return 0; } if (!consumeNewline(*target+len-2)) { - free(*target); - *target = NULL; return 0; } (*target)[len-2] = '\0'; @@ -82,14 +79,10 @@ long process(FILE *fp) { while(1) { if (!multi) pos = ftell(fp); - if (!readArgc(fp, &argc)) { - break; - } + if (!readArgc(fp, &argc)) break; for (i = 0; i < argc; i++) { - if (!readString(fp,&str)) { - break; - } + if (!readString(fp,&str)) break; if (i == 0) { if (strcasecmp(str, "multi") == 0) { if (multi++) { @@ -106,7 +99,7 @@ long process(FILE *fp) { free(str); } - /* Check if loop was finished */ + /* Stop if the loop did not finish */ if (i < argc) { if (str) free(str); break; @@ -116,34 +109,30 @@ long process(FILE *fp) { if (feof(fp) && multi && strlen(error) == 0) { ERROR("Reached EOF before reading EXEC for MULTI"); } - if (strlen(error) > 0) { printf("%s\n", error); } - return pos; } int main(int argc, char **argv) { - /* expect the first argument to be the dump file */ - if (argc <= 1) { - printf("Usage: %s [--fix] \n", argv[0]); - exit(0); - } - char *filename; int fix = 0; - if (argc == 3) { + + if (argc < 2) { + printf("Usage: %s [--fix] \n", argv[0]); + exit(1); + } else if (argc == 2) { + filename = argv[1]; + } else if (argc == 3) { if (strcmp(argv[1],"--fix") != 0) { printf("Invalid argument: %s\n", argv[1]); exit(1); } - fix = 1; filename = argv[2]; - } else if (argc == 2) { - filename = argv[1]; + fix = 1; } else { - printf("Invalid argument"); + printf("Invalid arguments\n"); exit(1); } @@ -166,16 +155,26 @@ int main(int argc, char **argv) { } long pos = process(fp); - if (pos < size) { + long diff = size-pos; + if (diff > 0) { if (fix) { + char buf[2]; + printf("This will shrink the AOF from %ld bytes, with %ld bytes, to %ld bytes\n",size,diff,pos); + printf("Continue? [y/N]: "); + if (fgets(buf,sizeof(buf),stdin) == NULL || + strncasecmp(buf,"y",1) != 0) { + printf("Aborting...\n"); + exit(1); + } if (ftruncate(fileno(fp), pos) == -1) { - printf("Could not truncate AOF to size %ld\n", pos); + printf("Failed to truncate AOF\n"); exit(1); } else { - printf("AOF succesfully truncated to %ld bytes\n", pos); + printf("Successfully truncated AOF\n"); } } else { - printf("First invalid operation at offset %ld\n", pos); + printf("AOF is not valid\n"); + exit(1); } } else { printf("AOF is valid\n");