Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
别团等shy哥发育
redis
提交
8feb3d26
R
redis
项目概览
别团等shy哥发育
/
redis
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8feb3d26
编写于
5月 14, 2013
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
CONFIG REWRITE: actually rewrite the config file, atomically.
上级
a461376c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
63 addition
and
3 deletion
+63
-3
src/config.c
src/config.c
+63
-3
未找到文件。
src/config.c
浏览文件 @
8feb3d26
...
...
@@ -28,9 +28,11 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "redis.h"
#include <fcntl.h>
#include <sys/stat.h>
static
struct
{
const
char
*
name
;
const
int
value
;
...
...
@@ -1442,6 +1444,63 @@ void rewriteConfigRemoveOrphaned(struct rewriteConfigState *state) {
dictReleaseIterator
(
di
);
}
/* This function overwrites the old configuration file with the new content.
*
* 1) The old file length is obtained.
* 2) If the new content is smaller, padding is added.
* 3) A single write(2) call is used to replace the content of the file.
* 4) Later the file is truncated to the length of the new content.
*
* This way we are sure the file is left in a consistent state even if the
* process is stopped between any of the four operations.
*
* The function returns 0 on success, otherwise -1 is returned and errno
* set accordingly. */
int
rewriteConfigOverwriteFile
(
char
*
configfile
,
sds
content
)
{
int
retval
=
0
;
int
fd
=
open
(
configfile
,
O_RDWR
|
O_CREAT
);
int
content_size
=
sdslen
(
content
),
padding
=
0
;
struct
stat
sb
;
sds
content_padded
;
/* 1) Open the old file (or create a new one if it does not
* exist), get the size. */
if
(
fd
==
-
1
)
return
-
1
;
/* errno set by open(). */
if
(
fstat
(
fd
,
&
sb
)
==
-
1
)
{
close
(
fd
);
return
-
1
;
/* errno set by fstat(). */
}
/* 2) Pad the content at least match the old file size. */
content_padded
=
sdsdup
(
content
);
if
(
content_size
<
sb
.
st_size
)
{
/* If the old file was bigger, pad the content with
* a newline plus as many "#" chars as required. */
padding
=
sb
.
st_size
-
content_size
;
content_padded
=
sdsgrowzero
(
content_padded
,
sb
.
st_size
);
content_padded
[
content_size
]
=
'\n'
;
memset
(
content_padded
+
content_size
+
1
,
'#'
,
padding
-
1
);
}
/* 3) Write the new content using a single write(2). */
if
(
write
(
fd
,
content_padded
,
strlen
(
content_padded
))
==
-
1
)
{
retval
=
-
1
;
goto
cleanup
;
}
/* 4) Truncate the file to the right length if we used padding. */
if
(
padding
)
{
if
(
ftruncate
(
fd
,
content_size
)
==
-
1
)
{
/* Non critical error... */
}
}
cleanup:
sdsfree
(
content_padded
);
close
(
fd
);
return
retval
;
}
/* Rewrite the configuration file at "path".
* If the configuration file already exists, we try at best to retain comments
* and overall structure.
...
...
@@ -1453,6 +1512,7 @@ void rewriteConfigRemoveOrphaned(struct rewriteConfigState *state) {
int
rewriteConfig
(
char
*
path
)
{
struct
rewriteConfigState
*
state
;
sds
newcontent
;
int
retval
;
/* Step 1: read the old config into our rewrite state. */
if
((
state
=
rewriteConfigReadOldFile
(
path
))
==
NULL
)
return
-
1
;
...
...
@@ -1544,11 +1604,11 @@ int rewriteConfig(char *path) {
/* Step 4: generate a new configuration file from the modified state
* and write it into the original file. */
newcontent
=
rewriteConfigGetContentFromState
(
state
);
printf
(
"%s
\n
"
,
newcontent
);
retval
=
rewriteConfigOverwriteFile
(
server
.
configfile
,
newcontent
);
sdsfree
(
newcontent
);
rewriteConfigReleaseState
(
state
);
return
0
;
return
retval
;
}
/*-----------------------------------------------------------------------------
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录