I am working on a code that, among other things, must save a zero-filled file. It can expect to create a file from 10MB to even 1GB blank.
It must be similar to this Unix command:
dd if=/dev/zero of=my_image.dsk count=20480
I could do this one work with small sizes:
int totalSize = 1024*1024;
char buf[totalSize];
strcpy(buf,"");
NSData *theData = [NSData dataWithBytes:buf length:sizeof(buf)];
//NSLog(@"%@", theData);
NSLog(@"%lu", sizeof(buf));
[theData writeToFile:@"/Users/foo/Desktop/my_image.dsk" atomically:YES];
But if I try a bigger value (1024*1024*10, for instance), it crashes. So I tried:
NSFileManager *ddd = [[NSFileManager alloc ] init];
[ddd createFileAtPath:@"/Users/foo/Desktop/my_image.dsk" contents:[NSData data] attributes:nil];
It creates an empty file, but is not good because the file size is zero. It should not be zero.
I spent hours trying to find an answer without success. I want to do this in Obj-C, but C is also an option before I go nuts.
Please, someone give-me some light!
Thanks in advance!
-- Edit --
Thanks everyone, but one more thing: is it possible to write without allocating everything on memory?
You can use
dd
. This will let you write, e.g., 10 GB without worrying about memory or address space.An advantage is that sophisticated users can kill
dd
if something goes wrong.About sandboxing: My suspicion is that this will work fine even in a sandbox, but I'd be curious to know for sure. According to documentation (link), a subprocess "simply inherits the sandbox of the process that created it." This is exactly how you'd expect it to work on Unix.
You can be sure that
dd
will stick around since Apple claims that OS X conforms to SUSv3.