Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
逆着光、寻觅
rt-thread
提交
6c319002
R
rt-thread
项目概览
逆着光、寻觅
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6c319002
编写于
2月 12, 2014
作者:
B
Bernard Xiong
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #237 from prife/dfs
dfs: support copy directory
上级
a6fa98f1
f015f36d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
166 addition
and
2 deletion
+166
-2
components/dfs/src/dfs_file.c
components/dfs/src/dfs_file.c
+166
-2
未找到文件。
components/dfs/src/dfs_file.c
浏览文件 @
6c319002
...
...
@@ -593,7 +593,7 @@ void cat(const char* filename)
FINSH_FUNCTION_EXPORT
(
cat
,
print
file
)
#define BUF_SZ 4096
void
copy
(
const
char
*
src
,
const
char
*
dst
)
static
void
copyfile
(
const
char
*
src
,
const
char
*
dst
)
{
struct
dfs_fd
src_fd
;
rt_uint8_t
*
block_ptr
;
...
...
@@ -637,7 +637,171 @@ void copy(const char *src, const char *dst)
dfs_file_close
(
&
fd
);
rt_free
(
block_ptr
);
}
FINSH_FUNCTION_EXPORT
(
copy
,
copy
source
file
to
destination
file
)
extern
int
mkdir
(
const
char
*
path
,
mode_t
mode
);
static
void
copydir
(
const
char
*
src
,
const
char
*
dst
)
{
struct
dfs_fd
fd
;
struct
dirent
dirent
;
struct
stat
stat
;
int
length
;
if
(
dfs_file_open
(
&
fd
,
src
,
DFS_O_DIRECTORY
)
<
0
)
{
rt_kprintf
(
"open %s failed
\n
"
,
src
);
return
;
}
do
{
rt_memset
(
&
dirent
,
0
,
sizeof
(
struct
dirent
));
length
=
dfs_file_getdents
(
&
fd
,
&
dirent
,
sizeof
(
struct
dirent
));
if
(
length
>
0
)
{
char
*
src_entry_full
=
RT_NULL
;
char
*
dst_entry_full
=
RT_NULL
;
if
(
strcmp
(
dirent
.
d_name
,
".."
)
==
0
||
strcmp
(
dirent
.
d_name
,
"."
)
==
0
)
continue
;
/* build full path for each file */
if
((
src_entry_full
=
dfs_normalize_path
(
src
,
dirent
.
d_name
))
==
RT_NULL
)
{
rt_kprintf
(
"out of memory!
\n
"
);
break
;
}
if
((
dst_entry_full
=
dfs_normalize_path
(
dst
,
dirent
.
d_name
))
==
RT_NULL
)
{
rt_kprintf
(
"out of memory!
\n
"
);
rt_free
(
src_entry_full
);
break
;
}
rt_memset
(
&
stat
,
0
,
sizeof
(
struct
stat
));
if
(
dfs_file_stat
(
src_entry_full
,
&
stat
)
!=
0
)
{
rt_kprintf
(
"open file: %s failed
\n
"
,
dirent
.
d_name
);
continue
;
}
if
(
DFS_S_ISDIR
(
stat
.
st_mode
))
{
mkdir
(
dst_entry_full
,
0
);
copydir
(
src_entry_full
,
dst_entry_full
);
}
else
{
copyfile
(
src_entry_full
,
dst_entry_full
);
}
rt_free
(
src_entry_full
);
rt_free
(
dst_entry_full
);
}
}
while
(
length
>
0
);
dfs_file_close
(
&
fd
);
}
static
const
char
*
_get_path_lastname
(
const
char
*
path
)
{
char
*
ptr
;
if
((
ptr
=
strrchr
(
path
,
'/'
))
==
RT_NULL
)
return
path
;
/* skip the '/' then return */
return
++
ptr
;
}
void
copy
(
const
char
*
src
,
const
char
*
dst
)
{
#define FLAG_SRC_TYPE 0x03
#define FLAG_SRC_IS_DIR 0x01
#define FLAG_SRC_IS_FILE 0x02
#define FLAG_SRC_NON_EXSIT 0x00
#define FLAG_DST_TYPE 0x0C
#define FLAG_DST_IS_DIR 0x04
#define FLAG_DST_IS_FILE 0x08
#define FLAG_DST_NON_EXSIT 0x00
struct
stat
stat
;
rt_uint32_t
flag
=
0
;
/* check the staus of src and dst */
if
(
dfs_file_stat
(
src
,
&
stat
)
<
0
)
{
rt_kprintf
(
"copy failed, bad %s
\n
"
,
src
);
return
;
}
if
(
DFS_S_ISDIR
(
stat
.
st_mode
))
flag
|=
FLAG_SRC_IS_DIR
;
else
flag
|=
FLAG_SRC_IS_FILE
;
if
(
dfs_file_stat
(
dst
,
&
stat
)
<
0
)
{
flag
|=
FLAG_DST_NON_EXSIT
;
}
else
{
if
(
DFS_S_ISDIR
(
stat
.
st_mode
))
flag
|=
FLAG_DST_IS_DIR
;
else
flag
|=
FLAG_DST_IS_FILE
;
}
//2. check status
if
((
flag
&
FLAG_SRC_IS_DIR
)
&&
(
flag
&
FLAG_DST_IS_FILE
))
{
rt_kprintf
(
"cp faild, cp dir to file is not permitted!
\n
"
);
return
;
}
//3. do copy
if
(
flag
&
FLAG_SRC_IS_FILE
)
{
if
(
flag
&
FLAG_DST_IS_DIR
)
{
char
*
fdst
;
fdst
=
dfs_normalize_path
(
dst
,
_get_path_lastname
(
src
));
if
(
fdst
==
NULL
)
{
rt_kprintf
(
"out of memory
\n
"
);
return
;
}
copyfile
(
src
,
fdst
);
rt_free
(
fdst
);
}
else
{
copyfile
(
src
,
dst
);
}
}
else
//flag & FLAG_SRC_IS_DIR
{
if
(
flag
&
FLAG_DST_IS_DIR
)
{
char
*
fdst
;
fdst
=
dfs_normalize_path
(
dst
,
_get_path_lastname
(
src
));
if
(
fdst
==
NULL
)
{
rt_kprintf
(
"out of memory
\n
"
);
return
;
}
mkdir
(
fdst
,
0
);
copydir
(
src
,
fdst
);
rt_free
(
fdst
);
}
else
if
((
flag
&
FLAG_DST_TYPE
)
==
FLAG_DST_NON_EXSIT
)
{
mkdir
(
dst
,
0
);
copydir
(
src
,
dst
);
}
else
{
copydir
(
src
,
dst
);
}
}
}
FINSH_FUNCTION_EXPORT
(
copy
,
copy
file
or
dir
)
#endif
/* @} */
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录