This repository was archived by the owner on Jul 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathapple.rs
More file actions
117 lines (112 loc) · 4.23 KB
/
apple.rs
File metadata and controls
117 lines (112 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use objc::rc::autoreleasepool;
use objc::runtime::Object;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::path::PathBuf;
// We need to link to Foundation to access the NSFileManager class
#[link(name = "Foundation", kind = "framework")]
extern "C" {}
/// Type of directory to lookup from macOS/iOS
#[repr(u64)]
pub enum SearchPathDirectory {
/// Applications directory, depending on domain. /Applications or ~/Applications typically
Application = 1,
AdminApplication = 4,
/// Library folder, can be /Library (system) or ~/Library (user)
Library = 5,
/// Location of usere's home directories, typically /Users
Users = 7,
/// Documentation, not sure if used...
Documentation = 8,
/// Documents folder, typically ~/Documents
Documents = 9,
AutosavedInformation = 11,
/// User's desktop folder, typically ~/Desktop
Desktop = 12,
/// Caches folder, Library/Caches
Caches = 13,
/// Applicatino support, Library/Application Support
///
/// Typical home of non-userdefaults app data & settings
ApplicationSupport = 14,
/// Downloads folder, ~/Downloads
Downloads = 15,
/// Movies folder, ~/Movies
Movies = 17,
/// Music folder, ~/Music
Music = 18,
/// Pictures folder, ~/Pictures
Pictures = 19,
/// PPDs folder, Library/Printers/PPDs
PrinterDescription = 20,
/// Public folder, ~/Public
SharedPublic = 21,
/// Preference Panes, Library/PreferencePanes
PreferencePanes = 22,
/// User scripts folder for calling application, ~/Library/Application Scripts/code-signing-id
ApplicationScripts = 23,
/// Trash folder
Trash = 102,
}
/// Domain for path to return, dirs currently mostly deals with user dirs so likely want UserDomain
#[repr(u64)]
pub enum SearchPathDomainMask {
/// Looks up directory in user's domain, so ~
UserDomain = 1,
/// Local system domain, which is folders typically found in /Library
LocalDomain = 2,
/// Publically available locations on the local network
NetworkDomain = 4,
/// Read only system locations, /System (may be completely unavailable on newer systems?)
SystemDomain = 8,
/// Looks up directories in all of the current domains and future ones apple may add
AllDomains = 65535,
}
/// Returns first path found on macOS/iOS systems given the requested type of path, within given domain
///
/// Even if a path is returned, it may not exist yet and require creation
pub fn path_for_dir(dir: SearchPathDirectory, domain: SearchPathDomainMask) -> Option<PathBuf> {
let mut result = None;
autoreleasepool(|| {
let cls = class!(NSFileManager);
unsafe {
let obj: *mut Object = msg_send![cls, defaultManager];
let url: *mut Object = msg_send![obj, URLForDirectory:dir inDomain:domain as u64 appropriateForURL:0 create:false error:0];
if !url.is_null() {
let path: *mut Object = msg_send![url, path];
let s: *const c_char = msg_send![path, UTF8String];
let c_str = CStr::from_ptr(s);
match c_str.to_str() {
Err(error) => {
println!("Error getting home dir string: {}", error);
}
Ok(string) => result = Some(PathBuf::from(string.to_owned())),
};
} else {
println!("Failed to get dir");
}
}
});
result
}
/// Returns user's home directory, or sandbox if called within sandboxed app
pub fn home_dir() -> Option<PathBuf> {
unsafe {
let mut result = None;
autoreleasepool(|| {
let cls = class!(NSFileManager);
let obj: *mut Object = msg_send![cls, defaultManager];
let url: *mut Object = msg_send![obj, homeDirectoryForCurrentUser];
let path: *mut Object = msg_send![url, path];
let s: *const c_char = msg_send![path, UTF8String];
let c_str = CStr::from_ptr(s);
match c_str.to_str() {
Err(error) => {
println!("Error getting home dir string: {}", error);
}
Ok(string) => result = Some(PathBuf::from(string.to_owned())),
};
});
result
}
}