forked from awslabs/llrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchmod.rs
49 lines (44 loc) · 1.15 KB
/
chmod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#[cfg(unix)]
use llrt_utils::result::ResultExt;
use rquickjs::{Ctx, Result};
#[cfg(unix)]
use std::os::unix::prelude::PermissionsExt;
#[cfg(unix)]
pub(crate) fn chmod_error(path: &str) -> String {
["Can't set permissions of \"", path, "\""].concat()
}
pub(crate) async fn set_mode(ctx: Ctx<'_>, path: &str, mode: u32) -> Result<()> {
#[cfg(unix)]
{
tokio::fs::set_permissions(path, PermissionsExt::from_mode(mode))
.await
.or_throw_msg(&ctx, &chmod_error(path))?;
}
#[cfg(not(unix))]
{
_ = ctx;
_ = path;
_ = mode;
}
Ok(())
}
pub(crate) fn set_mode_sync(ctx: Ctx<'_>, path: &str, mode: u32) -> Result<()> {
#[cfg(unix)]
{
std::fs::set_permissions(path, PermissionsExt::from_mode(mode))
.or_throw_msg(&ctx, &chmod_error(path))?;
}
#[cfg(not(unix))]
{
_ = ctx;
_ = path;
_ = mode;
}
Ok(())
}
pub async fn chmod(ctx: Ctx<'_>, path: String, mode: u32) -> Result<()> {
set_mode(ctx, &path, mode).await
}
pub fn chmod_sync(ctx: Ctx<'_>, path: String, mode: u32) -> Result<()> {
set_mode_sync(ctx, &path, mode)
}