Rollup merge of #40516 - alexcrichton:no-cache-handles, r=aturon
std: Don't cache stdio handles on Windows This alters the stdio code on Windows to always call `GetStdHandle` whenever the stdio read/write functions are called as this allows us to track changes to the value over time (such as if a process calls `SetStdHandle` while it's running). Closes #40490
This commit is contained in:
commit
76cc08b45a
3 changed files with 110 additions and 55 deletions
64
src/test/run-pass-fulldeps/switch-stdout.rs
Normal file
64
src/test/run-pass-fulldeps/switch-stdout.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2012-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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(rustc_private)]
|
||||
|
||||
extern crate rustc_back;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use rustc_back::tempdir::TempDir;
|
||||
|
||||
#[cfg(unix)]
|
||||
fn switch_stdout_to(file: File) {
|
||||
use std::os::unix::prelude::*;
|
||||
|
||||
extern {
|
||||
fn dup2(old: i32, new: i32) -> i32;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
assert_eq!(dup2(file.as_raw_fd(), 1), 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn switch_stdout_to(file: File) {
|
||||
use std::os::windows::prelude::*;
|
||||
|
||||
extern "system" {
|
||||
fn SetStdHandle(nStdHandle: u32, handle: *mut u8) -> i32;
|
||||
}
|
||||
|
||||
const STD_OUTPUT_HANDLE: u32 = (-11i32) as u32;
|
||||
|
||||
unsafe {
|
||||
let rc = SetStdHandle(STD_OUTPUT_HANDLE,
|
||||
file.into_raw_handle() as *mut _);
|
||||
assert!(rc != 0);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let td = TempDir::new("foo").unwrap();
|
||||
let path = td.path().join("bar");
|
||||
let f = File::create(&path).unwrap();
|
||||
|
||||
println!("foo");
|
||||
std::io::stdout().flush().unwrap();
|
||||
switch_stdout_to(f);
|
||||
println!("bar");
|
||||
std::io::stdout().flush().unwrap();
|
||||
|
||||
let mut contents = String::new();
|
||||
File::open(&path).unwrap().read_to_string(&mut contents).unwrap();
|
||||
assert_eq!(contents, "bar\n");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue