| 1 |
#![allow(non_upper_case_globals, non_camel_case_types)] |
| 2 |
|
| 3 |
extern crate libc; |
| 4 |
|
| 5 |
use std::ffi::CString; |
| 6 |
use std::ptr; |
| 7 |
use std::str; |
| 8 |
|
| 9 |
pub type Boolean = std::os::raw::c_uchar; |
| 10 |
|
| 11 |
pub type CFRef = *mut ::std::os::raw::c_void; |
| 12 |
|
| 13 |
pub type CFIndex = ::std::os::raw::c_long; |
| 14 |
pub type CFTimeInterval = f64; |
| 15 |
|
| 16 |
#[doc(hidden)] |
| 17 |
pub enum CFError {} |
| 18 |
|
| 19 |
pub type CFAllocatorRef = CFRef; |
| 20 |
pub type CFMutableArrayRef = CFRef; |
| 21 |
pub type CFURLRef = CFRef; |
| 22 |
pub type CFErrorRef = *mut CFError; |
| 23 |
pub type CFStringRef = CFRef; |
| 24 |
pub type CFRunLoopRef = CFRef; |
| 25 |
|
| 26 |
pub const NULL: CFRef = 0 as CFRef; |
| 27 |
pub const NULL_REF_PTR: *mut CFRef = 0 as *mut CFRef; |
| 28 |
|
| 29 |
pub type CFAllocatorRetainCallBack = |
| 30 |
extern "C" fn(*const std::os::raw::c_void) -> *const std::os::raw::c_void; |
| 31 |
pub type CFAllocatorReleaseCallBack = extern "C" fn(*const std::os::raw::c_void); |
| 32 |
pub type CFAllocatorCopyDescriptionCallBack = |
| 33 |
extern "C" fn(*const std::os::raw::c_void) -> *const CFStringRef; |
| 34 |
|
| 35 |
pub type CFURLPathStyle = CFIndex; |
| 36 |
|
| 37 |
pub const kCFAllocatorDefault: CFAllocatorRef = NULL; |
| 38 |
pub const kCFURLPOSIXPathStyle: CFURLPathStyle = 0; |
| 39 |
pub const kCFURLHFSPathStyle: CFURLPathStyle = 1; |
| 40 |
pub const kCFURLWindowsPathStyle: CFURLPathStyle = 2; |
| 41 |
|
| 42 |
pub const kCFStringEncodingUTF8: CFStringEncoding = 0x08000100; |
| 43 |
pub type CFStringEncoding = u32; |
| 44 |
|
| 45 |
pub const kCFCompareEqualTo: CFIndex = 0; |
| 46 |
pub type CFComparisonResult = CFIndex; |
| 47 |
|
| 48 |
|
| 49 |
pub const kCFCompareCaseInsensitive: CFStringCompareFlags = 1; |
| 50 |
pub type CFStringCompareFlags = std::os::raw::c_ulong; |
| 51 |
|
| 52 |
pub type CFArrayRetainCallBack = |
| 53 |
extern "C" fn(CFAllocatorRef, *const std::os::raw::c_void) -> *const std::os::raw::c_void; |
| 54 |
pub type CFArrayReleaseCallBack = extern "C" fn(CFAllocatorRef, *const std::os::raw::c_void); |
| 55 |
pub type CFArrayCopyDescriptionCallBack = extern "C" fn(*const std::os::raw::c_void) -> CFStringRef; |
| 56 |
pub type CFArrayEqualCallBack = |
| 57 |
extern "C" fn(*const std::os::raw::c_void, *const std::os::raw::c_void) -> Boolean; |
| 58 |
|
| 59 |
#[repr(C)] |
| 60 |
pub struct CFArrayCallBacks { |
| 61 |
version: CFIndex, |
| 62 |
retain: Option<CFArrayRetainCallBack>, |
| 63 |
release: Option<CFArrayReleaseCallBack>, |
| 64 |
cp: Option<CFArrayCopyDescriptionCallBack>, |
| 65 |
equal: Option<CFArrayEqualCallBack>, |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
#[link(name = "CoreServices", kind = "framework")] |
| 70 |
extern "C" { |
| 71 |
pub static kCFTypeArrayCallBacks: CFArrayCallBacks; |
| 72 |
pub static kCFRunLoopDefaultMode: CFStringRef; |
| 73 |
|
| 74 |
pub fn CFRelease(res: CFRef); |
| 75 |
pub fn CFShow(res: CFRef); |
| 76 |
pub fn CFCopyDescription(cf: CFRef) -> CFStringRef; |
| 77 |
|
| 78 |
pub fn CFRunLoopRun(); |
| 79 |
pub fn CFRunLoopStop(run_loop: CFRunLoopRef); |
| 80 |
pub fn CFRunLoopGetCurrent() -> CFRunLoopRef; |
| 81 |
|
| 82 |
pub fn CFArrayCreateMutable( |
| 83 |
allocator: CFRef, |
| 84 |
capacity: CFIndex, |
| 85 |
callbacks: *const CFArrayCallBacks, |
| 86 |
) -> CFMutableArrayRef; |
| 87 |
pub fn CFArrayInsertValueAtIndex(arr: CFMutableArrayRef, position: CFIndex, element: CFRef); |
| 88 |
pub fn CFArrayAppendValue(aff: CFMutableArrayRef, element: CFRef); |
| 89 |
pub fn CFArrayGetCount(arr: CFMutableArrayRef) -> CFIndex; |
| 90 |
pub fn CFArrayGetValueAtIndex(arr: CFMutableArrayRef, index: CFIndex) -> CFRef; |
| 91 |
|
| 92 |
pub fn CFURLCreateFileReferenceURL( |
| 93 |
allocator: CFRef, |
| 94 |
url: CFURLRef, |
| 95 |
err: *mut CFErrorRef, |
| 96 |
) -> CFURLRef; |
| 97 |
pub fn CFURLCreateFilePathURL( |
| 98 |
allocator: CFRef, |
| 99 |
url: CFURLRef, |
| 100 |
err: *mut CFErrorRef, |
| 101 |
) -> CFURLRef; |
| 102 |
pub fn CFURLCreateFromFileSystemRepresentation( |
| 103 |
allocator: CFRef, |
| 104 |
path: *const ::std::os::raw::c_char, |
| 105 |
len: CFIndex, |
| 106 |
is_directory: bool, |
| 107 |
) -> CFURLRef; |
| 108 |
pub fn CFURLCopyAbsoluteURL(res: CFURLRef) -> CFURLRef; |
| 109 |
pub fn CFURLCopyLastPathComponent(res: CFURLRef) -> CFStringRef; |
| 110 |
pub fn CFURLCreateCopyDeletingLastPathComponent(allocator: CFRef, url: CFURLRef) -> CFURLRef; |
| 111 |
pub fn CFURLCreateCopyAppendingPathComponent( |
| 112 |
allocation: CFRef, |
| 113 |
url: CFURLRef, |
| 114 |
path: CFStringRef, |
| 115 |
is_directory: bool, |
| 116 |
) -> CFURLRef; |
| 117 |
pub fn CFURLCopyFileSystemPath(anUrl: CFURLRef, path_style: CFURLPathStyle) -> CFStringRef; |
| 118 |
|
| 119 |
pub fn CFURLResourceIsReachable(res: CFURLRef, err: *mut CFErrorRef) -> bool; |
| 120 |
|
| 121 |
pub fn CFShowStr(str: CFStringRef); |
| 122 |
pub fn CFStringGetCString( |
| 123 |
theString: CFStringRef, |
| 124 |
buffer: *mut ::std::os::raw::c_char, |
| 125 |
buffer_size: CFIndex, |
| 126 |
encoding: CFStringEncoding, |
| 127 |
) -> bool; |
| 128 |
pub fn CFStringGetCStringPtr( |
| 129 |
theString: CFStringRef, |
| 130 |
encoding: CFStringEncoding, |
| 131 |
) -> *const ::std::os::raw::c_char; |
| 132 |
pub fn CFStringCreateWithCString( |
| 133 |
alloc: CFRef, |
| 134 |
source: *const ::std::os::raw::c_char, |
| 135 |
encoding: CFStringEncoding, |
| 136 |
) -> CFStringRef; |
| 137 |
|
| 138 |
pub fn CFStringCompare( |
| 139 |
theString1: CFStringRef, |
| 140 |
theString2: CFStringRef, |
| 141 |
compareOptions: CFStringCompareFlags, |
| 142 |
) -> CFComparisonResult; |
| 143 |
pub fn CFArrayRemoveValueAtIndex(theArray: CFMutableArrayRef, idx: CFIndex); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
pub unsafe fn str_path_to_cfstring_ref(source: &str, err: &mut CFErrorRef) -> CFStringRef { |
| 148 |
let c_path = CString::new(source).unwrap(); |
| 149 |
let c_len = libc::strlen(c_path.as_ptr()); |
| 150 |
let mut url = CFURLCreateFromFileSystemRepresentation( |
| 151 |
kCFAllocatorDefault, |
| 152 |
c_path.as_ptr(), |
| 153 |
c_len as CFIndex, |
| 154 |
false, |
| 155 |
); |
| 156 |
let mut placeholder = CFURLCopyAbsoluteURL(url); |
| 157 |
CFRelease(url); |
| 158 |
|
| 159 |
let imaginary: CFRef = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); |
| 160 |
|
| 161 |
while !CFURLResourceIsReachable(placeholder, ptr::null_mut()) { |
| 162 |
let child = CFURLCopyLastPathComponent(placeholder); |
| 163 |
CFArrayInsertValueAtIndex(imaginary, 0, child); |
| 164 |
CFRelease(child); |
| 165 |
|
| 166 |
url = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, placeholder); |
| 167 |
CFRelease(placeholder); |
| 168 |
placeholder = url; |
| 169 |
} |
| 170 |
|
| 171 |
url = CFURLCreateFileReferenceURL(kCFAllocatorDefault, placeholder, err); |
| 172 |
if !err.is_null() { |
| 173 |
return ptr::null_mut(); |
| 174 |
} |
| 175 |
|
| 176 |
CFRelease(placeholder); |
| 177 |
placeholder = CFURLCreateFilePathURL(kCFAllocatorDefault, url, err); |
| 178 |
if !err.is_null() { |
| 179 |
return ptr::null_mut(); |
| 180 |
} |
| 181 |
|
| 182 |
CFRelease(url); |
| 183 |
|
| 184 |
if imaginary != kCFAllocatorDefault { |
| 185 |
let mut count = 0; |
| 186 |
while count < CFArrayGetCount(imaginary) { |
| 187 |
let component = CFArrayGetValueAtIndex(imaginary, count); |
| 188 |
url = CFURLCreateCopyAppendingPathComponent( |
| 189 |
kCFAllocatorDefault, |
| 190 |
placeholder, |
| 191 |
component, |
| 192 |
false, |
| 193 |
); |
| 194 |
CFRelease(placeholder); |
| 195 |
placeholder = url; |
| 196 |
count += 1; |
| 197 |
} |
| 198 |
CFRelease(imaginary); |
| 199 |
} |
| 200 |
|
| 201 |
let cf_path = CFURLCopyFileSystemPath(placeholder, kCFURLPOSIXPathStyle); |
| 202 |
CFRelease(placeholder); |
| 203 |
cf_path |
| 204 |
} |