Testing for a Network Volume
Q: How can I reliably determine if a volume is mounted over the network?
A: There are numerous ways to test whether a volume is a network volume. The best method to use depends on the layer that you're working at:
If you're working on general application code, you should use NSURL for this. Construct a URL for any item on the volume and then call
-getResourceValue:forKey:error:
to get theNSURLVolumeIsLocalKey
key; the returned value will be false (kCFBooleanFalse
) for a network volume and true (kCFBooleanTrue
) for a local volume.If you're programming at the BSD layer, you can call statfs and test the
MNT_LOCAL
flag in thef_flags
field that it returns. Alternatively, you can call getattrlist to request theATTR_VOL_MOUNTFLAGS
attribute, which can be more efficient thanstatfs
under some circumstances.
One word of warning here: the distinction between network volumes and local volumes is somewhat blurry these days. For example, you can't assume that network volumes are slow, or that local volumes don't have to deal with file locking issues. In general you should test for the existence of specific functionality rather than making assumptions based on the volume type. For example, you can use the NSURLVolumeSupportsAdvisoryFileLockingKey
key to determine whether a volume supports advisory locking.
Document Revision History
Date | Notes |
---|---|
2011-09-20 | Rewritten to describe the latest techniques. |
1998-06-01 | New document that shows how to determine if a volume is mounted over the network. |
Copyright © 2011 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2011-09-20