OSDN Git Service

Changed source files headings to meet the FSF recommendations.
[android-x86/external-exfat.git] / libexfat / mount.c
1 /*
2         mount.c (22.10.09)
3         exFAT file system implementation library.
4
5         Copyright (C) 2009, 2010  Andrew Nayenko
6
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.
11
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.
16
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/>.
19 */
20
21 #include "exfat.h"
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #define _XOPEN_SOURCE /* for tzset() in Linux */
30 #include <time.h>
31
32 static uint64_t rootdir_size(const struct exfat* ef)
33 {
34         uint64_t clusters = 0;
35         cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
36
37         while (!CLUSTER_INVALID(rootdir_cluster))
38         {
39                 clusters++;
40                 /* root directory cannot be contiguous because there is no flag
41                    to indicate this */
42                 rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster);
43         }
44         return clusters * CLUSTER_SIZE(*ef->sb);
45 }
46
47 static const char* get_option(const char* options, const char* option_name)
48 {
49         const char* p;
50         size_t length = strlen(option_name);
51
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;
55         return NULL;
56 }
57
58 static int get_int_option(const char* options, const char* option_name,
59                 int base, int default_value)
60 {
61         const char* p = get_option(options, option_name);
62
63         if (p == NULL)
64                 return default_value;
65         return strtol(p, NULL, base);
66 }
67
68 static int match_option(const char* options, const char* option_name)
69 {
70         const char* p;
71         size_t length = strlen(option_name);
72
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'))
76                         return 1;
77         return 0;
78 }
79
80 static void parse_options(struct exfat* ef, const char* options)
81 {
82         int sys_umask = umask(0);
83         int opt_umask;
84
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;
89
90         ef->uid = get_int_option(options, "uid", 10, geteuid());
91         ef->gid = get_int_option(options, "gid", 10, getegid());
92
93         ef->ro = match_option(options, "ro");
94         ef->noatime = match_option(options, "noatime");
95 }
96
97 int exfat_mount(struct exfat* ef, const char* spec, const char* options)
98 {
99         tzset();
100         memset(ef, 0, sizeof(struct exfat));
101
102         ef->sb = malloc(sizeof(struct exfat_super_block));
103         if (ef->sb == NULL)
104         {
105                 exfat_error("memory allocation failed");
106                 return -ENOMEM;
107         }
108         memset(ef->sb, 0, sizeof(struct exfat_super_block));
109
110         parse_options(ef, options);
111
112         ef->fd = open(spec, ef->ro ? O_RDONLY : O_RDWR);
113         if (ef->fd < 0)
114         {
115                 free(ef->sb);
116                 exfat_error("failed to open `%s'", spec);
117                 return -EIO;
118         }
119
120         exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
121         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
122         {
123                 close(ef->fd);
124                 free(ef->sb);
125                 exfat_error("exFAT file system is not found");
126                 return -EIO;
127         }
128
129         ef->zero_block = malloc(BLOCK_SIZE(*ef->sb));
130         if (ef->zero_block == NULL)
131         {
132                 close(ef->fd);
133                 free(ef->sb);
134                 exfat_error("failed to allocate zero block");
135                 return -ENOMEM;
136         }
137         memset(ef->zero_block, 0, BLOCK_SIZE(*ef->sb));
138
139         ef->root = malloc(sizeof(struct exfat_node));
140         if (ef->root == NULL)
141         {
142                 free(ef->zero_block);
143                 close(ef->fd);
144                 free(ef->sb);
145                 exfat_error("failed to allocate root node");
146                 return -ENOMEM;
147         }
148         memset(ef->root, 0, sizeof(struct exfat_node));
149         ef->root->flags = EXFAT_ATTRIB_DIR;
150         ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
151         ef->root->fptr_cluster = ef->root->start_cluster;
152         ef->root->name[0] = cpu_to_le16('\0');
153         ef->root->size = rootdir_size(ef);
154         /* exFAT does not have time attributes for the root directory */
155         ef->root->mtime = 0;
156         ef->root->atime = 0;
157         /* always keep at least 1 reference to the root node */
158         exfat_get_node(ef->root);
159
160         return 0;
161 }
162
163 void exfat_unmount(struct exfat* ef)
164 {
165         exfat_put_node(ef, ef->root);
166         exfat_reset_cache(ef);
167         free(ef->root);
168         ef->root = NULL;
169         free(ef->zero_block);
170         ef->zero_block = NULL;
171         free(ef->cmap.chunk);
172         ef->cmap.chunk = NULL;
173         close(ef->fd);
174         ef->fd = 0;
175         free(ef->sb);
176         ef->sb = NULL;
177         free(ef->upcase);
178         ef->upcase = NULL;
179         ef->upcase_chars = 0;
180 }