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`.
This commit is contained in:
Peter Huene
2019-10-30 13:05:18 -07:00
committed by Jakub Konka
parent f4d3f08fc6
commit 3b7a00f474
6 changed files with 22 additions and 28 deletions

View File

@@ -1,6 +1,5 @@
use crate::{Error, Result}; use crate::{Error, Result};
use std::convert::TryInto; use std::convert::TryInto;
use std::ffi::CString;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> { pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
@@ -10,7 +9,3 @@ pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
.try_into() .try_into()
.map_err(Into::into) // u128 doesn't fit into u64 .map_err(Into::into) // u128 doesn't fit into u64
} }
pub(crate) fn str_to_cstring(s: &str) -> Result<CString> {
CString::new(s.as_bytes()).map_err(|_| Error::EILSEQ)
}

View File

@@ -1,9 +1,7 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use crate::helpers::str_to_cstring;
use crate::sys::host_impl; use crate::sys::host_impl;
use crate::sys::hostcalls_impl::fs_helpers::*; use crate::sys::hostcalls_impl::fs_helpers::*;
use crate::{fdentry::FdEntry, host, Error, Result}; use crate::{fdentry::FdEntry, host, Error, Result};
use std::ffi::CString;
use std::fs::File; use std::fs::File;
use std::path::{Component, Path}; use std::path::{Component, Path};
@@ -21,10 +19,6 @@ impl PathGet {
pub(crate) fn path(&self) -> &str { pub(crate) fn path(&self) -> &str {
&self.path &self.path
} }
pub(crate) fn path_cstring(&self) -> Result<CString> {
str_to_cstring(&self.path)
}
} }
/// Normalizes a path to ensure that the target path is located under the directory provided. /// Normalizes a path to ensure that the target path is located under the directory provided.

View File

@@ -1,7 +1,7 @@
use super::osfile::OsFile; use super::osfile::OsFile;
use crate::helpers::str_to_cstring;
use crate::hostcalls_impl::PathGet; use crate::hostcalls_impl::PathGet;
use crate::sys::host_impl; use crate::sys::host_impl;
use crate::sys::unix::str_to_cstring;
use crate::{host, Error, Result}; use crate::{host, Error, Result};
use nix::libc::{self, c_long, c_void}; use nix::libc::{self, c_long, c_void};
use std::convert::TryInto; use std::convert::TryInto;
@@ -12,7 +12,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> {
use nix::errno; use nix::errno;
use nix::libc::unlinkat; use nix::libc::unlinkat;
let path_cstr = resolved.path_cstring()?; let path_cstr = str_to_cstring(resolved.path())?;
// nix doesn't expose unlinkat() yet // nix doesn't expose unlinkat() yet
match unsafe { unlinkat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0) } { 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}; use nix::{errno::Errno, fcntl::AtFlags, libc::symlinkat, sys::stat::fstatat};
let old_path_cstr = str_to_cstring(old_path)?; 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 old_path = {:?}", old_path);
log::debug!("path_symlink resolved = {:?}", resolved); 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<()> { pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
use nix::{errno::Errno, fcntl::AtFlags, libc::renameat, sys::stat::fstatat}; use nix::{errno::Errno, fcntl::AtFlags, libc::renameat, sys::stat::fstatat};
let old_path_cstr = resolved_old.path_cstring()?; let old_path_cstr = str_to_cstring(resolved_old.path())?;
let new_path_cstr = resolved_new.path_cstring()?; let new_path_cstr = str_to_cstring(resolved_new.path())?;
let res = unsafe { let res = unsafe {
renameat( renameat(

View File

@@ -4,6 +4,7 @@ use super::fs_helpers::*;
use crate::helpers::systemtime_to_timestamp; use crate::helpers::systemtime_to_timestamp;
use crate::hostcalls_impl::{FileType, PathGet}; use crate::hostcalls_impl::{FileType, PathGet};
use crate::sys::host_impl; use crate::sys::host_impl;
use crate::sys::unix::str_to_cstring;
use crate::{host, Error, Result}; use crate::{host, Error, Result};
use nix::libc; use nix::libc;
use std::convert::TryInto; 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<()> { pub(crate) fn path_create_directory(resolved: PathGet) -> Result<()> {
use nix::libc::mkdirat; use nix::libc::mkdirat;
let path_cstr = resolved.path_cstring()?; let path_cstr = str_to_cstring(resolved.path())?;
// nix doesn't expose mkdirat() yet // nix doesn't expose mkdirat() yet
match unsafe { mkdirat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0o777) } { match unsafe { mkdirat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0o777) } {
0 => Ok(()), 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<()> { pub(crate) fn path_link(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
use nix::libc::linkat; use nix::libc::linkat;
let old_path_cstr = resolved_old.path_cstring()?; let old_path_cstr = str_to_cstring(resolved_old.path())?;
let new_path_cstr = resolved_new.path_cstring()?; let new_path_cstr = str_to_cstring(resolved_new.path())?;
// Not setting AT_SYMLINK_FOLLOW fails on most filesystems // Not setting AT_SYMLINK_FOLLOW fails on most filesystems
let atflags = libc::AT_SYMLINK_FOLLOW; 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<usize> { pub(crate) fn path_readlink(resolved: PathGet, buf: &mut [u8]) -> Result<usize> {
use nix::errno::Errno; 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. // 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. // 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::errno;
use nix::libc::{unlinkat, AT_REMOVEDIR}; 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 // nix doesn't expose unlinkat() yet
match unsafe { match unsafe {

View File

@@ -1,11 +1,10 @@
use super::osfile::OsFile; use super::osfile::OsFile;
use crate::helpers::str_to_cstring;
use crate::hostcalls_impl::PathGet; use crate::hostcalls_impl::PathGet;
use crate::sys::host_impl; use crate::sys::host_impl;
use crate::sys::unix::str_to_cstring;
use crate::{host, Error, Result}; use crate::{host, Error, Result};
use nix::libc::{self, c_long, c_void}; use nix::libc::{self, c_long, c_void};
use std::convert::TryInto; use std::convert::TryInto;
use std::ffi::CString;
use std::fs::File; use std::fs::File;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use std::os::unix::prelude::AsRawFd; use std::os::unix::prelude::AsRawFd;
@@ -14,7 +13,7 @@ pub(crate) fn path_unlink_file(resolved: PathGet) -> Result<()> {
use nix::errno; use nix::errno;
use nix::libc::unlinkat; use nix::libc::unlinkat;
let path_cstr = resolved.path_cstring()?; let path_cstr = str_to_cstring(resolved.path())?;
// nix doesn't expose unlinkat() yet // nix doesn't expose unlinkat() yet
let res = unsafe { unlinkat(resolved.dirfd().as_raw_fd(), path_cstr.as_ptr(), 0) }; 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}; use nix::{errno::Errno, libc::symlinkat};
let old_path_cstr = str_to_cstring(old_path)?; 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 old_path = {:?}", old_path);
log::debug!("path_symlink resolved = {:?}", resolved); 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<()> { pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Result<()> {
use nix::libc::renameat; use nix::libc::renameat;
let old_path_cstr = resolved_old.path_cstring()?; let old_path_cstr = str_to_cstring(resolved_old.path())?;
let new_path_cstr = resolved_new.path_cstring()?; let new_path_cstr = str_to_cstring(resolved_new.path())?;
let res = unsafe { let res = unsafe {
renameat( renameat(

View File

@@ -14,7 +14,8 @@ mod bsd;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
mod linux; mod linux;
use crate::Result; use crate::{Error, Result};
use std::ffi::CString;
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::path::Path; use std::path::Path;
@@ -26,6 +27,10 @@ pub(crate) fn dev_null() -> Result<File> {
.map_err(Into::into) .map_err(Into::into)
} }
pub(crate) fn str_to_cstring(s: &str) -> Result<CString> {
CString::new(s.as_bytes()).map_err(|_| Error::EILSEQ)
}
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> { pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
File::open(path).map_err(Into::into) File::open(path).map_err(Into::into)
} }