Determine if a network share exists before mounting

前端 未结 1 498
梦谈多话
梦谈多话 2021-01-01 02:46

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 *vo         


        
相关标签:
1条回答
  • 2021-01-01 03:04

    This answer uses Private Frameworks. As naixn points out in the comments, this means it could break even on a dot release.

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

    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.

    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.

    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];
    }
    
    0 讨论(0)
提交回复
热议问题