OSDN Git Service

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