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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
pub use dunce::canonicalize;
use std::path::Path;
#[cfg(all(unix, not(any(target_vendor = "apple", target_os = "android"))))]
fn attempt_dbus_call(path: &Path) -> bool {
use std::ffi::OsString;
use zbus::{blocking::Connection, dbus_proxy, Result};
#[dbus_proxy(
default_service = "org.freedesktop.FileManager1",
interface = "org.freedesktop.FileManager1",
default_path = "/org/freedesktop/FileManager1"
)]
trait FileManager {
fn show_folders(&self, uris: &[&str], startup_id: &str) -> Result<()>;
fn show_items(&self, uris: &[&str], startup_id: &str) -> Result<()>;
}
let con = match Connection::session() {
Ok(v) => v,
Err(_) => return false,
};
let proxy = match FileManagerProxyBlocking::new(&con) {
Ok(v) => v,
Err(_) => return false,
};
let mut uri = OsString::from("file://");
let f = match canonicalize(path.as_os_str()) {
Ok(v) => v,
Err(_) => return false,
};
uri.push(f);
let res = match path.is_dir() {
true => proxy.show_folders(&[&uri.to_string_lossy()], "test"),
false => proxy.show_items(&[&uri.to_string_lossy()], "test")
};
res.is_ok()
}
#[cfg(all(unix, not(any(target_vendor = "apple", target_os = "android"))))]
fn attempt_xdg_open(path: &Path) -> bool {
use std::ffi::OsString;
use std::process::Command;
let mut uri = OsString::from("file://");
let f = match canonicalize(path.as_os_str()) {
Ok(v) => v,
Err(_) => return false,
};
uri.push(f);
let res = Command::new("xdg-open")
.args([&*uri.to_string_lossy()])
.output();
res.is_ok()
}
#[cfg(target_os = "macos")]
#[link(name = "AppKit", kind = "framework")]
extern "C" {}
pub fn open<T: AsRef<Path>>(path: T) -> bool {
let path = path.as_ref();
cfg_if::cfg_if! {
if #[cfg(windows)] {
unsafe {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::UI::Shell::ShellExecuteW;
use windows_sys::Win32::UI::WindowsAndMessaging::SW_SHOW;
use windows_sys::Win32::Foundation::PWSTR;
let operation = ['o' as u16, 'p' as u16, 'e' as u16, 'n' as u16, 0x0000];
let mut file: Vec<u16> = path.as_os_str().encode_wide().collect();
file.push(0x0000);
let file: PWSTR = std::mem::transmute(file.as_ptr());
let operation: PWSTR = std::mem::transmute(operation.as_ptr());
let res = ShellExecuteW(0, operation, file, std::ptr::null_mut(), std::ptr::null_mut(), SW_SHOW as _);
res > 32
}
} else if #[cfg(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "android"))))] {
let mut flag = attempt_dbus_call(path);
if !flag {
flag = attempt_xdg_open(path);
}
flag
} else if #[cfg(target_os = "macos")] {
use std::os::unix::ffi::OsStrExt;
use std::os::raw::c_ulong;
use objc::class;
use objc::msg_send;
use objc::sel;
use objc::sel_impl;
use objc::runtime::Object;
const NS_UTF8_STRING_ENCODING: c_ulong = 4;
let f = match canonicalize(path.as_os_str()) {
Ok(v) => v,
Err(_) => return false
};
let isdir = path.is_dir();
unsafe {
let nsstring = class!(NSString);
let nsurl = class!(NSURL);
let nsarray = class!(NSArray);
let nsworkspace = class!(NSWorkspace);
let mut str: *mut Object = msg_send![nsstring, alloc];
str = msg_send![str,
initWithBytes: f.as_os_str().as_bytes().as_ptr()
length: f.as_os_str().len() as c_ulong
encoding: NS_UTF8_STRING_ENCODING
];
let mut url: *mut Object = msg_send![nsurl, alloc];
url = msg_send![url,
initFileURLWithPath: str
isDirectory: isdir
];
if isdir {
let workspace: *mut Object = msg_send![nsworkspace, sharedWorkspace];
let _: () = msg_send![workspace, openURL: url];
} else {
let arr: *mut Object = msg_send![nsarray, arrayWithObject: url];
let workspace: *mut Object = msg_send![nsworkspace, sharedWorkspace];
let _: () = msg_send![workspace, activateFileViewerSelectingURLs: arr];
}
let _: () = msg_send![url, release];
let _: () = msg_send![str, release];
true
}
} else {
false
}
}
}
pub fn hide<T: AsRef<Path>>(path: T) -> bool {
let path = path.as_ref();
if !path.exists() {
return false;
}
cfg_if::cfg_if! {
if #[cfg(unix)] {
use os_str_bytes::OsStrBytes;
use os_str_bytes::OsStringBytes;
use std::ffi::OsString;
use std::path::PathBuf;
if let Some(str) = path.file_name() {
let bytes = str.to_raw_bytes();
if bytes[0] == b'.' {
return true;
}
let mut vec = bytes.to_vec();
vec.insert(0, b'.');
let mut copy: PathBuf = path.into();
copy.set_file_name(OsString::from_raw_vec(vec).unwrap());
return std::fs::rename(path, copy).is_ok();
}
false
} else {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::SetFileAttributesW;
use windows_sys::Win32::Storage::FileSystem::GetFileAttributesW;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_HIDDEN;
use windows_sys::Win32::Storage::FileSystem::INVALID_FILE_ATTRIBUTES;
use windows_sys::Win32::Foundation::PWSTR;
let mut file: Vec<u16> = path.as_os_str().encode_wide().collect();
file.push(0x0000);
unsafe {
let file: PWSTR = std::mem::transmute(file.as_ptr());
let attrs = GetFileAttributesW(file);
if attrs == INVALID_FILE_ATTRIBUTES {
return false;
}
SetFileAttributesW(file, attrs | FILE_ATTRIBUTE_HIDDEN) != 0
}
}
}
}
pub fn unhide<T: AsRef<Path>>(path: T) -> bool {
let path = path.as_ref();
if !path.exists() {
return false;
}
cfg_if::cfg_if! {
if #[cfg(unix)] {
use os_str_bytes::OsStrBytes;
use os_str_bytes::OsStringBytes;
use std::ffi::OsString;
use std::path::PathBuf;
if let Some(str) = path.file_name() {
let bytes = str.to_raw_bytes();
if bytes[0] != b'.' {
return true;
}
let mut vec = bytes.to_vec();
vec.remove(0);
let mut copy: PathBuf = path.into();
copy.set_file_name(OsString::from_raw_vec(vec).unwrap());
return std::fs::rename(path, copy).is_ok();
}
false
} else {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::SetFileAttributesW;
use windows_sys::Win32::Storage::FileSystem::GetFileAttributesW;
use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_HIDDEN;
use windows_sys::Win32::Storage::FileSystem::INVALID_FILE_ATTRIBUTES;
use windows_sys::Win32::Foundation::PWSTR;
let mut file: Vec<u16> = path.as_os_str().encode_wide().collect();
file.push(0x0000);
unsafe {
let file: PWSTR = std::mem::transmute(file.as_ptr());
let attrs = GetFileAttributesW(file);
if attrs == INVALID_FILE_ATTRIBUTES {
return false;
}
SetFileAttributesW(file, attrs & !FILE_ATTRIBUTE_HIDDEN) != 0
}
}
}
}