/*
* Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/ #include "fmacros.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "config.h" #define ERROR(...) { \
char __buf[]; \
sprintf(__buf, __VA_ARGS__); \
sprintf(error, "0x%16llx: %s", (long long)epos, __buf); \
} static char error[];
static off_t epos; int consumeNewline(char *buf) {
if (strncmp(buf,"\r\n",) != ) {
ERROR("Expected \\r\\n, got: %02x%02x",buf[],buf[]);
return ;
}
return ;
} int readLong(FILE *fp, char prefix, long *target) {
char buf[], *eptr;
epos = ftello(fp);
if (fgets(buf,sizeof(buf),fp) == NULL) {
return ;
}
if (buf[] != prefix) {
ERROR("Expected prefix '%c', got: '%c'",buf[],prefix);
return ;
}
*target = strtol(buf+,&eptr,);
return consumeNewline(eptr);
} int readBytes(FILE *fp, char *target, long length) {
long real;
epos = ftello(fp);
real = fread(target,,length,fp);
if (real != length) {
ERROR("Expected to read %ld bytes, got %ld bytes",length,real);
return ;
}
return ;
} int readString(FILE *fp, char** target) {
long len;
*target = NULL;
if (!readLong(fp,'$',&len)) {
return ;
} /* Increase length to also consume \r\n */
len += ;
*target = (char*)malloc(len);
if (!readBytes(fp,*target,len)) {
return ;
}
if (!consumeNewline(*target+len-)) {
return ;
}
(*target)[len-] = '\0';
return ;
} int readArgc(FILE *fp, long *target) {
return readLong(fp,'*',target);
} off_t process(FILE *fp) {
long argc;
off_t pos = ;
int i, multi = ;
char *str; while() {
if (!multi) pos = ftello(fp);
if (!readArgc(fp, &argc)) break; for (i = ; i < argc; i++) {
if (!readString(fp,&str)) break;
if (i == ) {
if (strcasecmp(str, "multi") == ) {
if (multi++) {
ERROR("Unexpected MULTI");
break;
}
} else if (strcasecmp(str, "exec") == ) {
if (--multi) {
ERROR("Unexpected EXEC");
break;
}
}
}
free(str);
} /* Stop if the loop did not finish */
if (i < argc) {
if (str) free(str);
break;
}
} if (feof(fp) && multi && strlen(error) == ) {
ERROR("Reached EOF before reading EXEC for MULTI");
}
if (strlen(error) > ) {
printf("%s\n", error);
}
return pos;
} int main(int argc, char **argv) {
char *filename;
int fix = ; if (argc < ) {
printf("Usage: %s [--fix] <file.aof>\n", argv[]);
exit();
} else if (argc == ) {
filename = argv[];
} else if (argc == ) {
if (strcmp(argv[],"--fix") != ) {
printf("Invalid argument: %s\n", argv[]);
exit();
}
filename = argv[];
fix = ;
} else {
printf("Invalid arguments\n");
exit();
} FILE *fp = fopen(filename,"r+");
if (fp == NULL) {
printf("Cannot open file: %s\n", filename);
exit();
} struct redis_stat sb;
if (redis_fstat(fileno(fp),&sb) == -) {
printf("Cannot stat file: %s\n", filename);
exit();
} off_t size = sb.st_size;
if (size == ) {
printf("Empty file: %s\n", filename);
exit();
} off_t pos = process(fp);
off_t diff = size-pos;
printf("AOF analyzed: size=%lld, ok_up_to=%lld, diff=%lld\n",
(long long) size, (long long) pos, (long long) diff);
if (diff > ) {
if (fix) {
char buf[];
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",) != ) {
printf("Aborting...\n");
exit();
}
if (ftruncate(fileno(fp), pos) == -) {
printf("Failed to truncate AOF\n");
exit();
} else {
printf("Successfully truncated AOF\n");
}
} else {
printf("AOF is not valid\n");
exit();
}
} else {
printf("AOF is valid\n");
} fclose(fp);
return ;
}

1、如何使用?这里有什么意义,为什么需要这样使用?

