Blender  V3.3
storage_apple.mm
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
10 #import <Foundation/Foundation.h>
11 #include <string>
12 #include <sys/xattr.h>
13 
14 #include "BLI_fileops.h"
15 #include "BLI_path_util.h"
16 
17 /* Extended file attribute used by OneDrive to mark placeholder files. */
18 static const char *ONEDRIVE_RECALLONOPEN_ATTRIBUTE = "com.microsoft.OneDrive.RecallOnOpen";
19 
24 /* False alarm by clang-tidy: #getFileSystemRepresentation changes the return value argument. */
25 /* NOLINTNEXTLINE: readability-non-const-parameter. */
26 bool BLI_file_alias_target(const char *filepath, char r_targetpath[FILE_MAXDIR])
27 {
28  /* clang-format off */
29  @autoreleasepool {
30  /* clang-format on */
31  NSError *error = nil;
32  NSURL *shortcutURL = [[NSURL alloc] initFileURLWithFileSystemRepresentation:filepath
33  isDirectory:NO
34  relativeToURL:nil];
35  const NSURL *targetURL = [NSURL URLByResolvingAliasFileAtURL:shortcutURL
36  options:NSURLBookmarkResolutionWithoutUI
37  error:&error];
38  const BOOL isSame = [shortcutURL isEqual:targetURL] and
39  ([[[shortcutURL path] stringByStandardizingPath]
40  isEqualToString:[[targetURL path] stringByStandardizingPath]]);
41 
42  if (targetURL == nil) {
43  return false;
44  }
45  if (isSame) {
46  [targetURL getFileSystemRepresentation:r_targetpath maxLength:FILE_MAXDIR];
47  return false;
48  }
49  /* Note that the if-condition may also change the value of `r_targetpath`. */
50  if (![targetURL getFileSystemRepresentation:r_targetpath maxLength:FILE_MAXDIR]) {
51  return false;
52  }
53  }
54 
55  return true;
56 }
57 
65 static bool find_attribute(const std::string &attributes, const char *search_attribute)
66 {
67  /* Attributes is a list of consecutive null-terminated strings. */
68  const char *end = attributes.data() + attributes.size();
69  for (const char *item = attributes.data(); item < end; item += strlen(item) + 1) {
70  if (STREQ(item, search_attribute)) {
71  return true;
72  }
73  }
74 
75  return false;
76 }
77 
84 static bool test_onedrive_file_is_placeholder(const char *path)
85 {
86  /* NOTE: Currently only checking for the "com.microsoft.OneDrive.RecallOnOpen" extended file
87  * attribute. In theory this attribute can also be set on files that aren't located inside a
88  * OneDrive folder. Maybe additional checks are required? */
89 
90  /* Get extended file attributes */
91  ssize_t size = listxattr(path, nullptr, 0, XATTR_NOFOLLOW);
92  if (size < 1) {
93  return false;
94  }
95 
96  std::string attributes(size, '\0');
97  size = listxattr(path, attributes.data(), size, XATTR_NOFOLLOW);
98  /* In case listxattr() has failed the second time it's called. */
99  if (size < 1) {
100  return false;
101  }
102 
103  /* Check for presence of 'com.microsoft.OneDrive.RecallOnOpen' attribute. */
105 }
106 
113 static bool test_file_is_offline(const char *path)
114 {
115  /* Logic for additional cloud storage providers could be added in the future. */
117 }
118 
120 {
121  int ret = 0;
122 
123  /* clang-format off */
124  @autoreleasepool {
125  /* clang-format on */
126  const NSURL *fileURL = [[NSURL alloc] initFileURLWithFileSystemRepresentation:path
127  isDirectory:NO
128  relativeToURL:nil];
129 
130  /* Querying NSURLIsReadableKey and NSURLIsWritableKey keys for OneDrive placeholder files
131  * triggers their unwanted download. */
132  NSArray *resourceKeys = nullptr;
133  const bool is_offline = test_file_is_offline(path);
134 
135  if (is_offline) {
136  resourceKeys = @[ NSURLIsAliasFileKey, NSURLIsHiddenKey ];
137  }
138  else {
139  resourceKeys =
140  @[ NSURLIsAliasFileKey, NSURLIsHiddenKey, NSURLIsReadableKey, NSURLIsWritableKey ];
141  }
142 
143  const NSDictionary *resourceKeyValues = [fileURL resourceValuesForKeys:resourceKeys error:nil];
144 
145  const bool is_alias = [resourceKeyValues[(void)(@"@%"), NSURLIsAliasFileKey] boolValue];
146  const bool is_hidden = [resourceKeyValues[(void)(@"@%"), NSURLIsHiddenKey] boolValue];
147  const bool is_readable = is_offline ||
148  [resourceKeyValues[(void)(@"@%"), NSURLIsReadableKey] boolValue];
149  const bool is_writable = is_offline ||
150  [resourceKeyValues[(void)(@"@%"), NSURLIsWritableKey] boolValue];
151 
152  if (is_alias) {
153  ret |= FILE_ATTR_ALIAS;
154  }
155  if (is_hidden) {
157  }
158  if (is_readable && !is_writable) {
160  }
161  if (!is_readable) {
163  }
164  if (is_offline) {
166  }
167  }
168 
169  return (eFileAttributes)ret;
170 }
171 
172 const char *BLI_expand_tilde(const char *path_with_tilde)
173 {
174  static char path_expanded[FILE_MAX];
175  @autoreleasepool {
176  const NSString *const str_with_tilde = [[NSString alloc] initWithCString:path_with_tilde
177  encoding:NSUTF8StringEncoding];
178  if (!str_with_tilde) {
179  return nullptr;
180  }
181  const NSString *const str_expanded = [str_with_tilde stringByExpandingTildeInPath];
182  [str_expanded getCString:path_expanded
183  maxLength:sizeof(path_expanded)
184  encoding:NSUTF8StringEncoding];
185  }
186  return path_expanded;
187 }
File and directory operations.
eFileAttributes
Definition: BLI_fileops.h:86
@ FILE_ATTR_ALIAS
Definition: BLI_fileops.h:97
@ FILE_ATTR_HIDDEN
Definition: BLI_fileops.h:88
@ FILE_ATTR_READONLY
Definition: BLI_fileops.h:87
@ FILE_ATTR_SYSTEM
Definition: BLI_fileops.h:89
@ FILE_ATTR_OFFLINE
Definition: BLI_fileops.h:96
#define FILE_MAX
#define FILE_MAXDIR
#define STREQ(a, b)
SSIZE_T ssize_t
Definition: BLI_winstuff.h:71
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
CCL_NAMESPACE_BEGIN struct Options options
SyclQueue void void size_t num_bytes void
static void error(const char *str)
Definition: meshlaplacian.c:51
return ret
eFileAttributes BLI_file_attributes(const char *path)
static const char * ONEDRIVE_RECALLONOPEN_ATTRIBUTE
bool BLI_file_alias_target(const char *filepath, char r_targetpath[FILE_MAXDIR])
static bool test_onedrive_file_is_placeholder(const char *path)
static bool test_file_is_offline(const char *path)
const char * BLI_expand_tilde(const char *path_with_tilde)
static bool find_attribute(const std::string &attributes, const char *search_attribute)