From 3b7a00f4742b6a427b64d79a17c7d4f6938bc004 Mon Sep 17 00:00:00 2001 From: Peter Huene Date: Wed, 30 Oct 2019 13:05:18 -0700 Subject: [PATCH] Fix unused function warnings on Windows. Unfortunately the helpers added in #154 were only used from non-Windows implementations, which caused compiler warnings on Windows. This commit moves the helper to be unix-specific and removes the tiny wrapper around calling `str_to_cstring` on `PathGet.path`. --- src/helpers.rs | 5 ----- src/hostcalls_impl/fs_helpers.rs | 6 ------ src/sys/unix/bsd/hostcalls_impl.rs | 10 +++++----- src/sys/unix/hostcalls_impl/fs.rs | 11 ++++++----- src/sys/unix/linux/hostcalls_impl.rs | 11 +++++------ src/sys/unix/mod.rs | 7 ++++++- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 34044b47f2..4d99b96093 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,6 +1,5 @@ use crate::{Error, Result}; use std::convert::TryInto; -use std::ffi::CString; use std::time::{SystemTime, UNIX_EPOCH}; pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result { @@ -10,7 +9,3 @@ pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result { .try_into() .map_err(Into::into) // u128 doesn't fit into u64 } - -pub(crate) fn str_to_cstring(s: &str) -> Result { - CString::new(s.as_bytes()).map_err(|_| Error::EILSEQ) -} diff --git a/src/hostcalls_impl/fs_helpers.rs b/src/hostcalls_impl/fs_helpers.rs index e1da33b47f..a9d83a254e 100644 --- a/src/hostcalls_impl/fs_helpers.rs +++ b/src/hostcalls_impl/fs_helpers.rs @@ -1,9 +1,7 @@ #![allow(non_camel_case_types)] -use crate::helpers::str_to_cstring; use crate::sys::host_impl; use crate::sys::hostcalls_impl::fs_helpers::*; use crate::{fdentry::FdEntry, host, Error, Result}; -use std::ffi::CString; use std::fs::File; use std::path::{Component, Path}; @@ -21,10 +19,6 @@ impl PathGet { pub(crate) fn path(&self) -> &str { &self.path } - - pub(crate) fn path_cstring(&self) -> Result { - str_to_cstring(&self.path) - } } /// Normalizes a path to ensure that the target path is located under the directory provided. diff --git a/src/sys/unix/bsd/hostcalls_impl.rs b/src/sys/unix/bsd/hostcalls_impl.rs index ce583b1384..141db6c934 100644 --- a/src/sys/unix/bsd/hostcalls_impl.rs +++ b/src/sys/unix/bsd/hostcalls_impl.rs @@ -1,7 +1,7 @@ use super::osfile::OsFile; -use crate::helpers::str_to_cstring; use crate::hostcalls_impl::PathGet; use crate::sys::host_impl; +use crate::sys::unix::str_to_cstring; use crate::{host, Error, Result}; use nix::libc::{self, c_long, c_void}; use std::convert::TryInto; @@ -12,7 +12,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> { use nix::errno; use nix::libc::unlinkat; - let path_cstr = resolved.path_cstring()?; + let path_cstr = str_to_cstring(resolved.path())?; // nix doesn't expose unlinkat() yet match unsafe { unlinkat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0) } { @@ -53,7 +53,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> { use nix::{errno::Errno, fcntl::AtFlags, libc::symlinkat, sys::stat::fstatat}; let old_path_cstr = str_to_cstring(old_path)?; - let new_path_cstr = resolved.path_cstring()?; + let new_path_cstr = str_to_cstring(resolved.path())?; log::debug!("path_symlink old_path = {:?}", old_path); log::debug!("path_symlink resolved = {:?}", resolved); @@ -93,8 +93,8 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> { pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> { use nix::{errno::Errno, fcntl::AtFlags, libc::renameat, sys::stat::fstatat}; - let old_path_cstr = resolved_old.path_cstring()?; - let new_path_cstr = resolved_new.path_cstring()?; + let old_path_cstr = str_to_cstring(resolved_old.path())?; + let new_path_cstr = str_to_cstring(resolved_new.path())?; let res = unsafe { renameat( diff --git a/src/sys/unix/hostcalls_impl/fs.rs b/src/sys/unix/hostcalls_impl/fs.rs index 81ad04ac11..9ceb4442e7 100644 --- a/src/sys/unix/hostcalls_impl/fs.rs +++ b/src/sys/unix/hostcalls_impl/fs.rs @@ -4,6 +4,7 @@ use super::fs_helpers::*; use crate::helpers::systemtime_to_timestamp; use crate::hostcalls_impl::{FileType, PathGet}; use crate::sys::host_impl; +use crate::sys::unix::str_to_cstring; use crate::{host, Error, Result}; use nix::libc; use std::convert::TryInto; @@ -57,7 +58,7 @@ pub(crate) fn fd_fdstat_set_flags(fd: &File, fdflags: host::__wasi_fdflags_t) -> pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> { use nix::libc::mkdirat; - let path_cstr = resolved.path_cstring()?; + let path_cstr = str_to_cstring(resolved.path())?; // nix doesn't expose mkdirat() yet match unsafe { mkdirat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0o777) } { 0 => Ok(()), @@ -67,8 +68,8 @@ pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> { pub(crate) fn path_link(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> { use nix::libc::linkat; - let old_path_cstr = resolved_old.path_cstring()?; - let new_path_cstr = resolved_new.path_cstring()?; + let old_path_cstr = str_to_cstring(resolved_old.path())?; + let new_path_cstr = str_to_cstring(resolved_new.path())?; // Not setting AT_SYMLINK_FOLLOW fails on most filesystems let atflags = libc::AT_SYMLINK_FOLLOW; @@ -183,7 +184,7 @@ pub(crate) fn path_open( pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> Result { use nix::errno::Errno; - let path_cstr = resolved.path_cstring()?; + let path_cstr = str_to_cstring(resolved.path())?; // Linux requires that the buffer size is positive, whereas POSIX does not. // Use a fake buffer to store the results if the size is zero. @@ -340,7 +341,7 @@ pub(crate) fn path_remove_directory(resolved: PathGet) -> Result<()> { use nix::errno; use nix::libc::{unlinkat, AT_REMOVEDIR}; - let path_cstr = resolved.path_cstring()?; + let path_cstr = str_to_cstring(resolved.path())?; // nix doesn't expose unlinkat() yet match unsafe { diff --git a/src/sys/unix/linux/hostcalls_impl.rs b/src/sys/unix/linux/hostcalls_impl.rs index 835b210d14..3c756b77b0 100644 --- a/src/sys/unix/linux/hostcalls_impl.rs +++ b/src/sys/unix/linux/hostcalls_impl.rs @@ -1,11 +1,10 @@ use super::osfile::OsFile; -use crate::helpers::str_to_cstring; use crate::hostcalls_impl::PathGet; use crate::sys::host_impl; +use crate::sys::unix::str_to_cstring; use crate::{host, Error, Result}; use nix::libc::{self, c_long, c_void}; use std::convert::TryInto; -use std::ffi::CString; use std::fs::File; use std::mem::MaybeUninit; use std::os::unix::prelude::AsRawFd; @@ -14,7 +13,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> { use nix::errno; use nix::libc::unlinkat; - let path_cstr = resolved.path_cstring()?; + let path_cstr = str_to_cstring(resolved.path())?; // nix doesn't expose unlinkat() yet let res = unsafe { unlinkat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0) }; @@ -29,7 +28,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> { use nix::{errno::Errno, libc::symlinkat}; let old_path_cstr = str_to_cstring(old_path)?; - let new_path_cstr = resolved.path_cstring()?; + let new_path_cstr = str_to_cstring(resolved.path())?; log::debug!("path_symlink old_path = {:?}", old_path); log::debug!("path_symlink resolved = {:?}", resolved); @@ -50,8 +49,8 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> { pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> { use nix::libc::renameat; - let old_path_cstr = resolved_old.path_cstring()?; - let new_path_cstr = resolved_new.path_cstring()?; + let old_path_cstr = str_to_cstring(resolved_old.path())?; + let new_path_cstr = str_to_cstring(resolved_new.path())?; let res = unsafe { renameat( diff --git a/src/sys/unix/mod.rs b/src/sys/unix/mod.rs index 7b7fa65757..2bed69f61c 100644 --- a/src/sys/unix/mod.rs +++ b/src/sys/unix/mod.rs @@ -14,7 +14,8 @@ mod bsd; #[cfg(target_os = "linux")] mod linux; -use crate::Result; +use crate::{Error, Result}; +use std::ffi::CString; use std::fs::{File, OpenOptions}; use std::path::Path; @@ -26,6 +27,10 @@ pub(crate) fn dev_null() -> Result { .map_err(Into::into) } +pub(crate) fn str_to_cstring(s: &str) -> Result { + CString::new(s.as_bytes()).map_err(|_| Error::EILSEQ) +} + pub fn preopen_dir>(path: P) -> Result { File::open(path).map_err(Into::into) }