問題描述
識別 macOS 上的託管文件夾 (Identify managed folders on macOS)
如果我的客戶將 SQLite 數據庫放在託管文件夾中,我經常會遇到麻煩。對於託管文件夾,我的意思是:
- 已安裝的捲
- Dropbox文件夾
- iCloud Drive文件夾
- FUSE等 li>
是否有一種安全的方法來識別此類位置以警告用戶?
參考解法
方法 1:
Dropbox does offer a way to programmatically get the path of the local Dropbox folder(s) (if any):
https://help.dropbox.com/installs‑integrations/desktop/locate‑dropbox‑folder#programmatically
方法 2:
Based on the hint of @Greg I came up with a solution in ObjC using some private helpers, but I guess the idea should become clear:
‑ (BOOL)isDropbox:(NSURL *)url {
// https://help.dropbox.com/de‑de/installs‑integrations/desktop/locate‑dropbox‑folder#programmatically
id json = [[NSData dataWithContentsOfURL:hxFileURL(@"~/.dropbox/info.json".stringByExpandingTildeInPath)] fromJSON];
NSString *ppath = json[@"personal"][@"path"];
NSString *bpath = json[@"business"][@"path"];
NSString *path = hxFilePath(url);
return (ppath && [path hasPrefix:ppath]) || (bpath && [path hasPrefix:bpath]);
}
‑ (BOOL)hoIsLocal:(NSURL *)url {
@try {
if (![FS hoIsDir:url]) {
url = [url URLByDeletingLastPathComponent];
}
// Is it local in general?
ERROR_DEF;
NSNumber *state;
[url getResourceValue:&state forKey:NSURLVolumeIsLocalKey error:&error];
XLogInfo(@"state=%@ url=%@", state, url);
// If so it could be stored in iCloud Drive
if (state.boolValue) {
BOOL ubiq = [FS isUbiquitousItemAtURL:url];
XLogInfo(@"ubiq=%@ url=%@", @(ubiq), url);
// Or on Dropbox
if (!ubiq) {
BOOL dbx = [FS isDropbox:url];
XLogInfo(@"dbx=%@ url=%@", @(dbx), url);
return !dbx;
}
}
}
@catch (id ex) {
XLogException(ex);
}
return NO;
}