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.This is relatively simple in the POSIX API:
The offset is specified using an integer of type
off_t
, which might not be large enough for your specific needs. Check your system API documentation to discover the 64-bit file interfaces if it isn't large enough.The best part of this approach is that it only takes a few bytes of program memory -- you're not allocating 10 gigabytes of nothing to write your file.
Another even simpler approach is to use the
truncate(2)
system call. Not all platforms support extending the file withtruncate(2)
, but for the ones that do, it's one call.Thanks everyone. I came to a solution. This is basically a sketch of the code that I am about to put in a cocoa app.
Works fine and I think I only need some precautions with the size variable and it will be enough.
Try this code:
You could also try this, less of a memory hog, and it works with a 1 GB file.