forked from dorimanx/exfat-nofuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exfat_oal.c
153 lines (133 loc) · 4.7 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
/************************************************************************/
/* */
/* PROJECT : exFAT & FAT12/16/32 File System */
/* FILE : exfat_oal.c */
/* PURPOSE : exFAT OS Adaptation Layer */
/* (Semaphore Functions & Real-Time Clock Functions) */
/* */
/*----------------------------------------------------------------------*/
/* NOTES */
/* */
/*----------------------------------------------------------------------*/
/* REVISION HISTORY (Ver 0.9) */
/* */
/* - 2010.11.15 [Joosun Hahn] : first writing */
/* */
/************************************************************************/
#include <linux/semaphore.h>
#include <linux/time.h>
#include "exfat_config.h"
#include "exfat_global.h"
#include "exfat_api.h"
#include "exfat_oal.h"
/*======================================================================*/
/* */
/* SEMAPHORE FUNCTIONS */
/* */
/*======================================================================*/
DECLARE_MUTEX(z_sem);
INT32 sm_init(struct semaphore *sm)
{
sema_init(sm, 1);
return(0);
} /* end of sm_init */
INT32 sm_P(struct semaphore *sm)
{
down(sm);
return 0;
} /* end of sm_P */
void sm_V(struct semaphore *sm)
{
up(sm);
} /* end of sm_V */
/*======================================================================*/
/* */
/* REAL-TIME CLOCK FUNCTIONS */
/* */
/*======================================================================*/
extern struct timezone sys_tz;
/*
* The epoch of FAT timestamp is 1980.
* : bits : value
* date: 0 - 4: day (1 - 31)
* date: 5 - 8: month (1 - 12)
* date: 9 - 15: year (0 - 127) from 1980
* time: 0 - 4: sec (0 - 29) 2sec counts
* time: 5 - 10: min (0 - 59)
* time: 11 - 15: hour (0 - 23)
*/
#define SECS_PER_MIN (60)
#define SECS_PER_HOUR (60 * 60)
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
#define UNIX_SECS_1980 315532800L
#if BITS_PER_LONG == 64
#define UNIX_SECS_2108 4354819200L
#endif
/* days between 1.1.70 and 1.1.80 (2 leap days) */
#define DAYS_DELTA (365 * 10 + 2)
/* 120 (2100 - 1980) isn't leap year */
#define YEAR_2100 120
#define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != YEAR_2100)
/* Linear day numbers of the respective 1sts in non-leap years. */
static time_t days_in_year[] = {
/* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0,
};
TIMESTAMP_T *tm_current(TIMESTAMP_T *tp)
{
struct timespec ts = CURRENT_TIME_SEC;
time_t second = ts.tv_sec;
time_t day, leap_day, month, year;
second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
/* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
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;
year = day / 365;
leap_day = (year + 3) / 4;
if (year > YEAR_2100) /* 2100 isn't leap year */
leap_day--;
if (year * 365 + leap_day > day)
year--;
leap_day = (year + 3) / 4;
if (year > YEAR_2100) /* 2100 isn't leap year */
leap_day--;
day -= year * 365 + leap_day;
if (IS_LEAP_YEAR(year) && day == days_in_year[3]) {
month = 2;
} else {
if (IS_LEAP_YEAR(year) && day > days_in_year[3])
day--;
for (month = 1; month < 12; month++) {
if (days_in_year[month + 1] > day)
break;
}
}
day -= 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);
} /* end of tm_current */
/* end of exfat_oal.c */