1 // Copyright 2013 The Servo Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9 
10 //! A URL type for Core Foundation.
11 
12 pub use core_foundation_sys::url::*;
13 
14 use base::{TCFType, CFIndex};
15 use string::{CFString};
16 
17 use core_foundation_sys::base::{kCFAllocatorDefault, Boolean};
18 use std::fmt;
19 use std::ptr;
20 use std::path::{Path, PathBuf};
21 
22 use libc::{c_char, strlen, PATH_MAX};
23 
24 #[cfg(unix)]
25 use std::os::unix::ffi::OsStrExt;
26 #[cfg(unix)]
27 use std::ffi::OsStr;
28 
29 
30 declare_TCFType!(CFURL, CFURLRef);
31 impl_TCFType!(CFURL, CFURLRef, CFURLGetTypeID);
32 
33 impl fmt::Debug for CFURL {
34     #[inline]
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result35     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36         unsafe {
37             let string: CFString = TCFType::wrap_under_get_rule(CFURLGetString(self.0));
38             write!(f, "{}", string.to_string())
39         }
40     }
41 }
42 
43 impl CFURL {
from_path<P: AsRef<Path>>(path: P, isDirectory: bool) -> Option<CFURL>44     pub fn from_path<P: AsRef<Path>>(path: P, isDirectory: bool) -> Option<CFURL> {
45         let path_bytes;
46         #[cfg(unix)]
47         {
48             path_bytes = path.as_ref().as_os_str().as_bytes()
49         }
50         #[cfg(not(unix))]
51         {
52             // XXX: Getting non-valid UTF8 paths into CoreFoundation on Windows is going to be unpleasant
53             // CFURLGetWideFileSystemRepresentation might help
54             path_bytes = match path.as_ref().to_str() {
55                 Some(path) => path,
56                 None => return None,
57             }
58         }
59 
60         unsafe {
61             let url_ref = CFURLCreateFromFileSystemRepresentation(ptr::null_mut(), path_bytes.as_ptr(), path_bytes.len() as CFIndex, isDirectory as u8);
62             if url_ref.is_null() {
63                 return None;
64             }
65             Some(TCFType::wrap_under_create_rule(url_ref))
66         }
67     }
68 
from_file_system_path(filePath: CFString, pathStyle: CFURLPathStyle, isDirectory: bool) -> CFURL69     pub fn from_file_system_path(filePath: CFString, pathStyle: CFURLPathStyle, isDirectory: bool) -> CFURL {
70         unsafe {
71             let url_ref = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, filePath.as_concrete_TypeRef(), pathStyle, isDirectory as u8);
72             TCFType::wrap_under_create_rule(url_ref)
73         }
74     }
75 
76     #[cfg(unix)]
to_path(&self) -> Option<PathBuf>77     pub fn to_path(&self) -> Option<PathBuf> {
78         // implementing this on Windows is more complicated because of the different OsStr representation
79         unsafe {
80             let mut buf = [0u8; PATH_MAX as usize];
81             let result = CFURLGetFileSystemRepresentation(self.0, true as Boolean, buf.as_mut_ptr(), buf.len() as CFIndex);
82             if result == false as Boolean {
83                 return None;
84             }
85             let len = strlen(buf.as_ptr() as *const c_char);
86             let path = OsStr::from_bytes(&buf[0..len]);
87             Some(PathBuf::from(path))
88         }
89     }
90 
get_string(&self) -> CFString91     pub fn get_string(&self) -> CFString {
92         unsafe {
93             TCFType::wrap_under_get_rule(CFURLGetString(self.0))
94         }
95     }
96 
get_file_system_path(&self, pathStyle: CFURLPathStyle) -> CFString97     pub fn get_file_system_path(&self, pathStyle: CFURLPathStyle) -> CFString {
98         unsafe {
99             TCFType::wrap_under_create_rule(CFURLCopyFileSystemPath(self.as_concrete_TypeRef(), pathStyle))
100         }
101     }
102 
absolute(&self) -> CFURL103     pub fn absolute(&self) -> CFURL {
104         unsafe {
105             TCFType::wrap_under_create_rule(CFURLCopyAbsoluteURL(self.as_concrete_TypeRef()))
106         }
107     }
108 }
109 
110 #[test]
file_url_from_path()111 fn file_url_from_path() {
112     let path = "/usr/local/foo/";
113     let cfstr_path = CFString::from_static_string(path);
114     let cfurl = CFURL::from_file_system_path(cfstr_path, kCFURLPOSIXPathStyle, true);
115     assert_eq!(cfurl.get_string().to_string(), "file:///usr/local/foo/");
116 }
117 
118 #[cfg(unix)]
119 #[test]
non_utf8()120 fn non_utf8() {
121     use std::ffi::OsStr;
122     let path = Path::new(OsStr::from_bytes(b"/\xC0/blame"));
123     let cfurl = CFURL::from_path(path, false).unwrap();
124     assert_eq!(cfurl.to_path().unwrap(), path);
125     let len = unsafe { CFURLGetBytes(cfurl.as_concrete_TypeRef(), ptr::null_mut(), 0) };
126     assert_eq!(len, 17);
127 }
128 
129 #[test]
absolute_file_url()130 fn absolute_file_url() {
131     use core_foundation_sys::url::CFURLCreateWithFileSystemPathRelativeToBase;
132     use std::path::PathBuf;
133 
134     let path = "/usr/local/foo";
135     let file = "bar";
136 
137     let cfstr_path = CFString::from_static_string(path);
138     let cfstr_file = CFString::from_static_string(file);
139     let cfurl_base = CFURL::from_file_system_path(cfstr_path, kCFURLPOSIXPathStyle, true);
140     let cfurl_relative: CFURL = unsafe {
141         let url_ref = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorDefault,
142             cfstr_file.as_concrete_TypeRef(),
143             kCFURLPOSIXPathStyle,
144             false as u8,
145             cfurl_base.as_concrete_TypeRef());
146         TCFType::wrap_under_create_rule(url_ref)
147     };
148 
149     let mut absolute_path = PathBuf::from(path);
150     absolute_path.push(file);
151 
152     assert_eq!(cfurl_relative.get_file_system_path(kCFURLPOSIXPathStyle).to_string(), file);
153     assert_eq!(cfurl_relative.absolute().get_file_system_path(kCFURLPOSIXPathStyle).to_string(),
154         absolute_path.to_str().unwrap());
155 }
156