OSDN Git Service

Split EPOCH_DIFF_SEC macro into two macros for simplicity.
[android-x86/external-exfat.git] / libexfat / utils.c
1 /*
2  *  utils.c
3  *  exFAT file system implementation library.
4  *
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.
8  */
9
10 #include "exfat.h"
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #define _XOPEN_SOURCE /* for timezone in Linux */
17 #include <time.h>
18 #include <sys/time.h>
19
20 static uint64_t rootdir_size(const struct exfat* ef)
21 {
22         uint64_t clusters = 0;
23         cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
24
25         while (!CLUSTER_INVALID(rootdir_cluster))
26         {
27                 clusters++;
28                 /* root directory cannot be contiguous because there is no flag
29                    to indicate this */
30                 rootdir_cluster = exfat_next_cluster(ef, rootdir_cluster, 0);
31         }
32         return clusters * CLUSTER_SIZE(*ef->sb);
33 }
34
35 int exfat_mount(struct exfat* ef, const char* spec)
36 {
37         tzset();
38
39         ef->sb = malloc(sizeof(struct exfat_super_block));
40         if (ef->sb == NULL)
41         {
42                 exfat_error("memory allocation failed");
43                 return -ENOMEM;
44         }
45
46         ef->fd = open(spec, O_RDONLY); /* currently read only */
47         if (ef->fd < 0)
48         {
49                 free(ef->sb);
50                 exfat_error("failed to open `%s'", spec);
51                 return -EIO;
52         }
53
54         exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
55         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
56         {
57                 close(ef->fd);
58                 free(ef->sb);
59                 exfat_error("exFAT file system is not found");
60                 return -EIO;
61         }
62
63         ef->upcase = NULL;
64         ef->upcase_chars = 0;
65         ef->rootdir_size = rootdir_size(ef);
66
67         return 0;
68 }
69
70 void exfat_unmount(struct exfat* ef)
71 {
72         close(ef->fd);
73         ef->fd = 0;
74         free(ef->sb);
75         ef->sb = NULL;
76         free(ef->upcase);
77         ef->upcase = NULL;
78         ef->upcase_chars = 0;
79 }
80
81 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
82 {
83         memset(stbuf, 0, sizeof(struct stat));
84         if (node->flags & EXFAT_ATTRIB_DIR)
85                 stbuf->st_mode = S_IFDIR | 0755;
86         else
87                 stbuf->st_mode = S_IFREG | 0444;
88         stbuf->st_nlink = 1;
89         stbuf->st_size = node->size;
90         stbuf->st_mtime = node->mtime;
91         stbuf->st_atime = node->atime;
92         stbuf->st_ctime = 0; /* unapplicable */
93 }
94
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)
109
110 static const time_t days_in_year[] =
111 {
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
114 };
115
116 union exfat_date
117 {
118         uint16_t raw;
119         struct
120         {
121                 uint16_t day   : 5; /* 1-31 */
122                 uint16_t month : 4; /* 1-12 */
123                 uint16_t year  : 7; /* 1-127 (+1980) */
124         };
125 };
126
127 union exfat_time
128 {
129         uint16_t raw;
130         struct
131         {
132                 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
133                 uint16_t min    : 6; /* 0-59 */
134                 uint16_t hour   : 5; /* 0-23 */
135         };
136 };
137
138 time_t exfat_exfat2unix(le16_t date, le16_t time)
139 {
140         union exfat_date edate;
141         union exfat_time etime;
142         time_t unix_time = EPOCH_DIFF_SEC;
143
144         edate.raw = le16_to_cpu(date);
145         etime.raw = le16_to_cpu(time);
146
147         /*
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);
151         */
152
153         if (edate.day == 0 || edate.month == 0 || edate.month > 12)
154         {
155                 exfat_error("bad date %hu-%02hu-%02hu",
156                                 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
157                 return 0;
158         }
159         if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
160         {
161                 exfat_error("bad time %hu:%02hu:%02hu",
162                         etime.hour, etime.min, etime.twosec * 2);
163                 return 0;
164         }
165
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)
169                 * SEC_IN_DAY;
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;
175
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;
180
181         /* exFAT stores timestamps in local time, so we correct it to UTC */
182         unix_time += timezone;
183
184         return unix_time;
185 }
186
187 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
188 {
189         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
190                 exfat_bug("failed to convert name to UTF-8");
191 }