+#include "fmacros.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
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';
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++) {
free(str);
}
- /* Check if loop was finished */
+ /* Stop if the loop did not finish */
if (i < argc) {
if (str) free(str);
break;
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] <file.aof>\n", argv[0]);
- exit(0);
- }
-
char *filename;
int fix = 0;
- if (argc == 3) {
+
+ if (argc < 2) {
+ printf("Usage: %s [--fix] <file.aof>\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);
}
}
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");