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

Function access

crates/vm/src/stdlib/posix.rs:476–508  ·  view source on GitHub ↗
(path: OsPath, mode: u8, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

474
475 #[pyfunction]
476 pub(super) fn access(path: OsPath, mode: u8, vm: &VirtualMachine) -> PyResult<bool> {
477 use std::os::unix::fs::MetadataExt;
478
479 let flags = AccessFlags::from_bits(mode).ok_or_else(|| {
480 vm.new_value_error(
481 "One of the flags is wrong, there are only 4 possibilities F_OK, R_OK, W_OK and X_OK",
482 )
483 })?;
484
485 let metadata = match fs::metadata(&path.path) {
486 Ok(m) => m,
487 // If the file doesn't exist, return False for any access check
488 Err(_) => return Ok(false),
489 };
490
491 // if it's only checking for F_OK
492 if flags == AccessFlags::F_OK {
493 return Ok(true); // File exists
494 }
495
496 let user_id = metadata.uid();
497 let group_id = metadata.gid();
498 let mode = metadata.mode();
499
500 let perm = get_right_permission(mode, Uid::from_raw(user_id), Gid::from_raw(group_id))
501 .map_err(|err| err.into_pyexception(vm))?;
502
503 let r_ok = !flags.contains(AccessFlags::R_OK) || perm.is_readable;
504 let w_ok = !flags.contains(AccessFlags::W_OK) || perm.is_writable;
505 let x_ok = !flags.contains(AccessFlags::X_OK) || perm.is_executable;
506
507 Ok(r_ok && w_ok && x_ok)
508 }
509
510 #[pyattr]
511 fn environ(vm: &VirtualMachine) -> PyDictRef {

Callers

nothing calls this directly

Calls 7

metadataFunction · 0.85
get_right_permissionFunction · 0.85
ok_or_elseMethod · 0.80
uidMethod · 0.80
modeMethod · 0.45
into_pyexceptionMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected