• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: mount() from Cocoa App
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mount() from Cocoa App


  • Subject: Re: mount() from Cocoa App
  • From: Jim Luther <email@hidden>
  • Date: Mon, 7 Nov 2005 10:32:51 -0800

Dalton,

I was working on something for you last week but got busy with other things. I finished it this morning. Feel free to use it.

- Jim Luther


#include <string.h> #include <unistd.h> #include <CoreServices/CoreServices.h>

static void usage(void)
{
fprintf(stderr, "usage: ServerIsMounted [-h] -u <url>\n");
fprintf(stderr, " ServerIsMounted determines if a URL to a server volume is already mounted.\n");
fprintf(stderr, " -h Shows this help message.\n");
fprintf(stderr, " -u <inputUrl> The URL to parse.\n");
}



int URLIsMounted(CFURLRef inputUrl, Boolean *isMounted) { CFStringRef inputScheme; CFStringRef inputHostName; CFMutableStringRef inputPath; CFStringRef volScheme; CFStringRef volHostName; CFMutableStringRef volPath; ItemCount volumeIndex; FSVolumeRefNum vRefNum; CFURLRef volUrl; OSStatus result;

	*isMounted = FALSE;
	inputScheme = inputHostName = volScheme = volHostName = NULL;
	inputPath = volPath = NULL;

	result = paramErr;

	inputScheme = CFURLCopyScheme(inputUrl);
	require(inputScheme != NULL, Exit);

	inputHostName = CFURLCopyHostName(inputUrl);
	require(inputHostName != NULL, Exit);

inputPath = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFURLCopyPath(inputUrl));
require(inputPath != NULL, Exit);

/* make sure path is terminated with a "/" */
if ( !CFStringHasSuffix(inputPath, CFSTR("/")) ) {
CFStringAppend(inputPath, CFSTR("/"));
}


	volumeIndex = 0;
	while ( TRUE ) {
		++volumeIndex;
		if ( volScheme != NULL ) {
			CFRelease(volScheme);
			volScheme = NULL;
		}

		if ( volHostName != NULL ) {
			CFRelease(volHostName);
			volHostName = NULL;
		}

		if ( volPath != NULL ) {
			CFRelease(volPath);
			volPath = NULL;
		}

result = FSGetVolumeInfo(kFSInvalidVolumeRefNum, volumeIndex, &vRefNum, kFSVolInfoNone, NULL, NULL, NULL);
if ( result == nsvErr ) {
/* no more volumes - exit normally */
result = noErr;
break;
}
if ( result != noErr ) {
continue; /* try next volume */
}


		result = FSCopyURLForVolume(vRefNum, &volUrl);
		if ( result != noErr ) {
			continue;	/* try next volume */
		}

		volScheme = CFURLCopyScheme(volUrl);
		if ( volScheme == NULL ) {
			continue;	/* try next volume */
		}

		volHostName = CFURLCopyHostName(volUrl);
		if ( volHostName == NULL ) {
			continue;	/* try next volume */
		}

volPath = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFURLCopyPath(volUrl));
if ( volPath == NULL ) {
continue; /* try next volume */
}


		/* make sure path is terminated with a "/" */
		if ( !CFStringHasSuffix(volPath, CFSTR("/")) ) {
			CFStringAppend(volPath, CFSTR("/"));
		}

if ( (CFStringCompare(inputScheme, volScheme, kCFCompareCaseInsensitive) == kCFCompareEqualTo) &&
(CFStringCompare(inputHostName, volHostName, kCFCompareCaseInsensitive) == kCFCompareEqualTo) &&
(CFStringCompare(inputPath, volPath, 0) == kCFCompareEqualTo) ) {
/* found a match -- inputUrl is already mounted */
*isMounted = TRUE;
break;
}
}


Exit:

	if ( volScheme != NULL ) {
		CFRelease(volScheme);
	}

	if ( volHostName != NULL ) {
		CFRelease(volHostName);
	}

	if ( volPath != NULL ) {
		CFRelease(volPath);
	}

	if ( inputScheme != NULL ) {
		CFRelease(inputScheme);
	}

	if ( inputHostName != NULL ) {
		CFRelease(inputHostName);
	}

	if ( inputPath != NULL ) {
		CFRelease(inputPath);
	}

	return ( (result == noErr) ? EXIT_SUCCESS : EXIT_FAILURE );
}


