Skip to content

Move rustrt::last_os_error into core #4859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ impl *libc::FILE: Writer {
*self);
if nout != len as size_t {
error!("error writing buffer");
log(error, os::last_os_error());
log(error, os::last_error());
die!();
}
}
Expand Down Expand Up @@ -657,7 +657,7 @@ impl fd_t: Writer {
let nout = libc::write(*self, vb, len as size_t);
if nout < 0 as ssize_t {
error!("error writing buffer");
log(error, os::last_os_error());
log(error, os::last_error());
die!();
}
count += nout as uint;
Expand Down Expand Up @@ -731,7 +731,7 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
};
if fd < (0 as c_int) {
result::Err(fmt!("error opening %s: %s", path.to_str(),
os::last_os_error()))
os::last_error()))
} else {
result::Ok(fd_writer(fd, true))
}
Expand Down
16 changes: 11 additions & 5 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ extern mod rustrt {
unsafe fn rust_path_exists(path: *libc::c_char) -> c_int;
unsafe fn rust_list_files2(&&path: ~str) -> ~[~str];
unsafe fn rust_process_wait(handle: c_int) -> c_int;
unsafe fn last_os_error() -> ~str;
unsafe fn rust_set_exit_status(code: libc::intptr_t);
}

extern mod rustllvm {
unsafe fn LLVMGetLastError() -> *libc::c_char;
}

pub const tmpbuf_sz : uint = 1000u;

