add fs_copy_file/fs_copy_dir/dir_exist

This commit is contained in:
lixianjing 2021-03-02 18:15:11 +08:00
parent deb6ce6a57
commit 3100206787
5 changed files with 189 additions and 0 deletions

View File

@ -3,6 +3,7 @@
2020/03/02
* 删除将QT XML生成AWTK UI的转换工具感谢陈谭提供补丁
* 修复widget\_clone处理tr\_text的BUG(感谢网友Eagle提供补丁)。
* 增加函数fs\_copy\_file、fs\_copy\_dir和dir\_exist。
2020/03/01
* 修复slide\_view通过indicator快速切换page的时候导致有概 率出现播放动画异常(感谢智明提供补丁)

View File

@ -298,6 +298,12 @@ bool_t file_exist(const char* name) {
return fs_file_exist(os_fs(), name);
}
bool_t dir_exist(const char* name) {
return_value_if_fail(name != NULL, FALSE);
return fs_dir_exist(os_fs(), name);
}
ret_t fs_test_file(fs_t* fs) {
char buff[32];
fs_file_t* fp = NULL;
@ -344,6 +350,8 @@ ret_t fs_test_dir(fs_t* fs) {
fs_item_t item;
fs_dir_t* dir = NULL;
fs_remove_dir_r(fs, "./a");
fs_remove_dir_r(fs, "./b");
assert(!fs_dir_exist(fs, "./a"));
assert(fs_create_dir(fs, "./a") == RET_OK);
assert(fs_dir_exist(fs, "./a"));
@ -520,3 +528,103 @@ ret_t fs_remove_dir_r(fs_t* fs, const char* name) {
return ret;
}
#ifndef TK_COPY_BUFF_SIZE
#define TK_COPY_BUFF_SIZE 4096
#endif /*TK_COPY_BUFF_SIZE*/
static ret_t fs_copy_file_fd(fs_file_t* fsrc, fs_file_t* fdst) {
int32_t r = 0;
ret_t ret = RET_OK;
uint8_t* buff = TKMEM_ALLOC(TK_COPY_BUFF_SIZE + 1);
return_value_if_fail(buff != NULL, RET_OOM);
while ((r = fs_file_read(fsrc, buff, TK_COPY_BUFF_SIZE)) > 0) {
buff[r] = '\0';
if (fs_file_write(fdst, buff, r) < r) {
log_debug("write file failed\n");
ret = RET_IO;
break;
}
}
TKMEM_FREE(buff);
return ret;
}
ret_t fs_copy_file(fs_t* fs, const char* src, const char* dst) {
ret_t ret = RET_FAIL;
fs_file_t* fsrc = NULL;
fs_file_t* fdst = NULL;
char dirname[MAX_PATH + 1];
return_value_if_fail(fs != NULL && src != NULL && dst != NULL, RET_BAD_PARAMS);
return_value_if_fail(file_exist(src), RET_BAD_PARAMS);
path_dirname(dst, dirname, MAX_PATH);
if (!dir_exist(dirname)) {
return_value_if_fail(fs_create_dir_r(fs, dirname) == RET_OK, RET_IO);
}
fdst = fs_open_file(fs, dst, "wb+");
return_value_if_fail(fdst != NULL, RET_IO);
fsrc = fs_open_file(fs, src, "rb");
if (fsrc != NULL) {
ret = fs_copy_file_fd(fsrc, fdst);
fs_file_close(fsrc);
}
fs_file_close(fdst);
return ret;
}
static ret_t fs_copy_item(fs_t* fs, fs_item_t* item, const char* src, const char* dst) {
char subsrc[MAX_PATH + 1];
char subdst[MAX_PATH + 1];
path_build(subsrc, MAX_PATH, src, item->name, NULL);
path_build(subdst, MAX_PATH, dst, item->name, NULL);
if (!fs_dir_exist(fs, dst)) {
return_value_if_fail(fs_create_dir_r(fs, dst) == RET_OK, RET_IO);
}
log_debug("%s ==> %s\n", subsrc, subdst);
if (item->is_dir) {
return fs_copy_dir(fs, subsrc, subdst);
} else {
return fs_copy_file(fs, subsrc, subdst);
}
}
ret_t fs_copy_dir(fs_t* fs, const char* src, const char* dst) {
fs_item_t item;
ret_t ret = RET_OK;
fs_dir_t* dir = NULL;
return_value_if_fail(fs != NULL && src != NULL && dst != NULL, RET_BAD_PARAMS);
return_value_if_fail(fs_dir_exist(fs, src), RET_BAD_PARAMS);
if (!fs_dir_exist(fs, dst)) {
return_value_if_fail(fs_create_dir_r(fs, dst) == RET_OK, RET_IO);
}
dir = fs_open_dir(fs, src);
return_value_if_fail(dir != NULL, RET_BAD_PARAMS);
do {
if (fs_dir_read(dir, &item) != RET_OK) {
break;
}
if (tk_str_eq(item.name, ".") || tk_str_eq(item.name, "..")) {
continue;
} else {
ret = fs_copy_item(fs, &item, src, dst);
}
if (ret != RET_OK) {
break;
}
} while (TRUE);
fs_dir_close(dir);
return ret;
}

View File

@ -467,6 +467,32 @@ bool_t fs_file_exist(fs_t* fs, const char* name);
*/
ret_t fs_file_rename(fs_t* fs, const char* name, const char* new_name);
/**
* @method fs_copy_file
*
*
*
* @param {fs_t*} fs os_fs()
* @param {const char*} src
* @param {const char*} dst
*
* @return {ret_t} RET_OK表示成功
*/
ret_t fs_copy_file(fs_t* fs, const char* src, const char* dst);
/**
* @method fs_copy_dir
*
*
*
* @param {fs_t*} fs os_fs()
* @param {const char*} src
* @param {const char*} dst
*
* @return {ret_t} RET_OK表示成功
*/
ret_t fs_copy_dir(fs_t* fs, const char* src, const char* dst);
/**
* @method fs_open_dir
*
@ -686,6 +712,17 @@ fs_t* os_fs(void);
*/
bool_t file_exist(const char* name);
/**
* @method dir_exist
*
*
*
* @param {const char*} name
*
* @return {bool_t} TRUE表示成功
*/
bool_t dir_exist(const char* name);
/**
* @method file_remove
*

View File

@ -325,6 +325,8 @@ typedef ret_t (*tk_destroy_t)(void* data);
typedef ret_t (*tk_on_done_t)(void* data);
typedef ret_t (*tk_on_result_t)(void* ctx, const void* data);
typedef bool_t (*tk_is_valid_t)(void* data);
/*TRUE 保留FALSE 忽略*/
typedef bool_t (*tk_filter_t)(void* ctx, const void* data);
typedef int (*tk_compare_t)(const void* a, const void* b);
typedef ret_t (*tk_visit_t)(void* ctx, const void* data);

View File

@ -154,3 +154,44 @@ TEST(Fs, create_dir_r) {
ASSERT_EQ(fs_remove_dir_r(os_fs(), "a"), RET_OK);
ASSERT_EQ(fs_dir_exist(os_fs(), "a"), FALSE);
}
TEST(Fs, copy_file) {
const char* src = "./test.txt";
const char* dst = "./a/b/test.txt";
ASSERT_EQ(file_write(src, "hello", 5), RET_OK);
ASSERT_EQ(file_exist(src), TRUE);
ASSERT_EQ(file_exist(dst), FALSE);
ASSERT_EQ(fs_copy_file(os_fs(), src, dst), RET_OK);
ASSERT_EQ(fs_remove_dir_r(os_fs(), "a"), RET_OK);
dst = "test1.txt";
ASSERT_EQ(fs_copy_file(os_fs(), src, dst), RET_OK);
ASSERT_EQ(file_remove(src), RET_OK);
ASSERT_EQ(file_remove(dst), RET_OK);;
ASSERT_EQ(file_exist(src), FALSE);
ASSERT_EQ(file_exist(dst), FALSE);
}
TEST(Fs, copy_dir) {
const char* src = "./a";
const char* dst = "./b";
ASSERT_EQ(fs_create_dir_r(os_fs(), "./a/a1"), RET_OK);
ASSERT_EQ(fs_create_dir_r(os_fs(), "./a/a2"), RET_OK);
ASSERT_EQ(fs_create_dir_r(os_fs(), "./a/a3/a4"), RET_OK);
ASSERT_EQ(file_write("./a/a3/a4/test.txt", "hello", 5), RET_OK);
ASSERT_EQ(fs_copy_dir(os_fs(), src, dst), RET_OK);
ASSERT_EQ(dir_exist("./b"), TRUE);
ASSERT_EQ(dir_exist("./b/a1"), TRUE);
ASSERT_EQ(dir_exist("./b/a2"), TRUE);
ASSERT_EQ(dir_exist("./b/a3"), TRUE);
ASSERT_EQ(file_exist("./b/a3/a4/test.txt"), TRUE);
ASSERT_EQ(fs_remove_dir_r(os_fs(), "a"), RET_OK);
ASSERT_EQ(fs_remove_dir_r(os_fs(), "b"), RET_OK);
}