redis源码(八)redis-check-aof.c的更多相关文章

  1. Redis 源码简洁剖析 15 - AOF

    AOF 是什么 AOF 持久化的实现 命令追加 AOF 文件的写入和同步 AOF 文件的载入和数据还原 AOF 重写 为什么需要重写 什么是重写 如何重写 AOF 后台重写 为什么需要后台重写 带来的 ...

  2. Redis源码研究--redis.h

    ------------7月3日------------ /* The redisOp structure defines a Redis Operation, that is an instance ...

  3. Redis源码剖析--源码结构解析

    请持续关注我的个人博客:https://zcheng.ren 找工作那会儿,看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识.在面试过程中,redis确实 ...

  4. Redis源码漂流记(二)-搭建Redis调试环境

    Redis源码漂流记(二)-搭建Redis调试环境 一.目标 搭建Redis调试环境 简要理解Redis命令运转流程 二.前提 1.有一些c知识简单基础(变量命名.常用数据类型.指针等) 可以参考这篇 ...

  5. 玩一把redis源码(一):为redis添加自己的列表类型

    2019年第一篇文档,为2019年做个良好的开端,本文档通过step by step的方式向读者展示如何为redis添加一个数据类型,阅读本文档后读者对redis源码的执行逻辑会有比较清晰的认识,并且 ...

  6. redis源码(一):为redis添加自己的列表类型

    本文档分为三大部分: 环境介绍与效果演示 redis接收命令到返回数据的执行逻辑 代码实现 文档的重点和难点在第三部分,完全阅读本文档需要读者具备基本的c语言和数据结构知识. 环境介绍和效果演示环境介 ...

  7. Redis源码阅读(二)高可用设计——复制

    Redis源码阅读(二)高可用设计-复制 复制的概念:Redis的复制简单理解就是一个Redis服务器从另一台Redis服务器复制所有的Redis数据库数据,能保持两台Redis服务器的数据库数据一致 ...

  8. Redis源码剖析

    Redis源码剖析和注释(一)---链表结构 Redis源码剖析和注释(二)--- 简单动态字符串 Redis源码剖析和注释(三)--- Redis 字典结构 Redis源码剖析和注释(四)--- 跳 ...

  9. Redis源码分析:serverCron - redis源码笔记

    [redis源码分析]http://blog.csdn.net/column/details/redis-source.html   Redis源代码重要目录 dict.c:也是很重要的两个文件,主要 ...

  10. Redis源码解析:15Resis主从复制之从节点流程

    Redis的主从复制功能,可以实现Redis实例的高可用,避免单个Redis 服务器的单点故障,并且可以实现负载均衡. 一:主从复制过程 Redis的复制功能分为同步(sync)和命令传播(comma ...

随机推荐

  1. java开发相关工具安装包分享

    链接:https://pan.baidu.com/s/19rSlXhrZ9AtNdai64tErGQ 提取码:04up

  2. 重启监听卡在connecting to的问题

    问题描述:lsnrctl start启动监听起不来,一直卡在connecting to半天 1.[oracle@orcl ~]$ lsnrctl start 一直卡半天,就是连不上,按照以前的解决办法 ...

  3. Python中BaseException和Exception的区别

    BaseException 是 Exception 的父类,作为子类的Exception无法截获父类BaseException类型的错误 BaseException: 包含所有built-in exc ...

  4. 记录 Docker 的学习过程 (自建私有仓库)

    私有仓库的创建 node1#wget http://harbor.orientsoft.cn/harbor-v1.4.0/harbor-offline-installer-v1.4.0.tgz nod ...

  5. 1级搭建类111-Oracle 19c SI FS(Windows Server 2019)公开

    Oracle 19c 单实例文件系统在Windows Server 2019上的安装 在线查看

  6. C#中怎样将数组的顺序打乱随机排序

    场景 在ZedGraph随机生成颜色时需要从颜色数组中取颜色对象. Color数组存取的是System.Drawing.Color的颜色. 其顺序是相邻的颜色,颜色差距不大,在取颜色时按顺序取颜色时, ...

  7. WPF 不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改

    该问题出现在WPF中的VM类中,ObservableCollection类型,该类型的 CollectionView 不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改, ...

  8. CSS-定义样式表

    1.HTML标记定义 p{属性:属性值;属性1:属性1} <p>...</p> 注:p可以叫做选择器,定义那个标记中的内容执行其中的样式.一个选择器可以控制若干个样式属性,他们 ...

  9. Web Workers - (Worker(专有) and SharedWorker(共享))

    Web Worker为Web内容在后台线程中运行脚本提供了一种简单的方法 线程可以执行任务而不干扰用户界面 可以使用XMLHttpRequest执行 I/O (尽管responseXML和channe ...

  10. mac 命令行下 vim 的使用

    vi/vim 使用实例 使用 vi 来建立名为 test.txt 的文件 vi test.txt1按下 ESC 按钮回到一般模式 在一般模式中按下 :wq 储存后离开 vi 基本上 vi/vim 共分 ...