Subversion Repositories Kolibri OS

Rev

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