Re: problem with dataWithContentsOfFile on Lion
Re: problem with dataWithContentsOfFile on Lion
- Subject: Re: problem with dataWithContentsOfFile on Lion
- From: Kevin Perry <email@hidden>
- Date: Thu, 21 Jul 2011 11:11:16 -0700
Please check the NSData.h header and Foundation release notes for Lion.
Because mapping files on non-local drives (which may be disconnected or removed at any moment, resulting in a crash if you attempt to access parts of the file that haven't been faulted in) is unsafe, NSData on Lion has changed the meaning of NSDataReadingMapped to be a "hint" (see NSDataReadingMappedIfSafe). It will only map the file if it detects that doing so is safe.
If you are willing to accept the risk of mapping "unsafe" files, simply pass NSDataReadingMappedAlways instead of NSDataReadingMapped.
-KP
On Jul 21, 2011, at 11:01 AM, Wilker wrote:
> Hi guys,
>
> I updated to Lion yesterday, but I'm having an issue now with [NSData
> dataWithContentsOfFile]
>
> This code is to generate a hash based on 64kb of start and 64kb of end from
> a file:
>
> + (NSString *)generateHashFromPath:(NSString *)path
>
> {
>
> const NSUInteger CHUNK_SIZE = 65536;
>
>
>
> NSError *error = nil;
>
> NSData *fileData = [NSData dataWithContentsOfFile:path options:
> NSDataReadingMapped | NSDataReadingUncached error:&error];
>
>
>
> if (error) {
>
> return nil;
>
> }
>
>
>
> const uint8_t* buffer = [fileData bytes];
>
>
>
> NSUInteger byteLength = [fileData length];
>
> NSUInteger byteOffset = 0;
>
>
>
> if (byteLength > CHUNK_SIZE) {
>
> byteOffset = byteLength - CHUNK_SIZE;
>
> byteLength = CHUNK_SIZE;
>
> }
>
>
>
> CC_MD5_CTX md5;
>
> CC_MD5_Init(&md5);
>
>
>
> CC_MD5_Update(&md5, buffer, (CC_LONG) byteLength);
>
> CC_MD5_Update(&md5, buffer + byteOffset, (CC_LONG) byteLength);
>
>
>
> unsigned char digest[CC_MD5_DIGEST_LENGTH];
>
>
>
> CC_MD5_Final(digest, &md5);
>
>
>
> NSMutableString *hexDigest = [NSMutableString stringWithCapacity:
> CC_MD5_DIGEST_LENGTH * 2];
>
>
>
> for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
>
> [hexDigest appendFormat:@"x", digest[i]];
>
> }
>
>
>
> return [hexDigest lowercaseString];
>
> }
>
> Before Lion, it really works well and fast, even on Wifi external drive
> (through Airport Extreme), but now it get's really slow... I did some
> checks, and now its reading the entire file... instead of just read 128kb
> (start and end). Anyone have an ideia on why its happening now? And how to
> make it works as before on Snow Leopard?
> ---
> Wilker LĂșcio
> http://about.me/wilkerlucio/bio
> Kajabi Consultant
> +55 81 82556600
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden