Rev 9277 | Rev 9422 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5598 | pavelyakov | 1 | #ifndef INCLUDE_FILESYSTEM_H |
2 | #define INCLUDE_FILESYSTEM_H |
||
7219 | leency | 3 | #print "[include |
5598 | pavelyakov | 4 | |
5640 | pavelyakov | 5 | #ifndef INCLUDE_DATE_H |
6 | #include "../lib/date.h" |
||
5598 | pavelyakov | 7 | #endif |
8 | |||
7878 | leency | 9 | #ifndef INCLUDE_COLLECTION_H |
10 | #include "../lib/collection.h" |
||
11 | #endif |
||
12 | |||
7369 | leency | 13 | //===================================================// |
14 | // // |
||
15 | // Basic System Functions // |
||
16 | // // |
||
17 | //===================================================// |
||
18 | |||
8750 | leency | 19 | :struct F70{ |
3067 | leency | 20 | dword func; |
21 | dword param1; |
||
22 | dword param2; |
||
23 | dword param3; |
||
24 | dword param4; |
||
25 | char rezerv; |
||
26 | dword name; |
||
8750 | leency | 27 | } f70; |
3067 | leency | 28 | |
5487 | leency | 29 | :struct BDVK { |
30 | dword readonly:1, hidden:1, system:1, volume_label:1, isfolder:1, notarchived:1, :0; |
||
3067 | leency | 31 | byte type_name; |
4898 | leency | 32 | byte rez1, rez2, selected; |
5487 | leency | 33 | dword timecreate; |
34 | date datecreate; |
||
3067 | leency | 35 | dword timelastaccess; |
5487 | leency | 36 | date datelastaccess; |
3067 | leency | 37 | dword timelastedit; |
5487 | leency | 38 | date datelastedit; |
3067 | leency | 39 | dword sizelo; |
40 | dword sizehi; |
||
41 | char name[518]; |
||
42 | }; |
||
8949 | leency | 43 | #define ATR_READONLY 1 |
44 | #define ATR_HIDDEN 2 |
||
45 | #define ATR_SYSTEM 4 |
||
46 | #define ATR_VOL_LABEL 8 |
||
8944 | leency | 47 | #define ATR_FOLDER 0x10 |
8949 | leency | 48 | #define ATR_NONARH 0x20 |
3067 | leency | 49 | |
50 | |||
3440 | leency | 51 | :dword GetFileInfo(dword file_path, bdvk_struct) |
52 | { |
||
8750 | leency | 53 | f70.func = 5; |
54 | f70.param1 = |
||
55 | f70.param2 = |
||
56 | f70.param3 = 0; |
||
57 | f70.param4 = bdvk_struct; |
||
58 | f70.rezerv = 0; |
||
59 | f70.name = file_path; |
||
3440 | leency | 60 | $mov eax,70 |
8750 | leency | 61 | $mov ebx,#f70.func |
3444 | leency | 62 | $int 0x40 |
3440 | leency | 63 | } |
64 | |||
9277 | leency | 65 | :dword GetVolumeLabel(dword _path) |
66 | { |
||
67 | BDVK bdvk; |
||
68 | //if (ESBYTE[_path+1]=='k') || (ESBYTE[_path+2]=='y') return NULL; |
||
69 | f70.func = 5; |
||
70 | f70.param1 = 0; |
||
71 | f70.param2 = 1; |
||
72 | f70.param3 = 1; |
||
73 | f70.param4 = #bdvk; |
||
74 | f70.rezerv = 0; |
||
75 | f70.name = _path; |
||
76 | $mov eax,70 |
||
77 | $mov ebx,#f70.func |
||
78 | $int 0x40 |
||
79 | return #bdvk.name; |
||
80 | } |
||
81 | |||
5554 | punk_joker | 82 | :dword SetFileInfo(dword file_path, bdvk_struct) |
83 | { |
||
8750 | leency | 84 | f70.func = 6; |
85 | f70.param1 = |
||
86 | f70.param2 = |
||
87 | f70.param3 = 0; |
||
88 | f70.param4 = bdvk_struct; |
||
89 | f70.rezerv = 0; |
||
90 | f70.name = file_path; |
||
5554 | punk_joker | 91 | $mov eax,70 |
8750 | leency | 92 | $mov ebx,#f70.func |
5554 | punk_joker | 93 | $int 0x40 |
94 | } |
||
95 | |||
5472 | leency | 96 | :signed int RunProgram(dword run_path, run_param) |
3067 | leency | 97 | { |
8750 | leency | 98 | f70.func = 7; |
99 | f70.param1 = |
||
100 | f70.param3 = |
||
101 | f70.param4 = |
||
102 | f70.rezerv = 0; |
||
103 | f70.param2 = run_param; |
||
104 | f70.name = run_path; |
||
3067 | leency | 105 | $mov eax,70 |
8750 | leency | 106 | $mov ebx,#f70.func |
3067 | leency | 107 | $int 0x40 |
108 | } |
||
109 | |||
110 | :int CreateDir(dword new_folder_path) |
||
111 | { |
||
8750 | leency | 112 | f70.func = 9; |
113 | f70.param1 = |
||
114 | f70.param2 = |
||
115 | f70.param3 = |
||
116 | f70.param4 = |
||
117 | f70.rezerv = 0; |
||
118 | f70.name = new_folder_path; |
||
3067 | leency | 119 | $mov eax,70 |
8750 | leency | 120 | $mov ebx,#f70.func |
3067 | leency | 121 | $int 0x40 |
122 | } |
||
123 | |||
124 | :int DeleteFile(dword del_file_path) |
||
125 | { |
||
8750 | leency | 126 | f70.func = 8; |
127 | f70.param1 = |
||
128 | f70.param2 = |
||
129 | f70.param3 = |
||
130 | f70.param4 = |
||
131 | f70.rezerv = 0; |
||
132 | f70.name = del_file_path; |
||
3067 | leency | 133 | $mov eax,70 |
8750 | leency | 134 | $mov ebx,#f70.func |
3067 | leency | 135 | $int 0x40 |
136 | } |
||
137 | |||
7210 | leency | 138 | :int ReadFile(dword offset, data_size, buffer, file_path) |
3067 | leency | 139 | { |
8750 | leency | 140 | f70.func = 0; |
141 | f70.param1 = offset; |
||
142 | f70.param2 = 0; |
||
143 | f70.param3 = data_size; |
||
144 | f70.param4 = buffer; |
||
145 | f70.rezerv = 0; |
||
146 | f70.name = file_path; |
||
3067 | leency | 147 | $mov eax,70 |
8750 | leency | 148 | $mov ebx,#f70.func |
3067 | leency | 149 | $int 0x40 |
150 | } |
||
151 | |||
7227 | leency | 152 | :int CreateFile(dword data_size, buffer, file_path) |
3067 | leency | 153 | { |
8750 | leency | 154 | f70.func = 2; |
155 | f70.param1 = 0; |
||
156 | f70.param2 = 0; |
||
157 | f70.param3 = data_size; |
||
158 | f70.param4 = buffer; |
||
159 | f70.rezerv = 0; |
||
160 | f70.name = file_path; |
||
3067 | leency | 161 | $mov eax,70 |
8750 | leency | 162 | $mov ebx,#f70.func |
3067 | leency | 163 | $int 0x40 |
5542 | leency | 164 | } |
3067 | leency | 165 | |
5640 | pavelyakov | 166 | //////////////////////////////////////// |
167 | // WriteInFileThatAlredyExists // |
||
168 | //////////////////////////////////////// |
||
7227 | leency | 169 | :int WriteFile(dword offset, data_size, buffer, file_path) |
5542 | leency | 170 | { |
8750 | leency | 171 | f70.func = 3; |
172 | f70.param1 = offset; |
||
173 | f70.param2 = 0; |
||
174 | f70.param3 = data_size; |
||
175 | f70.param4 = buffer; |
||
176 | f70.rezerv = 0; |
||
177 | f70.name = file_path; |
||
5542 | leency | 178 | $mov eax,70 |
8750 | leency | 179 | $mov ebx,#f70.func |
5542 | leency | 180 | $int 0x40 |
7210 | leency | 181 | } |
5542 | leency | 182 | |
8750 | leency | 183 | :int RenameMove(dword path_to, path_from) |
184 | { |
||
185 | f70.func = 10; |
||
186 | f70.param1 = |
||
187 | f70.param2 = |
||
188 | f70.param3 = 0; |
||
189 | f70.param4 = path_to; |
||
190 | f70.rezerv = 0; |
||
191 | f70.name = path_from; |
||
192 | $mov eax,70 |
||
193 | $mov ebx,#f70.func |
||
194 | $int 0x40 |
||
195 | } |
||
196 | |||
3067 | leency | 197 | :int ReadDir(dword file_count, read_buffer, dir_path) |
3363 | leency | 198 | { |
8750 | leency | 199 | f70.func = 1; |
200 | f70.param1 = |
||
201 | f70.param2 = |
||
202 | f70.rezerv = 0; |
||
203 | f70.param3 = file_count; |
||
204 | f70.param4 = read_buffer; |
||
205 | f70.name = dir_path; |
||
3067 | leency | 206 | $mov eax,70 |
8750 | leency | 207 | $mov ebx,#f70.func |
3067 | leency | 208 | $int 0x40 |
209 | } |
||
210 | |||
7533 | leency | 211 | //ECX - buf pointer |
7422 | leency | 212 | inline fastcall void SetCurDir( ECX) |
213 | { |
||
214 | EAX=30; |
||
215 | EBX=1; |
||
216 | $int 0x40 |
||
217 | } |
||
218 | |||
7533 | leency | 219 | //ECX - buf pointer |
220 | //EDX - buf size |
||
221 | inline fastcall void GetCurDir( ECX, EDX) |
||
222 | { |
||
223 | EAX=30; |
||
224 | EBX=2; |
||
225 | $int 0x40 |
||
226 | } |
||
227 | |||
8949 | leency | 228 | :void read_file(dword path1, buf, size) |
8383 | leency | 229 | { |
230 | EAX = 68; |
||
231 | EBX = 27; |
||
232 | ECX = path1; |
||
233 | $int 0x40; |
||
234 | ESDWORD[size] = EDX; |
||
235 | ESDWORD[buf] = EAX; |
||
236 | } |
||
237 | |||
7369 | leency | 238 | //===================================================// |
239 | // // |
||
240 | // Misc // |
||
241 | // // |
||
242 | //===================================================// |
||
243 | |||
7202 | leency | 244 | :bool dir_exists(dword fpath) |
3440 | leency | 245 | { |
7219 | leency | 246 | char buf[32]; |
247 | if (!ReadDir(0, #buf, fpath)) return true; |
||
248 | return false; |
||
249 | } |
||
250 | |||
8392 | leency | 251 | :dword get_file_size(dword _path) |
252 | { |
||
253 | BDVK bdvk; |
||
254 | if (GetFileInfo(_path, #bdvk)!=0) return 0; |
||
255 | else return bdvk.sizelo; |
||
256 | } |
||
257 | |||
7219 | leency | 258 | /* |
259 | // This implementation of dir_exists() is faster than |
||
260 | // previous but here virtual folders like |
||
261 | // '/' and '/tmp' are not recognised as FOLDERS |
||
262 | // by GetFileInfo() => BDVK.isfolder attribute :( |
||
263 | |||
264 | :bool dir_exists(dword fpath) |
||
265 | { |
||
3440 | leency | 266 | BDVK fpath_atr; |
6930 | leency | 267 | if (GetFileInfo(fpath, #fpath_atr) != 0) return false; |
5487 | leency | 268 | return fpath_atr.isfolder; |
3440 | leency | 269 | } |
7219 | leency | 270 | */ |
271 | |||
7202 | leency | 272 | :bool file_exists(dword fpath) |
6251 | leency | 273 | { |
274 | BDVK ReadFile_atr; |
||
275 | if (! GetFileInfo(fpath, #ReadFile_atr)) return true; |
||
276 | return false; |
||
277 | } |
||
3877 | leency | 278 | |
3444 | leency | 279 | enum |
3363 | leency | 280 | { |
3444 | leency | 281 | DIRS_ALL, |
282 | DIRS_NOROOT, |
||
283 | DIRS_ONLYREAL |
||
284 | }; |
||
285 | :int GetDir(dword dir_buf, file_count, path, doptions) |
||
7878 | leency | 286 | dword buf, fcount, error; |
287 | char readbuf[32]; |
||
3444 | leency | 288 | { |
7878 | leency | 289 | error = ReadDir(0, #readbuf, path); |
3363 | leency | 290 | if (!error) |
291 | { |
||
7878 | leency | 292 | fcount = ESDWORD[#readbuf+8]; |
293 | buf = malloc(fcount+1*304+32); |
||
3363 | leency | 294 | ReadDir(fcount, buf, path); |
3440 | leency | 295 | //fcount=EBX; |
3444 | leency | 296 | |
297 | if (doptions == DIRS_ONLYREAL) |
||
298 | { |
||
299 | if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);} |
||
300 | if (!strcmp("..",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);} |
||
301 | } |
||
302 | if (doptions == DIRS_NOROOT) |
||
303 | { |
||
304 | if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);} |
||
305 | } |
||
306 | |||
3363 | leency | 307 | ESDWORD[dir_buf] = buf; |
308 | ESDWORD[file_count] = fcount; |
||
309 | } |
||
3440 | leency | 310 | else |
3067 | leency | 311 | { |
7878 | leency | 312 | ESDWORD[dir_buf] = 0; |
3440 | leency | 313 | ESDWORD[file_count] = 0; |
3067 | leency | 314 | } |
3440 | leency | 315 | return error; |
3067 | leency | 316 | } |
317 | |||
3432 | leency | 318 | :dword abspath(dword relative_path) //GetAbsolutePathFromRelative() |
319 | { |
||
320 | char absolute_path[4096]; |
||
4137 | leency | 321 | if (ESBYTE[relative_path]=='/') |
322 | { |
||
323 | strcpy(#absolute_path, relative_path); |
||
324 | } |
||
325 | else |
||
326 | { |
||
6735 | leency | 327 | strcpy(#absolute_path, I_Path); |
4137 | leency | 328 | absolute_path[strrchr(#absolute_path, '/')] = '\0'; |
329 | strcat(#absolute_path, relative_path); |
||
330 | } |
||
3432 | leency | 331 | return #absolute_path; |
332 | } |
||
5483 | leency | 333 | |
7202 | leency | 334 | :dword GetIni(dword ini_path, ini_name) //search it on /kolibrios/ then on /sys/ |
7197 | leency | 335 | { |
7202 | leency | 336 | strcpy(ini_path, "/kolibrios/settings/"); |
337 | strcat(ini_path, ini_name); |
||
338 | if (!file_exists(ini_path)) { |
||
7213 | leency | 339 | strcpy(ini_path, "/sys/SETTINGS/"); |
7202 | leency | 340 | strcat(ini_path, ini_name); |
7197 | leency | 341 | } |
7202 | leency | 342 | return ini_path; |
7197 | leency | 343 | } |
344 | |||
7369 | leency | 345 | :dword notify(dword notify_param) |
346 | { |
||
347 | return RunProgram("/sys/@notify", notify_param); |
||
348 | } |
||
349 | |||
350 | :void die(dword _last_msg) |
||
351 | { |
||
352 | notify(_last_msg); |
||
353 | ExitProcess(); |
||
354 | } |
||
355 | |||
8946 | leency | 356 | :bool file_name_is_8_3(dword name) |
357 | { |
||
358 | strlen(name); |
||
359 | if (EAX>12) return false; |
||
360 | $push eax |
||
361 | strrchr(name, '.'); |
||
362 | $pop ebx |
||
363 | |||
364 | //EAX = dot pos |
||
365 | //EBX = name length |
||
366 | |||
367 | if (EAX) { |
||
368 | if (EBX-EAX>3) return false; |
||
369 | } else { |
||
370 | if (EBX>8) return false; |
||
371 | } |
||
372 | return true; |
||
373 | } |
||
374 | |||
7369 | leency | 375 | //===================================================// |
376 | // // |
||
377 | // Convert Size // |
||
378 | // // |
||
379 | //===================================================// |
||
380 | |||
5591 | pavelyakov | 381 | :byte ConvertSize_size_prefix[8]; |
5576 | pavelyakov | 382 | :dword ConvertSize(dword bytes) |
5483 | leency | 383 | { |
5591 | pavelyakov | 384 | byte size_nm[4]; |
9301 | leency | 385 | if (bytes>=1073741824) strlcpy(#size_nm, "GB",2); |
386 | else if (bytes>=1048576) strlcpy(#size_nm, "MB",2); |
||
387 | else if (bytes>=1024) strlcpy(#size_nm, "KB",2); |
||
9003 | leency | 388 | else strlcpy(#size_nm, "B ",2); |
6989 | leency | 389 | while (bytes>1023) bytes >>= 10; |
5591 | pavelyakov | 390 | sprintf(#ConvertSize_size_prefix,"%d %s",bytes,#size_nm); |
391 | return #ConvertSize_size_prefix; |
||
5493 | leency | 392 | } |
7219 | leency | 393 | |
6988 | leency | 394 | :dword ConvertSize64(dword bytes_lo, bytes_hi) |
395 | { |
||
396 | if (bytes_hi > 0) { |
||
6989 | leency | 397 | if (bytes_lo>=1073741824) bytes_lo >>= 30; else bytes_lo = 0; |
9301 | leency | 398 | sprintf(#ConvertSize_size_prefix,"%d GB",bytes_hi<<2 + bytes_lo); |
6988 | leency | 399 | return #ConvertSize_size_prefix; |
400 | } |
||
401 | else return ConvertSize(bytes_lo); |
||
402 | } |
||
7219 | leency | 403 | |
7369 | leency | 404 | :unsigned char size[25]; |
5493 | leency | 405 | :dword ConvertSizeToKb(unsigned int bytes) |
406 | { |
||
407 | dword kb_line; |
||
408 | |||
6568 | leency | 409 | if (bytes >= 1024) |
410 | { |
||
411 | kb_line = itoa(bytes / 1024); |
||
412 | strcpy(#size, kb_line); |
||
9301 | leency | 413 | strcat(#size, " KB"); |
6568 | leency | 414 | } |
415 | else { |
||
416 | kb_line = itoa(bytes); |
||
417 | strcpy(#size, kb_line); |
||
9003 | leency | 418 | strcat(#size, " B"); |
6568 | leency | 419 | } |
5493 | leency | 420 | |
421 | return #size; |
||
5598 | pavelyakov | 422 | } |
7210 | leency | 423 | |
7369 | leency | 424 | //===================================================// |
425 | // // |
||
426 | // Copy // |
||
427 | // // |
||
428 | //===================================================// |
||
429 | |||
7210 | leency | 430 | :int CopyFileAtOnce(dword size, copyFrom, copyTo) |
431 | dword cbuf; |
||
432 | int error; |
||
433 | { |
||
434 | cbuf = malloc(size); |
||
435 | if (error = ReadFile(0, size, cbuf, copyFrom)) |
||
436 | { |
||
437 | debugln("Error: CopyFileAtOnce->ReadFile"); |
||
438 | } |
||
439 | else |
||
440 | { |
||
7227 | leency | 441 | if (error = CreateFile(size, cbuf, copyTo)) debugln("Error: CopyFileAtOnce->CreateFile"); |
7210 | leency | 442 | } |
443 | free(cbuf); |
||
444 | return error; |
||
445 | } |
||
446 | |||
447 | :int CopyFileByBlocks(dword size, copyFrom, copyTo) |
||
448 | dword cbuf; |
||
449 | int error=-1; |
||
450 | dword offpos=0; |
||
7227 | leency | 451 | int block_size=1024*1024*4; //copy by 4 MiB |
7210 | leency | 452 | { |
7227 | leency | 453 | if (GetFreeRAM()>1024*78) { |
454 | //Set block size 32 MiB |
||
455 | block_size <<= 3; |
||
456 | } |
||
7210 | leency | 457 | cbuf = malloc(block_size); |
7227 | leency | 458 | if (error = CreateFile(0, 0, copyTo)) |
459 | { |
||
460 | debugln("Error: CopyFileByBlocks->CreateFile"); |
||
461 | size = -1; |
||
462 | } |
||
7210 | leency | 463 | while(offpos < size) |
464 | { |
||
465 | error = ReadFile(offpos, block_size, cbuf, copyFrom); |
||
466 | if (error = 6) { //File ended before last byte was readed |
||
467 | block_size = EBX; |
||
7227 | leency | 468 | if (block_size+offpos>=size) error=0; |
7210 | leency | 469 | } |
470 | else |
||
7227 | leency | 471 | if (error!=0) { |
472 | debugln("Error: CopyFileByBlocks->ReadFile"); |
||
473 | break; |
||
474 | } |
||
475 | if (error = WriteFile(offpos, block_size, cbuf, copyTo)) { |
||
476 | debugln("Error: CopyFileByBlocks->WriteFile"); |
||
477 | break; |
||
478 | } |
||
7210 | leency | 479 | offpos += block_size; |
480 | } |
||
481 | free(cbuf); |
||
482 | return error; |
||
483 | } |
||
484 | |||
7369 | leency | 485 | //===================================================// |
486 | // // |
||
487 | // Directory Size // |
||
488 | // // |
||
489 | //===================================================// |
||
7210 | leency | 490 | |
7878 | leency | 491 | :struct DIR_SIZE |
7369 | leency | 492 | { |
493 | BDVK dir_info; |
||
494 | dword folders; |
||
495 | dword files; |
||
496 | dword bytes; |
||
7878 | leency | 497 | dword get(); |
498 | dword calculate_loop(); |
||
499 | }; |
||
7369 | leency | 500 | |
7878 | leency | 501 | :dword DIR_SIZE::get(dword way1) |
7369 | leency | 502 | { |
503 | folders = files = bytes = 0; |
||
7878 | leency | 504 | if (!way1) return 0; |
505 | calculate_loop(way1); |
||
7369 | leency | 506 | } |
507 | |||
7878 | leency | 508 | :dword DIR_SIZE::calculate_loop(dword way) |
7369 | leency | 509 | { |
510 | dword dirbuf, fcount, i, filename; |
||
511 | dword cur_file; |
||
7878 | leency | 512 | if (!way) return 0; |
7369 | leency | 513 | if (dir_exists(way)) |
514 | { |
||
515 | cur_file = malloc(4096); |
||
516 | // In the process of recursive descent, memory must be allocated dynamically, |
||
517 | // because the static memory -> was a bug !!! But unfortunately pass away to sacrifice speed. |
||
518 | GetDir(#dirbuf, #fcount, way, DIRS_ONLYREAL); |
||
519 | for (i=0; i |
||
520 | { |
||
521 | filename = i*304+dirbuf+72; |
||
522 | sprintf(cur_file,"%s/%s",way,filename); |
||
523 | |||
8944 | leency | 524 | if (ESDWORD[filename-40] & ATR_FOLDER ) |
7369 | leency | 525 | { |
526 | folders++; |
||
527 | calculate_loop(cur_file); |
||
528 | } |
||
529 | else |
||
530 | { |
||
531 | GetFileInfo(cur_file, #dir_info); |
||
532 | bytes += dir_info.sizelo; |
||
533 | files++; |
||
534 | } |
||
535 | } |
||
536 | free(cur_file); |
||
537 | free(dirbuf); |
||
538 | } |
||
7878 | leency | 539 | return files; |
7369 | leency | 540 | } |
541 | |||
7878 | leency | 542 | |
5598 | pavelyakov | 543 | #endif>=><=>2><2> |