本文主要是介绍漫话Redis源码之三十,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
几个读取和函数,挺直接明了,在处理边界问题和异常情况时,尤其要小心:
int readLong(FILE *fp, char prefix, long *target) {char buf[128], *eptr;epos = ftello(fp);if (fgets(buf,sizeof(buf),fp) == NULL) {return 0;}if (buf[0] != prefix) {ERROR("Expected prefix '%c', got: '%c'",prefix,buf[0]);return 0;}*target = strtol(buf+1,&eptr,10);return consumeNewline(eptr);
}int readBytes(FILE *fp, char *target, long length) {long real;epos = ftello(fp);real = fread(target,1,length,fp);if (real != length) {ERROR("Expected to read %ld bytes, got %ld bytes",length,real);return 0;}return 1;
}int readString(FILE *fp, char** target) {long len;*target = NULL;if (!readLong(fp,'$',&len)) {return 0;}/* Increase length to also consume \r\n */len += 2;*target = (char*)zmalloc(len);if (!readBytes(fp,*target,len)) {return 0;}if (!consumeNewline(*target+len-2)) {return 0;}(*target)[len-2] = '\0';return 1;
}int readArgc(FILE *fp, long *target) {return readLong(fp,'*',target);
}off_t process(FILE *fp) {long argc;off_t pos = 0;int i, multi = 0;char *str;while(1) {if (!multi) pos = ftello(fp);if (!readArgc(fp, &argc)) break;for (i = 0; i < argc; i++) {if (!readString(fp,&str)) break;if (i == 0) {if (strcasecmp(str, "multi") == 0) {if (multi++) {ERROR("Unexpected MULTI");break;}} else if (strcasecmp(str, "exec") == 0) {if (--multi) {ERROR("Unexpected EXEC");break;}}}zfree(str);}/* Stop if the loop did not finish */if (i < argc) {if (str) zfree(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 redis_check_aof_main(int argc, char **argv) {char *filename;int fix = 0;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);}filename = argv[2];fix = 1;} else {printf("Invalid arguments\n");exit(1);}FILE *fp = fopen(filename,"r+");if (fp == NULL) {printf("Cannot open file: %s\n", filename);exit(1);}struct redis_stat sb;if (redis_fstat(fileno(fp),&sb) == -1) {printf("Cannot stat file: %s\n", filename);exit(1);}off_t size = sb.st_size;if (size == 0) {printf("Empty file: %s\n", filename);exit(1);}/* This AOF file may have an RDB preamble. Check this to start, and if this* is the case, start processing the RDB part. */if (size >= 8) { /* There must be at least room for the RDB header. */char sig[5];int has_preamble = fread(sig,sizeof(sig),1,fp) == 1 &&memcmp(sig,"REDIS",sizeof(sig)) == 0;rewind(fp);if (has_preamble) {printf("The AOF appears to start with an RDB preamble.\n""Checking the RDB preamble to start:\n");if (redis_check_rdb_main(argc,argv,fp) == C_ERR) {printf("RDB preamble of AOF file is not sane, aborting.\n");exit(1);} else {printf("RDB preamble is OK, proceeding with AOF tail...\n");}}}off_t pos = process(fp);off_t diff = size-pos;printf("AOF analyzed: size=%lld, ok_up_to=%lld, ok_up_to_line=%lld, diff=%lld\n",(long long) size, (long long) pos, line, (long long) diff);if (diff > 0) {if (fix) {char buf[2];printf("This will shrink the AOF from %lld bytes, with %lld bytes, to %lld bytes\n",(long long)size,(long long)diff,(long long)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("Failed to truncate AOF\n");exit(1);} else {printf("Successfully truncated AOF\n");}} else {printf("AOF is not valid. ""Use the --fix option to try fixing it.\n");exit(1);}} else {printf("AOF is valid\n");}fclose(fp);exit(0);
}
这篇关于漫话Redis源码之三十的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!