Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ junction = "1.2.0"
libc = "0.2.169"
libffi = "4.1"
log = "0.4.27"
nix = { version = "0.29", features = ["fs", "user", "process", "term", "time", "signal", "ioctl", "socket", "sched", "zerocopy", "dir", "hostname", "net", "poll"] }
nix = { version = "0.30", features = ["fs", "user", "process", "term", "time", "signal", "ioctl", "socket", "sched", "zerocopy", "dir", "hostname", "net", "poll"] }
malachite-bigint = "0.6"
malachite-q = "0.6"
malachite-base = "0.6"
Expand Down
4 changes: 4 additions & 0 deletions common/src/crt_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ impl Owned {
pub fn into_raw(self) -> Raw {
self.inner.into_raw_fd()
}

pub fn leak<'fd>(self) -> Borrowed<'fd> {
unsafe { Borrowed::borrow_raw(self.into_raw()) }
}
}

#[cfg(unix)]
Expand Down
36 changes: 17 additions & 19 deletions stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ mod mmap {
use nix::sys::stat::fstat;
use nix::unistd;
use num_traits::Signed;
use std::fs::File;
use rustpython_common::crt_fd;
use std::io::{self, Write};
use std::ops::{Deref, DerefMut};
#[cfg(unix)]
use std::os::unix::io::{FromRawFd, RawFd};

fn advice_try_from_i32(vm: &VirtualMachine, i: i32) -> PyResult<Advice> {
Ok(match i {
Expand Down Expand Up @@ -179,7 +177,7 @@ mod mmap {
struct PyMmap {
closed: AtomicCell<bool>,
mmap: PyMutex<Option<MmapObj>>,
fd: RawFd,
fd: AtomicCell<i32>,
offset: libc::off_t,
size: AtomicCell<usize>,
pos: AtomicCell<usize>, // relative to offset
Expand All @@ -190,7 +188,7 @@ mod mmap {
#[derive(FromArgs)]
struct MmapNewArgs {
#[pyarg(any)]
fileno: RawFd,
fileno: i32,
#[pyarg(any)]
length: isize,
#[pyarg(any, default = MAP_SHARED)]
Expand Down Expand Up @@ -340,7 +338,8 @@ mod mmap {
}
};

if fd != -1 {
let fd = unsafe { crt_fd::Borrowed::try_borrow_raw(fd) }.ok();
if let Some(fd) = fd {
Comment thread
coolreader18 marked this conversation as resolved.
Resolved
let metadata = fstat(fd)
.map_err(|err| io::Error::from_raw_os_error(err as i32).to_pyexception(vm))?;
let file_len = metadata.st_size;
Expand All @@ -366,27 +365,27 @@ mod mmap {
let mmap_opt = mmap_opt.offset(offset.try_into().unwrap()).len(map_size);

let (fd, mmap) = || -> std::io::Result<_> {
if fd == -1 {
let mmap = MmapObj::Write(mmap_opt.map_anon()?);
Ok((fd, mmap))
} else {
let new_fd = unistd::dup(fd)?;
if let Some(fd) = fd {
let new_fd: crt_fd::Owned = unistd::dup(fd)?.into();
let mmap = match access {
AccessMode::Default | AccessMode::Write => {
MmapObj::Write(unsafe { mmap_opt.map_mut(fd) }?)
MmapObj::Write(unsafe { mmap_opt.map_mut(&new_fd) }?)
}
AccessMode::Read => MmapObj::Read(unsafe { mmap_opt.map(fd) }?),
AccessMode::Copy => MmapObj::Write(unsafe { mmap_opt.map_copy(fd) }?),
AccessMode::Read => MmapObj::Read(unsafe { mmap_opt.map(&new_fd) }?),
AccessMode::Copy => MmapObj::Write(unsafe { mmap_opt.map_copy(&new_fd) }?),
};
Ok((new_fd, mmap))
Ok((Some(new_fd), mmap))
} else {
let mmap = MmapObj::Write(mmap_opt.map_anon()?);
Ok((None, mmap))
}
}()
.map_err(|e| e.to_pyexception(vm))?;

let m_obj = Self {
closed: AtomicCell::new(false),
mmap: PyMutex::new(Some(mmap)),
fd,
fd: AtomicCell::new(fd.map_or(-1, |fd| fd.into_raw())),
offset,
size: AtomicCell::new(map_size),
pos: AtomicCell::new(0),
Expand Down Expand Up @@ -841,9 +840,8 @@ mod mmap {

#[pymethod]
fn size(&self, vm: &VirtualMachine) -> std::io::Result<PyIntRef> {
let new_fd = unistd::dup(self.fd)?;
let file = unsafe { File::from_raw_fd(new_fd) };
let file_len = file.metadata()?.len();
let fd = unsafe { crt_fd::Borrowed::try_borrow_raw(self.fd.load())? };
let file_len = fstat(fd)?.st_size;
Ok(PyInt::from(file_len).into_ref(&vm.ctx))
}

Expand Down
Loading