KISS

Keep It Simple Stupid

iOS: Testing no space left situation

| comments

How do you test that your app handles “no space left on device” errors properly? There is a (boring) way to stuff up your device until it’s full and test that way. It’s not the most convenient way though, especially if you want to test faster on the iOS Simulator. Here’s my solution: mount a small RAM disk for the Applications directory in the simulator.

Following are the commands to prepare and mount a RAM disk:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# create a new 200MB RAM disk
mydev=$( hdiutil attach -nomount ram://$(( 200 * 1024 * 1024 / 512 )) )

# (optional) should print the new disk:
diskutil list

# create a new FS on it (here, /dev/disk2 is the created disk; I naturally tried
# using $mydev for that and failed with an error about /dev/rdisk2, although the
# value was indeed /dev/disk2)
newfs_hfs /dev/disk2

# as mounting will hide the existing files, we need to copy them over first
# make an arbitrary directory and mount there
mkdir ~/"Library/Application Support/iPhone Simulator/7.1-64/1/"
mount -t hfs /dev/disk2 ~/"Library/Application Support/iPhone Simulator/7.1-64/1/"

# kill the iOS Simulator and copy the data
pkill -li iphone
rsync -av --delete-excluded ~/Library/Application\ Support/iPhone\ Simulator/7.1-64/{Applications/,1/}

# remount to the proper location
umount /dev/disk2
mount -t hfs /dev/disk2 ~/"Library/Application Support/iPhone Simulator/7.1-64/Applications/"

Now you can easily fill up this virtual disk. Please note the data will be destroyed on OS restart or disk ejection.

1
2
3
4
5
6
7
8
# show disk usage
df -h | grep /disk2

# unmount
umount /dev/disk2

# remove the RAM disk
diskutil eject /dev/disk2

This is a simple function that checks for this kind of error:

1
2
3
4
5
static inline BOOL isNoSpaceLeftError(NSError *error)
{
    return ([NSCocoaErrorDomain isEqualToString:error.domain] &&
            (NSFileWriteOutOfSpaceError == error.code));
}

Sources: http://osxdaily.com/2007/03/23/create-a-ram-disk-in-mac-os-x/, http://superuser.com/questions/456803/create-ram-disk-mount-to-specific-folder-in-osx.

iOS

Don't hesitate to leave a comment below. NB! If you don't see a comment form under the post, it's most likely that an extension (such as Ghostery, NoScript, or AdBlock) of your browser blocks the scripts from disqus.com, and you can unblock that.

« Xcode and AppleScript Job Config History plugin error »

Comments