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.
19 static uint64_t rootdir_size(const struct exfat* ef)
21 uint64_t clusters = 0;
22 cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
24 while (!CLUSTER_INVALID(rootdir_cluster))
27 /* root directory cannot be contiguous because there is no flag
29 rootdir_cluster = exfat_next_cluster(ef, rootdir_cluster, 0);
31 return clusters * CLUSTER_SIZE(*ef->sb);
34 int exfat_mount(struct exfat* ef, const char* spec)
36 ef->sb = malloc(sizeof(struct exfat_super_block));
39 exfat_error("memory allocation failed");
43 ef->fd = open(spec, O_RDONLY); /* currently read only */
47 exfat_error("failed to open `%s'", spec);
51 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
52 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
56 exfat_error("exFAT file system is not found");
62 ef->rootdir_size = rootdir_size(ef);
67 void exfat_unmount(struct exfat* ef)
78 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
80 memset(stbuf, 0, sizeof(struct stat));
81 if (node->flags & EXFAT_ATTRIB_DIR)
82 stbuf->st_mode = S_IFDIR | 0755;
84 stbuf->st_mode = S_IFREG | 0444;
86 stbuf->st_size = node->size;
87 stbuf->st_mtime = node->mtime;
88 stbuf->st_atime = node->atime;
89 stbuf->st_ctime = 0; /* unapplicable */
92 #define SEC_IN_MIN 60ll
93 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
94 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
95 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
96 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
97 #define UNIX_EPOCH_YEAR 1970
98 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
99 #define EXFAT_EPOCH_YEAR 1980
100 /* number of years from Unix epoch to exFAT epoch */
101 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
102 /* number of seconds from Unix epoch to exFAT epoch (considering leap years) */
103 #define EPOCH_DIFF_SEC (EPOCH_DIFF_YEAR*365 + EPOCH_DIFF_YEAR/4) * SEC_IN_DAY
105 static const time_t days_in_year[] =
107 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
108 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
116 uint16_t day : 5; /* 1-31 */
117 uint16_t month : 4; /* 1-12 */
118 uint16_t year : 7; /* 1-127 (+1980) */
127 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
128 uint16_t min : 6; /* 0-59 */
129 uint16_t hour : 5; /* 0-23 */
133 static time_t get_time_shift(void)
138 if (gettimeofday(&tv, &tz) != 0)
140 return tz.tz_minuteswest * SEC_IN_MIN;
143 time_t exfat_exfat2unix(le16_t date, le16_t time)
145 union exfat_date edate;
146 union exfat_time etime;
147 time_t unix_time = EPOCH_DIFF_SEC;
149 edate.raw = le16_to_cpu(date);
150 etime.raw = le16_to_cpu(time);
153 exfat_debug("%hu-%02hu-%02hu %hu:%02hu:%02hu",
154 edate.year + 1980, edate.month, edate.day,
155 etime.hour, etime.min, etime.twosec * 2);
158 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
160 exfat_error("bad date %hu-%02hu-%02hu",
161 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
164 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
166 exfat_error("bad time %hu:%02hu:%02hu",
167 etime.hour, etime.min, etime.twosec * 2);
171 /* every 4th year between 1904 and 2096 is leap */
172 unix_time += edate.year * SEC_IN_YEAR + edate.year / 4 * SEC_IN_DAY;
173 unix_time += days_in_year[edate.month] * SEC_IN_DAY;
174 /* if it's leap year and February has passed we should add 1 day */
175 if (edate.year % 4 == 0 && edate.month > 2)
176 unix_time += SEC_IN_DAY;
177 unix_time += (edate.day - 1) * SEC_IN_DAY;
179 unix_time += etime.hour * SEC_IN_HOUR;
180 unix_time += etime.min * SEC_IN_MIN;
181 /* exFAT represents time with 2 sec granularity */
182 unix_time += etime.twosec * 2;
184 /* exFAT stores timestamps in local time, so we correct it to UTC */
185 unix_time += get_time_shift();
190 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
192 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
193 exfat_bug("failed to convert name to UTF-8");