3 * exFAT file system implementation library.
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.
16 #define _XOPEN_SOURCE /* for timezone in Linux */
20 static uint64_t rootdir_size(const struct exfat* ef)
22 uint64_t clusters = 0;
23 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
25 while (!CLUSTER_INVALID(rootdir_cluster))
28 /* root directory cannot be contiguous because there is no flag
30 rootdir_cluster = exfat_next_cluster(ef, rootdir_cluster, 0);
32 return clusters * CLUSTER_SIZE(*ef->sb);
35 int exfat_mount(struct exfat* ef, const char* spec)
39 ef->sb = malloc(sizeof(struct exfat_super_block));
42 exfat_error("memory allocation failed");
46 ef->fd = open(spec, O_RDONLY); /* currently read only */
50 exfat_error("failed to open `%s'", spec);
54 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
55 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
59 exfat_error("exFAT file system is not found");
65 ef->rootdir_size = rootdir_size(ef);
67 ef->root = malloc(sizeof(struct exfat_node));
72 exfat_error("failed to allocate root node");
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 */
83 /* always keep at least 1 reference to the root node */
84 exfat_get_node(ef->root);
89 void exfat_unmount(struct exfat* ef)
91 exfat_put_node(ef->root);
92 exfat_reset_cache(ef);
100 ef->upcase_chars = 0;
103 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
105 memset(stbuf, 0, sizeof(struct stat));
106 if (node->flags & EXFAT_ATTRIB_DIR)
107 stbuf->st_mode = S_IFDIR | 0755;
109 stbuf->st_mode = S_IFREG | 0444;
111 stbuf->st_size = node->size;
112 stbuf->st_mtime = node->mtime;
113 stbuf->st_atime = node->atime;
114 stbuf->st_ctime = 0; /* unapplicable */
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)
138 static const time_t days_in_year[] =
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
149 uint16_t day : 5; /* 1-31 */
150 uint16_t month : 4; /* 1-12 */
151 uint16_t year : 7; /* 1-127 (+1980) */
160 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
161 uint16_t min : 6; /* 0-59 */
162 uint16_t hour : 5; /* 0-23 */
166 time_t exfat_exfat2unix(le16_t date, le16_t time)
168 union exfat_date edate;
169 union exfat_time etime;
170 time_t unix_time = EPOCH_DIFF_SEC;
172 edate.raw = le16_to_cpu(date);
173 etime.raw = le16_to_cpu(time);
175 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
177 exfat_error("bad date %hu-%02hu-%02hu",
178 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
181 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
183 exfat_error("bad time %hu:%02hu:%02hu",
184 etime.hour, etime.min, etime.twosec * 2);
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;
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;
201 /* exFAT stores timestamps in local time, so we correct it to UTC */
202 unix_time += timezone;
207 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time)
209 union exfat_date edate;
210 union exfat_time etime;
211 time_t shift = EPOCH_DIFF_SEC + timezone;
215 /* time before exFAT epoch cannot be represented */
216 if (unix_time < shift)
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++)
226 int leap_day = (IS_LEAP_YEAR(edate.year) && i == 2);
227 int leap_sub = (IS_LEAP_YEAR(edate.year) && i >= 3);
229 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
232 days -= days_in_year[i] + leap_sub;
236 edate.day = days + 1;
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;
242 *date = cpu_to_le16(edate.raw);
243 *time = cpu_to_le16(etime.raw);
246 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
248 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
249 exfat_bug("failed to convert name to UTF-8");