summaryrefslogtreecommitdiff
path: root/updater/utils.cpp
blob: daed5a8b41ddb8a62124eeb5a778295a6107b510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "common.h"
#include "utils.h"

bool VersionFromString(const char *szVer, DWORD *pdwVer) {
	char *p = (char *)szVer;
	*pdwVer = 0;

	int bytes = 1; // we start in the first 'byte'
	int digit_count = 0;
	while(*p && bytes <= 4 && digit_count <= 3) {
		if(*p >= '0' && *p <= '9') {
			*pdwVer = (*pdwVer & 0xFFFFFF00) + (*pdwVer & 0xFF) * 10 + (*p - '0');
			digit_count++;
		} else if(*p == '.') {
			*pdwVer = *pdwVer << 8;
			bytes++;
			digit_count = 0;
		} else {
			if(bytes < 3) // allow other chars on the end (e.g. space)
				return false; // incompatible version string
			else 
				return true;
		}
		p++;
	}

	// version must be x.x.x.x format (for now - until a convention is established
	// whereby we assume '0' bytes as either prefix or suffix)
	// 15/3/06 - allowing 3 digit version numbers (ostensibly for spamfilter definition files which use date for version)
	return (bytes >= 3);

}

int CheckForFileID(char *update_url, char *version_url, char *name) {
	if(strlen(update_url) > 45 && strncmp(update_url, MIM_DOWNLOAD_URL_PREFIX, 45) == 0) {
		char *p = update_url + 45;
		return atoi(p);
	}
	if(strlen(update_url) > 51 && strncmp(update_url, "http://www.miranda-im.org/download/feed.php?dlfile=", 51) == 0) {
		char *p = update_url + 51;
		return atoi(p);
	}
	if(strlen(update_url) > 47 && strncmp(update_url, "http://miranda-im.org/download/feed.php?dlfile=", 47) == 0) {
		char *p = update_url + 47;
		return atoi(p);
	}
	return -1;
}

bool CreatePath(const TCHAR *path) {
	if(!path) return false;

	TCHAR folder[MAX_PATH];
	_tcscpy(folder, path);
	size_t i = 0;

	if(folder[0] && folder[1] && folder[1] == ':') i += 2; // skip drive letter

	SetLastError(ERROR_SUCCESS);
	TCHAR *p = &folder[i];
	while(folder[i]) {
		p = _tcschr(p, _T('\\'));
		if(p) {
			i = p - folder;
			p++;

			if(folder[i]) folder[i] = 0;
		} else 
			i = _tcslen(folder);

		/*
		// this fails when the user does not have permission to create intermediate folders
		if(!CreateDirectory(folder, 0)) {
			DWORD err = GetLastError();
			if(err != ERROR_ALREADY_EXISTS) {
				//MessageBox(0, Translate("Could not create temporary folder."), Translate("Error"), MB_OK | MB_ICONERROR);
				//return 1;
				return false;
			}
		}
		*/
		CreateDirectory(folder, 0);

		folder[i] = path[i];
	}

	DWORD lerr = GetLastError();
	return (lerr == ERROR_SUCCESS || lerr == ERROR_ALREADY_EXISTS);

	/*
	// this function seems to be unavailable in shell32.dll under NT4
	int ret = SHCreateDirectoryEx(0, path, 0);
	return  (ret == ERROR_SUCCESS) || (ret == ERROR_FILE_EXISTS) || (ret == ERROR_ALREADY_EXISTS);
	*/
}

// must 'mir_free' return val
TCHAR *GetTString(const char *asc)
{
	if (!asc) return NULL;
	return (TCHAR*)CallService(MS_LANGPACK_PCHARTOTCHAR, 0, (LPARAM)asc);
}

void RemoveFolder(const TCHAR *src_folder) {
	TCHAR szFilesPath[MAX_PATH], szNewFileName[MAX_PATH];
	
	_tcscpy(szFilesPath, src_folder);
	_tcscat(szFilesPath, _T("\\*.*"));

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData);
	if(hFileSearch != INVALID_HANDLE_VALUE) {
		do {
			if(findData.cFileName[0] != _T('.')) {
				_tcslwr(findData.cFileName);
				_tcscpy(szNewFileName, src_folder);
				_tcscat(szNewFileName, _T("\\"));
				_tcscat(szNewFileName, findData.cFileName);

				if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
					// recurse
					RemoveFolder(szNewFileName);
				else
					DeleteFile(szNewFileName);
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}

	RemoveDirectory(src_folder);
}

bool FolderIsEmpty(const TCHAR *folder) {
	TCHAR szFilesPath[MAX_PATH];
	
	_tcscpy(szFilesPath, folder);
	_tcscat(szFilesPath, _T("\\*.*"));

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData);
	if(hFileSearch != INVALID_HANDLE_VALUE) {
		do {
			if(_tcscmp(findData.cFileName, _T(".")) != 0 && _tcscmp(findData.cFileName, _T("..")) != 0) {
				FindClose(hFileSearch);
				return false;
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}

	return true;
}

bool DeleteNonDlls(const TCHAR *folder) {
	TCHAR szFilesPath[MAX_PATH], szFilename[MAX_PATH];

	{
		char buff[200];
#ifdef _UNICODE
		char mbFname[MAX_PATH];
		WideCharToMultiByte(CP_ACP, 0, folder, -1, mbFname, MAX_PATH, 0, 0);
		sprintf(buff, "Deleting non-dlls in %s", mbFname);
#else
		sprintf(buff, "Deleting non-dlls in %s", folder);
#endif
		NLog(buff);
	}

	_tcscpy(szFilesPath, folder);
	_tcscpy(szFilename, folder);
	_tcscat(szFilename, _T("\\"));
	TCHAR *p = szFilename + _tcslen(szFilename);
	_tcscat(szFilesPath, _T("\\*.*"));

	WIN32_FIND_DATA findData;
	HANDLE hFileSearch = FindFirstFile(szFilesPath, &findData);
	if(hFileSearch != INVALID_HANDLE_VALUE) {
		do {
			if(_tcscmp(findData.cFileName, _T(".")) != 0 && _tcscmp(findData.cFileName, _T("..")) != 0) {
				_tcscpy(p, findData.cFileName);
				if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
					DeleteNonDlls(szFilename);
				} else {
					if(_tcsstr(findData.cFileName, _T(".dll")) == 0) {
						{
							char buff[200];
#ifdef _UNICODE
							char mbFname[MAX_PATH];
							WideCharToMultiByte(CP_ACP, 0, szFilename, -1, mbFname, MAX_PATH, 0, 0);
							sprintf(buff, "Deleting %s", mbFname);
#else
							sprintf(buff, "Deleting %s", szFilename);
#endif
							NLog(buff);
						}
						DeleteFile(szFilename);	
					}
				}
			}
		} while(FindNextFile(hFileSearch, &findData));
		FindClose(hFileSearch);
	}

	return true;
}