/[pearpc]/src/io/prom/fs/hfsplus/partitions.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /src/io/prom/fs/hfsplus/partitions.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Wed Sep 5 17:11:21 2007 UTC (16 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 6002 byte(s)
import upstream CVS
1 dpavlin 1 /*
2     * hfsutils - tools for reading and writing Macintosh HFS volumes
3     * Copyright (C) 2000 Klaus Halfmann <klaus.halfmann@t-online.de>
4     *
5     * Structures and Functions for accessing the mac partition map
6     * Copyright (C) 2002 Michael Schulze <mike.s@genion.de>
7     *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21     *
22     */
23    
24     # ifdef HAVE_CONFIG_H
25     # include "config.h"
26     # endif
27    
28     #include "swab.h"
29     #include "partitions.h"
30     #include "os.h"
31     #include "libhfsp.h"
32    
33     #include <stdlib.h>
34     #include <string.h>
35    
36     /*
37     * Returns the number of partitions in the given partition map.
38     * probably we don't need this since we have map->numparts
39     */
40     int partition_getnparts( partition_map *map) {
41     return map->partitions[ 0]->pmMapBlkCnt;
42     }
43    
44     /*
45     * Copies <length> bytes from the beginning of source to dest.
46     * source will be incrementet to the byte after length.
47     */
48     static int splitCharArray( char **source, char *dest, int length) {
49     memcpy( dest, *source, length);
50     dest[ length- 1]= 0; /* force last byte to null */
51    
52     (*source)+= length;
53    
54     return 0;
55     }
56    
57     int partition_fillstruct( ApplePartition *p, char *buf) {
58     p->pmSig = bswabU16_inc(&buf);
59     p->pmSigPad = bswabU16_inc(&buf);
60     p->pmMapBlkCnt = bswabU32_inc(&buf);
61     p->pmPyPartStart = bswabU32_inc(&buf);
62     p->pmPartBlkCnt = bswabU32_inc(&buf);
63     splitCharArray( &buf, p->pmPartName, 32);
64     splitCharArray( &buf, p->pmPartType, 32);
65     p->pmLgDataStart = bswabU32_inc(&buf);
66     p->pmDataCnt = bswabU32_inc(&buf);
67     p->pmPartStatus = bswabU32_inc(&buf);
68     p->pmLgBootStart = bswabU32_inc(&buf);
69     p->pmBootSize = bswabU32_inc(&buf);
70     p->pmBootAddr = bswabU32_inc(&buf);
71     p->pmBootAddr2 = bswabU32_inc(&buf);
72     p->pmBootEntry = bswabU32_inc(&buf);
73     p->pmBootEntry2 = bswabU32_inc(&buf);
74     p->pmBootCksum = bswabU32_inc(&buf);
75    
76     return 0;
77     }
78    
79     /* sort the partitions according to their occurance on disc
80     * hasi: we'd better use qsort instead of reinventing the wheel ....*/
81     void partition_sort( partition_map *map) {
82    
83     ApplePartition **partitions = map->partitions;
84     ApplePartition *min;
85     int i, j, numparts= map->numparts;
86    
87     for( i= 0; i < numparts; i++) {
88     for( j= i+ 1; j< numparts; j++) {
89     if( partitions[ j]->pmPyPartStart< partitions[ i]->pmPyPartStart) {
90     min= partitions[ j];
91     partitions[ j]= partitions[ i];
92     partitions[ i]= min;
93     }
94     }
95     }
96     }
97    
98     /*
99     * Returns the partition map of the given device
100     *
101     * @param fd filedescripator (see os.h) must already be opened.
102     */
103     int partition_getPartitionMap( partition_map *map, void *fd) {
104     char buf[ HFSP_BLOCKSZ];
105     ApplePartition first; /* we use that to get the number of partitions in the map */
106     int i, numparts;
107    
108     if( hfsp_os_seek( &fd, 1, HFSP_BLOCKSZ_BITS)!= 1)
109     return -1;
110    
111     /* read the first partition info from the map */
112     if( hfsp_os_read( &fd, buf, 1, HFSP_BLOCKSZ_BITS)!= 1)
113     return -1;
114     if( partition_fillstruct( &first, buf))
115     return -1;
116    
117     /* check if this is a partition map */
118     if( first.pmSig!= PARTITION_SIG) {
119     map->numparts= 0;
120     return 0;
121     }
122    
123     /* set the number of partitions and allocate memory */
124     map->numparts = numparts = first.pmMapBlkCnt;
125     map->parray = ( ApplePartition *)malloc( numparts* sizeof( ApplePartition));
126     map->partitions = ( ApplePartition **)malloc( numparts* sizeof( void *));
127    
128     /* copy the first partition info to map */
129     memcpy( map->parray, &first, sizeof( ApplePartition));
130     map->partitions[ 0]= map->parray;
131    
132     for( i= 1; i < numparts; i++) {
133     if( ( hfsp_os_read( &fd, buf, 1, HFSP_BLOCKSZ_BITS)!= 1) ||
134     ( partition_fillstruct( &( map->parray[ i]), buf))) {
135     free( map->partitions);
136     free( map->parray);
137     map->numparts = 0; // so partition_release() will work correctly
138     return -1;
139     }
140    
141     map->partitions[ i]= &( map->parray[ i]);
142     }
143    
144     // partition_sort( map);
145    
146     return numparts;
147     }
148    
149    
150     /*
151     * Works like you would expect just by looking at the signature. It's not
152     * necessary to consult this comment.
153     *
154     * -- Sebastian
155     */
156     #include <stdio.h>
157     UInt32 partition_getStartBlock(partition_map *map, const char *type, int num)
158     {
159     ApplePartition **partitions = map->partitions;
160     int i;
161     for (i=0; i < map->numparts; i++) {
162     printf("getStartBlock: %d %s\n", i, partitions[num]->pmPartType);
163     }
164     if (num >= map->numparts) return 0;
165     if (strcmp(partitions[num]->pmPartType, type)) return 0;
166     return partitions[num]->pmPyPartStart;
167     }
168    
169     /*
170     * Returns the startblock of the <num>th partition of the given type from the given
171     * Partition Map.
172     * @param map Partition map to get the information from
173     * @param type type of the desired partition
174     * @param num number of the desired partition (starting with 1)
175     * @return the start block of the partition or 0 if no such partition could be found
176     */
177     /*UInt32 partition_getStartBlock(partition_map *map, const char *type, int num)
178     {
179     int i, startblock = 0;
180     ApplePartition **partitions = map->partitions;
181    
182     for (i= 0; i< map->numparts && num> 0; i++) {
183     if (!strcmp(partitions[i]->pmPartType, type)) {
184     startblock= partitions[i]->pmPyPartStart;
185     num--;
186     }
187     }
188    
189     return num ? 0 : startblock;
190     }*/
191    
192     /*
193     * cleanup and free allocated memory.
194     */
195     void partition_release(partition_map *map)
196     {
197     if (map->numparts > 0) {
198     free(map->partitions);
199     free(map->parray);
200     map->numparts = 0;
201     }
202     }
203    

  ViewVC Help
Powered by ViewVC 1.1.26