OSDN Git Service

Initial code drop.
[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 int exfat_mount(struct exfat* ef, const char* spec)
20 {
21         ef->sb = malloc(sizeof(struct exfat_super_block));
22         if (ef->sb == NULL)
23         {
24                 exfat_error("memory allocation failed");
25                 return -ENOMEM;
26         }
27
28         ef->fd = open(spec, O_RDONLY); /* currently read only */
29         if (ef->fd < 0)
30         {
31                 free(ef->sb);
32                 exfat_error("failed to open `%s'", spec);
33                 return -EIO;
34         }
35
36         exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
37         if (memcmp(ef->sb->oem_name, "EXFAT   ", 8) != 0)
38         {
39                 close(ef->fd);
40                 free(ef->sb);
41                 exfat_error("exFAT file system is not found");
42                 return -EIO;
43         }
44
45         ef->mount_time = time(NULL);
46         ef->upcase = NULL;
47         ef->upcase_chars = 0;
48
49         return 0;
50 }
51
52 void exfat_unmount(struct exfat* ef)
53 {
54         close(ef->fd);
55         ef->fd = 0;
56         free(ef->sb);
57         ef->sb = NULL;
58         free(ef->upcase);
59         ef->upcase = NULL;
60         ef->upcase_chars = 0;
61 }
62
63 void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
64 {
65         memset(stbuf, 0, sizeof(struct stat));
66         if (node->flags & EXFAT_ATTRIB_DIR)
67                 stbuf->st_mode = S_IFDIR | 0755;
68         else
69                 stbuf->st_mode = S_IFREG | 0444;
70         stbuf->st_nlink = 1;
71         stbuf->st_size = node->size;
72         stbuf->st_mtime = node->mtime;
73         stbuf->st_atime = node->atime;
74         stbuf->st_ctime = 0; /* unapplicable */
75 }
76
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
89
90 static const time_t days_in_year[] =
91 {
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
94 };
95
96 union exfat_date
97 {
98         uint16_t raw;
99         struct
100         {
101                 uint16_t day   : 5; /* 1-31 */
102                 uint16_t month : 4; /* 1-12 */
103                 uint16_t year  : 7; /* 1-127 (+1980) */
104         };
105 };
106
107 union exfat_time
108 {
109         uint16_t raw;
110         struct
111         {
112                 uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
113                 uint16_t min    : 6; /* 0-59 */
114                 uint16_t hour   : 5; /* 0-23 */
115         };
116 };
117
118 static time_t get_time_shift(void)
119 {
120         struct timeval tv;
121         struct timezone tz;
122
123         if (gettimeofday(&tv, &tz) != 0)
124                 return 0;
125         return -tz.tz_minuteswest * SEC_IN_MIN;
126 }
127
128 time_t exfat_exfat2unix(le16_t date, le16_t time)
129 {
130         union exfat_date edate;
131         union exfat_time etime;
132         time_t unix_time = EPOCH_DIFF_SEC;
133
134         edate.raw = le16_to_cpu(date);
135         etime.raw = le16_to_cpu(time);
136
137         /*
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);
141         */
142
143         if (edate.day == 0 || edate.month == 0 || edate.month > 12)
144         {
145                 exfat_error("bad date %hu-%02hu-%02hu",
146                                 edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
147                 return 0;
148         }
149         if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
150         {
151                 exfat_error("bad time %hu:%02hu:%02hu",
152                         etime.hour, etime.min, etime.twosec * 2);
153                 return 0;
154         }
155
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;
163
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;
168
169         /* exFAT stores timestamps in local time, so we correct it to UTC */
170         unix_time -= get_time_shift();
171
172         return unix_time;
173 }
174
175 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
176 {
177         if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
178                 exfat_bug("failed to convert name to UTF-8");
179 }