From 450554ebf139d0b6ecde80f17c2ef89ff7de78bb Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Fri, 27 Jan 2017 11:33:24 +0200 Subject: Attempt at fixing dead code lints --- src/libstd/sys/windows/backtrace_gnu.rs | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/libstd/sys/windows/backtrace_gnu.rs (limited to 'src/libstd/sys/windows/backtrace_gnu.rs') diff --git a/src/libstd/sys/windows/backtrace_gnu.rs b/src/libstd/sys/windows/backtrace_gnu.rs new file mode 100644 index 00000000000..8282174a59e --- /dev/null +++ b/src/libstd/sys/windows/backtrace_gnu.rs @@ -0,0 +1,61 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use io; +use sys::c; +use path::PathBuf; +use fs::{OpenOptions, File}; +use sys::ext::fs::OpenOptionsExt; +use sys::handle::Handle; +use super::super::{fill_utf16_buf, os2path, to_u16s, wide_char_to_multi_byte}; + +fn query_full_process_image_name() -> io::Result { + unsafe { + let process_handle = Handle::new(c::OpenProcess(c::PROCESS_QUERY_INFORMATION, + c::FALSE, + c::GetCurrentProcessId())); + fill_utf16_buf(|buf, mut sz| { + if c::QueryFullProcessImageNameW(process_handle.raw(), 0, buf, &mut sz) == 0 { + 0 + } else { + sz + } + }, os2path) + } +} + +fn lock_and_get_executable_filename() -> io::Result<(PathBuf, File)> { + // We query the current image name, open the file without FILE_SHARE_DELETE so it + // can't be moved and then get the current image name again. If the names are the + // same than we have successfully locked the file + let image_name1 = query_full_process_image_name()?; + let file = OpenOptions::new() + .read(true) + .share_mode(c::FILE_SHARE_READ | c::FILE_SHARE_WRITE) + .open(&image_name1)?; + let image_name2 = query_full_process_image_name()?; + + if image_name1 != image_name2 { + return Err(io::Error::new(io::ErrorKind::Other, + "executable moved while trying to lock it")); + } + + Ok((image_name1, file)) +} + +// Get the executable filename for libbacktrace +// This returns the path in the ANSI code page and a File which should remain open +// for as long as the path should remain valid +pub fn get_executable_filename() -> io::Result<(Vec, File)> { + let (executable, file) = lock_and_get_executable_filename()?; + let u16_executable = to_u16s(executable.into_os_string())?; + Ok((wide_char_to_multi_byte(c::CP_ACP, c::WC_NO_BEST_FIT_CHARS, + &u16_executable, true)?, file)) +} -- cgit 1.4.1-3-g733a5 From 1b4a6c86fa2acc4385c3c420773484d61ecc22b8 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Sat, 28 Jan 2017 01:01:16 +0200 Subject: Use libc::c_char instead of i8 due to platforms with unsigned char --- src/libstd/sys/unix/backtrace/mod.rs | 3 ++- src/libstd/sys/windows/backtrace_gnu.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/libstd/sys/windows/backtrace_gnu.rs') diff --git a/src/libstd/sys/unix/backtrace/mod.rs b/src/libstd/sys/unix/backtrace/mod.rs index 62e9c24ac30..1eef89bf66f 100644 --- a/src/libstd/sys/unix/backtrace/mod.rs +++ b/src/libstd/sys/unix/backtrace/mod.rs @@ -94,8 +94,9 @@ mod printing; pub mod gnu { use io; use fs; + use libc::c_char; - pub fn get_executable_filename() -> io::Result<(Vec, fs::File)> { + pub fn get_executable_filename() -> io::Result<(Vec, fs::File)> { Err(io::Error::new(io::ErrorKind::Other, "Not implemented")) } } diff --git a/src/libstd/sys/windows/backtrace_gnu.rs b/src/libstd/sys/windows/backtrace_gnu.rs index 8282174a59e..f0d29dd4178 100644 --- a/src/libstd/sys/windows/backtrace_gnu.rs +++ b/src/libstd/sys/windows/backtrace_gnu.rs @@ -10,6 +10,7 @@ use io; use sys::c; +use libc::c_char; use path::PathBuf; use fs::{OpenOptions, File}; use sys::ext::fs::OpenOptionsExt; @@ -53,7 +54,7 @@ fn lock_and_get_executable_filename() -> io::Result<(PathBuf, File)> { // Get the executable filename for libbacktrace // This returns the path in the ANSI code page and a File which should remain open // for as long as the path should remain valid -pub fn get_executable_filename() -> io::Result<(Vec, File)> { +pub fn get_executable_filename() -> io::Result<(Vec, File)> { let (executable, file) = lock_and_get_executable_filename()?; let u16_executable = to_u16s(executable.into_os_string())?; Ok((wide_char_to_multi_byte(c::CP_ACP, c::WC_NO_BEST_FIT_CHARS, -- cgit 1.4.1-3-g733a5