From c77c5c4674c92b342132a56bd1b59f86af3d5a63 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Tue, 19 Feb 2013 01:54:05 +0000 Subject: [PATCH] Added is_restricted() to path --- src/libcore/path.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 96a921cf20bfb..1753862649f57 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -65,6 +65,7 @@ pub trait GenericPath { pure fn pop() -> Self; pure fn unsafe_join((&Self)) -> Self; + pure fn is_restricted() -> bool; pure fn normalize() -> Self; } @@ -496,6 +497,10 @@ impl GenericPath for PosixPath { } } + pure fn is_restricted() -> bool { + false + } + pure fn push_many(cs: &[~str]) -> PosixPath { let mut v = copy self.components; for cs.each |e| { @@ -738,6 +743,19 @@ impl GenericPath for WindowsPath { } } + pure fn is_restricted() -> bool { + match self.filestem() { + Some(stem) => { + match stem.to_lower() { + ~"con" | ~"aux" | ~"com1" | ~"com2" | ~"com3" | ~"com4" | + ~"lpt1" | ~"lpt2" | ~"lpt3" | ~"prn" | ~"nul" => true, + _ => false + } + }, + None => false + } + } + pure fn push_many(cs: &[~str]) -> WindowsPath { let mut v = copy self.components; for cs.each |e| { @@ -1094,4 +1112,12 @@ mod tests { .normalize()), "C:\\foo"); } + + #[test] + fn test_windows_path_restrictions() { + assert WindowsPath("hi").is_restricted() == false; + assert WindowsPath("C:\\NUL").is_restricted() == true; + assert WindowsPath("C:\\COM1.TXT").is_restricted() == true; + assert WindowsPath("c:\\prn.exe").is_restricted() == true; + } }