Subversion Repositories Kolibri OS

Rev

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