Skip to content

Commit

Permalink
Updated std::dynamic_lib to use std::path.
Browse files Browse the repository at this point in the history
  • Loading branch information
aatxe committed Mar 27, 2015
1 parent b0aad7d commit 6acf385
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
5 changes: 0 additions & 5 deletions src/librustc/plugin/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ use std::borrow::ToOwned;
use std::dynamic_lib::DynamicLibrary;
use std::env;
use std::mem;

#[allow(deprecated)]
use std::old_path;

use std::path::PathBuf;
use syntax::ast;
use syntax::codemap::{Span, COMMAND_LINE_SP};
Expand Down Expand Up @@ -110,7 +106,6 @@ impl<'a> PluginLoader<'a> {
symbol: String) -> PluginRegistrarFun {
// Make sure the path contains a / or the linker will search for it.
let path = env::current_dir().unwrap().join(&path);
let path = old_path::Path::new(path.to_str().unwrap());

let lib = match DynamicLibrary::open(Some(&path)) {
Ok(lib) => lib,
Expand Down
52 changes: 25 additions & 27 deletions src/libstd/dynamic_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@

#![unstable(feature = "std_misc")]
#![allow(missing_docs)]
#![allow(deprecated)] // will be addressed by #23197

use prelude::v1::*;

use env;
use ffi::CString;
use ffi::{AsOsStr, CString, OsString};
use mem;
use old_path::{Path, GenericPath};
use os;
use str;
use path::{Path, PathBuf};
#[cfg(not(target_os = "android"))] use os;
#[cfg(not(target_os = "android"))] use str;

pub struct DynamicLibrary {
handle: *mut u8
Expand Down Expand Up @@ -54,7 +53,7 @@ impl DynamicLibrary {
/// Lazily open a dynamic library. When passed None it gives a
/// handle to the calling process
pub fn open(filename: Option<&Path>) -> Result<DynamicLibrary, String> {
let maybe_library = dl::open(filename.map(|path| path.as_vec()));
let maybe_library = dl::open(filename.map(|path| path.as_os_str()));

// The dynamic library must not be constructed if there is
// an error opening the library so the destructor does not
Expand All @@ -68,19 +67,17 @@ impl DynamicLibrary {
/// Prepends a path to this process's search path for dynamic libraries
pub fn prepend_search_path(path: &Path) {
let mut search_path = DynamicLibrary::search_path();
search_path.insert(0, path.clone());
let newval = DynamicLibrary::create_path(&search_path);
env::set_var(DynamicLibrary::envvar(),
str::from_utf8(&newval).unwrap());
search_path.insert(0, path.to_path_buf());
env::set_var(DynamicLibrary::envvar(), &DynamicLibrary::create_path(&search_path));
}

/// From a slice of paths, create a new vector which is suitable to be an
/// environment variable for this platforms dylib search path.
pub fn create_path(path: &[Path]) -> Vec<u8> {
let mut newvar = Vec::new();
pub fn create_path(path: &[PathBuf]) -> OsString {
let mut newvar = OsString::new();
for (i, path) in path.iter().enumerate() {
if i > 0 { newvar.push(DynamicLibrary::separator()); }
newvar.push_all(path.as_vec());
newvar.push(path);
}
return newvar;
}
Expand All @@ -97,15 +94,15 @@ impl DynamicLibrary {
}
}

fn separator() -> u8 {
if cfg!(windows) {b';'} else {b':'}
fn separator() -> &'static str {
if cfg!(windows) { ";" } else { ":" }
}

/// Returns the current search path for dynamic libraries being used by this
/// process
pub fn search_path() -> Vec<Path> {
pub fn search_path() -> Vec<PathBuf> {
match env::var_os(DynamicLibrary::envvar()) {
Some(var) => os::split_paths(var.to_str().unwrap()),
Some(var) => env::split_paths(&var).collect(),
None => Vec::new(),
}
}
Expand Down Expand Up @@ -134,8 +131,8 @@ mod test {
use super::*;
use prelude::v1::*;
use libc;
use old_path::Path;
use mem;
use path::Path;

#[test]
#[cfg_attr(any(windows, target_os = "android"), ignore)] // FIXME #8818, #10379
Expand Down Expand Up @@ -192,12 +189,13 @@ mod test {
mod dl {
use prelude::v1::*;

use ffi::{CString, CStr};
use ffi::{CStr, OsStr};
use str;
use libc;
use os::unix::prelude::*;
use ptr;

pub fn open(filename: Option<&[u8]>) -> Result<*mut u8, String> {
pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
check_for_errors_in(|| {
unsafe {
match filename {
Expand All @@ -210,8 +208,8 @@ mod dl {

const LAZY: libc::c_int = 1;

unsafe fn open_external(filename: &[u8]) -> *mut u8 {
let s = CString::new(filename).unwrap();
unsafe fn open_external(filename: &OsStr) -> *mut u8 {
let s = filename.to_cstring().unwrap();
dlopen(s.as_ptr(), LAZY) as *mut u8
}

Expand Down Expand Up @@ -264,21 +262,22 @@ mod dl {

#[cfg(target_os = "windows")]
mod dl {
use ffi::OsStr;
use iter::IteratorExt;
use libc;
use libc::consts::os::extra::ERROR_CALL_NOT_IMPLEMENTED;
use ops::FnOnce;
use os;
use os::windows::prelude::*;
use option::Option::{self, Some, None};
use ptr;
use result::Result;
use result::Result::{Ok, Err};
use str;
use string::String;
use vec::Vec;
use sys::c::compat::kernel32::SetThreadErrorMode;

pub fn open(filename: Option<&[u8]>) -> Result<*mut u8, String> {
pub fn open(filename: Option<&OsStr>) -> Result<*mut u8, String> {
// disable "dll load failed" error dialog.
let mut use_thread_mode = true;
let prev_error_mode = unsafe {
Expand Down Expand Up @@ -308,9 +307,8 @@ mod dl {

let result = match filename {
Some(filename) => {
let filename_str = str::from_utf8(filename).unwrap();
let mut filename_str: Vec<u16> = filename_str.utf16_units().collect();
filename_str.push(0);
let filename_str: Vec<_> =
filename.encode_wide().chain(Some(0).into_iter()).collect();
let result = unsafe {
LoadLibraryW(filename_str.as_ptr() as *const libc::c_void)
};
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys/windows/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
//! this takes the route of using StackWalk64 in order to walk the stack.

#![allow(dead_code)]
#![allow(deprecated)] // for old path for dynamic lib

use prelude::v1::*;
use io::prelude::*;
Expand All @@ -34,7 +33,7 @@ use intrinsics;
use io;
use libc;
use mem;
use old_path::Path;
use path::Path;
use ptr;
use str;
use sync::{StaticMutex, MUTEX_INIT};
Expand Down

0 comments on commit 6acf385

Please sign in to comment.