Reputation: 1929
If i have a mount point how do i get the maximum file size supported by filesystem locally mounted on that mount point?
I tried statfs(2), statvfs(2) but nothing seems to mention file size limits. I tried capturing 'diskutil listFilesystems -plist' output but it only mentions min/max limits on partition size for each fs. Any other ideas?
Upvotes: 2
Views: 566
Reputation: 1
I have found that lseek under Linux will return an error if you seek beyond the maximum supported file size for the filesystem in which the file resides without having to write out any data. You can do a simple binary search to get the maximum supported file size. It's usually the first power of two that causes a seek error -1 so simply left shifting 0x1 in a loop until fseek returns an error and then trying the resulting value -1 will probably get you there very quickly. Just make sure you have compiled your code with -D_FILE_OFFSET_BITS=64 or you use the deprecated -D_LARGEFILE64_SOURCE and use lseek64.
This does not seem to work under windows or OS X. It seems lseek doesn't do bounds checking in those implementations. So far the best I've been able to do in c++ is to look at the filesystem type string returned by GetVolumeInformation in windows or the 64 bit version of fstatfs in OS X and return a theoretical limit and hope I didn't run in to any previously unseen filesystems. Make sure under OS X you define _DARWIN_USE_64_BIT_INODE when compiling. Using NSURL and fetching the NSURLVolumeMaximumFileSizeKey resource value as mentioned below seems to require Objective-c to implement and didn't work for my needs.
Creating a large file every time you mount a new filesystem VIA USB was not an acceptable solution. My attempts at doing this resulted in long hangs as the OS silently zeroed out the sectors in between. I couldn't even truncate or unlink the file until the operation was finished. How angry would you be if you plugged in your USB drive to download a file to it only to have the application you were using hang your drive for 10 minutes while it attempted to test the maximum file size? How would the application handle the situation where the drive was nearly full when it was first mounted?
It would be really nice if fstatfs returned this information.
Upvotes: 0
Reputation: 5316
Get an NSURL instance referring to the filesystem mountpoint (in fact an URL referring to anything within the mounted filesystem will work too) then call getResourceValue:forKey:error: or resourceValuesForKeys:error: to fetch the value for NSURLVolumeMaximumFileSizeKey.
Check the NSURL class reference for more info.
Upvotes: 1
Reputation: 44331
The VFS layer doesn't expose this data, so no. All you can do is try to create a file and check for EFBIG.
Upvotes: 0