Subversion Repositories Kolibri OS

Rev

Rev 9536 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3444 leency 1
//copyf - copy file or folder with content
5598 pavelyakov 2
#ifndef INCLUDE_COPYF_H
3
#define INCLUDE_COPYF_H
5676 pavelyakov 4
#print "[include ]\n"
5515 leency 5
 
5598 pavelyakov 6
#ifndef INCLUDE_FILESYSTEM_H
7219 leency 7
#include "../lib/fs.h"
5598 pavelyakov 8
#endif
9
 
9471 punk_joker 10
enum {
11
	FILE_DEFAULT=0,
12
	FILE_EXISTS,
13
	FILE_REPLACE,
14
	FILE_SKIP,
15
};
16
 
9505 leency 17
#define WRITE_ERROR_DEBUG 0
18
#define WRITE_ERROR_NOTIFY 1
19
:int writing_error_channel = WRITE_ERROR_DEBUG;
20
 
9471 punk_joker 21
int copy_state = FILE_DEFAULT;
9534 punk_joker 22
int saved_state = FILE_DEFAULT;
23
bool is_remember = false;
9471 punk_joker 24
 
3444 leency 25
:int copyf(dword from1, in1)
3434 leency 26
{
3440 leency 27
	dword error;
28
	BDVK CopyFile_atr1;
9536 punk_joker 29
	copy_state = FILE_DEFAULT;
5515 leency 30
 
3444 leency 31
	if (!from1) || (!in1)
32
	{
7210 leency 33
		notify("Error: too few copyf() params!");
7878 leency 34
		return -1;
3444 leency 35
	}
36
	if (error = GetFileInfo(from1, #CopyFile_atr1))
37
	{
4645 leency 38
		debugln("Error: copyf->GetFileInfo");
3444 leency 39
		return error;
40
	}
6251 leency 41
	if (dir_exists(from1))
3444 leency 42
		return CopyFolder(from1, in1);
3440 leency 43
	else
9471 punk_joker 44
	{
45
		while(1)
46
		{
47
			Operation_Draw_Progress(from1+strrchr(from1, '/'));
48
			if (copy_state == FILE_DEFAULT) || (copy_state == FILE_REPLACE)
49
			{
50
				error = CopyFile(from1, in1);
51
				if (error != 222)
52
				{
53
					return error;
54
				}
55
			}
56
			if (copy_state == FILE_SKIP)
57
			{
9534 punk_joker 58
				error = 0;
9471 punk_joker 59
				return 0;
60
			}
61
		}
4059 leency 62
	}
3440 leency 63
}
3434 leency 64
 
3440 leency 65
:int CopyFile(dword copy_from3, copy_in3)
66
{
67
	BDVK CopyFile_atr;
8868 leency 68
	dword error;
7210 leency 69
 
3444 leency 70
	if (error = GetFileInfo(copy_from3, #CopyFile_atr))
3877 leency 71
	{
4645 leency 72
		debugln("Error: CopyFile->GetFileInfo");
3877 leency 73
	}
7210 leency 74
	else
3434 leency 75
	{
9471 punk_joker 76
		if (file_exists(copy_in3)) && (copy_state != FILE_REPLACE)
77
		{
9534 punk_joker 78
			if (saved_state == FILE_DEFAULT) {
79
				copy_state = FILE_EXISTS;
80
			} else {
81
				copy_state = saved_state;
82
			}
9471 punk_joker 83
			return 222;
84
		}
7210 leency 85
		if (GetFreeRAM()-1024*1024 < CopyFile_atr.sizelo) //GetFreeRam-1Mb and convert to bytes
3877 leency 86
		{
7210 leency 87
			if (error = CopyFileByBlocks(CopyFile_atr.sizelo, copy_from3, copy_in3))
88
				debugln("Error: CopyFile->CopyFileByBlocks");
3877 leency 89
		}
7210 leency 90
		else {
91
			if (error = CopyFileAtOnce(CopyFile_atr.sizelo, copy_from3, copy_in3))
92
				debugln("Error: CopyFile->CopyFileAtOnce");
93
		}
3434 leency 94
	}
9505 leency 95
	if (error) write_error(copy_from3, error);
3440 leency 96
	return error;
3434 leency 97
}
98
 
3444 leency 99
:int CopyFolder(dword from2, in2)
3434 leency 100
{
3444 leency 101
	dword dirbuf, fcount, i, filename;
9471 punk_joker 102
	char copy_from2[4096], copy_in2[4096], error;
3434 leency 103
 
3444 leency 104
	if (error = GetDir(#dirbuf, #fcount, from2, DIRS_ONLYREAL))
3440 leency 105
	{
4645 leency 106
		debugln("Error: CopyFolder->GetDir");
9505 leency 107
		write_error(from2, error);
3440 leency 108
		free(dirbuf);
3444 leency 109
		return error;
3440 leency 110
	}
4023 leency 111
 
3444 leency 112
	if (chrnum(in2, '/')>2) && (error = CreateDir(in2))
3434 leency 113
	{
4645 leency 114
		debugln("Error: CopyFolder->CreateDir");
9505 leency 115
		write_error(in2, error);
3444 leency 116
		free(dirbuf);
117
		return error;
3434 leency 118
	}
119
 
120
	for (i=0; i
121
	{
9471 punk_joker 122
		copy_state = FILE_DEFAULT;
3434 leency 123
		filename = i*304+dirbuf+72;
5598 pavelyakov 124
		sprintf(#copy_from2,"%s/%s",from2,filename);
125
		sprintf(#copy_in2,"%s/%s",in2,filename);
3444 leency 126
 
8944 leency 127
		if ( ESDWORD[filename-40] & ATR_FOLDER ) //dir_exists?
3434 leency 128
		{
5598 pavelyakov 129
			if ( (!strncmp(filename, ".",1)) || (!strncmp(filename, "..",2)) ) continue;
3440 leency 130
			CopyFolder(#copy_from2, #copy_in2);
3434 leency 131
		}
132
		else
133
		{
9471 punk_joker 134
			while(1)
3444 leency 135
			{
9471 punk_joker 136
				Operation_Draw_Progress(filename+strrchr(filename, '/'));
137
				if (copy_state == FILE_DEFAULT) || (copy_state == FILE_REPLACE)
138
				{
139
					error = CopyFile(#copy_from2, #copy_in2);
140
					if (error != 222)
141
					{
142
						if (error)
143
						{
144
							if (fabs(error)==8) { debugln("Stop copying."); break;} //TODO: may be need grobal var like stop_all
145
							error=CopyFile(#copy_from2, #copy_in2); // #2 :)
146
						}
147
						break;
148
					}
149
				}
150
				if (copy_state == FILE_SKIP)
151
				{
9534 punk_joker 152
					error = 0;
9471 punk_joker 153
					break;
154
				}
3444 leency 155
			}
3434 leency 156
		}
157
	}
158
	free(dirbuf);
3444 leency 159
	return error;
3434 leency 160
}
161
 
4023 leency 162
#ifdef LANG_RUS
7645 leency 163
	unsigned char *ERROR_TEXT[]={
4023 leency 164
	"Код #0: успешно",
165
	"Ошибка #1: не определена база и/или раздел жёсткого диска",
166
	"Ошибка #2: функция не поддерживается для этой файловой системы",
4634 Albom 167
	"Ошибка #3: неизвестная файловая система",
4023 leency 168
	0,
169
	"Ошибка #5: файл или папка не найдены",
170
	"Ошибка #6: конец файла",
171
	"Ошибка #7: указатель находится все памяти приложения",
6691 pathoswith 172
	"Ошибка #8: недостаточно места на разделе",
173
	"Ошибка #9: ошибка файловой системы",
4023 leency 174
	"Ошибка #10: доступ запрещен",
5642 leency 175
	"Ошибка #11: ошибка устройства",
4023 leency 176
	0, 0, 0, 0, 0, 0, 0, 0, 0,
6691 pathoswith 177
	0, 0, 0, 0, 0, 0, 0, 0, 0,
4023 leency 178
	"Ошибка #30: недостаточно памяти",
179
	"Ошибка #31: файл не является исполняемым",
180
	"Ошибка #32: слишком много процессов", 0};
181
#else
7645 leency 182
	unsigned char *ERROR_TEXT[]={
8953 leency 183
	"Code #0 - No error",
4023 leency 184
	"Error #1 - Base or partition of a hard disk is not defined",
9505 leency 185
	"Error #2 - Function is not supported for this file system",
4023 leency 186
	"Error #3 - Unknown file system",
187
	0,
188
	"Error #5 - File or folder not found",
189
	"Error #6 - End of file",
190
	"Error #7 - Pointer lies outside of application memory",
6691 pathoswith 191
	"Error #8 - Not enough space on partition",
192
	"Error #9 - File system error",
4023 leency 193
	"Error #10 - Access denied",
194
	"Error #11 - Device error",
195
	0, 0, 0, 0, 0, 0, 0, 0, 0,
6691 pathoswith 196
	0, 0, 0, 0, 0, 0, 0, 0, 0,
4023 leency 197
	"Error #30 - Not enough memory",
198
	"Error #31 - File is not executable",
199
	"Error #32 - Too many processes", 0};
200
#endif
3440 leency 201
 
9505 leency 202
:dword get_error(int error_number)
3434 leency 203
{
9505 leency 204
	char error_message[256];
205
	error_number = fabs(error_number);
206
	if (error_number<=33) {
207
		strcpy(#error_message, ERROR_TEXT[error_number]);
208
	} else {
209
		miniprintf(#error_message,"%i - Unknown error number O_o",error_number);
210
	}
211
	return #error_message;
3434 leency 212
}
213
 
9505 leency 214
:void write_error(dword path, error_number)
3434 leency 215
{
9505 leency 216
	char notify_message[100];
4645 leency 217
	if (path) debugln(path);
218
	debugln(get_error(error_number));
9505 leency 219
	if (writing_error_channel == WRITE_ERROR_NOTIFY) {
220
		miniprintf(#notify_message, "'%s' -E", get_error(error_number));
221
		notify(#notify_message);
222
	}
5598 pavelyakov 223
}
224
 
225
#endif