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);
70 void exfat_unmount(struct exfat* ef)
81 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
83 memset(stbuf, 0, sizeof(struct stat));
84 if (node->flags & EXFAT_ATTRIB_DIR)
85 stbuf->st_mode = S_IFDIR | 0755;
87 stbuf->st_mode = S_IFREG | 0444;
89 stbuf->st_size = node->size;
90 stbuf->st_mtime = node->mtime;
91 stbuf->st_atime = node->atime;
92 stbuf->st_ctime = 0; /* unapplicable */
95 #define SEC_IN_MIN 60ll
96 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
97 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
98 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
99 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
100 #define UNIX_EPOCH_YEAR 1970
101 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
102 #define EXFAT_EPOCH_YEAR 1980
103 /* number of years from Unix epoch to exFAT epoch */
104 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
105 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
106 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
107 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
108 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
110 static const time_t days_in_year[] =
112 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
113 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
121 uint16_t day : 5; /* 1-31 */
122 uint16_t month : 4; /* 1-12 */
123 uint16_t year : 7; /* 1-127 (+1980) */
132 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
133 uint16_t min : 6; /* 0-59 */
134 uint16_t hour : 5; /* 0-23 */
138 time_t exfat_exfat2unix(le16_t date, le16_t time)
140 union exfat_date edate;
141 union exfat_time etime;
142 time_t unix_time = EPOCH_DIFF_SEC;
144 edate.raw = le16_to_cpu(date);
145 etime.raw = le16_to_cpu(time);
148 exfat_debug("%hu-%02hu-%02hu %hu:%02hu:%02hu",
149 edate.year + 1980, edate.month, edate.day,
150 etime.hour, etime.min, etime.twosec * 2);
153 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
155 exfat_error("bad date %hu-%02hu-%02hu",
156 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
159 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
161 exfat_error("bad time %hu:%02hu:%02hu",
162 etime.hour, etime.min, etime.twosec * 2);
166 /* every 4th year between 1904 and 2096 is leap */
167 unix_time += edate.year * SEC_IN_YEAR
168 + ((EXFAT_EPOCH_YEAR + edate.year - 1) / 4 - (EXFAT_EPOCH_YEAR - 1) / 4)
170 unix_time += days_in_year[edate.month] * SEC_IN_DAY;
171 /* if it's leap year and February has passed we should add 1 day */
172 if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
173 unix_time += SEC_IN_DAY;
174 unix_time += (edate.day - 1) * SEC_IN_DAY;
176 unix_time += etime.hour * SEC_IN_HOUR;
177 unix_time += etime.min * SEC_IN_MIN;
178 /* exFAT represents time with 2 sec granularity */
179 unix_time += etime.twosec * 2;
181 /* exFAT stores timestamps in local time, so we correct it to UTC */
182 unix_time += timezone;
187 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
189 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
190 exfat_bug("failed to convert name to UTF-8");