forked from mykeepass/KeePassLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectWriteMemoryMap.m
44 lines (39 loc) · 1.13 KB
/
DirectWriteMemoryMap.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// WritableMemoryMap.m
// KeePass2
//
// Created by Qiang Yu on 1/9/10.
// Copyright 2010 Qiang Yu. All rights reserved.
//
#import "DirectWriteMemoryMap.h"
#import <stdio.h>
#import <stdlib.h>
#import <sys/types.h>
#import <sys/stat.h>
#import <unistd.h>
#import <fcntl.h>
#import <sys/mman.h>
@implementation DirectWriteMemoryMap
-(id)initWithWritableNewFile:(NSString *)fileName size:(uint32_t)size{
if(self=[super init]){
_fd = open([fileName fileSystemRepresentation], O_RDWR|O_CREAT|O_TRUNC, (mode_t)0600);
if(_fd==-1){
@throw [NSException exceptionWithName:@"FileSystemException" reason:@"FileSystemException" userInfo:nil];
}
if(lseek(_fd, size-1, SEEK_SET)==-1||
write(_fd,"",1)!=1){
@throw [NSException exceptionWithName:@"FileSystemException" reason:@"FileSystemException" userInfo:nil];
}
_size = size;
_buffer = mmap(0, _size, PROT_READ|PROT_WRITE, MAP_SHARED, _fd, 0);
if(_buffer == MAP_FAILED){
close(_fd);
@throw [NSException exceptionWithName:@"FileSystemException" reason:@"FileSystemException" userInfo:nil];
}
}
return self;
}
-(uint8_t *)getBufferToWrite{
return _buffer;
}
@end