我正在Objective C中編寫一個小命令行實用程序,它將檢查給定路徑是否爲掛載點,如果不是,則將掛載網絡共享給它。我打算用bash寫這篇文章,但選擇嘗試學習Objective C。我要尋找目標C相當於是這樣的:檢查OSX中的目標C中的目錄是否爲掛載點
mount | grep some_path
基本上我可以用一個函數來測試給出的路徑是目前使用的掛載點。任何幫助,將不勝感激。謝謝!
我正在Objective C中編寫一個小命令行實用程序,它將檢查給定路徑是否爲掛載點,如果不是,則將掛載網絡共享給它。我打算用bash寫這篇文章,但選擇嘗試學習Objective C。我要尋找目標C相當於是這樣的:檢查OSX中的目標C中的目錄是否爲掛載點
mount | grep some_path
基本上我可以用一個函數來測試給出的路徑是目前使用的掛載點。任何幫助,將不勝感激。謝謝!
一些研究,我結束了使用此代碼,在任何情況下,人們需要在將來後:
NSArray * keys = [NSArray arrayWithObjects:NSURLVolumeURLForRemountingKey, nil];
NSArray * mountPaths = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];
NSError * error;
NSURL * remount;
for (NSURL * mountPath in mountPaths) {
[mountPath getResourceValue:&remount forKey:NSURLVolumeURLForRemountingKey error:&error];
if(remount){
if ([[[NSURL URLWithString:share] host] isEqualToString:[remount host]] && [[[NSURL URLWithString:share] path] isEqualToString:[remount path]]) {
printf("Already mounted at %s\n", [[mountPath path] UTF8String]);
return 0;
}
}
}
注意的NSURL份額傳遞到函數的路徑到遠程共享。通過重新裝入密鑰進行過濾可以爲遠程文件系統提供掛載點列表,因爲本地文件系統沒有該密鑰集。
:
func findMountPoint(shareURL: URL) -> URL?{
guard let urls = self.mountedVolumeURLs(includingResourceValuesForKeys: [.volumeURLForRemountingKey], options: [.produceFileReferenceURLs]) else {return nil}
for u in urls{
guard let resources = try? u.resourceValues(forKeys: [.volumeURLForRemountingKey]) else{
continue
}
guard let remountURL = resources.volumeURLForRemounting else{
continue
}
if remountURL.host == shareURL.host && remountURL.path == shareURL.path{
return u
}
}
return nil
}
' - [的NSFileManager mountedVolumeURLs ...]'可以用來列舉所有安裝的卷,但我無法找到一個Objective-C API來安裝卷(它們可以通過卸除' - [NSWorkspace unmount ...]'你需要使用[Disk Arbitration](磁盤仲裁)(https://developer.apple.com/library/mac/documentation/DriversKernelHardware/Conceptual/DiskArbitrationProgGuide/Introduction/Introduction.html)框架,只輸出一個C API,這就是說,根據你的要求,一個bash腳本可能更容易,因爲你不必擔心回調和運行循環。 – 2013-09-25 04:16:31
我會檢查一下。謝謝! – Bogdan