redis源码(八)redis-check-aof.c
/*
* 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的更多相关文章
- Redis 源码简洁剖析 15 - AOF
AOF 是什么 AOF 持久化的实现 命令追加 AOF 文件的写入和同步 AOF 文件的载入和数据还原 AOF 重写 为什么需要重写 什么是重写 如何重写 AOF 后台重写 为什么需要后台重写 带来的 ...
- Redis源码研究--redis.h
------------7月3日------------ /* The redisOp structure defines a Redis Operation, that is an instance ...
- Redis源码剖析--源码结构解析
请持续关注我的个人博客:https://zcheng.ren 找工作那会儿,看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识.在面试过程中,redis确实 ...
- Redis源码漂流记(二)-搭建Redis调试环境
Redis源码漂流记(二)-搭建Redis调试环境 一.目标 搭建Redis调试环境 简要理解Redis命令运转流程 二.前提 1.有一些c知识简单基础(变量命名.常用数据类型.指针等) 可以参考这篇 ...
- 玩一把redis源码(一):为redis添加自己的列表类型
2019年第一篇文档,为2019年做个良好的开端,本文档通过step by step的方式向读者展示如何为redis添加一个数据类型,阅读本文档后读者对redis源码的执行逻辑会有比较清晰的认识,并且 ...
- redis源码(一):为redis添加自己的列表类型
本文档分为三大部分: 环境介绍与效果演示 redis接收命令到返回数据的执行逻辑 代码实现 文档的重点和难点在第三部分,完全阅读本文档需要读者具备基本的c语言和数据结构知识. 环境介绍和效果演示环境介 ...
- Redis源码阅读(二)高可用设计——复制
Redis源码阅读(二)高可用设计-复制 复制的概念:Redis的复制简单理解就是一个Redis服务器从另一台Redis服务器复制所有的Redis数据库数据,能保持两台Redis服务器的数据库数据一致 ...
- Redis源码剖析
Redis源码剖析和注释(一)---链表结构 Redis源码剖析和注释(二)--- 简单动态字符串 Redis源码剖析和注释(三)--- Redis 字典结构 Redis源码剖析和注释(四)--- 跳 ...
- Redis源码分析:serverCron - redis源码笔记
[redis源码分析]http://blog.csdn.net/column/details/redis-source.html Redis源代码重要目录 dict.c:也是很重要的两个文件,主要 ...
- Redis源码解析:15Resis主从复制之从节点流程
Redis的主从复制功能,可以实现Redis实例的高可用,避免单个Redis 服务器的单点故障,并且可以实现负载均衡. 一:主从复制过程 Redis的复制功能分为同步(sync)和命令传播(comma ...
随机推荐
- @Autowired、@Resource、@Qualifier区别
@Autowired 1.属于spring的注解,如果不想和Spring耦合的太紧,就不推荐使用. 2.默认情况下,要求依赖对象必须存在,不能为null.如果允许为空,那么设置属性值required为 ...
- 【Vue2.x笔记3】从源码看watch对象
初始化 function initWatch (vm: Component, watch: Object) { for (const key in watch) { const handler = w ...
- A Bug's Life____并查集
English preparation: falsify 伪造:篡改:歪曲:证明...虚假 the sexual behavior of a rare species of bugs. 一种稀 ...
- php 爬虫采集
概述 现在爬虫技术算是一个普遍的技术了,各个语言的爬虫百家争鸣,但是根据笔者自己的感觉还是python是主流.爬虫涉及到太多的东西,笔者并不是专业的爬虫工程师,只不过个人兴趣分享一下.由于笔者是php ...
- Bootstrap Table踩坑——设置多级表头后只显示第一级表头问题解决办法
今天设置了Bootstrap Table的复杂表头,设置了多级表头(两行列名),但是只能显示第一级表头(第一行的列名),第二级的表头被第一级的表头覆盖.但是我仿照其他网上的其他设置复杂表头例子都能正常 ...
- 关闭 APIPA
遇到的问题:我在网卡2上设置了静态ip,可是出现了一个奇怪的ip地址169.254.*.*,如下图. 解决方法:关闭APIPA功能 按照下述的做法,自己在win7企业版上尝试了下,有效.不再出现169 ...
- C语言 Char* 和Char 用法
分类专栏: C语言 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/imxlw00/ar ...
- Quick Sort(快速排序)
Quick Sort Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a ...
- SVN的使用01
关于svn的使用以及TortoiseSVN常见操作 一.关于svn介绍 在介绍之前提一下,MyEclipse项目组的建立,以及源文件夹的创建. 新建的那一栏点击other 在搜索栏中搜索Java Wo ...
- 连接数据库报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
报错: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.soc ...