Skip to content

Commit 450554e

Browse files
committed
Attempt at fixing dead code lints
1 parent 4186037 commit 450554e

File tree

6 files changed

+106
-68
lines changed

6 files changed

+106
-68
lines changed

src/libstd/sys/unix/backtrace/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,19 @@
8383
/// to symbols. This is a bit of a hokey implementation as-is, but it works for
8484
/// all unix platforms we support right now, so it at least gets the job done.
8585
86-
use io;
87-
use fs;
88-
8986
pub use self::tracing::write;
9087

9188
// tracing impls:
9289
mod tracing;
9390
// symbol resolvers:
9491
mod printing;
9592

96-
pub fn get_executable_filename() -> io::Result<(Vec<i8>, fs::File)> {
97-
Err(io::Error::new(io::ErrorKind::Other, "Not implemented"))
93+
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "emscripten")))]
94+
pub mod gnu {
95+
use io;
96+
use fs;
97+
98+
pub fn get_executable_filename() -> io::Result<(Vec<i8>, fs::File)> {
99+
Err(io::Error::new(io::ErrorKind::Other, "Not implemented"))
100+
}
98101
}

src/libstd/sys/windows/backtrace.rs

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ use io;
3030
use libc::c_void;
3131
use mem;
3232
use ptr;
33-
use path::PathBuf;
34-
use fs::{OpenOptions, File};
35-
use sys::ext::fs::OpenOptionsExt;
3633
use sys::c;
3734
use sys::dynamic_lib::DynamicLibrary;
3835
use sys::mutex::Mutex;
39-
use sys::handle::Handle;
4036

