3 exFAT file system implementation library.
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/>.
27 #include <sys/types.h>
29 #define _XOPEN_SOURCE /* for tzset() in Linux */
32 static uint64_t rootdir_size(const struct exfat* ef)
34 uint64_t clusters = 0;
35 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
37 while (!CLUSTER_INVALID(rootdir_cluster))
40 /* root directory cannot be contiguous because there is no flag
42 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
44 return clusters * CLUSTER_SIZE(*ef->sb);
47 static const char* get_option(const char* options, const char* option_name)
50 size_t length = strlen(option_name);
52 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
53 if ((p == options || p[-1] == ',') && p[length] == '=')
54 return p + length + 1;
58 static int get_int_option(const char* options, const char* option_name,
59 int base, int default_value)
61 const char* p = get_option(options, option_name);
65 return strtol(p, NULL, base);
68 static int match_option(const char* options, const char* option_name)
71 size_t length = strlen(option_name);
73 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
74 if ((p == options || p[-1] == ',') &&
75 (p[length] == ',' || p[length] == '\0'))
80 static void parse_options(struct exfat* ef, const char* options)
82 int sys_umask = umask(0);
85 umask(sys_umask); /* restore umask */
86 opt_umask = get_int_option(options, "umask", 8, sys_umask);
87 ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
88 ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
90 ef->uid = get_int_option(options, "uid", 10, geteuid());
91 ef->gid = get_int_option(options, "gid", 10, getegid());
93 ef->ro = match_option(options, "ro");
94 ef->noatime = match_option(options, "noatime");
97 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
103 memset(ef, 0, sizeof(struct exfat));
105 ef->sb = malloc(sizeof(struct exfat_super_block));
108 exfat_error("memory allocation failed");
111 memset(ef->sb, 0, sizeof(struct exfat_super_block));
113 parse_options(ef, options);
115 ef->fd = open(spec, ef->ro ? O_RDONLY : O_RDWR);
119 exfat_error("failed to open `%s'", spec);
123 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
124 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
128 exfat_error("exFAT file system is not found");
131 fs_version = le16_to_cpu(ef->sb->version);
132 if (fs_version != 0x0100)
136 exfat_error("unsupported exFAT version: %hu.%hu",
137 fs_version >> 8, fs_version & 0xff);
140 if (ef->sb->fat_count != 1)
144 exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count);
147 /* officially exFAT supports cluster size up to 32 MB */
148 if ((int) ef->sb->block_bits + (int) ef->sb->bpc_bits > 25)
152 exfat_error("too big cluster size: 2^%d",
153 (int) ef->sb->block_bits + (int) ef->sb->bpc_bits);
157 ef->zero_block = malloc(BLOCK_SIZE(*ef->sb));
158 if (ef->zero_block == NULL)
162 exfat_error("failed to allocate zero block");
165 memset(ef->zero_block, 0, BLOCK_SIZE(*ef->sb));
167 ef->root = malloc(sizeof(struct exfat_node));
168 if (ef->root == NULL)
170 free(ef->zero_block);
173 exfat_error("failed to allocate root node");
176 memset(ef->root, 0, sizeof(struct exfat_node));
177 ef->root->flags = EXFAT_ATTRIB_DIR;
178 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
179 ef->root->fptr_cluster = ef->root->start_cluster;
180 ef->root->name[0] = cpu_to_le16('\0');
181 ef->root->size = rootdir_size(ef);
182 /* exFAT does not have time attributes for the root directory */
185 /* always keep at least 1 reference to the root node */
186 exfat_get_node(ef->root);
188 rc = exfat_cache_directory(ef, ef->root);
192 free(ef->zero_block);
201 void exfat_unmount(struct exfat* ef)
203 exfat_put_node(ef, ef->root);
204 exfat_reset_cache(ef);
207 free(ef->zero_block);
208 ef->zero_block = NULL;
209 free(ef->cmap.chunk);
210 ef->cmap.chunk = NULL;
217 ef->upcase_chars = 0;