OSDN Git Service

Initial code drop.
[android-x86/external-exfat.git] / libexfat / cluster.c
1 /*
2  *  cluster.c
3  *  exFAT file system implementation library.
4  *
5  *  Created by Andrew Nayenko on 03.09.09.
6  *  This software is distributed under the GNU General Public License 
7  *  version 3 or any later.
8  */
9
10 #include "exfat.h"
11
12 /*
13  * Cluster to block.
14  */
15 static uint32_t c2b(const struct exfat* ef, cluster_t cluster)
16 {
17         if (cluster < EXFAT_FIRST_DATA_CLUSTER)
18                 exfat_bug("invalid cluster number %u", cluster);
19         return le32_to_cpu(ef->sb->cluster_block_start) +
20                 ((cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->bpc_bits);
21 }
22
23 /*
24  * Cluster to absolute offset.
25  */
26 off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
27 {
28         return (off_t) c2b(ef, cluster) << ef->sb->block_bits;
29 }
30
31 /*
32  * Block to absolute offset.
33  */
34 static off_t b2o(const struct exfat* ef, uint32_t block)
35 {
36         return (off_t) block << ef->sb->block_bits;
37 }
38
39 cluster_t exfat_next_cluster(const struct exfat* ef, cluster_t cluster,
40                 int contiguous)
41 {
42         cluster_t next;
43         off_t fat_offset;
44
45         if (contiguous)
46                 return cluster + 1;
47         fat_offset = b2o(ef, le32_to_cpu(ef->sb->fat_block_start))
48                 + cluster * sizeof(cluster_t);
49         exfat_read_raw(&next, sizeof(next), fat_offset, ef->fd);
50         return next;
51 }
52
53 cluster_t exfat_advance_cluster(const struct exfat* ef, cluster_t cluster,
54                 int contiguous, uint32_t count)
55 {
56         uint32_t i;
57
58         for (i = 0; i < count; i++)
59         {
60                 cluster = exfat_next_cluster(ef, cluster, contiguous);
61                 if (CLUSTER_INVALID(cluster))
62                         break;
63         }
64         return cluster;
65 }