Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c82a42c1
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c82a42c1
编写于
3月 03, 2019
作者:
E
Edward Barnard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Change `std::fs::copy` to use `copyfile` on MacOS and iOS
上级
c0086b9e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
88 addition
and
2 deletion
+88
-2
src/libstd/fs.rs
src/libstd/fs.rs
+2
-1
src/libstd/sys/unix/fs.rs
src/libstd/sys/unix/fs.rs
+86
-1
未找到文件。
src/libstd/fs.rs
浏览文件 @
c82a42c1
...
...
@@ -1581,7 +1581,8 @@ pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()>
/// `O_CLOEXEC` is set for returned file descriptors.
/// On Windows, this function currently corresponds to `CopyFileEx`. Alternate
/// NTFS streams are copied but only the size of the main stream is returned by
/// this function.
/// this function. On MacOS, this function corresponds to `copyfile` with
/// `COPYFILE_ALL`
/// Note that, this [may change in the future][changes].
///
/// [changes]: ../io/index.html#platform-specific-behavior
...
...
src/libstd/sys/unix/fs.rs
浏览文件 @
c82a42c1
...
...
@@ -827,7 +827,10 @@ pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
Ok
(
PathBuf
::
from
(
OsString
::
from_vec
(
buf
)))
}
#[cfg(not(any(target_os
=
"linux"
,
target_os
=
"android"
)))]
#[cfg(not(any(target_os
=
"linux"
,
target_os
=
"android"
,
target_os
=
"macos"
,
target_os
=
"ios"
)))]
pub
fn
copy
(
from
:
&
Path
,
to
:
&
Path
)
->
io
::
Result
<
u64
>
{
use
crate
::
fs
::
File
;
if
!
from
.is_file
()
{
...
...
@@ -937,3 +940,85 @@ unsafe fn copy_file_range(
writer
.set_permissions
(
perm
)
?
;
Ok
(
written
)
}
#[cfg(any(target_os
=
"macos"
,
target_os
=
"ios"
))]
pub
fn
copy
(
from
:
&
Path
,
to
:
&
Path
)
->
io
::
Result
<
u64
>
{
const
COPYFILE_ACL
:
u32
=
1
<<
0
;
const
COPYFILE_STAT
:
u32
=
1
<<
1
;
const
COPYFILE_XATTR
:
u32
=
1
<<
2
;
const
COPYFILE_DATA
:
u32
=
1
<<
3
;
const
COPYFILE_SECURITY
:
u32
=
COPYFILE_STAT
|
COPYFILE_ACL
;
const
COPYFILE_METADATA
:
u32
=
COPYFILE_SECURITY
|
COPYFILE_XATTR
;
const
COPYFILE_ALL
:
u32
=
COPYFILE_METADATA
|
COPYFILE_DATA
;
const
COPYFILE_STATE_COPIED
:
u32
=
8
;
#[allow(non_camel_case_types)]
type
copyfile_state_t
=
*
mut
libc
::
c_void
;
#[allow(non_camel_case_types)]
type
copyfile_flags_t
=
u32
;
extern
"C"
{
fn
copyfile
(
from
:
*
const
libc
::
c_char
,
to
:
*
const
libc
::
c_char
,
state
:
copyfile_state_t
,
flags
:
copyfile_flags_t
,
)
->
libc
::
c_int
;
fn
copyfile_state_alloc
()
->
copyfile_state_t
;
fn
copyfile_state_free
(
state
:
copyfile_state_t
)
->
libc
::
c_int
;
fn
copyfile_state_get
(
state
:
copyfile_state_t
,
flag
:
u32
,
dst
:
*
mut
libc
::
c_void
,
)
->
libc
::
c_int
;
}
struct
FreeOnDrop
(
copyfile_state_t
);
impl
Drop
for
FreeOnDrop
{
fn
drop
(
&
mut
self
)
{
// The code below ensures that `FreeOnDrop` is never a null pointer
unsafe
{
// `copyfile_state_free` returns -1 if the `to` or `from` files
// cannot be closed. However, this is not considerd this an
// error.
copyfile_state_free
(
self
.0
);
}
}
}
if
!
from
.is_file
()
{
return
Err
(
Error
::
new
(
ErrorKind
::
InvalidInput
,
"the source path is not an existing regular file"
))
}
// We ensure that `FreeOnDrop` never contains a null pointer so it is
// always safe to call `copyfile_state_free`
let
state
=
unsafe
{
let
state
=
copyfile_state_alloc
();
if
state
.is_null
()
{
return
Err
(
crate
::
io
::
Error
::
last_os_error
());
}
FreeOnDrop
(
state
)
};
cvt
(
unsafe
{
copyfile
(
cstr
(
from
)
?
.as_ptr
(),
cstr
(
to
)
?
.as_ptr
(),
state
.0
,
COPYFILE_ALL
,
)
})
?
;
let
mut
bytes_copied
:
libc
::
off_t
=
0
;
cvt
(
unsafe
{
copyfile_state_get
(
state
.0
,
COPYFILE_STATE_COPIED
,
&
mut
bytes_copied
as
*
mut
libc
::
off_t
as
*
mut
libc
::
c_void
,
)
})
?
;
Ok
(
bytes_copied
as
u64
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录