/*
* 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. 1.(group by)如何让group by分组后,每组中的所有数据都显示出来

    问题描述:表如下,如何让这个表按device_id这个字段分组,且组中的每条数据都查寻出来?(假如说这个表名为:devicedata) 错误答案:select * from devicedata GR ...

  2. Truck History POJ - 1789 板子题

    #include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...

  3. 项目部署 ubuntu Django uwsgi配置

    1.进入项目文件夹 mkdir uwsgi_file vim uwsgi.ini 写入保存 [uwsgi] chdir = /home/mysite/my_project # 项目目录 module ...

  4. BZOJ 2306: [Ctsc2011]幸福路径

    Description 有向图 G有n个顶点 1, 2, -, n,点i 的权值为 w(i).现在有一只蚂蚁,从 给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条 边,它 ...

  5. 百度的Ueditor在VS2015/VS2010中配置需注意的地方

    1.下载:https://ueditor.baidu.com/website/download.html#uedito 官网已无1.4.3的.net版本下载,不知道百度在搞什么,下载完整版和ASP版本 ...

  6. python接口自动化之pytest环境准备与入门(五)

    安装的pytest版本应该与安装的python版本对应,不然会有问题 (我的环境是python3.6与pytest4.5.0) 1.安装pytest pip install pytest==4.5.0 ...

  7. Android Studio阶段性学习总结_1

    这半个月一直在学习Android方面的知识,对Android开发有了一个基本的认识,学会了Android studio的基本操作. 在建立第一个Android studio项目时,我遇到了很大的阻碍, ...

  8. [HNOI2001] 求正整数 - 背包dp,数论

    对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m. Solution (乍一看很简单却搞了好久?我真是太菜了) 根据因子个数计算公式 若 \(m = \prod p_i^{q_i}\) ...

  9. Linux之温故知新2

    1.关于ssh免密码登陆的ssh-keygen, ssh-copy-id的使用, 然后使用ssh-copy-id user@remote将公钥传给服务器, 以及别名 1 C:\Users\linxmo ...

  10. 白面系列 kafka

    kafka是一个分布式发布订阅消息系统,也可叫做MQ系统,MQ是Message Queue,消息队列. 通俗点,生产者往队列里写消息,消费者从队列里读.专业点,Producer通过TCP协议发送消息到 ...