4137
macro_rules! sym {
4238
($lib:expr, $e:expr, $t:ident) => (
@@ -55,6 +51,10 @@ mod printing;
5551
#[path = "printing/gnu.rs"]
5652
mod printing;
5753

54+
#[cfg(target_env = "gnu")]
55+
#[path = "backtrace_gnu.rs"]
56+
pub mod gnu;
57+
5858
type SymInitializeFn =
5959
unsafe extern "system" fn(c::HANDLE, *mut c_void,
6060
c::BOOL) -> c::BOOL;
@@ -161,47 +161,3 @@ unsafe fn _write(w: &mut Write) -> io::Result<()> {
161161

162162
Ok(())
163163
}
164-
165-
fn query_full_process_image_name() -> io::Result<PathBuf> {
166-
unsafe {
167-
let process_handle = Handle::new(c::OpenProcess(c::PROCESS_QUERY_INFORMATION,
168-
c::FALSE,
169-
c::GetCurrentProcessId()));
170-
super::fill_utf16_buf(|buf, mut sz| {
171-
if c::QueryFullProcessImageNameW(process_handle.raw(), 0, buf, &mut sz) == 0 {
172-
0
173-
} else {
174-
sz
175-
}
176-
}, super::os2path)
177-
}
178-
}
179-
180-
fn lock_and_get_executable_filename() -> io::Result<(PathBuf, File)> {
181-
// We query the current image name, open the file without FILE_SHARE_DELETE so it
182-
// can't be moved and then get the current image name again. If the names are the
183-
// same than we have successfully locked the file
184-
let image_name1 = query_full_process_image_name()?;
185-
let file = OpenOptions::new()
186-
.read(true)
187-
.share_mode(c::FILE_SHARE_READ | c::FILE_SHARE_WRITE)
188-
.open(&image_name1)?;
189-
let image_name2 = query_full_process_image_name()?;
190-
191-
if image_name1 != image_name2 {
192-
return Err(io::Error::new(io::ErrorKind::Other,
193-
"executable moved while trying to lock it"));
194-
}
195-
196-
Ok((image_name1, file))
197-
}
198-
199-
// Get the executable filename for libbacktrace
200-
// This returns the path in the ANSI code page and a File which should remain open
201-
// for as long as the path should remain valid
202-
pub fn get_executable_filename() -> io::Result<(Vec<i8>, File)> {
203-
let (executable, file) = lock_and_get_executable_filename()?;
204-
let u16_executable = super::to_u16s(executable.into_os_string())?;
205-
Ok((super::wide_char_to_multi_byte(c::CP_ACP, c::WC_NO_BEST_FIT_CHARS,
206-
&u16_executable, true)?, file))
207-
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use io;
12+
use sys::c;
13+
use path::PathBuf;
14+
use fs::{OpenOptions, File};
15+
use sys::ext::fs::OpenOptionsExt;
16+
use sys::handle::Handle;
17+
use super::super::{fill_utf16_buf, os2path, to_u16s, wide_char_to_multi_byte};
18+
19+
fn query_full_process_image_name() -> io::Result<PathBuf> {
20+
unsafe {
21+
let process_handle = Handle::new(c::OpenProcess(c::PROCESS_QUERY_INFORMATION,
22+
c::FALSE,
23+
c::GetCurrentProcessId()));
24+
fill_utf16_buf(|buf, mut sz| {
25+
if c::QueryFullProcessImageNameW(process_handle.raw(), 0, buf, &mut sz) == 0 {
26+
0
27+
} else {
28+
sz
29+
}
30+
}, os2path)
31+
}
32+
}
33+
34+
fn lock_and_get_executable_filename() -> io::Result<(PathBuf, File)> {
35+
// We query the current image name, open the file without FILE_SHARE_DELETE so it
36+
// can't be moved and then get the current image name again. If the names are the
37+
// same than we have successfully locked the file
38+
let image_name1 = query_full_process_image_name()?;
39+
let file = OpenOptions::new()
40+
.read(true)
41+
.share_mode(c::FILE_SHARE_READ | c::FILE_SHARE_WRITE)
42+
.open(&image_name1)?;
43+
let image_name2 = query_full_process_image_name()?;
44+
45+
if image_name1 != image_name2 {
46+
return Err(io::Error::new(io::ErrorKind::Other,
47+
"executable moved while trying to lock it"));
48+
}
49+
50+
Ok((image_name1, file))
51+
}
52+
53+
// Get the executable filename for libbacktrace
54+
// This returns the path in the ANSI code page and a File which should remain open
55+
// for as long as the path should remain valid
56+
pub fn get_executable_filename() -> io::Result<(Vec<i8>, File)> {
57+
let (executable, file) = lock_and_get_executable_filename()?;
58+
let u16_executable = to_u16s(executable.into_os_string())?;
59+
Ok((wide_char_to_multi_byte(c::CP_ACP, c::WC_NO_BEST_FIT_CHARS,
60+
&u16_executable, true)?, file))
61+
}

src/libstd/sys/windows/c.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ pub const WSAECONNREFUSED: c_int = 10061;
158158

159159
pub const MAX_PROTOCOL_CHAIN: DWORD = 7;
160160

161-
pub const PROCESS_QUERY_INFORMATION: DWORD = 0x0400;
162161
pub const TOKEN_READ: DWORD = 0x20008;
163162
pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
164163
pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x900a8;
@@ -220,10 +219,6 @@ pub const CREATE_NEW_PROCESS_GROUP: DWORD = 0x00000200;
220219
pub const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400;
221220
pub const STARTF_USESTDHANDLES: DWORD = 0x00000100;
222221

223-
pub const CP_ACP: UINT = 0;
224-
225-
pub const WC_NO_BEST_FIT_CHARS: DWORD = 0x00000400;
226-
227222
pub const AF_INET: c_int = 2;
228223
pub const AF_INET6: c_int = 23;
229224
pub const SD_BOTH: c_int = 2;
@@ -894,9 +889,6 @@ extern "system" {
894889
pNumArgs: *mut c_int) -> *mut *mut u16;
895890
pub fn GetTempPathW(nBufferLength: DWORD,
896891
lpBuffer: LPCWSTR) -> DWORD;
897-
pub fn OpenProcess(dwDesiredAccess: DWORD,
898-
bInheritHandle: BOOL,
899-
dwProcessId: DWORD) -> HANDLE;
900892
pub fn OpenProcessToken(ProcessHandle: HANDLE,
901893
DesiredAccess: DWORD,
902894
TokenHandle: *mut HANDLE) -> BOOL;
@@ -1153,12 +1145,6 @@ compat_fn! {
11531145
_dwFlags: DWORD) -> DWORD {
11541146
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
11551147
}
1156-
pub fn QueryFullProcessImageNameW(_hProcess: HANDLE,
1157-
_dwFlags: DWORD,
1158-
_lpExeName: LPWSTR,
1159-
_lpdwSize: LPDWORD) -> BOOL {
1160-
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1161-
}
11621148
pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL {
11631149
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
11641150
}
@@ -1201,3 +1187,34 @@ compat_fn! {
12011187
panic!("rwlocks not available")
12021188
}
12031189
}
1190+
1191+
#[cfg(target_env = "gnu")]
1192+
mod gnu {
1193+
use super::*;
1194+
1195+
pub const PROCESS_QUERY_INFORMATION: DWORD = 0x0400;
1196+
1197+
pub const CP_ACP: UINT = 0;
1198+
1199+
pub const WC_NO_BEST_FIT_CHARS: DWORD = 0x00000400;
1200+
1201+
extern "system" {
1202+
pub fn OpenProcess(dwDesiredAccess: DWORD,
1203+
bInheritHandle: BOOL,
1204+
dwProcessId: DWORD) -> HANDLE;
1205+
}
1206+
1207+
compat_fn! {
1208+
kernel32:
1209+
1210+
pub fn QueryFullProcessImageNameW(_hProcess: HANDLE,
1211+
_dwFlags: DWORD,
1212+
_lpExeName: LPWSTR,
1213+
_lpdwSize: LPDWORD) -> BOOL {
1214+
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1215+
}
1216+
}
1217+
}
1218+
1219+
#[cfg(target_env = "gnu")]
1220+
pub use self::gnu::*;

src/libstd/sys/windows/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ fn os2path(s: &[u16]) -> PathBuf {
172172
PathBuf::from(OsString::from_wide(s))
173173
}
174174

175+
#[allow(dead_code)] // Only used in backtrace::gnu::get_executable_filename()
175176
fn wide_char_to_multi_byte(code_page: u32,
176177
flags: u32,
177178
s: &[u16],

src/libstd/sys_common/gnu/libbacktrace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub fn print(w: &mut Write, idx: isize, addr: *mut libc::c_void,
126126
static mut STATE: *mut backtrace_state = ptr::null_mut();
127127
if !STATE.is_null() { return STATE }
128128

129-
let filename = match ::sys::backtrace::get_executable_filename() {
129+
let filename = match ::sys::backtrace::gnu::get_executable_filename() {
130130
Ok((filename, file)) => {
131131
// filename is purposely leaked here since libbacktrace requires
132132
// it to stay allocated permanently, file is also leaked so that

0 commit comments

Comments
 (0)