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");
60 ef->mount_time = time(NULL);
63 ef->rootdir_size = rootdir_size(ef);
68 void exfat_unmount(struct exfat* ef)
79 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
81 memset(stbuf, 0, sizeof(struct stat));
82 if (node->flags & EXFAT_ATTRIB_DIR)
83 stbuf->st_mode = S_IFDIR | 0755;
85 stbuf->st_mode = S_IFREG | 0444;
87 stbuf->st_size = node->size;
88 stbuf->st_mtime = node->mtime;
89 stbuf->st_atime = node->atime;
90 stbuf->st_ctime = 0; /* unapplicable */
93 #define SEC_IN_MIN 60ll
94 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
95 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
96 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
97 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
98 #define UNIX_EPOCH_YEAR 1970
99 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
100 #define EXFAT_EPOCH_YEAR 1980
101 /* number of years from Unix epoch to exFAT epoch */
102 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
103 /* number of seconds from Unix epoch to exFAT epoch (considering leap years) */
104 #define EPOCH_DIFF_SEC (EPOCH_DIFF_YEAR*365 + EPOCH_DIFF_YEAR/4) * SEC_IN_DAY
106 static const time_t days_in_year[] =
108 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
109 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
117 uint16_t day : 5; /* 1-31 */
118 uint16_t month : 4; /* 1-12 */
119 uint16_t year : 7; /* 1-127 (+1980) */
128 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
129 uint16_t min : 6; /* 0-59 */
130 uint16_t hour : 5; /* 0-23 */
134 static time_t get_time_shift(void)
139 if (gettimeofday(&tv, &tz) != 0)
141 return -tz.tz_minuteswest * SEC_IN_MIN;
144 time_t exfat_exfat2unix(le16_t date, le16_t time)
146 union exfat_date edate;
147 union exfat_time etime;
148 time_t unix_time = EPOCH_DIFF_SEC;
150 edate.raw = le16_to_cpu(date);
151 etime.raw = le16_to_cpu(time);
154 exfat_debug("%hu-%02hu-%02hu %hu:%02hu:%02hu",
155 edate.year + 1980, edate.month, edate.day,
156 etime.hour, etime.min, etime.twosec * 2);
159 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
161 exfat_error("bad date %hu-%02hu-%02hu",
162 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
165 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
167 exfat_error("bad time %hu:%02hu:%02hu",
168 etime.hour, etime.min, etime.twosec * 2);
172 /* every 4th year between 1904 and 2096 is leap */
173 unix_time += edate.year * SEC_IN_YEAR + edate.year / 4 * SEC_IN_DAY;
174 unix_time += days_in_year[edate.month] * SEC_IN_DAY;
175 /* if it's leap year and February has passed we should add 1 day */
176 if (edate.year % 4 == 0 && edate.month > 2)
177 unix_time += SEC_IN_DAY;
178 unix_time += edate.day * SEC_IN_DAY;
180 unix_time += etime.hour * SEC_IN_HOUR;
181 unix_time += etime.min * SEC_IN_MIN;
182 /* exFAT represents time with 2 sec granularity */
183 unix_time += etime.twosec * 2;
185 /* exFAT stores timestamps in local time, so we correct it to UTC */
186 unix_time -= get_time_shift();
191 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
193 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
194 exfat_bug("failed to convert name to UTF-8");