From 9d7ed436372901e79dddbdf2cbd83e2b78833f0a Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Mon, 20 Aug 2012 22:04:26 -0400 Subject: [PATCH 1/3] core: adding os::tmpdir() that returns a system temporary directory if it can find one, and none otherwise. --- src/libcore/os.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index 329172bb69ad9..6993eb82d0120 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -30,7 +30,7 @@ export close, fclose, fsync_fd, waitpid; export env, getenv, setenv, fdopen, pipe; export getcwd, dll_filename, self_exe_path; export exe_suffix, dll_suffix, sysname, arch, family; -export homedir, list_dir, list_dir_path, path_is_dir, path_exists, +export homedir, tmpdir, list_dir, list_dir_path, path_is_dir, path_exists, make_absolute, make_dir, remove_dir, change_dir, remove_file, copy_file; export last_os_error; @@ -464,6 +464,43 @@ fn homedir() -> option { } } +/** + * Returns the path to a temporary directory, if known. + * + * On Unix, returns the value of the 'TMPDIR' environment variable if it is + * set and non-empty and '/tmp' otherwise. + * + * On Windows, returns the value of, in order, the 'TMP', 'TEMP', + * 'USERPROFILE' environment variable if any are set and not the empty + * string. Otherwise, tmpdir returns option::none. + */ +fn tmpdir() -> option { + return lookup(); + + fn getenv_nonempty(v: Path) -> option { + match getenv(v) { + some(x) => + if str::is_empty(x) { + none + } else { + some(x) + }, + _ => none + } + } + + #[cfg(unix)] + fn lookup() -> option { + option::or(getenv_nonempty(~"TMPDIR"), some(~"/tmp")) + } + + #[cfg(windows)] + fn lookup() -> option { + option::or(getenv_nonempty(~"TMP"), + option::or(getenv_nonempty(~"TEMP"), + getenv_nonempty(~"USERPROFILE"))) + } +} /// Recursively walk a directory structure fn walk_dir(p: Path, f: fn(Path) -> bool) { From d5ec6973f87a7dad01151760444b636d6f327a0c Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Tue, 21 Aug 2012 14:35:58 -0400 Subject: [PATCH 2/3] core: adding test for os::tmpdir() --- src/libcore/os.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index 6993eb82d0120..2fb02a96e1973 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -968,6 +968,11 @@ mod tests { |s| setenv(~"USERPROFILE", s)); } + #[test] + fn tmpdir() { + option::iter(os::tmpdir(), |s| assert !str::is_empty(s)); + } + // Issue #712 #[test] fn test_list_dir_no_invalid_memory_access() { os::list_dir(~"."); } From 7b458813d0cf5f308d9c3b5decd7c4052362419c Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Tue, 21 Aug 2012 15:23:46 -0400 Subject: [PATCH 3/3] core: switching os::tmpdir() to always return a directory, by defaulting to Windows dir on windows, as per .NET --- src/libcore/os.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index 2fb02a96e1973..be50c71697fe1 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -465,16 +465,16 @@ fn homedir() -> option { } /** - * Returns the path to a temporary directory, if known. + * Returns the path to a temporary directory. * * On Unix, returns the value of the 'TMPDIR' environment variable if it is * set and non-empty and '/tmp' otherwise. * * On Windows, returns the value of, in order, the 'TMP', 'TEMP', - * 'USERPROFILE' environment variable if any are set and not the empty - * string. Otherwise, tmpdir returns option::none. + * 'USERPROFILE' environment variable if any are set and not the empty + * string. Otherwise, tmpdir returns the path to the Windows directory. */ -fn tmpdir() -> option { +fn tmpdir() -> Path { return lookup(); fn getenv_nonempty(v: Path) -> option { @@ -490,15 +490,18 @@ fn tmpdir() -> option { } #[cfg(unix)] - fn lookup() -> option { - option::or(getenv_nonempty(~"TMPDIR"), some(~"/tmp")) + fn lookup() -> Path { + option::get_default(getenv_nonempty(~"TMPDIR"), ~"/tmp") } #[cfg(windows)] - fn lookup() -> option { - option::or(getenv_nonempty(~"TMP"), - option::or(getenv_nonempty(~"TEMP"), - getenv_nonempty(~"USERPROFILE"))) + fn lookup() -> Path { + option::get_default( + option::or(getenv_nonempty(~"TMP"), + option::or(getenv_nonempty(~"TEMP"), + option::or(getenv_nonempty(~"USERPROFILE"), + getenv_nonempty(~"WINDIR")))), + ~"C:\\Windows") } } /// Recursively walk a directory structure @@ -970,7 +973,7 @@ mod tests { #[test] fn tmpdir() { - option::iter(os::tmpdir(), |s| assert !str::is_empty(s)); + assert !str::is_empty(os::tmpdir()); } // Issue #712