SayoriOS  0.3.3
fsm.c
1 
10 #include <io/ports.h>
11 #include <fs/fsm.h>
12 #include <lib/php/pathinfo.h>
13 #include "lib/php/str_contains.h"
14 #include "lib/php/explode.h"
15 #include "lib/sprintf.h"
16 #include "mem/vmm.h"
17 
18 #include "drv/disk/dpm.h"
19 
20 FSM G_FSM[255] = {0};
21 int C_FSM = 0;
22 bool fsm_debug = false;
23 
24 size_t fsm_DateConvertToUnix(FSM_TIME time) {
25  uint32_t seconds_per_day = 24 * 60 * 60;
26  size_t unix_time = 0;
27 
28  // Подсчет количества дней с начала Unix эпохи
29  for (uint32_t year = 1970; year < time.year; year++) {
30  uint32_t days_in_year = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 366 : 365;
31  unix_time += days_in_year * seconds_per_day;
32  }
33 
34  int8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
35  if (time.year % 4 == 0 && (time.year % 100 != 0 || time.year % 400 == 0)) {
36  month_days[1] = 29;
37  }
38 
39  // Добавление количества дней в текущем году
40  for (uint32_t month = 0; month < time.month - 1; month++) {
41  unix_time += month_days[month] * seconds_per_day;
42  }
43 
44  // Добавление количества дней в текущем месяце
45  unix_time += (time.day - 1) * seconds_per_day;
46 
47  // Добавление компонентов времени
48  unix_time += time.hour * 3600 + time.minute * 60 + time.second;
49 
50  return unix_time;
51 }
52 
53 
54 void fsm_convertUnix(uint32_t unix_time, FSM_TIME* time) {
55  if (fsm_debug) qemu_log("[FSM] Convert unix: %d",unix_time);
56  uint32_t seconds_per_day = 24 * 60 * 60;
57  uint32_t days = unix_time / seconds_per_day;
58 
59  uint32_t years = 1970;
60  uint32_t month, day;
61  while (1) {
62  uint32_t days_in_year = (years % 4 == 0 && (years % 100 != 0 || years % 400 == 0)) ? 366 : 365;
63  if (days < days_in_year) {
64  break;
65  }
66  days -= days_in_year;
67  years++;
68  }
69 
70  int8_t month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
71  if (years % 4 == 0 && (years % 100 != 0 || years % 400 == 0)) {
72  month_days[1] = 29;
73  }
74 
75  uint32_t year_day = days;
76  for (month = 0; month < 12; month++) {
77  if (year_day < month_days[month]) {
78  break;
79  }
80  year_day -= month_days[month];
81  }
82  day = year_day + 1;
83 
84  // Вычисляем компоненты времени
85  uint32_t seconds = unix_time % seconds_per_day;
86  uint32_t hour = seconds / 3600;
87  uint32_t minute = (seconds % 3600) / 60;
88  uint32_t second = seconds % 60;
89  //qemu_log("%d.%d.%d %d:%d:%d",years, month, day, hour, minute, second);
90 
91  time->year = years;
92  time->month = month;
93  time->day = day;
94  time->hour = hour;
95  time->minute = minute;
96  time->second = second;
97  //memcpy();
98  //qemu_log("%d.%d.%d %d:%d:%d", time->year, time->month, time->day, time->hour, time->minute, time->second);
99 }
100 
101 char* fsm_timePrintable(FSM_TIME time){
102  char* btime = 0;
103 
104  asprintf(&btime, "%04d.%02d.%02d %02d:%02d:%02d",
105  time.year,
106  time.month,
107  time.day,
108  time.hour,
109  time.minute,
110  time.second);
111 
112  return btime;
113 }
114 
115 int fsm_isPathToFile(const char* Path,const char* Name){
116  char* zpath = pathinfo(Name, PATHINFO_DIRNAME);
117  char* bpath = pathinfo(Name, PATHINFO_BASENAME);
118  bool isCheck1 = strcmpn(zpath,Path);
119  bool isCheck2 = strlen(bpath) == 0;
120  bool isCheck3 = str_contains(Name, Path);
121  size_t c1 = str_cdsp2(Path,'\\');
122  size_t c2 = str_cdsp2(Name,'\\');
123  size_t c3 = str_cdsp2(Path,'/');
124  size_t c4 = str_cdsp2(Name,'/');
125 
126  bool isCheck4 = ((c2 - c1) == 1) && (c4 == c3);
127  bool isCheck5 = ((c4 - c3) == 1) && (c2 == c1);
128 /*
129  qemu_log("[%d] [%d] [%d] [%d] [%d] %s", isCheck1, isCheck2, isCheck3, isCheck4, isCheck5, Name);
130  qemu_log(" |--- %d == %d",c1,c2);
131  qemu_log(" |--- %d == %d",c3,c4);*/
132  bool isPassed = ((isCheck1 && !isCheck2 && isCheck3) || (!isCheck1 && isCheck2 && isCheck3 && (isCheck4 || isCheck5)));
133 
134  kfree(zpath);
135  kfree(bpath);
136 
137  return isPassed;
138 }
139 
140 int fsm_getIDbyName(const char* Name){
141  for (int i = 0; i < C_FSM; i++){
142  if (!strcmpn(G_FSM[i].Name,Name)) continue;
143  return i;
144  }
145  return -1;
146 }
147 
148 void fsm_dump(FSM_FILE file){
149  qemu_log(" |--- Ready : %d",file.Ready);
150  qemu_log(" |--- Name : %s",file.Name);
151  qemu_log(" |--- Path : %s",file.Path);
152  qemu_log(" |--- Mode : %d",file.Mode);
153  qemu_log(" |--- Size : %d",file.Size);
154  qemu_log(" |--- Type : %d",file.Type);
155  qemu_log(" |--- Date : %d",file.LastTime.year);
156 }
157 
158 size_t fsm_read(int FIndex, char DIndex, const char* Name, size_t Offset, size_t Count, void* Buffer){
159  if (fsm_debug) qemu_log("[FSM] [READ] F:%d | D:%d | N:%d | O:%d | C:%d",FIndex,DIndex,Name,Offset,Count);
160  if (G_FSM[FIndex].Ready == 0) return 0;
161  if (fsm_debug) qemu_log("[FSM] [READ] GO TO DRIVER");
162  return G_FSM[FIndex].Read(DIndex,Name,Offset, Count, Buffer);
163 }
164 
165 
166 int fsm_create(int FIndex, char DIndex, const char* Name, int Mode){
167  if (G_FSM[FIndex].Ready == 0) return 0;
168  return G_FSM[FIndex].Create(DIndex,Name,Mode);
169 }
170 
171 
172 int fsm_delete(int FIndex, const char DIndex, const char* Name, int Mode){
173  if (G_FSM[FIndex].Ready == 0)
174  return 0;
175 
176  return G_FSM[FIndex].Delete(DIndex,Name,Mode);
177 }
178 
179 size_t fsm_write(int FIndex, const char DIndex, const char* Name, size_t Offset, size_t Count, void* Buffer){
180  if (G_FSM[FIndex].Ready == 0)
181  return 0;
182 
183  return G_FSM[FIndex].Write(DIndex,Name,Offset, Count, Buffer);
184 }
185 
186 FSM_FILE fsm_info(int FIndex,const char DIndex, const char* Name){
187  if (fsm_debug) qemu_log("[FSM] [INFO] F:%d | D:%d | N:%s",FIndex,DIndex,Name);
188  if (G_FSM[FIndex].Ready == 0){
189  if (fsm_debug) qemu_log("[FSM] [INFO] READY == 0");
190  return (FSM_FILE){};
191  }
192  if (fsm_debug) qemu_log("[FSM] [INFO] GO TO GFSM");
193  return G_FSM[FIndex].Info(DIndex,Name);
194 }
195 
196 FSM_DIR* fsm_dir(int FIndex,const char DIndex, const char* Name){
197  if (fsm_debug) qemu_log("[FSM] [DIR] F:%d | D:%d | N:%s",FIndex,DIndex,Name);
198 
199  if (G_FSM[FIndex].Ready == 0){
200  if (fsm_debug) qemu_log("[FSM] [INFO] READY == 0");
201  FSM_DIR* dir = kmalloc(sizeof(FSM_DIR));
202  return dir;
203  }
204 
205  return G_FSM[FIndex].Dir(DIndex, Name);
206 }
207 
208 void fsm_reg(const char* Name,int Splash,fsm_cmd_read_t Read, fsm_cmd_write_t Write, fsm_cmd_info_t Info, fsm_cmd_create_t Create, fsm_cmd_delete_t Delete, fsm_cmd_dir_t Dir, fsm_cmd_label_t Label, fsm_cmd_detect_t Detect){
209  G_FSM[C_FSM].Ready = 1;
210  G_FSM[C_FSM].Splash = Splash;
211  G_FSM[C_FSM].Read = Read;
212  G_FSM[C_FSM].Write = Write;
213  G_FSM[C_FSM].Info = Info;
214  G_FSM[C_FSM].Create = Create;
215  G_FSM[C_FSM].Delete = Delete;
216  G_FSM[C_FSM].Dir = Dir;
217  G_FSM[C_FSM].Label = Label;
218  G_FSM[C_FSM].Detect = Detect;
219  memcpy(G_FSM[C_FSM].Name,Name,strlen(Name));
220  qemu_log("[FSM] Registration of the '%s' file system driver is complete.",Name);
221  C_FSM++;
222 }
223 
224 int fsm_getMode(int FIndex){
225  if (G_FSM[FIndex].Ready == 0) return 0;
226 
227  return G_FSM[FIndex].Splash;
228 }
229 
230 
231 void fsm_dpm_update(char Letter){
232  char BLANK[128] = {'U','n','k','n','o','w','n',0};
233  if (Letter == -1){
234  // Global update
235  for(int i = 0; i < 26; i++){
236  int DISKID = i + 65;
237  dpm_LabelUpdate(DISKID, BLANK);
238  dpm_FileSystemUpdate(DISKID, BLANK);
239  DPM_Disk dpm = dpm_info(DISKID);
240 
241  if (dpm.Ready != 1) {
242  continue;
243  }
244 
245  qemu_note("SCANNING PARTITIONS ON: %c", DISKID);
246  mbr_dump_all(DISKID);
247 
248  for(int f = 0; f < C_FSM; f++){
249  qemu_note("[FSM] [DPM] >>> Disk %c | Test %s", DISKID, G_FSM[f].Name);
250 
251  int detect = G_FSM[f].Detect(DISKID);
252 
253  if (detect != 1)
254  continue;
255 
256  char* lab_test = kcalloc(1,129);
257 
258  G_FSM[f].Label(DISKID,lab_test);
259 
260  dpm_LabelUpdate(DISKID, lab_test);
261  dpm_FileSystemUpdate(DISKID, G_FSM[f].Name);
262 
263  qemu_note(" | Label: %s", lab_test);
264 
265  kfree(lab_test);
266 
267  break;
268  }
269  }
270  } else {
271  // Personal update
272  int DISKID = Letter;
273  dpm_LabelUpdate(DISKID, BLANK);
274  dpm_FileSystemUpdate(DISKID, BLANK);
275  for(int f = 0; f < C_FSM; f++){
276  qemu_note("[FSM] [DPM] >>> Disk %c | Test %s", DISKID, G_FSM[f].Name);
277  int detect = G_FSM[f].Detect(DISKID);
278 
279  if (detect != 1)
280  continue;
281 
282  char* lab_test = kcalloc(1,129);
283 
284  G_FSM[f].Label(DISKID, lab_test);
285  dpm_LabelUpdate(DISKID, lab_test);
286  dpm_FileSystemUpdate(DISKID, G_FSM[f].Name);
287  qemu_note("[FSM] [DPM] ^^^ Disk %c | Label: %s", DISKID, lab_test);
288 
289  kfree(lab_test);
290 
291  break;
292  }
293  }
294 }
void dpm_LabelUpdate(char Letter, char *Label)
Definition: dpm.c:241
void dpm_FileSystemUpdate(char Letter, char *FileSystem)
Definition: dpm.c:229
uint32_t str_cdsp2(const char *a_str, char del)
Функция отладки
Definition: explode.c:21
size_t strlen(const char *str)
Возращает длину строки
Definition: string.c:88
bool strcmpn(const char *str1, const char *str2)
Сравнение строк
Definition: string.c:270
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173