|
23 | 23 | const std::string kNotDetermined{"not determined"};
|
24 | 24 | const std::string kLimited{"limited"};
|
25 | 25 |
|
| 26 | +std::string CheckFileAccessLevel(NSString *path) { |
| 27 | + int fd = open([path cStringUsingEncoding:kCFStringEncodingUTF8], O_RDONLY); |
| 28 | + if (fd != -1) { |
| 29 | + close(fd); |
| 30 | + return kAuthorized; |
| 31 | + } |
| 32 | + |
| 33 | + if (errno == ENOENT) |
| 34 | + return kNotDetermined; |
| 35 | + |
| 36 | + if (errno == EPERM || errno == EACCES) |
| 37 | + return kDenied; |
| 38 | + |
| 39 | + return kNotDetermined; |
| 40 | +} |
| 41 | + |
26 | 42 | PHAccessLevel GetPHAccessLevel(const std::string &type)
|
27 | 43 | API_AVAILABLE(macosx(10.16)) {
|
28 | 44 | return type == "read-write" ? PHAccessLevelReadWrite : PHAccessLevelAddOnly;
|
@@ -247,25 +263,27 @@ bool HasOpenSystemPreferencesDialog() {
|
247 | 263 |
|
248 | 264 | // Returns a status indicating whether the user has Full Disk Access.
|
249 | 265 | std::string FDAAuthStatus() {
|
250 |
| - std::string auth_status = kNotDetermined; |
251 |
| - NSString *path; |
252 | 266 | NSString *home_folder = GetUserHomeFolderPath();
|
| 267 | + NSMutableArray<NSString *> *files = [[NSMutableArray alloc] |
| 268 | + initWithObjects:[home_folder stringByAppendingPathComponent: |
| 269 | + @"Library/Safari/Bookmarks.plist"], |
| 270 | + @"/Library/Application Support/com.apple.TCC/TCC.db", |
| 271 | + @"/Library/Preferences/com.apple.TimeMachine.plist", nil]; |
253 | 272 |
|
254 | 273 | if (@available(macOS 10.15, *)) {
|
255 |
| - path = [home_folder |
256 |
| - stringByAppendingPathComponent:@"Library/Safari/CloudTabs.db"]; |
257 |
| - } else { |
258 |
| - path = [home_folder |
259 |
| - stringByAppendingPathComponent:@"Library/Safari/Bookmarks.plist"]; |
| 274 | + [files addObject:[home_folder stringByAppendingPathComponent: |
| 275 | + @"Library/Safari/CloudTabs.db"]]; |
260 | 276 | }
|
261 | 277 |
|
262 |
| - NSFileManager *manager = [NSFileManager defaultManager]; |
263 |
| - BOOL file_exists = [manager fileExistsAtPath:path]; |
264 |
| - NSData *data = [NSData dataWithContentsOfFile:path]; |
265 |
| - if (data == nil && file_exists) { |
266 |
| - auth_status = kDenied; |
267 |
| - } else if (file_exists) { |
268 |
| - auth_status = kAuthorized; |
| 278 | + std::string auth_status = kNotDetermined; |
| 279 | + for (NSString *file in files) { |
| 280 | + const std::string can_read = CheckFileAccessLevel(file); |
| 281 | + if (can_read == kAuthorized) { |
| 282 | + break; |
| 283 | + auth_status = kAuthorized; |
| 284 | + } else if (can_read == kDenied) { |
| 285 | + auth_status = kDenied; |
| 286 | + } |
269 | 287 | }
|
270 | 288 |
|
271 | 289 | return auth_status;
|
|
0 commit comments