Skip to content

Commit 23cbf06

Browse files
authored
Merge pull request RustPython#2057 from BasixKOR/os-chmod-win
Implement os.chmod on Windows
2 parents b14d277 + 43e6837 commit 23cbf06

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

vm/src/stdlib/os.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,27 @@ mod nt {
22912291
get_stats().map_err(|e| convert_io_error(vm, e))
22922292
}
22932293

2294+
#[pyfunction]
2295+
fn chmod(
2296+
path: PyPathLike,
2297+
dir_fd: DirFd,
2298+
mode: u32,
2299+
follow_symlinks: FollowSymlinks,
2300+
vm: &VirtualMachine,
2301+
) -> PyResult<()> {
2302+
const S_IWRITE: u32 = 128;
2303+
let path = make_path(vm, &path, &dir_fd);
2304+
let metadata = if follow_symlinks.follow_symlinks {
2305+
fs::metadata(path)
2306+
} else {
2307+
fs::symlink_metadata(path)
2308+
};
2309+
let meta = metadata.map_err(|err| convert_io_error(vm, err))?;
2310+
let mut permissions = meta.permissions();
2311+
permissions.set_readonly(mode & S_IWRITE != 0);
2312+
fs::set_permissions(path, permissions).map_err(|err| convert_io_error(vm, err))
2313+
}
2314+
22942315
// cwait is available on MSVC only (according to CPython)
22952316
#[cfg(target_env = "msvc")]
22962317
extern "C" {

0 commit comments

Comments
 (0)