diff --git a/contrib/bindings/python/pathrs/_pathrs.py b/contrib/bindings/python/pathrs/_pathrs.py index 2146510b..add9955d 100644 --- a/contrib/bindings/python/pathrs/_pathrs.py +++ b/contrib/bindings/python/pathrs/_pathrs.py @@ -517,8 +517,7 @@ def creat(self, path, filemode, mode="r", extra_flags=0): fd = libpathrs_so.pathrs_creat(self.fileno(), path, flags, filemode) if fd < 0: raise Error._fetch(fd) or INTERNAL_ERROR - # TODO: This should actually return an os.fdopen. - return Handle(fd) + return os.fdopen(fd, mode) # TODO: creat_raw? diff --git a/go-pathrs/root_linux.go b/go-pathrs/root_linux.go index 72e8134c..876d2062 100644 --- a/go-pathrs/root_linux.go +++ b/go-pathrs/root_linux.go @@ -106,21 +106,17 @@ func (r *Root) ResolveNoFollow(path string) (*Handle, error) { // Create creates a file within the Root's directory tree at the given path, // and returns a handle to the file. The provided mode is used for the new file // (the process's umask applies). -func (r *Root) Create(path string, flags int, mode os.FileMode) (*Handle, error) { +func (r *Root) Create(path string, flags int, mode os.FileMode) (*os.File, error) { unixMode, err := toUnixMode(mode) if err != nil { return nil, err } - return withFileFd(r.inner, func(rootFd uintptr) (*Handle, error) { + return withFileFd(r.inner, func(rootFd uintptr) (*os.File, error) { handleFd, err := pathrsCreat(rootFd, path, flags, unixMode) if err != nil { return nil, err } - handleFile, err := mkFile(uintptr(handleFd)) - if err != nil { - return nil, err - } - return &Handle{inner: handleFile}, nil + return mkFile(uintptr(handleFd)) }) } diff --git a/src/capi/ret.rs b/src/capi/ret.rs index f2a988a4..e219cfc4 100644 --- a/src/capi/ret.rs +++ b/src/capi/ret.rs @@ -19,7 +19,10 @@ use crate::{capi::error as capi_error, error::Error, Handle, Root}; -use std::os::unix::io::{IntoRawFd, OwnedFd}; +use std::{ + fs::File, + os::unix::io::{IntoRawFd, OwnedFd}, +}; use libc::c_int; @@ -64,6 +67,12 @@ impl IntoCReturn for Handle { } } +impl IntoCReturn for File { + fn into_c_return(self) -> CReturn { + OwnedFd::from(self).into_c_return() + } +} + impl IntoCReturn for Result where V: IntoCReturn, diff --git a/src/root.rs b/src/root.rs index 192457b4..d78f6769 100644 --- a/src/root.rs +++ b/src/root.rs @@ -29,7 +29,7 @@ use crate::{ }; use std::{ - fs::Permissions, + fs::{File, Permissions}, io::Error as IOError, os::unix::{ ffi::OsStrExt, @@ -367,7 +367,7 @@ impl Root { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { self.as_ref().create_file(path, flags, perm) } @@ -829,7 +829,7 @@ impl RootRef<'_> { path: P, mut flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { // The path doesn't exist yet, so we need to get a safe reference to the // parent and just operate on the final (slashless) component. let (dir, name) = self @@ -851,7 +851,7 @@ impl RootRef<'_> { } })?; - Ok(Handle::from_fd_unchecked(fd)) + Ok(fd.into()) } /// Within the [`RootRef`]'s tree, create a directory and any of its parent diff --git a/src/tests/capi/root.rs b/src/tests/capi/root.rs index 221ca040..6a4df1b0 100644 --- a/src/tests/capi/root.rs +++ b/src/tests/capi/root.rs @@ -32,7 +32,7 @@ use crate::{ }; use std::{ - fs::Permissions, + fs::{File, Permissions}, os::unix::{ fs::PermissionsExt, io::{AsFd, BorrowedFd, OwnedFd}, @@ -140,14 +140,14 @@ impl CapiRoot { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { let root_fd = self.inner.as_fd(); let path = capi_utils::path_to_cstring(path); capi_utils::call_capi_fd(|| unsafe { capi::core::pathrs_creat(root_fd.into(), path.as_ptr(), flags.bits(), perm.mode()) }) - .map(CapiHandle::from_fd_unchecked) + .map(File::from) } fn mkdir_all>( @@ -269,7 +269,7 @@ impl RootImpl for CapiRoot { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { self.create_file(path, flags, perm) } @@ -348,7 +348,7 @@ impl RootImpl for &CapiRoot { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { CapiRoot::create_file(self, path, flags, perm) } diff --git a/src/tests/traits/root.rs b/src/tests/traits/root.rs index a186f1bb..16e14894 100644 --- a/src/tests/traits/root.rs +++ b/src/tests/traits/root.rs @@ -26,7 +26,7 @@ use crate::{ }; use std::{ - fs::Permissions, + fs::{File, Permissions}, os::unix::io::{AsFd, OwnedFd}, path::{Path, PathBuf}, }; @@ -57,7 +57,7 @@ pub(in crate::tests) trait RootImpl: AsFd + std::fmt::Debug + Sized { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result; + ) -> Result; fn mkdir_all>( &self, @@ -122,7 +122,7 @@ impl RootImpl for Root { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { self.create_file(path, flags, perm) } @@ -199,7 +199,7 @@ impl RootImpl for &Root { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { Root::create_file(self, path, flags, perm) } @@ -276,7 +276,7 @@ impl RootImpl for RootRef<'_> { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { self.create_file(path, flags, perm) } @@ -353,7 +353,7 @@ impl RootImpl for &RootRef<'_> { path: P, flags: OpenFlags, perm: &Permissions, - ) -> Result { + ) -> Result { RootRef::create_file(self, path, flags, perm) }