3 * exFAT file system implementation library.
5 * Created by Andrew Nayenko on 22.10.09.
6 * This software is distributed under the GNU General Public License
7 * version 3 or any later.
16 #include <sys/types.h>
18 #define _XOPEN_SOURCE /* for tzset() in Linux */
21 static uint64_t rootdir_size(const struct exfat* ef)
23 uint64_t clusters = 0;
24 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
26 while (!CLUSTER_INVALID(rootdir_cluster))
29 /* root directory cannot be contiguous because there is no flag
31 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
33 return clusters * CLUSTER_SIZE(*ef->sb);
36 static const char* get_option(const char* options, const char* option_name)
39 size_t length = strlen(option_name);
41 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
42 if ((p == options || p[-1] == ',') && p[length] == '=')
43 return p + length + 1;
47 static int get_int_option(const char* options, const char* option_name,
48 int base, int default_value)
50 const char* p = get_option(options, option_name);
54 return strtol(p, NULL, base);
57 static int match_option(const char* options, const char* option_name)
60 size_t length = strlen(option_name);
62 for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
63 if ((p == options || p[-1] == ',') &&
64 (p[length] == ',' || p[length] == '\0'))
69 static void parse_options(struct exfat* ef, const char* options)
71 int sys_umask = umask(0);
74 umask(sys_umask); /* restore umask */
75 opt_umask = get_int_option(options, "umask", 8, sys_umask);
76 ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
77 ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
79 ef->uid = get_int_option(options, "uid", 10, geteuid());
80 ef->gid = get_int_option(options, "gid", 10, getegid());
82 ef->ro = match_option(options, "ro");
83 ef->noatime = match_option(options, "noatime");
86 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
89 memset(ef, 0, sizeof(struct exfat));
91 ef->sb = malloc(sizeof(struct exfat_super_block));
94 exfat_error("memory allocation failed");
97 memset(ef->sb, 0, sizeof(struct exfat_super_block));
99 parse_options(ef, options);
101 ef->fd = open(spec, ef->ro ? O_RDONLY : O_RDWR);
105 exfat_error("failed to open `%s'", spec);
109 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
110 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
114 exfat_error("exFAT file system is not found");
118 ef->zero_block = malloc(BLOCK_SIZE(*ef->sb));
119 if (ef->zero_block == NULL)
123 exfat_error("failed to allocate zero block");
126 memset(ef->zero_block, 0, BLOCK_SIZE(*ef->sb));
128 ef->root = malloc(sizeof(struct exfat_node));
129 if (ef->root == NULL)
131 free(ef->zero_block);
134 exfat_error("failed to allocate root node");
137 memset(ef->root, 0, sizeof(struct exfat_node));
138 ef->root->flags = EXFAT_ATTRIB_DIR;
139 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
140 ef->root->fptr_cluster = ef->root->start_cluster;
141 ef->root->name[0] = cpu_to_le16('\0');
142 ef->root->size = rootdir_size(ef);
143 /* exFAT does not have time attributes for the root directory */
146 /* always keep at least 1 reference to the root node */
147 exfat_get_node(ef->root);
152 void exfat_unmount(struct exfat* ef)
154 exfat_put_node(ef, ef->root);
155 exfat_reset_cache(ef);
158 free(ef->zero_block);
159 ef->zero_block = NULL;
160 free(ef->cmap.chunk);
161 ef->cmap.chunk = NULL;
168 ef->upcase_chars = 0;