3 Clusters Bitmap creation code.
5 Copyright (C) 2009, 2010 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 off_t cbm_alignment(void)
31 return CLUSTER_SIZE(sb);
36 return DIV_ROUND_UP(le32_to_cpu(sb.cluster_count), CHAR_BIT);
39 int cbm_write(off_t base, int fd)
41 uint32_t allocated_clusters =
42 DIV_ROUND_UP(cbm_size(), CLUSTER_SIZE(sb)) +
43 DIV_ROUND_UP(uct_size(), CLUSTER_SIZE(sb)) +
44 DIV_ROUND_UP(rootdir_size(), CLUSTER_SIZE(sb));
45 size_t bitmap_size = DIV_ROUND_UP(allocated_clusters, CHAR_BIT);
46 uint8_t* bitmap = malloc(bitmap_size);
52 for (i = 0; i < bitmap_size * CHAR_BIT; i++)
53 if (i < allocated_clusters)
57 if (write(fd, bitmap, bitmap_size) == -1)
61 sb.cluster_sector_start = cpu_to_le32(base / SECTOR_SIZE(sb));
62 bitmap_entry.start_cluster = cpu_to_le32(OFFSET_TO_CLUSTER(base));
63 bitmap_entry.size = cpu_to_le64(cbm_size());