OSDN Git Service

Implement nodes cache (in-core directory structure representation).
[android-x86/external-exfat.git] / libexfat / utils.c
1 /*
2  *  utils.c
3  *  exFAT file system implementation library.
4  *
5  *  Created by Andrew Nayenko on 04.09.09.
6  *  This software is distributed under the GNU General Public License 
7  *  version 3 or any later.
8  */
9
10 #include "exfat.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #define _XOPEN_SOURCE /* for timezone in Linux */
17 #include <time.h>
18 #include <sys/time.h>
19
20 static uint64_t rootdir_size(const struct exfat* ef)
21 {
22         uint64_t clusters = 0;
23         cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
24
25         while (!CLUSTER_INVALID(rootdir_cluster))
26         {
27                 clusters++;
28                 /* root directory cannot be contiguous because there is no flag
29                    to indicate this */
30                 rootdir_cluster = exfat_next_cluster(ef, rootdir_cluster, 0);
31         }
32         return clusters * CLUSTER_SIZE(*ef->sb);
33 }
34
35 int exfat_mount(struct exfat* ef, const char* spec)
36 {
37         tzset();
38
39         ef->sb = malloc(sizeof(struct exfat_super_block));
40         if (ef->sb == NULL)
41         {
42                 exfat_error("memory allocation failed");
43                 return -ENOMEM;
44         }
45
46         ef->fd = open(spec, O_RDONLY); /* currently read only */
47         if (ef->fd < 0)
48         {
49                 free(ef->sb);
50                 exfat_error("failed to open `%s'", spec);
51                 return -EIO;
52         }
53
54         exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
55         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
56         {
57                 close(ef->fd);
58                 free(ef->sb);
59                 exfat_error("exFAT file system is not found");
60                 return -EIO;
61         }
62
63         ef->upcase = NULL;
64         ef->upcase_chars = 0;
65         ef->rootdir_size = rootdir_size(ef);
66
67         ef->root = malloc(sizeof(struct exfat_node));
68         if (ef->root == NULL)
69         {
70                 close(ef->fd);
71                 free(ef->sb);
72                 exfat_error("failed to allocate root node");
73                 return -ENOMEM;
74         }
75         memset(ef->root, 0, sizeof(struct exfat_node));
76         ef->root->flags = EXFAT_ATTRIB_DIR;
77         ef->root->size = ef->rootdir_size;
78         ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
79         ef->root->name[0] = cpu_to_le16('\0');
80         /* exFAT does not have time attributes for the root directory */
81         ef->root->mtime = 0;
82         ef->root->atime = 0;
83         /* always keep at least 1 reference to the root node */
84         exfat_get_node(ef->root);
85
86         return 0;
87 }
88
89 void exfat_unmount(struct exfat* ef)
90 {
91         exfat_put_node(ef->root);
92         exfat_reset_cache(ef);
93         ef->root = NULL;
94         close(ef->fd);
95         ef->fd = 0;
96         free(ef->sb);
97         ef->sb = NULL;
98         free(ef->upcase);
99         ef->upcase = NULL;
100         ef->upcase_chars = 0;
101 }
102
103 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
104 {
105         memset(stbuf, 0, sizeof(struct stat));
106         if (node->flags & EXFAT_ATTRIB_DIR)
107                 stbuf->st_mode = S_IFDIR | 0755;
108         else
109                 stbuf->st_mode = S_IFREG | 0444;
110         stbuf->st_nlink = 1;
111         stbuf->st_size = node->size;
112         stbuf->st_mtime = node->mtime;
113         stbuf->st_atime = node->atime;
114         stbuf->st_ctime = 0; /* unapplicable */
115 }
116
117 #define SEC_IN_MIN 60ll
118 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
119 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
120 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
121 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
122 #define UNIX_EPOCH_YEAR 1970
123 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
124 #define EXFAT_EPOCH_YEAR 1980
125 /* number of years from Unix epoch to exFAT epoch */
126 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
127 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
128 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
129 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
130 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
131 /* number of leap years passed from exFAT epoch to the specified year
132    (excluding the specified year itself) */
133 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
134                 - (EXFAT_EPOCH_YEAR - 1) / 4)
135 /* checks whether the specified year is leap */
136 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
137
138 static const time_t days_in_year[] =
139 {
140         /* Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec */
141         0,   0,  31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334
142 };
143
144 union exfat_date
145 {
146         uint16_t raw;
147         struct
148         {
149                 uint16_t day   : 5; /* 1-31 */
150                 uint16_t month : 4; /* 1-12 */
151                 uint16_t year  : 7; /* 1-127 (+1980) */
152         };
153 };
154
155 union exfat_time
156 {
157         uint16_t raw;
158         struct
159         {
160                 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
161                 uint16_t min    : 6; /* 0-59 */
162                 uint16_t hour   : 5; /* 0-23 */
163         };
164 };
165
166 time_t exfat_exfat2unix(le16_t date, le16_t time)
167 {
168         union exfat_date edate;
169         union exfat_time etime;
170         time_t unix_time = EPOCH_DIFF_SEC;
171
172         edate.raw = le16_to_cpu(date);
173         etime.raw = le16_to_cpu(time);
174
175         if (edate.day == 0 || edate.month == 0 || edate.month > 12)
176         {
177                 exfat_error("bad date %hu-%02hu-%02hu",
178                                 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
179                 return 0;
180         }
181         if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
182         {
183                 exfat_error("bad time %hu:%02hu:%02hu",
184                         etime.hour, etime.min, etime.twosec * 2);
185                 return 0;
186         }
187
188         /* every 4th year between 1904 and 2096 is leap */
189         unix_time += edate.year * SEC_IN_YEAR + LEAP_YEARS(edate.year) * SEC_IN_DAY;
190         unix_time += days_in_year[edate.month] * SEC_IN_DAY;
191         /* if it's leap year and February has passed we should add 1 day */
192         if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
193                 unix_time += SEC_IN_DAY;
194         unix_time += (edate.day - 1) * SEC_IN_DAY;
195
196         unix_time += etime.hour * SEC_IN_HOUR;
197         unix_time += etime.min * SEC_IN_MIN;
198         /* exFAT represents time with 2 sec granularity */
199         unix_time += etime.twosec * 2;
200
201         /* exFAT stores timestamps in local time, so we correct it to UTC */
202         unix_time += timezone;
203
204         return unix_time;
205 }
206
207 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
208 {
209         union exfat_date edate;
210         union exfat_time etime;
211         time_t shift = EPOCH_DIFF_SEC + timezone;
212         int days;
213         int i;
214
215         /* time before exFAT epoch cannot be represented */
216         if (unix_time < shift)
217                 unix_time = shift;
218
219         unix_time -= shift;
220
221         days = unix_time / SEC_IN_DAY;
222         edate.year = (4 * days) / (4 * 365 + 1);
223         days -= edate.year * 365 + LEAP_YEARS(edate.year);
224         for (i = 1; i <= 12; i++)
225         {
226                 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
227                 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
228
229                 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
230                 {
231                         edate.month = i;
232                         days -= days_in_year[i] + leap_sub;
233                         break;
234                 }
235         }
236         edate.day = days + 1;
237
238         etime.hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
239         etime.min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
240         etime.twosec = (unix_time % SEC_IN_MIN) / 2;
241
242         *date = cpu_to_le16(edate.raw);
243         *time = cpu_to_le16(etime.raw);
244 }
245
246 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
247 {
248         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
249                 exfat_bug("failed to convert name to UTF-8");
250 }