1/*
2 * Copyright (c) 2001-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *  BLDumpVolumeFinderInfo.c
25 *  bless
26 *
27 *  Created by Shantonu Sen <ssen@apple.com> on Thu Apr 19 2001.
28 *  Copyright (c) 2001-2007 Apple Inc. All Rights Reserved.
29 *
30 *  $Id: BLDumpVolumeFinderInfo.c,v 1.24 2006/02/20 22:49:54 ssen Exp $
31 *
32 */
33
34#include <stdlib.h>
35#include <stdio.h>
36#include <unistd.h>
37#include <fcntl.h>
38#include <string.h>
39#include <sys/param.h>
40
41#include <CoreFoundation/CoreFoundation.h>
42
43#include "bless.h"
44#include "bless_private.h"
45
46/*
47 * 1. getattrlist on the mountpoint to get the volume id
48 * 2. read in the finder words
49 * 3. for the directories we're interested in, get the entries in /.vol
50 */
51int BLCreateVolumeInformationDictionary(BLContextPtr context, const char * mountpoint,
52					CFDictionaryRef *outDict) {
53    uint32_t finderinfo[8];
54    int err;
55    uint32_t i;
56    uint32_t dirID;
57    CFMutableDictionaryRef dict = NULL;
58    CFMutableArrayRef infarray = NULL;
59
60    char blesspath[MAXPATHLEN];
61
62    err = BLGetVolumeFinderInfo(context, mountpoint, finderinfo);
63    if(err) {
64        return 1;
65    }
66
67    infarray = CFArrayCreateMutable(kCFAllocatorDefault,
68				    8,
69				    &kCFTypeArrayCallBacks);
70
71    dict =  CFDictionaryCreateMutable(kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
72
73    for(i = 0; i< 8-2; i++) {
74      CFMutableDictionaryRef word =
75	CFDictionaryCreateMutable(kCFAllocatorDefault,6, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
76      CFTypeRef val;
77
78      dirID = finderinfo[i];
79      blesspath[0] = '\0';
80
81      err = BLLookupFileIDOnMount(context, mountpoint, dirID, blesspath);
82
83      val = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &dirID);
84      CFDictionaryAddValue(word, CFSTR("Directory ID"), val);
85      CFRelease(val); val = NULL;
86
87      val = CFStringCreateWithCString(kCFAllocatorDefault, blesspath, kCFStringEncodingUTF8);
88      CFDictionaryAddValue(word, CFSTR("Path"), val);
89      CFRelease(val); val = NULL;
90
91      if(strlen(blesspath) == 0 || 0 == strcmp(mountpoint, "/")) {
92	  val = CFStringCreateWithCString(kCFAllocatorDefault, blesspath, kCFStringEncodingUTF8);
93      } else {
94	  val = CFStringCreateWithCString(kCFAllocatorDefault, blesspath+strlen(mountpoint), kCFStringEncodingUTF8);
95      }
96      CFDictionaryAddValue(word, CFSTR("Relative Path"), val);
97      CFRelease(val); val = NULL;
98
99      CFArrayAppendValue(infarray, word);
100      CFRelease(word); word = NULL;
101    }
102
103    CFDictionaryAddValue(dict, CFSTR("Finder Info"),
104			 infarray);
105
106    CFRelease(infarray); infarray = NULL;
107
108    {
109        CFNumberRef vsdbref = NULL;
110        uint64_t vsdb;
111        vsdb = (*(uint64_t *)&finderinfo[8-2]);
112
113        vsdbref = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &vsdb);
114        CFDictionaryAddValue(dict, CFSTR("VSDB ID"), vsdbref);
115        CFRelease(vsdbref); vsdbref = NULL;
116    }
117
118    *outDict = dict;
119    return 0;
120}
121