pub fn getcwd() -> Path {
Expand Down Expand Up @@ -767,9 +770,12 @@ pub fn remove_file(p: &Path) -> bool {
}

/// Get a string representing the platform-dependent last error
pub fn last_os_error() -> ~str {
pub fn last_error() -> ~str {
unsafe {
rustrt::last_os_error()
let e = rustllvm::LLVMGetLastError();
let err_str = str::raw::from_c_str(e);
libc::free(e as *libc::c_void);
err_str
}
}

Expand Down Expand Up @@ -1000,8 +1006,8 @@ mod tests {
use vec;

#[test]
pub fn last_os_error() {
log(debug, os::last_os_error());
pub fn last_error() {
log(debug, os::last_error());
}

#[test]
Expand Down
44 changes: 0 additions & 44 deletions src/rt/rust_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,6 @@ timegm(struct tm *tm)
}
#endif


extern "C" CDECL rust_str*
last_os_error() {
rust_task *task = rust_get_current_task();

LOG(task, task, "last_os_error()");

#if defined(__WIN32__)
LPTSTR buf;
DWORD err = GetLastError();
DWORD res = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &buf, 0, NULL);
if (!res) {
task->fail();
return NULL;
}
#elif defined(_GNU_SOURCE) && !defined(__ANDROID__)
char cbuf[BUF_BYTES];
char *buf = strerror_r(errno, cbuf, sizeof(cbuf));
if (!buf) {
task->fail();
return NULL;
}
#else
char buf[BUF_BYTES];
int err = strerror_r(errno, buf, sizeof(buf));
if (err) {
task->fail();
return NULL;
}
#endif

rust_str * st = make_str(task->kernel, buf, strlen(buf),
"last_os_error");
#ifdef __WIN32__
LocalFree((HLOCAL)buf);
#endif
return st;
}

extern "C" CDECL rust_str *
rust_getcwd() {
rust_task *task = rust_get_current_task();
Expand Down
1 change: 0 additions & 1 deletion src/rt/rustrt.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ rust_gmtime
rust_localtime
rust_timegm
rust_mktime
last_os_error
new_task
precise_time_ns
rand_free
Expand Down
8 changes: 8 additions & 0 deletions src/rustllvm/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/SourceMgr.h"
Expand Down Expand Up @@ -72,6 +73,13 @@ extern "C" const char *LLVMRustGetLastError(void) {
return LLVMRustError;
}

extern "C" const char *LLVMGetLastError(void) {
std::string e = StrError();
char *err = (char *) malloc(e.length() + 1);
std::strcpy(err, e.c_str());
return err;
}

extern "C" void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM);

extern "C" void LLVMRustAddPrintModulePass(LLVMPassManagerRef PMR,
Expand Down
1 change: 1 addition & 0 deletions src/rustllvm/rustllvm.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,4 @@ LLVMX86MMXTypeInContext
LLVMConstNamedStruct
LLVMStructCreateNamed
LLVMStructSetBody
LLVMGetLastError
4 changes: 2 additions & 2 deletions src/test/auxiliary/anon-extern-mod-cross-crate-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
// except according to those terms.

#[abi = "cdecl"];
#[link_name = "rustrt"];
#[link_name = "rustllvm"];
#[link(name = "anonexternmod",
vers = "0.1")];

#[crate_type = "lib"];
extern {
fn last_os_error() -> ~str;
unsafe fn LLVMGetLastError() -> *core::libc::c_char;
}
4 changes: 2 additions & 2 deletions src/test/auxiliary/foreign_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

#[link(name="foreign_lib", vers="0.0")];

pub extern mod rustrt {
pub fn last_os_error() -> ~str;
pub extern mod rustllvm {
pub unsafe fn LLVMGetLastError() -> *core::libc::c_char;
}
5 changes: 1 addition & 4 deletions src/test/run-fail/morestack2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@

extern mod std;

extern mod rustrt {
pub fn last_os_error() -> ~str;
}

fn getbig_call_c_and_fail(i: int) {
if i != 0 {
getbig_call_c_and_fail(i - 1);
} else {
unsafe {
rustrt::last_os_error();
core::os::last_error();
die!();
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/run-pass/anon-extern-mod-cross-crate-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ extern mod anonexternmod;
use anonexternmod::*;

pub fn main() {
last_os_error();
unsafe {
LLVMGetLastError();
}
}
6 changes: 3 additions & 3 deletions src/test/run-pass/anon-extern-mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
// except according to those terms.

#[abi = "cdecl"]
#[link_name = "rustrt"]
#[link_name = "rustllvm"]
extern {
fn last_os_error() -> ~str;
unsafe fn LLVMGetLastError() -> *libc::c_char;
}

pub fn main() {
unsafe {
let _ = last_os_error();
let _ = LLVMGetLastError();
}
}
16 changes: 8 additions & 8 deletions src/test/run-pass/foreign-dupe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
// calling pin_task and that's having wierd side-effects.

#[abi = "cdecl"]
#[link_name = "rustrt"]
extern mod rustrt1 {
pub fn last_os_error() -> ~str;
#[link_name = "rustllvm"]
extern mod rustllvm1 {
pub unsafe fn LLVMGetLastError() -> *libc::c_char;
}

#[abi = "cdecl"]
#[link_name = "rustrt"]
extern mod rustrt2 {
pub fn last_os_error() -> ~str;
#[link_name = "rustllvm"]
extern mod rustllvm2 {
pub unsafe fn LLVMGetLastError() -> *libc::c_char;
}

pub fn main() {
unsafe {
rustrt1::last_os_error();
rustrt2::last_os_error();
rustllvm1::LLVMGetLastError();
rustllvm2::LLVMGetLastError();
}
}
4 changes: 3 additions & 1 deletion src/test/run-pass/invoke-external-foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
extern mod foreign_lib;

pub fn main() {
let foo = foreign_lib::rustrt::last_os_error();
unsafe {
let _foo = foreign_lib::rustllvm::LLVMGetLastError();
}
}
3 changes: 0 additions & 3 deletions src/test/run-pass/morestack6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ extern mod rustrt {
pub fn debug_get_stk_seg() -> *u8;

pub fn rust_get_sched_id() -> libc::intptr_t;
pub fn last_os_error() -> ~str;
pub fn rust_getcwd() -> ~str;
pub fn get_task_id() -> libc::intptr_t;
pub fn rust_sched_threads();
pub fn rust_get_task();
}

fn calllink01() { unsafe { rustrt::rust_get_sched_id(); } }
fn calllink02() { unsafe { rustrt::last_os_error(); } }
fn calllink03() { unsafe { rustrt::rust_getcwd(); } }
fn calllink08() { unsafe { rustrt::get_task_id(); } }
fn calllink09() { unsafe { rustrt::rust_sched_threads(); } }
Expand Down Expand Up @@ -56,7 +54,6 @@ fn runtest2(f: extern fn(), frame_backoff: u32, last_stk: *u8) -> u32 {
pub fn main() {
let fns = ~[
calllink01,
calllink02,
calllink03,
calllink08,
calllink09,
Expand Down