Debian Codegraph @

platform/macos/dir_access_macos.mm

raw
1 /**************************************************************************/
2 /* dir_access_macos.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "dir_access_macos.h"
32
33 #if defined(UNIX_ENABLED)
34
35 #include "core/config/project_settings.h"
36
37 #include <sys/mount.h>
38 #include <cerrno>
39
40 #import <AppKit/NSWorkspace.h>
41 #import <Foundation/Foundation.h>
42
43 String DirAccessMacOS::get_filesystem_type() const {
44 struct statfs fs;
45 if (statfs(current_dir.utf8().get_data(), &fs) != 0) {
46 return "";
47 }
48 return String::utf8(fs.f_fstypename).to_upper();
49 }
50
51 String DirAccessMacOS::fix_unicode_name(const char *p_name) const {
52 String fname;
53 if (p_name != nullptr) {
54 NSString *nsstr = [[NSString stringWithUTF8String:p_name] precomposedStringWithCanonicalMapping];
55 fname.append_utf8([nsstr UTF8String]);
56 }
57
58 return fname;
59 }
60
61 int DirAccessMacOS::get_drive_count() {
62 NSArray *res_keys = [NSArray arrayWithObjects:NSURLVolumeURLKey, NSURLIsSystemImmutableKey, nil];
63 NSArray *vols = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:res_keys options:NSVolumeEnumerationSkipHiddenVolumes];
64
65 return [vols count];
66 }
67
68 String DirAccessMacOS::get_drive(int p_drive) {
69 NSArray *res_keys = [NSArray arrayWithObjects:NSURLVolumeURLKey, NSURLIsSystemImmutableKey, nil];
70 NSArray *vols = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:res_keys options:NSVolumeEnumerationSkipHiddenVolumes];
71 int count = [vols count];
72
73 ERR_FAIL_INDEX_V(p_drive, count, "");
74
75 String volname;
76 NSString *path = [vols[p_drive] path];
77
78 volname.append_utf8([path UTF8String]);
79
80 return volname;
81 }
82
83 bool DirAccessMacOS::is_hidden(const String &p_name) {
84 String f = get_current_dir().path_join(p_name);
85 NSURL *url = [NSURL fileURLWithPath:@(f.utf8().get_data())];
86 NSNumber *hidden = nil;
87 if (![url getResourceValue:&hidden forKey:NSURLIsHiddenKey error:nil]) {
88 return DirAccessUnix::is_hidden(p_name);
89 }
90 return [hidden boolValue];
91 }
92
93 bool DirAccessMacOS::is_case_sensitive(const String &p_path) const {
94 String f = p_path;
95 if (!f.is_absolute_path()) {
96 f = get_current_dir().path_join(f);
97 }
98 f = fix_path(f);
99
100 NSURL *url = [NSURL fileURLWithPath:@(f.utf8().get_data())];
101 NSNumber *cs = nil;
102 if (![url getResourceValue:&cs forKey:NSURLVolumeSupportsCaseSensitiveNamesKey error:nil]) {
103 return false;
104 }
105 return [cs boolValue];
106 }
107
108 bool DirAccessMacOS::is_bundle(const String &p_file) const {
109 String f = p_file;
110 if (!f.is_absolute_path()) {
111 f = get_current_dir().path_join(f);
112 }
113 f = fix_path(f);
114
115 return [[NSWorkspace sharedWorkspace] isFilePackageAtPath:[NSString stringWithUTF8String:f.utf8().get_data()]];
116 }
117
118 #endif // UNIX_ENABLED