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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* The "SDK Manager" is for Windows only.
* This simple .exe will sit at the root of the Windows SDK
* and currently simply executes tools\android.bat.
* Eventually it should simply replace the batch file.
*
* TODO:
* - create temp dir, always copy *.jar there, exec android.jar
* - get jars to copy from some file
* - use a version number to copy jars only if needed (tools.revision?)
*/
#ifdef _WIN32
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <windows.h>
int _enable_dprintf = 0;
void dprintf(char *msg, ...) {
va_list ap;
va_start(ap, msg);
if (_enable_dprintf) {
vfprintf(stderr, msg, ap);
}
va_end(ap);
}
void display_error(LPSTR description) {
DWORD err = GetLastError();
LPSTR s, s2;
fprintf(stderr, "%s, error %ld\n", description, err);
if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | /* dwFlags */
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, /* lpSource */
err, /* dwMessageId */
0, /* dwLanguageId */
(LPSTR)&s, /* lpBuffer */
0, /* nSize */
NULL) != 0) { /* va_list args */
fprintf(stderr, "%s", s);
s2 = (LPSTR) malloc(strlen(description) + strlen(s) + 5);
sprintf(s2, "%s\r\n%s", description, s);
MessageBox(NULL, s2, "Android SDK Manager - Error", MB_OK);
free(s2);
LocalFree(s);
}
}
HANDLE create_temp_file(LPSTR temp_filename) {
HANDLE file_handle = INVALID_HANDLE_VALUE;
LPSTR temp_path = (LPSTR) malloc(MAX_PATH);
/* Get the temp directory path using GetTempPath.
GetTempFilename indicates that the temp path dir should not be larger than MAX_PATH-14.
*/
int ret = GetTempPath(MAX_PATH - 14, temp_path);
if (ret > MAX_PATH || ret == 0) {
display_error("GetTempPath failed");
free(temp_path);
return INVALID_HANDLE_VALUE;
}
/* Now get a temp filename in the temp directory. */
if (!GetTempFileName(temp_path, "txt", 0, temp_filename)) {
display_error("GetTempFileName failed");
} else {
SECURITY_ATTRIBUTES sattr;
ZeroMemory(&sattr, sizeof(sattr));
sattr.nLength = sizeof(SECURITY_ATTRIBUTES);
sattr.bInheritHandle = TRUE;
file_handle = CreateFile(temp_filename, // filename
GENERIC_WRITE, // access: write
FILE_SHARE_READ, // share mode: read OK
&sattr, // security attributes
CREATE_ALWAYS, // create even if exists
FILE_ATTRIBUTE_NORMAL, // flags and attributes
NULL); // template
if (file_handle == INVALID_HANDLE_VALUE) {
display_error("Create temp file failed");
}
}
free(temp_path);
return file_handle;
}
void read_temp_file(LPSTR temp_filename) {
HANDLE handle;
handle = CreateFile(temp_filename, // filename
GENERIC_READ, // access: read
FILE_SHARE_READ, // share mode: read OK
NULL, // security attributes
OPEN_EXISTING, // only open existing file
FILE_ATTRIBUTE_NORMAL, // flags and attributes
NULL); // template
if (handle == INVALID_HANDLE_VALUE) {
display_error("Open temp file failed");
return;
}
/* Cap the size we're reading.
4K is good enough to display in a message box.
*/
DWORD size = 4096;
LPSTR buffer = (LPSTR) malloc(size + 1);
LPSTR p = buffer;
DWORD num_left = size;
DWORD num_read;
do {
if (!ReadFile(handle, p, num_left, &num_read, NULL)) {
display_error("Read Output failed");
break;
}
num_left -= num_read;
p += num_read;
} while (num_read > 0);
if (p != buffer) {
*p = 0;
/* Only output the buffer if it contains special keywords WARNING or ERROR. */
char* s1 = strstr(buffer, "WARNING");
char* s2 = strstr(buffer, "ERROR");
if (s2 != NULL && s2 < s1) {
s1 = s2;
}
if (s1 != NULL) {
/* We end the message at the first occurence of [INFO]. */
s2 = strstr(s1, "[INFO]");
if (s2 != NULL) {
*s2 = 0;
}
MessageBox(NULL, s1, "Android SDK Manager - Output", MB_OK);
}
}
free(buffer);
if (!CloseHandle(handle)) {
display_error("CloseHandle read temp file failed");
}
}
int sdk_launcher() {
int result = 0;
STARTUPINFO startup;
PROCESS_INFORMATION pinfo;
CHAR program_dir[MAX_PATH];
int ret, pos;
CHAR temp_filename[MAX_PATH];
HANDLE temp_handle;
ZeroMemory(&pinfo, sizeof(pinfo));
temp_handle = create_temp_file(temp_filename);
if (temp_handle == INVALID_HANDLE_VALUE) {
return 1;
}
ZeroMemory(&startup, sizeof(startup));
startup.cb = sizeof(startup);
startup.dwFlags = STARTF_USESTDHANDLES;
startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
startup.hStdOutput = temp_handle;
startup.hStdError = temp_handle;
/* get path of current program, to switch dirs here when executing the command. */
ret = GetModuleFileName(NULL, program_dir, sizeof(program_dir));
if (ret == 0) {
display_error("Failed to get program's filename:");
result = 1;
} else {
/* Remove the last segment to keep only the directory. */
pos = ret - 1;
while (pos > 0 && program_dir[pos] != '\\') {
--pos;
}
program_dir[pos] = 0;
}
if (!result) {
dprintf("Program dir: %s\n", program_dir);
ret = CreateProcess(
NULL, /* program path */
"tools\\android.bat update sdk", /* command-line */
NULL, /* process handle is not inheritable */
NULL, /* thread handle is not inheritable */
TRUE, /* yes, inherit some handles */
CREATE_NO_WINDOW, /* we don't want a console */
NULL, /* use parent's environment block */
program_dir, /* use parent's starting directory */
&startup, /* startup info, i.e. std handles */
&pinfo);
dprintf("CreateProcess returned %d\n", ret);
if (!ret) {
display_error("Failed to execute tools\\android.bat:");
result = 1;
} else {
dprintf("Wait for process to finish.\n");
WaitForSingleObject(pinfo.hProcess, INFINITE);
CloseHandle(pinfo.hProcess);
CloseHandle(pinfo.hThread);
}
}
dprintf("Cleanup.\n");
if (!CloseHandle(temp_handle)) {
display_error("CloseHandle temp file failed");
}
if (!result) {
read_temp_file(temp_filename);
}
if (!DeleteFile(temp_filename)) {
display_error("Delete temp file failed");
}
return result;
}
int main(int argc, char **argv) {
_enable_dprintf = argc > 1 && strcmp(argv[1], "-v") == 0;
dprintf("Verbose debug mode.\n");
return sdk_launcher();
}
#endif /* _WIN32 */
|