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

Function lockf

crates/stdlib/src/fcntl.rs:164–219  ·  view source on GitHub ↗
(
        _io::Fildes(fd): _io::Fildes,
        cmd: i32,
        len: OptionalArg<PyIntRef>,
        start: OptionalArg<PyIntRef>,
        whence: OptionalArg<i32>,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

162 #[cfg(not(any(target_os = "wasi", target_os = "redox")))]
163 #[pyfunction]
164 fn lockf(
165 _io::Fildes(fd): _io::Fildes,
166 cmd: i32,
167 len: OptionalArg<PyIntRef>,
168 start: OptionalArg<PyIntRef>,
169 whence: OptionalArg<i32>,
170 vm: &VirtualMachine,
171 ) -> PyResult {
172 macro_rules! try_into_l_type {
173 ($l_type:path) => {
174 $l_type
175 .try_into()
176 .map_err(|e| vm.new_overflow_error(format!("{e}")))
177 };
178 }
179
180 let mut l: libc::flock = unsafe { core::mem::zeroed() };
181 l.l_type = if cmd == libc::LOCK_UN {
182 try_into_l_type!(libc::F_UNLCK)
183 } else if (cmd & libc::LOCK_SH) != 0 {
184 try_into_l_type!(libc::F_RDLCK)
185 } else if (cmd & libc::LOCK_EX) != 0 {
186 try_into_l_type!(libc::F_WRLCK)
187 } else {
188 return Err(vm.new_value_error("unrecognized lockf argument"));
189 }?;
190 l.l_start = match start {
191 OptionalArg::Present(s) => s.try_to_primitive(vm)?,
192 OptionalArg::Missing => 0,
193 };
194 l.l_len = match len {
195 OptionalArg::Present(l_) => l_.try_to_primitive(vm)?,
196 OptionalArg::Missing => 0,
197 };
198 l.l_whence = match whence {
199 OptionalArg::Present(w) => w
200 .try_into()
201 .map_err(|e| vm.new_overflow_error(format!("{e}")))?,
202 OptionalArg::Missing => 0,
203 };
204 let ret = unsafe {
205 libc::fcntl(
206 fd,
207 if (cmd & libc::LOCK_NB) != 0 {
208 libc::F_SETLK
209 } else {
210 libc::F_SETLKW
211 },
212 &l,
213 )
214 };
215 if ret < 0 {
216 return Err(vm.new_last_errno_error());
217 }
218 Ok(vm.ctx.new_int(ret).into())
219 }
220}

Callers

nothing calls this directly

Calls 5

fcntlFunction · 0.85
try_to_primitiveMethod · 0.80
new_last_errno_errorMethod · 0.80
ErrClass · 0.50
new_intMethod · 0.45

Tested by

no test coverage detected