MCPcopy Index your code
hub / github.com/RustPython/RustPython / chmod

Function chmod

crates/vm/src/stdlib/nt.rs:355–408  ·  view source on GitHub ↗
(args: ChmodArgs<'_>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

353
354 #[pyfunction]
355 fn chmod(args: ChmodArgs<'_>, vm: &VirtualMachine) -> PyResult<()> {
356 let ChmodArgs {
357 path,
358 mode,
359 dir_fd,
360 follow_symlinks,
361 } = args;
362 let [] = dir_fd.0;
363
364 // If path is a file descriptor, use fchmod
365 if let OsPathOrFd::Fd(fd) = path {
366 if follow_symlinks.into_option().is_some() {
367 return Err(
368 vm.new_value_error("chmod: follow_symlinks is not supported with fd argument")
369 );
370 }
371 return fchmod_impl(fd.as_raw(), mode, vm);
372 }
373
374 let OsPathOrFd::Path(path) = path else {
375 unreachable!()
376 };
377
378 let follow_symlinks = follow_symlinks.into_option().unwrap_or(false);
379
380 if follow_symlinks {
381 use windows_sys::Win32::Storage::FileSystem::{
382 CreateFileW, FILE_FLAG_BACKUP_SEMANTICS, FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE,
383 FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_WRITE_ATTRIBUTES, OPEN_EXISTING,
384 };
385
386 let wide = path.to_wide_cstring(vm)?;
387 let handle = unsafe {
388 CreateFileW(
389 wide.as_ptr(),
390 FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES,
391 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
392 core::ptr::null(),
393 OPEN_EXISTING,
394 FILE_FLAG_BACKUP_SEMANTICS,
395 core::ptr::null_mut(),
396 )
397 };
398 if handle == INVALID_HANDLE_VALUE {
399 let err = io::Error::last_os_error();
400 return Err(OSErrorBuilder::with_filename(&err, path, vm));
401 }
402 let result = win32_hchmod(handle, mode, vm);
403 unsafe { Foundation::CloseHandle(handle) };
404 result
405 } else {
406 win32_lchmod(&path, mode, vm)
407 }
408 }
409
410 /// Get the real file name (with correct case) without accessing the file.
411 /// Uses FindFirstFileW to get the name as stored on the filesystem.

Callers

nothing calls this directly

Calls 9

fchmod_implFunction · 0.85
win32_hchmodFunction · 0.85
CloseHandleFunction · 0.85
win32_lchmodFunction · 0.85
into_optionMethod · 0.80
ErrClass · 0.50
as_rawMethod · 0.45
to_wide_cstringMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected