forked from AndyLavr/exfat-nofuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexfat_oal.c
153 lines (125 loc) · 3.57 KB
/
exfat_oal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Some of the source code in this file came from "linux/fs/fat/misc.c". */
/*
* linux/fs/fat/misc.c
*
* Written 1992,1993 by Werner Almesberger
* 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
* and date_dos2unix for date==0 by Igor Zhbanov([email protected])
*/
/*
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <linux/semaphore.h>
#include <linux/time.h>
#include "exfat_config.h"
#include "exfat_global.h"
#include "exfat_api.h"
#include "exfat_oal.h"
DECLARE_MUTEX(z_sem);
s32 sm_init(struct semaphore *sm)
{
sema_init(sm, 1);
return(0);
}
s32 sm_P(struct semaphore *sm)
{
down(sm);
return 0;
}
void sm_V(struct semaphore *sm)
{
up(sm);
}
extern struct timezone sys_tz;
#define UNIX_SECS_1980 315532800L
#if BITS_PER_LONG == 64
#define UNIX_SECS_2108 4354819200L
#endif
#define DAYS_DELTA_DECADE (365 * 10 + 2)
#define NO_LEAP_YEAR_2100 (120)
#define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != NO_LEAP_YEAR_2100)
#define SECS_PER_MIN (60)
#define SECS_PER_HOUR (60 * SECS_PER_MIN)
#define SECS_PER_DAY (24 * SECS_PER_HOUR)
#define MAKE_LEAP_YEAR(leap_year, year) \
do { \
if (unlikely(year > NO_LEAP_YEAR_2100)) \
leap_year = ((year + 3) / 4) - 1; \
else \
leap_year = ((year + 3) / 4); \
} while(0)
static time_t accum_days_in_year[] = {
0, 0, 31, 59, 90,120,151,181,212,243,273,304,334, 0, 0, 0,
};
TIMESTAMP_T *tm_current(TIMESTAMP_T *tp, u8 tz_utc)
{
time_t second, day, leap_day, month, year;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,8,0)
struct timespec ts;
ts = CURRENT_TIME_SEC;
second = ts.tv_sec;
#else
second = ktime_get_real_seconds();
#endif
if (!tz_utc)
second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
if (second < UNIX_SECS_1980) {
tp->sec = 0;
tp->min = 0;
tp->hour = 0;
tp->day = 1;
tp->mon = 1;
tp->year = 0;
return(tp);
}
#if BITS_PER_LONG == 64
if (second >= UNIX_SECS_2108) {
tp->sec = 59;
tp->min = 59;
tp->hour = 23;
tp->day = 31;
tp->mon = 12;
tp->year = 127;
return(tp);
}
#endif
day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
year = day / 365;
MAKE_LEAP_YEAR(leap_day, year);
if (year * 365 + leap_day > day)
year--;
MAKE_LEAP_YEAR(leap_day, year);
day -= year * 365 + leap_day;
if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
month = 2;
} else {
if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
day--;
for (month = 1; month < 12; month++) {
if (accum_days_in_year[month + 1] > day)
break;
}
}
day -= accum_days_in_year[month];
tp->sec = second % SECS_PER_MIN;
tp->min = (second / SECS_PER_MIN) % 60;
tp->hour = (second / SECS_PER_HOUR) % 24;
tp->day = day + 1;
tp->mon = month;
tp->year = year;
return(tp);
}