Rev 7639 | Rev 8392 | 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 | |||
5487 | 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; |
||
27 | }; |
||
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 | }; |
||
43 | |||
44 | |||
5487 | leency | 45 | |
5472 | leency | 46 | :f70 getinfo_file_70; |
3440 | leency | 47 | :dword GetFileInfo(dword file_path, bdvk_struct) |
48 | { |
||
49 | getinfo_file_70.func = 5; |
||
50 | getinfo_file_70.param1 = |
||
51 | getinfo_file_70.param2 = |
||
52 | getinfo_file_70.param3 = 0; |
||
53 | getinfo_file_70.param4 = bdvk_struct; |
||
54 | getinfo_file_70.rezerv = 0; |
||
55 | getinfo_file_70.name = file_path; |
||
56 | $mov eax,70 |
||
57 | $mov ebx,#getinfo_file_70.func |
||
3444 | leency | 58 | $int 0x40 |
3440 | leency | 59 | } |
60 | |||
5554 | punk_joker | 61 | :f70 setinfo_file_70; |
62 | :dword SetFileInfo(dword file_path, bdvk_struct) |
||
63 | { |
||
64 | setinfo_file_70.func = 6; |
||
65 | setinfo_file_70.param1 = |
||
66 | setinfo_file_70.param2 = |
||
67 | setinfo_file_70.param3 = 0; |
||
68 | setinfo_file_70.param4 = bdvk_struct; |
||
69 | setinfo_file_70.rezerv = 0; |
||
70 | setinfo_file_70.name = file_path; |
||
71 | $mov eax,70 |
||
72 | $mov ebx,#setinfo_file_70.func |
||
73 | $int 0x40 |
||
74 | } |
||
75 | |||
5472 | leency | 76 | :f70 run_file_70; |
77 | :signed int RunProgram(dword run_path, run_param) |
||
3067 | leency | 78 | { |
79 | run_file_70.func = 7; |
||
80 | run_file_70.param1 = |
||
81 | run_file_70.param3 = |
||
82 | run_file_70.param4 = |
||
83 | run_file_70.rezerv = 0; |
||
84 | run_file_70.param2 = run_param; |
||
85 | run_file_70.name = run_path; |
||
86 | $mov eax,70 |
||
87 | $mov ebx,#run_file_70.func |
||
88 | $int 0x40 |
||
89 | } |
||
90 | |||
5472 | leency | 91 | :f70 create_dir_70; |
3067 | leency | 92 | :int CreateDir(dword new_folder_path) |
93 | { |
||
94 | create_dir_70.func = 9; |
||
95 | create_dir_70.param1 = |
||
96 | create_dir_70.param2 = |
||
97 | create_dir_70.param3 = |
||
98 | create_dir_70.param4 = |
||
99 | create_dir_70.rezerv = 0; |
||
100 | create_dir_70.name = new_folder_path; |
||
101 | $mov eax,70 |
||
102 | $mov ebx,#create_dir_70.func |
||
103 | $int 0x40 |
||
104 | } |
||
105 | |||
5472 | leency | 106 | :f70 del_file_70; |
3067 | leency | 107 | :int DeleteFile(dword del_file_path) |
108 | { |
||
109 | del_file_70.func = 8; |
||
110 | del_file_70.param1 = |
||
111 | del_file_70.param2 = |
||
112 | del_file_70.param3 = |
||
113 | del_file_70.param4 = |
||
114 | del_file_70.rezerv = 0; |
||
115 | del_file_70.name = del_file_path; |
||
116 | $mov eax,70 |
||
117 | $mov ebx,#del_file_70.func |
||
118 | $int 0x40 |
||
119 | } |
||
120 | |||
5472 | leency | 121 | :f70 read_file_70; |
7210 | leency | 122 | :int ReadFile(dword offset, data_size, buffer, file_path) |
3067 | leency | 123 | { |
124 | read_file_70.func = 0; |
||
7210 | leency | 125 | read_file_70.param1 = offset; |
3067 | leency | 126 | read_file_70.param2 = 0; |
7210 | leency | 127 | read_file_70.param3 = data_size; |
128 | read_file_70.param4 = buffer; |
||
3067 | leency | 129 | read_file_70.rezerv = 0; |
7210 | leency | 130 | read_file_70.name = file_path; |
3067 | leency | 131 | $mov eax,70 |
132 | $mov ebx,#read_file_70.func |
||
133 | $int 0x40 |
||
134 | } |
||
135 | |||
5472 | leency | 136 | :f70 write_file_70; |
7227 | leency | 137 | :int CreateFile(dword data_size, buffer, file_path) |
3067 | leency | 138 | { |
139 | write_file_70.func = 2; |
||
140 | write_file_70.param1 = 0; |
||
141 | write_file_70.param2 = 0; |
||
7210 | leency | 142 | write_file_70.param3 = data_size; |
143 | write_file_70.param4 = buffer; |
||
3067 | leency | 144 | write_file_70.rezerv = 0; |
7210 | leency | 145 | write_file_70.name = file_path; |
3067 | leency | 146 | $mov eax,70 |
147 | $mov ebx,#write_file_70.func |
||
148 | $int 0x40 |
||
5542 | leency | 149 | } |
3067 | leency | 150 | |
5640 | pavelyakov | 151 | //////////////////////////////////////// |
152 | // WriteInFileThatAlredyExists // |
||
153 | //////////////////////////////////////// |
||
5542 | leency | 154 | :f70 write_file_offset_70; |
7227 | leency | 155 | :int WriteFile(dword offset, data_size, buffer, file_path) |
5542 | leency | 156 | { |
157 | write_file_offset_70.func = 3; |
||
158 | write_file_offset_70.param1 = offset; |
||
159 | write_file_offset_70.param2 = 0; |
||
7210 | leency | 160 | write_file_offset_70.param3 = data_size; |
161 | write_file_offset_70.param4 = buffer; |
||
5542 | leency | 162 | write_file_offset_70.rezerv = 0; |
7210 | leency | 163 | write_file_offset_70.name = file_path; |
5542 | leency | 164 | $mov eax,70 |
165 | $mov ebx,#write_file_offset_70.func |
||
166 | $int 0x40 |
||
7210 | leency | 167 | } |
5542 | leency | 168 | |
5472 | leency | 169 | :f70 read_dir_70; |
3067 | leency | 170 | :int ReadDir(dword file_count, read_buffer, dir_path) |
3363 | leency | 171 | { |
3067 | leency | 172 | read_dir_70.func = 1; |
173 | read_dir_70.param1 = |
||
174 | read_dir_70.param2 = |
||
175 | read_dir_70.rezerv = 0; |
||
176 | read_dir_70.param3 = file_count; |
||
177 | read_dir_70.param4 = read_buffer; |
||
178 | read_dir_70.name = dir_path; |
||
179 | $mov eax,70 |
||
180 | $mov ebx,#read_dir_70.func |
||
181 | $int 0x40 |
||
182 | } |
||
183 | |||
7533 | leency | 184 | //ECX - buf pointer |
7422 | leency | 185 | inline fastcall void SetCurDir( ECX) |
186 | { |
||
187 | EAX=30; |
||
188 | EBX=1; |
||
189 | $int 0x40 |
||
190 | } |
||
191 | |||
7533 | leency | 192 | //ECX - buf pointer |
193 | //EDX - buf size |
||
194 | inline fastcall void GetCurDir( ECX, EDX) |
||
195 | { |
||
196 | EAX=30; |
||
197 | EBX=2; |
||
198 | $int 0x40 |
||
199 | } |
||
200 | |||
7369 | leency | 201 | //===================================================// |
202 | // // |
||
203 | // Misc // |
||
204 | // // |
||
205 | //===================================================// |
||
206 | |||
7202 | leency | 207 | :bool dir_exists(dword fpath) |
3440 | leency | 208 | { |
7219 | leency | 209 | char buf[32]; |
210 | if (!ReadDir(0, #buf, fpath)) return true; |
||
211 | return false; |
||
212 | } |
||
213 | |||
214 | /* |
||
215 | // This implementation of dir_exists() is faster than |
||
216 | // previous but here virtual folders like |
||
217 | // '/' and '/tmp' are not recognised as FOLDERS |
||
218 | // by GetFileInfo() => BDVK.isfolder attribute :( |
||
219 | |||
220 | :bool dir_exists(dword fpath) |
||
221 | { |
||
3440 | leency | 222 | BDVK fpath_atr; |
6930 | leency | 223 | if (GetFileInfo(fpath, #fpath_atr) != 0) return false; |
5487 | leency | 224 | return fpath_atr.isfolder; |
3440 | leency | 225 | } |
7219 | leency | 226 | */ |
227 | |||
7202 | leency | 228 | :bool file_exists(dword fpath) |
6251 | leency | 229 | { |
230 | BDVK ReadFile_atr; |
||
231 | if (! GetFileInfo(fpath, #ReadFile_atr)) return true; |
||
232 | return false; |
||
233 | } |
||
3877 | leency | 234 | |
3444 | leency | 235 | enum |
3363 | leency | 236 | { |
3444 | leency | 237 | DIRS_ALL, |
238 | DIRS_NOROOT, |
||
239 | DIRS_ONLYREAL |
||
240 | }; |
||
241 | :int GetDir(dword dir_buf, file_count, path, doptions) |
||
7878 | leency | 242 | dword buf, fcount, error; |
243 | char readbuf[32]; |
||
3444 | leency | 244 | { |
7878 | leency | 245 | error = ReadDir(0, #readbuf, path); |
3363 | leency | 246 | if (!error) |
247 | { |
||
7878 | leency | 248 | fcount = ESDWORD[#readbuf+8]; |
249 | buf = malloc(fcount+1*304+32); |
||
3363 | leency | 250 | ReadDir(fcount, buf, path); |
3440 | leency | 251 | //fcount=EBX; |
3444 | leency | 252 | |
253 | if (doptions == DIRS_ONLYREAL) |
||
254 | { |
||
255 | if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);} |
||
256 | if (!strcmp("..",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);} |
||
257 | } |
||
258 | if (doptions == DIRS_NOROOT) |
||
259 | { |
||
260 | if (!strcmp(".",buf+72)) {fcount--; memmov(buf,buf+304,fcount*304);} |
||
261 | } |
||
262 | |||
3363 | leency | 263 | ESDWORD[dir_buf] = buf; |
264 | ESDWORD[file_count] = fcount; |
||
265 | } |
||
3440 | leency | 266 | else |
3067 | leency | 267 | { |
7878 | leency | 268 | ESDWORD[dir_buf] = 0; |
3440 | leency | 269 | ESDWORD[file_count] = 0; |
3067 | leency | 270 | } |
3440 | leency | 271 | return error; |
3067 | leency | 272 | } |
273 | |||
3432 | leency | 274 | :dword abspath(dword relative_path) //GetAbsolutePathFromRelative() |
275 | { |
||
276 | char absolute_path[4096]; |
||
4137 | leency | 277 | if (ESBYTE[relative_path]=='/') |
278 | { |
||
279 | strcpy(#absolute_path, relative_path); |
||
280 | } |
||
281 | else |
||
282 | { |
||
6735 | leency | 283 | strcpy(#absolute_path, I_Path); |
4137 | leency | 284 | absolute_path[strrchr(#absolute_path, '/')] = '\0'; |
285 | strcat(#absolute_path, relative_path); |
||
286 | } |
||
3432 | leency | 287 | return #absolute_path; |
288 | } |
||
5483 | leency | 289 | |
7202 | leency | 290 | :dword GetIni(dword ini_path, ini_name) //search it on /kolibrios/ then on /sys/ |
7197 | leency | 291 | { |
7202 | leency | 292 | strcpy(ini_path, "/kolibrios/settings/"); |
293 | strcat(ini_path, ini_name); |
||
294 | if (!file_exists(ini_path)) { |
||
7213 | leency | 295 | strcpy(ini_path, "/sys/SETTINGS/"); |
7202 | leency | 296 | strcat(ini_path, ini_name); |
7197 | leency | 297 | } |
7202 | leency | 298 | return ini_path; |
7197 | leency | 299 | } |
300 | |||
7369 | leency | 301 | :dword notify(dword notify_param) |
302 | { |
||
303 | return RunProgram("/sys/@notify", notify_param); |
||
304 | } |
||
305 | |||
306 | :void die(dword _last_msg) |
||
307 | { |
||
308 | notify(_last_msg); |
||
309 | ExitProcess(); |
||
310 | } |
||
311 | |||
312 | //===================================================// |
||
313 | // // |
||
314 | // Convert Size // |
||
315 | // // |
||
316 | //===================================================// |
||
317 | |||
5591 | pavelyakov | 318 | :byte ConvertSize_size_prefix[8]; |
5576 | pavelyakov | 319 | :dword ConvertSize(dword bytes) |
5483 | leency | 320 | { |
5591 | pavelyakov | 321 | byte size_nm[4]; |
5803 | leency | 322 | if (bytes>=1073741824) strlcpy(#size_nm, "Gb",2); |
323 | else if (bytes>=1048576) strlcpy(#size_nm, "Mb",2); |
||
324 | else if (bytes>=1024) strlcpy(#size_nm, "Kb",2); |
||
325 | else strlcpy(#size_nm, "b ",2); |
||
6989 | leency | 326 | while (bytes>1023) bytes >>= 10; |
5591 | pavelyakov | 327 | sprintf(#ConvertSize_size_prefix,"%d %s",bytes,#size_nm); |
328 | return #ConvertSize_size_prefix; |
||
5493 | leency | 329 | } |
7219 | leency | 330 | |
6988 | leency | 331 | :dword ConvertSize64(dword bytes_lo, bytes_hi) |
332 | { |
||
333 | if (bytes_hi > 0) { |
||
6989 | leency | 334 | if (bytes_lo>=1073741824) bytes_lo >>= 30; else bytes_lo = 0; |
335 | sprintf(#ConvertSize_size_prefix,"%d Gb",bytes_hi<<2 + bytes_lo); |
||
6988 | leency | 336 | return #ConvertSize_size_prefix; |
337 | } |
||
338 | else return ConvertSize(bytes_lo); |
||
339 | } |
||
7219 | leency | 340 | |
7369 | leency | 341 | :unsigned char size[25]; |
5493 | leency | 342 | :dword ConvertSizeToKb(unsigned int bytes) |
343 | { |
||
344 | dword kb_line; |
||
345 | |||
6568 | leency | 346 | if (bytes >= 1024) |
347 | { |
||
348 | kb_line = itoa(bytes / 1024); |
||
349 | strcpy(#size, kb_line); |
||
350 | strcat(#size, " Kb"); |
||
351 | } |
||
352 | else { |
||
353 | kb_line = itoa(bytes); |
||
354 | strcpy(#size, kb_line); |
||
355 | strcat(#size, " b"); |
||
356 | } |
||
5493 | leency | 357 | |
358 | return #size; |
||
5598 | pavelyakov | 359 | } |
7210 | leency | 360 | |
7369 | leency | 361 | //===================================================// |
362 | // // |
||
363 | // Copy // |
||
364 | // // |
||
365 | //===================================================// |
||
366 | |||
7210 | leency | 367 | :int CopyFileAtOnce(dword size, copyFrom, copyTo) |
368 | dword cbuf; |
||
369 | int error; |
||
370 | { |
||
371 | cbuf = malloc(size); |
||
372 | if (error = ReadFile(0, size, cbuf, copyFrom)) |
||
373 | { |
||
374 | debugln("Error: CopyFileAtOnce->ReadFile"); |
||
375 | } |
||
376 | else |
||
377 | { |
||
7227 | leency | 378 | if (error = CreateFile(size, cbuf, copyTo)) debugln("Error: CopyFileAtOnce->CreateFile"); |
7210 | leency | 379 | } |
380 | free(cbuf); |
||
381 | return error; |
||
382 | } |
||
383 | |||
384 | :int CopyFileByBlocks(dword size, copyFrom, copyTo) |
||
385 | dword cbuf; |
||
386 | int error=-1; |
||
387 | dword offpos=0; |
||
7227 | leency | 388 | int block_size=1024*1024*4; //copy by 4 MiB |
7210 | leency | 389 | { |
7227 | leency | 390 | if (GetFreeRAM()>1024*78) { |
391 | //Set block size 32 MiB |
||
392 | block_size <<= 3; |
||
393 | } |
||
7210 | leency | 394 | cbuf = malloc(block_size); |
7227 | leency | 395 | if (error = CreateFile(0, 0, copyTo)) |
396 | { |
||
397 | debugln("Error: CopyFileByBlocks->CreateFile"); |
||
398 | size = -1; |
||
399 | } |
||
7210 | leency | 400 | while(offpos < size) |
401 | { |
||
402 | error = ReadFile(offpos, block_size, cbuf, copyFrom); |
||
403 | if (error = 6) { //File ended before last byte was readed |
||
404 | block_size = EBX; |
||
7227 | leency | 405 | if (block_size+offpos>=size) error=0; |
7210 | leency | 406 | } |
407 | else |
||
7227 | leency | 408 | if (error!=0) { |
409 | debugln("Error: CopyFileByBlocks->ReadFile"); |
||
410 | break; |
||
411 | } |
||
412 | if (error = WriteFile(offpos, block_size, cbuf, copyTo)) { |
||
413 | debugln("Error: CopyFileByBlocks->WriteFile"); |
||
414 | break; |
||
415 | } |
||
7210 | leency | 416 | offpos += block_size; |
417 | } |
||
418 | free(cbuf); |
||
419 | return error; |
||
420 | } |
||
421 | |||
7369 | leency | 422 | //===================================================// |
423 | // // |
||
424 | // Directory Size // |
||
425 | // // |
||
426 | //===================================================// |
||
7210 | leency | 427 | |
7878 | leency | 428 | :struct DIR_SIZE |
7369 | leency | 429 | { |
430 | BDVK dir_info; |
||
431 | dword folders; |
||
432 | dword files; |
||
433 | dword bytes; |
||
7878 | leency | 434 | dword get(); |
435 | dword calculate_loop(); |
||
436 | }; |
||
7369 | leency | 437 | |
7878 | leency | 438 | :dword DIR_SIZE::get(dword way1) |
7369 | leency | 439 | { |
440 | folders = files = bytes = 0; |
||
7878 | leency | 441 | if (!way1) return 0; |
442 | calculate_loop(way1); |
||
7369 | leency | 443 | } |
444 | |||
7878 | leency | 445 | :dword DIR_SIZE::calculate_loop(dword way) |
7369 | leency | 446 | { |
447 | dword dirbuf, fcount, i, filename; |
||
448 | dword cur_file; |
||
7878 | leency | 449 | if (!way) return 0; |
7369 | leency | 450 | if (dir_exists(way)) |
451 | { |
||
452 | cur_file = malloc(4096); |
||
453 | // In the process of recursive descent, memory must be allocated dynamically, |
||
454 | // because the static memory -> was a bug !!! But unfortunately pass away to sacrifice speed. |
||
455 | GetDir(#dirbuf, #fcount, way, DIRS_ONLYREAL); |
||
456 | for (i=0; i |
||
457 | { |
||
458 | filename = i*304+dirbuf+72; |
||
459 | sprintf(cur_file,"%s/%s",way,filename); |
||
460 | |||
461 | if (TestBit(ESDWORD[filename-40], 4) ) |
||
462 | { |
||
463 | folders++; |
||
464 | calculate_loop(cur_file); |
||
465 | } |
||
466 | else |
||
467 | { |
||
468 | GetFileInfo(cur_file, #dir_info); |
||
469 | bytes += dir_info.sizelo; |
||
470 | files++; |
||
471 | } |
||
472 | } |
||
473 | free(cur_file); |
||
474 | free(dirbuf); |
||
475 | } |
||
7878 | leency | 476 | return files; |
7369 | leency | 477 | } |
478 | |||
7878 | leency | 479 | |
5598 | pavelyakov | 480 | #endif>=><=>2><2> |