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 int exfat_mount(struct exfat* ef, const char* spec)
21 ef->sb = malloc(sizeof(struct exfat_super_block));
24 exfat_error("memory allocation failed");
28 ef->fd = open(spec, O_RDONLY); /* currently read only */
32 exfat_error("failed to open `%s'", spec);
36 exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
37 if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
41 exfat_error("exFAT file system is not found");
45 ef->mount_time = time(NULL);
52 void exfat_unmount(struct exfat* ef)
63 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
65 memset(stbuf, 0, sizeof(struct stat));
66 if (node->flags & EXFAT_ATTRIB_DIR)
67 stbuf->st_mode = S_IFDIR | 0755;
69 stbuf->st_mode = S_IFREG | 0444;
71 stbuf->st_size = node->size;
72 stbuf->st_mtime = node->mtime;
73 stbuf->st_atime = node->atime;
74 stbuf->st_ctime = 0; /* unapplicable */
77 #define SEC_IN_MIN 60ll
78 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
79 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
80 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
81 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
82 #define UNIX_EPOCH_YEAR 1970
83 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
84 #define EXFAT_EPOCH_YEAR 1980
85 /* number of years from Unix epoch to exFAT epoch */
86 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
87 /* number of seconds from Unix epoch to exFAT epoch (considering leap years) */
88 #define EPOCH_DIFF_SEC (EPOCH_DIFF_YEAR*365 + EPOCH_DIFF_YEAR/4) * SEC_IN_DAY
90 static const time_t days_in_year[] =
92 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
93 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
101 uint16_t day : 5; /* 1-31 */
102 uint16_t month : 4; /* 1-12 */
103 uint16_t year : 7; /* 1-127 (+1980) */
112 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
113 uint16_t min : 6; /* 0-59 */
114 uint16_t hour : 5; /* 0-23 */
118 static time_t get_time_shift(void)
123 if (gettimeofday(&tv, &tz) != 0)
125 return -tz.tz_minuteswest * SEC_IN_MIN;
128 time_t exfat_exfat2unix(le16_t date, le16_t time)
130 union exfat_date edate;
131 union exfat_time etime;
132 time_t unix_time = EPOCH_DIFF_SEC;
134 edate.raw = le16_to_cpu(date);
135 etime.raw = le16_to_cpu(time);
138 exfat_debug("%hu-%02hu-%02hu %hu:%02hu:%02hu",
139 edate.year + 1980, edate.month, edate.day,
140 etime.hour, etime.min, etime.twosec * 2);
143 if (edate.day == 0 || edate.month == 0 || edate.month > 12)
145 exfat_error("bad date %hu-%02hu-%02hu",
146 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
149 if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
151 exfat_error("bad time %hu:%02hu:%02hu",
152 etime.hour, etime.min, etime.twosec * 2);
156 /* every 4th year between 1904 and 2096 is leap */
157 unix_time += edate.year * SEC_IN_YEAR + edate.year / 4 * SEC_IN_DAY;
158 unix_time += days_in_year[edate.month] * SEC_IN_DAY;
159 /* if it's leap year and February has passed we should add 1 day */
160 if (edate.year % 4 == 0 && edate.month > 2)
161 unix_time += SEC_IN_DAY;
162 unix_time += edate.day * SEC_IN_DAY;
164 unix_time += etime.hour * SEC_IN_HOUR;
165 unix_time += etime.min * SEC_IN_MIN;
166 /* exFAT represents time with 2 sec granularity */
167 unix_time += etime.twosec * 2;
169 /* exFAT stores timestamps in local time, so we correct it to UTC */
170 unix_time -= get_time_shift();
175 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
177 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
178 exfat_bug("failed to convert name to UTF-8");