atomicbird/atomictools — Issue #1: Alternate file size method
Author: bazineta
Created: 2012-02-19 04:11:06+00:00
State: open

Nice read, and quite helpful. A suggestion; since we have an open file descriptor and we're down in the rhubarb anyway, it's easy to use fstat() to obtain the file size.

Original method:

```objc
// Get file size
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
if (fileAttributes == nil) {
close(fd);
return nil;
}
NSNumber *fileSize = [fileAttributes objectForKey:NSFileSize];
```

Alternate method:

```objc
#import

// Get file size

struct stat statbuf;

if (fstat(fd, &statbuf) == -1)
{
close(fd);
return nil;
}

// mmap
void *mappedFile;
mappedFile = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (mappedFile == MAP_FAILED) {
NSLog(@"Map failed, errno=%d, %s", errno, strerror(errno));
return nil;
}
```