1 Star 0 Fork 46

wangfengtu/src-iSulad

forked from src-openEuler/iSulad 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0025-utils-add-fdatasync-when-do-atomic-write-file.patch 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
gaohuatao 提交于 2020-11-12 19:32 . update from openeuler
From 8888ca136de8f864df919f6823b357238a328488 Mon Sep 17 00:00:00 2001
From: lifeng68 <[email protected]>
Date: Tue, 10 Nov 2020 10:08:56 +0800
Subject: [PATCH 25/28] utils: add fdatasync when do atomic write file
Signed-off-by: lifeng68 <[email protected]>
---
src/utils/cutils/utils_file.c | 35 ++++++++++++++++++++++++++++++++++-
1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/src/utils/cutils/utils_file.c b/src/utils/cutils/utils_file.c
index 9f7f5fe..744b425 100644
--- a/src/utils/cutils/utils_file.c
+++ b/src/utils/cutils/utils_file.c
@@ -1403,6 +1403,39 @@ out:
return result;
}
+static int do_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode)
+{
+ int ret = 0;
+ int dst_fd = -1;
+ ssize_t len = 0;
+
+ dst_fd = util_open(fname, O_WRONLY | O_CREAT | O_TRUNC, mode);
+ if (dst_fd < 0) {
+ ERROR("Creat file: %s, failed: %s", fname, strerror(errno));
+ ret = -1;
+ goto free_out;
+ }
+
+ len = util_write_nointr(dst_fd, content, content_len);
+ if (len < 0 || ((size_t)len) != content_len) {
+ ret = -1;
+ ERROR("Write file failed: %s", strerror(errno));
+ goto free_out;
+ }
+
+ if (fdatasync(dst_fd) != 0) {
+ ret = -1;
+ SYSERROR("Failed to sync data of file:%s", fname);
+ goto free_out;
+ }
+
+free_out:
+ if (dst_fd >= 0) {
+ close(dst_fd);
+ }
+ return ret;
+}
+
int util_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode)
{
int ret = 0;
@@ -1427,7 +1460,7 @@ int util_atomic_write_file(const char *fname, const char *content, size_t conten
goto free_out;
}
- ret = util_write_file(tmp_file, content, content_len, mode);
+ ret = do_atomic_write_file(tmp_file, content, content_len, mode);
if (ret != 0) {
ERROR("Failed to write content to tmp file for %s", tmp_file);
ret = -1;
--
2.20.1
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wangfengtu/src-iSulad.git
[email protected]:wangfengtu/src-iSulad.git
wangfengtu
src-iSulad
src-iSulad
master

搜索帮助