2

I have the following scenario:

In my application, the user must have a specific dmg file mounted (at all times). This dmg contains some data the application needs to process and be always available.

I am looking at cases when "something goes bad". One of these cases is that the dmg file, while mounted, has been moved to another location (another folder etc).

To check this, I am using the hdiutil info -plist command which produces a property list with all information for mounted dmg files.

The important keys of this property list for my case are the image-path key and the image-alias key; The first is the actual location of the dmg (or the path from it was mounted originally) and the second is where the dmg actually is at the point when hdiutil is executed (has been moved).

image-path is a string value - image-alias is data object.

A typical plist looks like this:

<key>image-alias</key>
    <data>
        AAAAAAF0AAIAAAxNYWNpbnRvc2ggSEQAAAAAAAAAAAAAAAAAAADP
        KL64SCsAAAAJ1UgMRVNNYXN0ZXIuZG1nAAAAAAAAAAAAAAAAAAAA
        AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABeS
        ls/YacBkZXZpZGRza/////8AAAkgAAAAAAAAAAAAAAAAAAAACURv
        d25sb2FkcwAAEAAIAADPKKKYAAAAEQcwB0AGUAcgAuAGQAbQBnAA
        8AGgAMAE0AYQBjAGkAbgB0AG8AcwBoACAASABEABIAIVVzZXJzL3
        BtYXIvRG93bmxvYWRzL0VTTWFzdGVy
    </data>
<key>image-encrypted</key>
    <false/>
<key>image-path</key>
    <string>/Users/user/Downloads/Master.dmg</string>

(when opened in xcode the image alias data look like:

<00000000 01740002 6163696e 20484400 00000000 00000000 00000000 0000cf28 beb8482b 00000009 00000000 00000000 00000000 00000000 ... >

On the other hand, when running (terminal) the hdiutil info without the -plist (outputs on screen) it returns:

$ hdiutil info
framework       : 371.1
driver          : 10.9v371.1
================================================
image-path      : /Users/user/Desktop/Master.dmg
image-alias     : /Users/user/Desktop/Master.dmg
shadow-path     : <none>
...

(this is what the command should return normally. On the "something went bad" case I'm looking at, image-path and image-alias should be different paths)

I have tried already to

NSData *aliasData = [NSData dataWithData:[images valueForKey:@"image-alias"]];
NSString *alias = [[NSString alloc] initWithData:aliasData encoding:NSSymbolStringEncoding];

with SymbolString, UTF, ASCII but it always return null.

So here comes the question:

Does anyone know what kind of Data is the image-alias object so that I can convert in string and use it accordingly?

Thanks in advance!

Pericles
  • 493
  • 7
  • 15

1 Answers1

0

After some research, lots of time and finally an Apple Technical Question, this object is a CFURL containing BookmarkData.

To convert it to NSString so that it is human-readable and comparable with other strings (in this case the image-path), a conversion is needed to be done using

CFURLCreateBookmarkDataFromAliasRecord and CFURLCreateByResolvingBookmarkData

With reference to Milliways' excellent answer, once the image-alias data are set in

NSData *aliasData = dmgArray[i][@"image-alias"];

the function:

NSURL *aliasMountPoint(NSData *aliasMPData)
{
    CFErrorRef *err = NULL;
    CFDataRef aliasBookmark = CFURLCreateBookmarkDataFromAliasRecord(kCFAllocatorDefault, (__bridge CFDataRef)aliasMPData);

    if (aliasBookmark == nil)
        return nil;
    else
    {
        CFURLRef resolvedUrl = CFURLCreateByResolvingBookmarkData (NULL, aliasBookmark, kCFBookmarkResolutionWithoutUIMask, NULL, NULL, NO, err);
        return CFBridgingRelease(resolvedUrl);
    }
}

will perform the conversion. Then call it with:

NSString *imagealias = [aliasMountPoint(aliasData) absoluteString];

(remember, this is a CFURL we convert in NSURL and the absoluteString converts it to NSString)

Since the NSString contains the absolute path, a

imagealias = [imagealias substringFromIndex:7]

to remove the file:// at the beginning of the string.

Then simply compare the two strings in image-path and image-alias, proceed as you intend to, handle any errors in the aliasMountPoint function the way you need to handle them and all is set!

Community
  • 1
  • 1
Pericles
  • 493
  • 7
  • 15