Re: How to identify the server from which a volume was mounted?
Re: How to identify the server from which a volume was mounted?
- Subject: Re: How to identify the server from which a volume was mounted?
- From: Jerry Krinock <email@hidden>
- Date: Thu, 2 Jul 2009 14:10:16 -0700
On 2009 Jul 02, at 01:11, Quinn wrote:
There's no really good way to do this....
Generally I'd recommend you get the server URL using
FSCopyURLForVolume and proceed from there.
Well, it seems to work good enough for me. Just document in such a
way that someone won't expect it to work with the Klingon Filing
Protocol of year 2025.
Thanks, Quinn.
#import <Cocoa/Cocoa.h>
@interface SSYVolumeServerGuy : NSObject {
}
/*!
@brief Gets the volume reference number of a given path.
@details This method is a wrapper around FSGetCatalogInfo,
which does return an OSErr, but we don't return it because
most of the time the error is simply that your path is not
a currently-mounted volume, unless something really weird
happened.
@result The volume reference number of the given path,
or 0 if it could not be obtained for any reason.
*/
+ (FSVolumeRefNum)volumeRefNumberForPath:(NSString*)path ;
/*!
@brief Returns the identifier of the host (server) from which
a given mounted volume was mounted.
@details The host name of a volume mounted via Apple
Filing Protocol (afp://) also includes the transport type
after a dot separator. In any case, another dot and
the domain completes the identifier. Example:
* "Suzie's Old Powerbook._afpovertcp._tcp.local"
@param volumeName The mounted volume name (the path
component after "/Volumes/") for which the host name
is desired.
*/
+ (NSString*)hostForVolumeName:(NSString*)volumeName ;
/*!
@brief Returns the name of the server of a given volume
which is mounted via Apple Filing Protocol (afp://)
@details Sends -hostnameForVolumeName and returns only the
first path component of the result.
@param volumeName See -hostnameForVolumeName
*/
+ (NSString*)afpServerDisplayNameForVolumeName:(NSString*)volumeName ;
@end
@implementation SSYVolumeServerGuy
+ (FSVolumeRefNum)volumeRefNumberForPath:(NSString*)path {
FSRef pathRef;
FSPathMakeRef(
(UInt8*)[path UTF8String],
&pathRef,
NULL
) ;
OSErr osErr;
FSCatalogInfo catInfo;
osErr = FSGetCatalogInfo(
&pathRef,
kFSCatInfoVolume,
&catInfo,
NULL,
NULL,
NULL
) ;
FSVolumeRefNum volumeRefNum = 0;
if(osErr == noErr) {
volumeRefNum = catInfo.volume;
}
return volumeRefNum ;
}
+ (NSString*)hostForVolumeName:(NSString*)volumeName {
NSString* path = [@"/Volumes"
stringByAppendingPathComponent:volumeName] ;
FSVolumeRefNum volRefNum = [SSYVolumeServerGuy
volumeRefNumberForPath:path] ;
NSString* host = nil ;
if (volRefNum != 0) {
CFURLRef url ;
// Note: OSStatus return value of this is ignored:
FSCopyURLForVolume (
volRefNum,
&url
) ;
host = [(NSURL*)url host] ;
}
return host ;
}
+ (NSString*)afpServerDisplayNameForVolumeName:(NSString*)volumeName {
NSString* host = [self hostForVolumeName:volumeName] ;
NSArray* components = [host componentsSeparatedByString:@"."] ;
NSString* serverDisplayName = nil ;
if ([components count] > 0) {
serverDisplayName = [components objectAtIndex:0] ;
}
return serverDisplayName ;
}
@end
_______________________________________________
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