/*
* 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. ng-指令

    在 Angular 中最常用的指令分为两种,它们分别是 属性型指令 和 结构型指令. NgClass 作用:添加或移除一组 CSS 类 NgStyle 作用:添加或移除一组 CSS 样式 NgMode ...

  2. javaweb利用javabean将数据库中内容遍历在页面输出

    效果如下图 它所实现的就是把数据库中一个表中所有的数据一条一条以表格的形式输出在网页上, 实现方法如下 首先我们要从数据库读取数据,这里要借助javabean来方便我们传递数据 以上面的为例,我要输出 ...

  3. 七月在线spark教程

    链接:https://pan.baidu.com/s/1Ir5GMuDqJQBmSavHC-hDgQ 提取码:qd2e

  4. .net mvc 使用 aspose.cells导出数据

    public class AsposeCellsHelper { public Workbook workBook; public Worksheet worksheet; Style style; ...

  5. NC反弹shell的几种方法

    假如ubuntu.CentOS为目标服务器系统 kali为攻击者的系统,ip为:192.168.0.4,开放7777端口且没被占用 最终是将ubuntu.CentOS的shell反弹到kali上 正向 ...

  6. 安装APACHE到CentOS(YUM)

    运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:httpd-2.4.6 硬件要求:无 安装过程 1.安装YUM-EPEL源 HTTP-Tools ...

  7. JDK下载安装与环境变量配置图文教程【超详细】

    JDK下载安装与环境变量配置图文教程[超详细] 创建时间:2019年11月13日11时02分 文章目录 1. JDK介绍 1.1 什么是JDK? 1.2 JDK版本介绍 2. JDK下载与安装 3.w ...

  8. H5_0015:判断是否是微信加载

        var e = document.createElement("script");     e.src = "https://res.wx.qq.com/open ...

  9. C# LINQ学习笔记二:LINQ标准查询操作概述

    本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5801249.html,记录一下学习过程以备后续查用. “标准查询运算符”是组成语言集成查询 (LINQ) 模式 ...

  10. 二分查找 python实现

    欢迎回来 [^first blood]. 要求A是升序数组 递归 只能查 数据存不存在,不能返回下标 def binary_find(A, m): if len(A) == 0: return -1 ...