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 void parse_options(struct exfat* ef, const char* options)
59 int sys_umask = umask(0);
62 umask(sys_umask); /* restore umask */
63 opt_umask = get_int_option(options, "umask", 8, sys_umask);
64 ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777;
65 ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777;
67 ef->uid = get_int_option(options, "uid", 10, geteuid());
68 ef->gid = get_int_option(options, "gid", 10, getegid());
71 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
74 memset(ef, 0, sizeof(struct exfat));
76 ef->sb = malloc(sizeof(struct exfat_super_block));
79 exfat_error("memory allocation failed");
82 memset(ef->sb, 0, sizeof(struct exfat_super_block));
84 parse_options(ef, options);
86 ef->fd = open(spec, O_RDWR);
90 exfat_error("failed to open `%s'", spec);
94 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
95 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
99 exfat_error("exFAT file system is not found");
103 ef->zero_block = malloc(BLOCK_SIZE(*ef->sb));
104 if (ef->zero_block == NULL)
108 exfat_error("failed to allocate zero block");
111 memset(ef->zero_block, 0, BLOCK_SIZE(*ef->sb));
113 ef->root = malloc(sizeof(struct exfat_node));
114 if (ef->root == NULL)
116 free(ef->zero_block);
119 exfat_error("failed to allocate root node");
122 memset(ef->root, 0, sizeof(struct exfat_node));
123 ef->root->flags = EXFAT_ATTRIB_DIR;
124 ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
125 ef->root->fptr_cluster = ef->root->start_cluster;
126 ef->root->name[0] = cpu_to_le16('\0');
127 ef->root->size = rootdir_size(ef);
128 /* exFAT does not have time attributes for the root directory */
131 /* always keep at least 1 reference to the root node */
132 exfat_get_node(ef->root);
137 void exfat_unmount(struct exfat* ef)
139 exfat_put_node(ef, ef->root);
140 exfat_reset_cache(ef);
143 free(ef->zero_block);
144 ef->zero_block = NULL;
145 free(ef->cmap.chunk);
146 ef->cmap.chunk = NULL;
153 ef->upcase_chars = 0;