bp3d_fs/utils/
hide.rs

1// Copyright (c) 2022, BlockProject 3D
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7//
8//     * Redistributions of source code must retain the above copyright notice,
9//       this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above copyright notice,
11//       this list of conditions and the following disclaimer in the documentation
12//       and/or other materials provided with the distribution.
13//     * Neither the name of BlockProject 3D nor the names of its contributors
14//       may be used to endorse or promote products derived from this software
15//       without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29use std::path::Path;
30
31/// Hides the given path in the current platform's file explorer.
32pub fn hide<T: AsRef<Path>>(path: T) -> bool {
33    let path = path.as_ref();
34    if !path.exists() {
35        return false;
36    }
37    cfg_if::cfg_if! {
38        if #[cfg(unix)] {
39            use os_str_bytes::OsStrBytes;
40            use os_str_bytes::OsStringBytes;
41            use std::ffi::OsString;
42            use std::path::PathBuf;
43            if let Some(str) = path.file_name() {
44                let bytes = str.to_raw_bytes();
45                if bytes[0] == b'.' {
46                    return true; //path is already hidden.
47                }
48                let mut vec = bytes.to_vec();
49                vec.insert(0, b'.');
50                let mut copy: PathBuf = path.into();
51                copy.set_file_name(OsString::from_raw_vec(vec).unwrap());
52                return std::fs::rename(path, copy).is_ok();
53            }
54            false //the path does not have a valid file name; can't do anything.
55        } else {
56            use std::os::windows::ffi::OsStrExt;
57            use windows_sys::Win32::Storage::FileSystem::SetFileAttributesW;
58            use windows_sys::Win32::Storage::FileSystem::GetFileAttributesW;
59            use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_HIDDEN;
60            use windows_sys::Win32::Storage::FileSystem::INVALID_FILE_ATTRIBUTES;
61            use windows_sys::Win32::Foundation::PWSTR;
62            let mut file: Vec<u16> = path.as_os_str().encode_wide().collect();
63            file.push(0x0000);
64            unsafe {
65                // Well windows-sys is badly designed it treats all strings as mutable
66                // even though the official MS docs uses constant strings
67                let file: PWSTR = std::mem::transmute(file.as_ptr());
68                let attrs = GetFileAttributesW(file);
69                if attrs == INVALID_FILE_ATTRIBUTES {
70                    return false;
71                }
72                SetFileAttributesW(file, attrs | FILE_ATTRIBUTE_HIDDEN) != 0
73            }
74        }
75    }
76}
77
78/// Un-hides the given path in the current platform's file explorer.
79pub fn unhide<T: AsRef<Path>>(path: T) -> bool {
80    let path = path.as_ref();
81    if !path.exists() {
82        return false;
83    }
84    cfg_if::cfg_if! {
85        if #[cfg(unix)] {
86            use os_str_bytes::OsStrBytes;
87            use os_str_bytes::OsStringBytes;
88            use std::ffi::OsString;
89            use std::path::PathBuf;
90            if let Some(str) = path.file_name() {
91                let bytes = str.to_raw_bytes();
92                if bytes[0] != b'.' {
93                    return true; //path is already visible.
94                }
95                let mut vec = bytes.to_vec();
96                vec.remove(0); //remove the '.' character from the file name.
97                let mut copy: PathBuf = path.into();
98                copy.set_file_name(OsString::from_raw_vec(vec).unwrap());
99                return std::fs::rename(path, copy).is_ok();
100            }
101            false //the path does not have a valid file name; can't do anything.
102        } else {
103            use std::os::windows::ffi::OsStrExt;
104            use windows_sys::Win32::Storage::FileSystem::SetFileAttributesW;
105            use windows_sys::Win32::Storage::FileSystem::GetFileAttributesW;
106            use windows_sys::Win32::Storage::FileSystem::FILE_ATTRIBUTE_HIDDEN;
107            use windows_sys::Win32::Storage::FileSystem::INVALID_FILE_ATTRIBUTES;
108            use windows_sys::Win32::Foundation::PWSTR;
109            let mut file: Vec<u16> = path.as_os_str().encode_wide().collect();
110            file.push(0x0000);
111            unsafe {
112                // Well windows-sys is badly designed it treats all strings as mutable
113                // even though the official MS docs uses constant strings
114                let file: PWSTR = std::mem::transmute(file.as_ptr());
115                let attrs = GetFileAttributesW(file);
116                if attrs == INVALID_FILE_ATTRIBUTES {
117                    return false;
118                }
119                SetFileAttributesW(file, attrs & !FILE_ATTRIBUTE_HIDDEN) != 0
120            }
121        }
122    }
123}