std: add a fs::remove_file function.

This commit is contained in:
Erick Tryzelaar 2012-03-02 14:49:15 -08:00
parent 3cb2cd1f71
commit 6b0c4822c1
5 changed files with 25 additions and 0 deletions

View File

@ -62,6 +62,7 @@ native mod libc {
fn mkdir(path: str::sbuf, mode: c_int) -> c_int;
fn rmdir(path: str::sbuf) -> c_int;
fn chdir(path: str::sbuf) -> c_int;
fn unlink(path: str::sbuf) -> c_int;
fn sysctl(name: *c_int, namelen: c_uint,
oldp: *u8, &oldlenp: size_t,

View File

@ -449,6 +449,27 @@ fn homedir() -> option<path> {
}
}
/*
Function: remove_file
Deletes an existing file.
*/
fn remove_file(p: path) -> bool {
ret unlink(p);
#[cfg(target_os = "win32")]
fn unlink(p: path) -> bool {
ret str::as_buf(p, {|buf| os::kernel32::DeleteFileA(buf)});
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn unlink(_p: path) -> bool {
ret str::as_buf(_p, {|buf| os::libc::unlink(buf) == 0i32 });
}
}
#[cfg(test)]
mod tests {
#[test]

View File

@ -63,6 +63,7 @@ native mod libc {
fn mkdir(path: str::sbuf, mode: c_int) -> c_int;
fn rmdir(path: str::sbuf) -> c_int;
fn chdir(path: str::sbuf) -> c_int;
fn unlink(path: str::sbuf) -> c_int;
}
mod libc_constants {

View File

@ -55,6 +55,7 @@ native mod libc {
fn mkdir(s: str::sbuf, mode: c_int) -> c_int;
fn rmdir(s: str::sbuf) -> c_int;
fn chdir(s: str::sbuf) -> c_int;
fn unlink(path: str::sbuf) -> c_int;
// FIXME: Needs varags
fn fcntl(fd: fd_t, cmd: c_int) -> c_int;

View File

@ -62,6 +62,7 @@ native mod kernel32 {
lpSecurityAttributes: LPSECURITY_ATTRIBUTES) -> bool;
fn RemoveDirectoryA(lpPathName: LPCTSTR) -> bool;
fn SetCurrentDirectoryA(lpPathName: LPCTSTR) -> bool;
fn DeleteFileA(lpFileName: LPCTSTR) -> bool;
}
// FIXME turn into constants