Subversion Repositories Kolibri OS

Rev

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