在安装之前确定是否存在网络共享 [英] Determine if a network share exists before mounting

查看:112
本文介绍了在安装之前确定是否存在网络共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一种工具,根据用户连接的无线网络自动挂载网络卷。安装卷很容易:

I'm working on a tool to automatically mount network volumes based on what wireless network the user is connected to. Mounting the volume is easy:

NSURL *volumeURL = /* The URL to the network volume */

// Attempt to mount the volume
FSVolumeRefNum volumeRefNum;
OSStatus error = FSMountServerVolumeSync((CFURLRef)volumeURL, NULL, NULL, NULL, &volumeRefNum, 0L);

但是,如果 volumeURL (如果某人关闭或删除了网络硬盘驱动器,例如),Finder弹出错误消息解释这一事实。我的目标是这不会发生 - 我想尝试挂载卷,但如果挂载失败,则会失败。

However, if there is no network share at volumeURL (if someone turned off or removed a network hard drive, for example), Finder pops up an error message explaining this fact. My goal is for this not to happen — I'd like to attempt to mount the volume, but fail silently if mounting fails.

有没有人有任何提示如何做这个?理想情况下,我想找到一种方法来检查共享是否存在,然后尝试挂载它(以避免不必要的工作)。

Does anyone have any tips on how to do this? Ideally, I'd like to find a way to check if the share exists before attempting to mount it (so as to avoid unnecessary work). If that's not possible, some way to tell the Finder not to display its error message would work as well.

推荐答案

如果这是不可能的,一些方法来告诉Finder不显示它的错误信息。这个答案使用私有框架。

没有办法只使用公共API(我可以在几个小时的搜索/反汇编后找到)。

There is no way to do this using only public API (that I can find after a couple of hours of searching/disassembling).

此代码将访问该URL,不显示任何UI元素通过或失败。这不仅包括错误,还包括身份验证对话框,选择对话框等。

This code will access the URL and not display any UI elements pass or fail. This includes not only errors, but authentication dialogs, selection dialogs, etc.

此外,Finder不显示这些消息,而是来自CoreServices的NetAuthApp。这里调用的函数( netfs_MountURLWithAuthenticationSync )直接从问题( FSMountServerVolumeSync )中的函数调用。调用它在这个级别让我们通过 kSuppressAllUI 标志。

Also, it's not Finder displaying those messages, but NetAuthApp from CoreServices. The function being called here (netfs_MountURLWithAuthenticationSync) is called directly from the function in the question (FSMountServerVolumeSync). Calling it at this level lets us pass the kSuppressAllUI flag.

成功后,rc为0,已安装目录的NSStrings列表。

On success, rc is 0 and mountpoints contains a list of NSStrings of the mounted directories.

//
// compile with:
//
//  gcc -o test test.m -framework NetFS -framework Foundation
include <inttypes.h>
#include <Foundation/Foundation.h>

// Calls to FSMountServerVolumeSync result in kSoftMount being set
// kSuppressAllUI was found to exist here:
// http://www.opensource.apple.com/source/autofs/autofs-109.8/mount_url/mount_url.c
// its value was found by trial and error
const uint32_t kSoftMount     = 0x10000;
const uint32_t kSuppressAllUI = 0x00100;

int main(int argc, char** argv)
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSURL *volumeURL = [NSURL URLWithString:@"afp://server/path"];
    NSArray* mountpoints = nil;

    const uint32_t flags = kSuppressAllUI | kSoftMount;

    const int rc = netfs_MountURLWithAuthenticationSync((CFURLRef)volumeURL, NULL, NULL,
            NULL, flags, (CFArrayRef)&mountpoints);

    NSLog(@"mountpoints: %@; status = 0x%x", mountpoints, rc);

    [pool release];
}

这篇关于在安装之前确定是否存在网络共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