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 *  BLGetDiskSectorsForFile.c
25 *  bless
26 *
27 *  Created by Shantonu Sen <ssen@apple.com> on Sat Jun 01 2002.
28 *  Copyright (c) 2002-2007 Apple Inc. All Rights Reserved.
29 *
30 *  $Id: BLGetDiskSectorsForFile.c,v 1.17 2006/02/20 22:49:55 ssen Exp $
31 *
32 */
33
34#include <CoreFoundation/CoreFoundation.h>
35
36#include <sys/fcntl.h>
37#include <sys/attr.h>
38#include <unistd.h>
39#include <sys/stat.h>
40#include <sys/mount.h>
41#include <hfs/hfs_format.h>
42#include <string.h>
43#include <stdio.h>
44#include <errno.h>
45
46#include "bless.h"
47#include "bless_private.h"
48
49struct extinfo {
50    uint32_t length;
51    HFSPlusExtentRecord extents;
52};
53
54struct allocinfo {
55    uint32_t length;
56    off_t allocationSize;
57};
58
59/*
60 * First determine the device and the extents on the mounted volume
61 * Then parse the device to see if an offset needs to be added
62 */
63
64int BLGetDiskSectorsForFile(BLContextPtr context, const char * path, off_t extents[8][2],
65                            char * device, int deviceLen) {
66
67    struct statfs sb;
68    struct extinfo info;
69    struct allocinfo ainfo;
70    struct attrlist alist, blist;
71    char buffer[512];
72    HFSMasterDirectoryBlock *mdb = (HFSMasterDirectoryBlock  *) buffer;
73    off_t sectorsPerBlock, offset;
74    char rawdev[MNAMELEN];
75    int i,  fd;
76    int ret;
77
78    ret = statfs(path, &sb);
79    if(ret) {
80        contextprintf(context, kBLLogLevelError,  "Can't get information for %s\n", path );
81        return 1;
82    }
83
84    strlcpy(device, sb.f_mntfromname, deviceLen);
85
86    alist.bitmapcount = 5;
87    alist.reserved = 0;
88    alist.commonattr = 0;
89    alist.volattr =  0;
90    alist.dirattr = 0;
91    alist.fileattr = ATTR_FILE_DATAEXTENTS;
92    alist.forkattr = 0;
93
94    ret = getattrlist(path, &alist, &info, sizeof(info), 1);
95    if(ret) {
96        contextprintf(context, kBLLogLevelError,  "Could not get extents for %s: %d\n", path, errno);
97        return 1;
98    }
99
100    blist.bitmapcount = 5;
101    blist.reserved = 0;
102    blist.commonattr = 0;
103    blist.volattr =  ATTR_VOL_MINALLOCATION|ATTR_VOL_INFO;
104    blist.dirattr = 0;
105    blist.fileattr = 0;
106    blist.forkattr = 0;
107
108    ret = getattrlist(sb.f_mntonname, &blist, &ainfo, sizeof(ainfo), 1);
109    if(ret) {
110        contextprintf(context, kBLLogLevelError, "Could not get allocation block size for %s: %d\n", sb.f_mntonname, errno);
111        return 1;
112    }
113
114    sectorsPerBlock = ainfo.allocationSize / 512;
115
116    sprintf(rawdev, "/dev/r%s", device+5);
117
118    fd = open(rawdev, O_RDONLY, 0);
119    if(fd == -1) {
120            contextprintf(context, kBLLogLevelError,  "Could not open device to read Master Directory Block: %d\n", errno);
121            return 3;
122    }
123
124    lseek(fd, 1024, SEEK_SET);
125
126    if(512 != read(fd, buffer, 512)) {
127            contextprintf(context, kBLLogLevelError,  "Failed to read Master Directory Block\n");
128            return 3;
129    }
130
131
132    offset = 0;
133    if(CFSwapInt16BigToHost(mdb->drSigWord) == kHFSPlusSigWord) {
134        // pure HFS+
135        offset = 0;
136    } else if ((CFSwapInt16BigToHost(mdb->drSigWord) == kHFSSigWord)
137            && (CFSwapInt16BigToHost(mdb->drEmbedSigWord) == kHFSPlusSigWord)) {
138        // HFS+ embedded
139        offset = CFSwapInt16BigToHost(mdb->drAlBlSt)
140            + CFSwapInt16BigToHost(mdb->drEmbedExtent.startBlock)*
141	               (CFSwapInt32BigToHost(mdb->drAlBlkSiz) >> 9);
142    } else {
143        // pure HFS
144        offset = CFSwapInt16BigToHost(mdb->drAlBlSt);
145    }
146
147    close(fd);
148
149    for(i=0; i<8; i++) {
150        extents[i][0] = info.extents[i].startBlock*sectorsPerBlock+offset;
151        extents[i][1] = info.extents[i].blockCount*sectorsPerBlock;
152    }
153
154    return 0;
155}
156