Add os sendfile function and python test

This commit is contained in:
Rodrigo Oliveira committed 2020-10-27 17:14:53 -03:00
1 parent 00920babba
commit a656c51696
2 files changed
+53

No files matched your search

+12
View File
@@ -17,6 +17,18 @@ assert_raises(FileNotFoundError,
assert_raises(FileNotFoundError,
lambda: os.rename('DOES_NOT_EXIST', 'DOES_NOT_EXIST 2'))
src_fd = os.open('README.md', os.O_RDONLY)
dest_fd = os.open('destination.md', os.O_RDWR | os.O_CREAT)
src_len = os.stat('README.md').st_size
bytes_sent = os.sendfile(dest_fd, src_fd, 0, src_len)
assert src_len == bytes_sent
os.lseek(dest_fd, 0, 0)
assert os.read(src_fd, src_len) == os.read(dest_fd, bytes_sent)
os.close(src_fd)
os.close(dest_fd)
try:
os.open('DOES_NOT_EXIST', 0)
except OSError as err:
+41
View File
@@ -337,6 +337,47 @@ mod _os {
Err(vm.new_os_error("os.open not implemented on this platform".to_owned()))
}
#[cfg(any(target_os = "linux"))]
#[pyfunction]
pub(crate) fn sendfile(
out_fd: i32,
in_fd: i32,
offset: i64,
count: u64,
vm: &VirtualMachine,
) -> PyResult {
let mut file_offset = offset;
let res =
nix::sys::sendfile::sendfile(out_fd, in_fd, Some(&mut file_offset), count as usize)
.map_err(|err| err.into_pyexception(vm))?;
Ok(vm.ctx.new_int(res as u64))
}
#[cfg(any(target_os = "macos"))]
#[pyfunction]
pub(crate) fn sendfile(
out_fd: i64,
in_fd: i64,
offset: u64,
count: u64,
headers: OptionalArg<PyObjectRef>,
trailers: OptionalArg<PyObjectRef>,
flags: OptionalArg<i64>,
vm: &VirtualMachine,
) -> PyResult {
let res = nix::sys::sendfile::sendfile(
in_fd,
out_fd,
offset,
Some(count as usize),
headers,
trailers,
)
.map_err(|err| err.into_pyexception(vm))?;
Ok(vm.ctx.new_int(res as u64))
}
#[pyfunction]
fn error(message: OptionalArg<PyStrRef>, vm: &VirtualMachine) -> PyResult {
let msg = message.map_or("".to_owned(), |msg| msg.borrow_value().to_owned());