Blender V5.3
storage_apple.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#import <Foundation/Foundation.h>
12#include <string>
13#include <sys/xattr.h>
14
15#include "BLI_fileops.hh"
16#include "BLI_path_utils.hh"
17#include "BLI_string.hh"
18
19namespace blender {
20
21/* Extended file attribute used by OneDrive to mark placeholder files. */
22static const char *ONEDRIVE_RECALLONOPEN_ATTRIBUTE = "com.microsoft.OneDrive.RecallOnOpen";
23
24bool BLI_file_alias_target(const char *filepath,
25 /* False alarm by clang-tidy: #getFileSystemRepresentation
26 * changes the return value argument. */
27 /* NOLINTNEXTLINE: readability-non-const-parameter. */
28 char r_targetpath[FILE_MAXDIR])
29{
30 /* clang-format off */
31 @autoreleasepool {
32 /* clang-format on */
33 NSError *error = nil;
34 NSURL *shortcutURL = [[NSURL alloc] initFileURLWithFileSystemRepresentation:filepath
35 isDirectory:NO
36 relativeToURL:nil];
37
38 /* Note, NSURLBookmarkResolutionWithoutMounting keeps blender from crashing when an alias can't
39 * be mounted */
40 NSURL *targetURL = [NSURL URLByResolvingAliasFileAtURL:shortcutURL
41 options:NSURLBookmarkResolutionWithoutUI |
42 NSURLBookmarkResolutionWithoutMounting
43 error:&error];
44 const BOOL isSame = [shortcutURL isEqual:targetURL] and
45 ([[[shortcutURL path] stringByStandardizingPath]
46 isEqualToString:[[targetURL path] stringByStandardizingPath]]);
47
48 if (targetURL == nil) {
49 return false;
50 }
51 if (isSame) {
52 [targetURL getFileSystemRepresentation:r_targetpath maxLength:FILE_MAXDIR];
53 return false;
54 }
55 /* Note that the `if` condition may also change the value of `r_targetpath`. */
56 if (![targetURL getFileSystemRepresentation:r_targetpath maxLength:FILE_MAXDIR]) {
57 return false;
58 }
59 }
60
61 return true;
62}
63
71static bool find_attribute(const std::string &attributes, const char *search_attribute)
72{
73 /* Attributes is a list of consecutive null-terminated strings. */
74 const char *end = attributes.data() + attributes.size();
75 for (const char *item = attributes.data(); item < end; item += strlen(item) + 1) {
76 if (STREQ(item, search_attribute)) {
77 return true;
78 }
79 }
80
81 return false;
82}
83
90static bool test_onedrive_file_is_placeholder(const char *path)
91{
92 /* NOTE: Currently only checking for the "com.microsoft.OneDrive.RecallOnOpen" extended file
93 * attribute. In theory this attribute can also be set on files that aren't located inside a
94 * OneDrive folder. Maybe additional checks are required? */
95
96 /* Get extended file attributes */
97 ssize_t size = listxattr(path, nullptr, 0, XATTR_NOFOLLOW);
98 if (size < 1) {
99 return false;
100 }
101
102 std::string attributes(size, '\0');
103 size = listxattr(path, attributes.data(), size, XATTR_NOFOLLOW);
104 /* In case listxattr() has failed the second time it's called. */
105 if (size < 1) {
106 return false;
107 }
108
109 /* Check for presence of 'com.microsoft.OneDrive.RecallOnOpen' attribute. */
111}
112
119static bool test_file_is_offline(const char *path)
120{
121 /* Logic for additional cloud storage providers could be added in the future. */
123}
124
125eFileAttributes BLI_file_attributes(const char *path)
126{
127 int ret = 0;
128
129 /* clang-format off */
130 @autoreleasepool {
131 /* clang-format on */
132 NSURL *fileURL = [[[NSURL alloc] initFileURLWithFileSystemRepresentation:path
133 isDirectory:NO
134 relativeToURL:nil] autorelease];
135
136 /* Querying NSURLIsReadableKey and NSURLIsWritableKey keys for OneDrive placeholder files
137 * triggers their unwanted download. */
138 NSArray *resourceKeys = nullptr;
139 const bool is_offline = test_file_is_offline(path);
140
141 if (is_offline) {
142 resourceKeys = @[ NSURLIsSymbolicLinkKey, NSURLIsAliasFileKey, NSURLIsHiddenKey ];
143 }
144 else {
145 resourceKeys = @[
146 NSURLIsSymbolicLinkKey,
147 NSURLIsAliasFileKey,
148 NSURLIsHiddenKey,
149 NSURLIsReadableKey,
150 NSURLIsWritableKey
151 ];
152 }
153
154 NSDictionary *resourceKeyValues = [fileURL resourceValuesForKeys:resourceKeys error:nil];
155
156 const bool is_symlink = [resourceKeyValues[NSURLIsSymbolicLinkKey] boolValue];
157 const bool is_alias = [resourceKeyValues[NSURLIsAliasFileKey] boolValue] && !is_symlink;
158 const bool is_hidden = [resourceKeyValues[NSURLIsHiddenKey] boolValue];
159 const bool is_readable = is_offline || [resourceKeyValues[NSURLIsReadableKey] boolValue];
160 const bool is_writable = is_offline || [resourceKeyValues[NSURLIsWritableKey] boolValue];
161
162 if (is_symlink) {
164 }
165 if (is_alias) {
167 }
168 if (is_hidden) {
170 }
171 if (is_readable && !is_writable) {
173 }
174 if (!is_readable) {
176 }
177 if (is_offline) {
179 }
180 }
181
182 return (eFileAttributes)ret;
183}
184
185char *BLI_current_working_dir(char *dir, const size_t maxncpy)
186{
187 /* Can't just copy to the *dir pointer, as [path getCString gets grumpy. */
188 char path_expanded[PATH_MAX];
189 @autoreleasepool {
190 NSString *path = [[NSFileManager defaultManager] currentDirectoryPath];
191 const size_t length = maxncpy > PATH_MAX ? PATH_MAX : maxncpy;
192 [path getCString:path_expanded maxLength:length encoding:NSUTF8StringEncoding];
193 BLI_strncpy(dir, path_expanded, maxncpy);
194 return dir;
195 }
196}
197
198bool BLI_change_working_dir(const char *dir)
199{
200 @autoreleasepool {
201 NSString *path = [[NSString alloc] initWithUTF8String:dir];
202 if ([[NSFileManager defaultManager] changeCurrentDirectoryPath:path] == YES) {
203 return true;
204 }
205 return false;
206 }
207}
208
209} // namespace blender
File and directory operations.
#define PATH_MAX
#define FILE_MAXDIR
#define STREQ(a, b)
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
CCL_NAMESPACE_BEGIN struct Options options
BMesh Iterators.
bool BLI_change_working_dir(const char *dir)
Definition storage.cc:65
static bool test_file_is_offline(const char *path)
eFileAttributes BLI_file_attributes(const char *path)
Definition storage.cc:239
@ FILE_ATTR_HIDDEN
@ FILE_ATTR_READONLY
@ FILE_ATTR_OFFLINE
@ FILE_ATTR_SYMLINK
@ FILE_ATTR_ALIAS
@ FILE_ATTR_SYSTEM
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static const char * ONEDRIVE_RECALLONOPEN_ATTRIBUTE
static bool find_attribute(const std::string &attributes, const char *search_attribute)
static bool test_onedrive_file_is_placeholder(const char *path)
bool BLI_file_alias_target(const char *filepath, char r_targetpath[768]) ATTR_WARN_UNUSED_RESULT
char * BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:83