int main (int argc, char * const argv[]) { int err; int ch; CFURLRef inputUrl; char inputUrlStr[0x1000]; Boolean isMounted;

err = EXIT_SUCCESS;
inputUrl = NULL;
/* crack command line args */
while ( ((ch = getopt(argc, argv, "hu:")) != -1) && (err == EXIT_SUCCESS) ) {
switch (ch) {
case 'u':
strcpy(inputUrlStr, optarg);
inputUrl = CFURLCreateWithBytes(kCFAllocatorDefault, (UInt8 *) optarg, strlen(inputUrlStr), kCFStringEncodingMacRoman, NULL);
if ( inputUrl == NULL ) {
err = EXIT_FAILURE;
}
break;
case 'h':
case '?':
default:
err = EXIT_FAILURE;
break;
}
}


	if ( !err ) {
		if ((argc > 3) || (inputUrl == NULL)) {
			err = EXIT_FAILURE;
		}
	}

	require_action(err == EXIT_SUCCESS, command_err, usage());

	err = URLIsMounted(inputUrl, &isMounted);
	if ( err == 0 ) {
		if ( isMounted ) {
			fprintf(stdout, "%s is mounted\n", inputUrlStr);
		}
		else {
			fprintf(stdout, "%s is not mounted\n", inputUrlStr);
		}
	}

command_err:

	if ( inputUrl != NULL ) {
		CFRelease(inputUrl);
	}

	return ( err );
}

On Nov 4, 2005, at 11:00 PM, Dalton Hamilton wrote:

Jim (and ALL), thanks very much for helping me figure this out. Your last email pointed me to what I needed. Here is the code that works:


// // main.m


#import <Cocoa/Cocoa.h>
enum
{
kBufferLength = 0x1000 /* 4K */
};
static void DisplayURLComponent(CFURLRef url, UInt8 *buffer, CFURLComponentType componentType, char *componentTypeStr)
{
CFRange range;
CFRange rangeIncludingSeparators;

/* now, get the components and display them */
range = CFURLGetByteRangeForComponent(url, componentType, &rangeIncludingSeparators);
if ( range.location != kCFNotFound )
{
char componentStr[kBufferLength];
char componentIncludingSeparatorsStr[kBufferLength];

strncpy(componentStr, (const char *)&buffer[range.location], range.length);
componentStr[range.length] = 0;
strncpy(componentIncludingSeparatorsStr, (const char *)&buffer [rangeIncludingSeparators.location], rangeIncludingSeparators.length);
componentIncludingSeparatorsStr[rangeIncludingSeparators.length] = 0;
fprintf(stdout, "%s: \"%s\" including separators: \"%s\"\n", componentTypeStr, componentStr, componentIncludingSeparatorsStr);
}
else
{
fprintf(stdout, "%s not found\n", componentTypeStr);
}
}


int main(int argc, char *argv[])
{


    //return NSApplicationMain(argc,  (const char **) argv);


ItemCount volumeIndex;
FSRef ref;

// Used in PBHGetVInfoSync
HParamBlockRec hpb;
FSVolumeRefNum actualVolumeRefNum;
OSErr result=0,ec=0;
char name[100];
CFURLRef url;
CFStringRef urlStr;
UInt8 buffer[kBufferLength];
CFIndex componentLength;



bzero(&hpb,sizeof(HParamBlockRec));
hpb.ioParam.ioNamePtr = (StringPtr)name;
/* Call FSGetVolumeInfo in loop to get all volumes starting with the first */
volumeIndex = 1;
do
{
bzero(name,100);

result = FSGetVolumeInfo(kFSInvalidVolumeRefNum, volumeIndex, &actualVolumeRefNum, kFSVolInfoNone, NULL, NULL, &ref);
if ( noErr == result )
{
hpb.volumeParam.ioVRefNum = (short)actualVolumeRefNum;
ec = PBHGetVInfoSync(&hpb);
if(ec == noErr)
{
if(hpb.volumeParam.ioVFSID != 0) // Means this is a mounted volume
{
printf("share name = %s\n",&hpb.ioParam.ioNamePtr[1]);
ec = FSCopyURLForVolume(actualVolumeRefNum, &url);
if (ec == noErr)
urlStr = CFURLGetString(url);
/* get the bytes from the URL */
componentLength = CFURLGetBytes(url, buffer, kBufferLength);
buffer[componentLength] = 0;
fprintf(stdout, "url: \"%s\"\n", buffer);


DisplayURLComponent(url, buffer, kCFURLComponentHost, "kCFURLComponentHost");

if (url != NULL) {
CFRelease(url);
}
}
}
++volumeIndex; // and the volumeIndex to get the next volume
}else
return(0);

} while ( noErr == result );
return(0);
}



_______________________________________________ Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: mount() from Cocoa App
      • From: Dalton Hamilton <email@hidden>
References: 
 >Re: mount() from Cocoa App (From: Dalton Hamilton <email@hidden>)

  • Prev by Date: Re: mount() from Cocoa App
  • Next by Date: A call to 'CloseOpenTransportInContext' blocks forever - why?
  • Previous by thread: Re: mount() from Cocoa App
  • Next by thread: Re: mount() from Cocoa App
  • Index(es):
    • Date
    • Thread