Skip to content

Commit 5d46d5d

Browse files
committed
fs: add chmod
1 parent eed8301 commit 5d46d5d

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

API.md

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Available globally
114114

115115
[writeFileSync](https://nodejs.org/api/fs.html#fswritefilesyncfile-data-options)
116116

117+
[chmodSync](https://nodejs.org/api/fs.html#fschmodsyncpath-mode)
118+
117119
## fs/promises
118120

119121
[access](https://nodejs.org/api/fs.html#fsstatpath-options-callback)
@@ -136,6 +138,8 @@ Available globally
136138

137139
[writeFile](https://nodejs.org/api/fs.html#fspromiseswritefilefile-data-options)
138140

141+
[chmod](https://nodejs.org/api/fs.html#fspromiseschmodpath-mode)
142+
139143
## module
140144

141145
[createRequire](https://nodejs.org/api/module.html#modulecreaterequirefilename)

modules/llrt_fs/src/chmod.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#[cfg(unix)]
2+
use llrt_utils::result::ResultExt;
3+
use rquickjs::{Ctx, Result};
4+
#[cfg(unix)]
5+
use std::os::unix::prelude::PermissionsExt;
6+
7+
pub async fn chmod(ctx: Ctx<'_>, path: String, mode: u32) -> Result<()> {
8+
#[cfg(unix)]
9+
{
10+
tokio::fs::set_permissions(&path, PermissionsExt::from_mode(mode))
11+
.await
12+
.or_throw_msg(&ctx, &["Can't set permissions of \"", &path, "\""].concat())?;
13+
}
14+
#[cfg(not(unix))]
15+
{
16+
_ = ctx;
17+
_ = path;
18+
_ = mode;
19+
}
20+
Ok(())
21+
}
22+
23+
pub fn chmod_sync(ctx: Ctx<'_>, path: String, mode: u32) -> Result<()> {
24+
#[cfg(unix)]
25+
{
26+
std::fs::set_permissions(&path, PermissionsExt::from_mode(mode))
27+
.or_throw_msg(&ctx, &["Can't set permissions of \"", &path, "\""].concat())?;
28+
}
29+
#[cfg(not(unix))]
30+
{
31+
_ = ctx;
32+
_ = path;
33+
_ = mode;
34+
}
35+
Ok(())
36+
}

modules/llrt_fs/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
mod access;
4+
mod chmod;
45
mod file_handle;
56
mod mkdir;
67
mod open;
@@ -18,6 +19,7 @@ use rquickjs::{
1819
use rquickjs::{Class, Ctx, Object, Result};
1920

2021
use self::access::{access, access_sync};
22+
use self::chmod::{chmod, chmod_sync};
2123
use self::file_handle::FileHandle;
2224
use self::mkdir::{mkdir, mkdir_sync, mkdtemp, mkdtemp_sync};
2325
use self::open::open;
@@ -95,6 +97,7 @@ impl ModuleDef for FsModule {
9597
declare.declare("statSync")?;
9698
declare.declare("writeFileSync")?;
9799
declare.declare("constants")?;
100+
declare.declare("chmodSync")?;
98101

99102
declare.declare("default")?;
100103

@@ -123,6 +126,7 @@ impl ModuleDef for FsModule {
123126
default.set("rmSync", Func::from(rmfile_sync))?;
124127
default.set("statSync", Func::from(stat_fn_sync))?;
125128
default.set("writeFileSync", Func::from(write_file_sync))?;
129+
default.set("chmodSync", Func::from(chmod_sync))?;
126130

127131
Ok(())
128132
})
@@ -145,6 +149,7 @@ fn export_promises<'js>(ctx: &Ctx<'js>, exports: &Object<'js>) -> Result<()> {
145149
exports.set("rm", Func::from(Async(rmfile)))?;
146150
exports.set("rmdir", Func::from(Async(rmdir)))?;
147151
exports.set("stat", Func::from(Async(stat_fn)))?;
152+
exports.set("chmod", Func::from(Async(chmod)))?;
148153

149154
Ok(())
150155
}

types/fs.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,12 @@ declare module "fs" {
325325
* @param [mode=fs.constants.F_OK]
326326
*/
327327
export function accessSync(path: PathLike, mode?: Mode): void;
328+
329+
/**
330+
* For detailed information, see the documentation of the asynchronous version of
331+
* this API: {@link chmod}.
332+
*
333+
* See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail.
334+
*/
335+
export function chmodSync(path: PathLike, mode: Mode): void;
328336
}

types/fs/promises.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,10 @@ declare module "fs/promises" {
502502
}
503503
| BufferEncoding
504504
): Promise<string>;
505+
506+
/**
507+
* Changes the permissions of a file.
508+
* @return Fulfills with `undefined` upon success.
509+
*/
510+
function chmod(path: PathLike, mode: Mode): Promise<void>;
505511
}

0 commit comments

Comments
 (0)