Subversion Repositories Kolibri OS

Rev

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