aboutsummaryrefslogtreecommitdiff
path: root/src/installer/setupapi.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/installer/setupapi.cpp')
-rw-r--r--src/installer/setupapi.cpp473
1 files changed, 473 insertions, 0 deletions
diff --git a/src/installer/setupapi.cpp b/src/installer/setupapi.cpp
new file mode 100644
index 0000000..b90fd04
--- /dev/null
+++ b/src/installer/setupapi.cpp
@@ -0,0 +1,473 @@
1/** BEGIN COPYRIGHT BLOCK
2 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
3 * Copyright (C) 2005 Red Hat, Inc.
4 * All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation version
9 * 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * END COPYRIGHT BLOCK **/
20#ifdef WINDOWS
21#define XP_WIN32
22#endif
23#ifndef MAX_PATH
24#define MAX_PATH 127
25#endif
26#ifndef TRUE
27#define TRUE (1==1)
28#endif
29#ifndef FALSE
30#define FALSE (1==0)
31#endif
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <errno.h>
35#include <stdio.h>
36#include <stdarg.h>
37#include <time.h>
38#include <string.h>
39#include <ctype.h>
40
41#ifdef XP_WIN32
42 #include <windows.h>
43 #include <regstr.h>
44 #include <direct.h>
45 #include <io.h> /* For _findfirst */
46#else
47
48
49 #include <sys/types.h>
50 #include <sys/statvfs.h>
51 #include <sys/socket.h> /* socket, bind */
52 #include <netinet/in.h> /* htonl */
53 #include <netdb.h> /* gethostbyname */
54 #include <arpa/inet.h> /* inet_ntoa */
55 #include <dirent.h>
56 #include <assert.h>
57
58#endif
59
60
61#define MINPORT 1024
62#define MAXPORT 65535
63
64
65
66 /*********************************************************************
67 **
68 ** FUNCTION: setupIsDirEmpty
69 ** Replaced: IsDirEmpty
70 **
71 ** DESCRIPTION: checks to see if directory empty
72 **
73 **
74 ** INPUTS:
75 **
76 ** RETURN:
77 ** TRUE or FALSE
78 **
79 ** SIDE EFFECTS:
80 ** none
81 ** RESTRICTIONS:
82 ** None
83 ** MEMORY:
84 **********************************************************************/
85
86int setupIsDirEmpty(const char * pszPath)
87 {
88#ifdef XP_WIN32
89 int iReturn = TRUE;
90 char szFileSpec[MAX_PATH];
91 WIN32_FIND_DATA fd;
92 HANDLE ff;
93
94 if (!(pszPath))
95 {
96 //setupLog(NULL, "setupIsDirEmpty failed: passed in NULL parameter for directory Name");
97 return FALSE;
98 }
99 snprintf(szFileSpec, sizeof(szFileSpec), "%s\\*", pszPath);
100 if (!memchr(szFileSpec, 0, sizeof(szFileSpec))) return FALSE; /*overflow*/
101
102 ff = FindFirstFile(szFileSpec, &fd);
103 if (ff != INVALID_HANDLE_VALUE)
104 {
105 do
106 {
107 if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
108 {
109 iReturn = FALSE;
110 break;
111 }
112 } while (FindNextFile(ff, &fd));
113 FindClose(ff);
114 }
115
116 return (iReturn);
117#else
118 DIR *dirp;
119 struct dirent *dp;
120 bool rc = TRUE;
121
122 dirp = opendir(pszPath);
123 while ((dp = readdir(dirp)) != NULL)
124 {
125 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, ".."))
126 {
127 rc = FALSE;
128 break;
129 }
130 }
131 closedir(dirp);
132 return rc;
133#endif
134
135 }
136
137 /*********************************************************************
138 **
139 ** FUNCTION: setupFileExist
140 ** Replaced: FileExist
141 **
142 ** DESCRIPTION: returns 1 if file exists.
143 **
144 ** INPUTS:
145 **
146 ** RETURN:
147 ** TRUE or FALSE
148 **
149 ** SIDE EFFECTS:
150 ** none
151 ** RESTRICTIONS:
152 ** None
153 ** MEMORY:
154 **********************************************************************/
155int setupFileExists(const char * pszFileName)
156 {
157 struct stat fi;
158
159 if ((stat (pszFileName, &fi) != -1) && ((fi.st_mode & S_IFDIR) == 0))
160 {
161 return TRUE;
162 }
163 else
164 {
165 return FALSE;
166 }
167 }
168
169
170 /*********************************************************************
171 **
172 ** FUNCTION: setupDirExists
173 ** Replaced: DirExists
174 **
175 ** DESCRIPTION: checks to see if directory exists
176 **
177 **
178 ** INPUTS:
179 **
180 ** RETURN:
181 ** TRUE or FALSE
182 **
183 ** SIDE EFFECTS:
184 ** none
185 ** RESTRICTIONS:
186 ** None
187 ** MEMORY:
188 **********************************************************************/
189int setupDirExists(const char * pszDirName)
190 {
191#ifdef XP_WIN32
192 unsigned int dwAttrs;
193 if (!(pszDirName))
194 {
195 //setupLog(NULL, "DirExists failed: passed in NULL parameter for directory");
196 return FALSE;
197 }
198 dwAttrs = GetFileAttributes(pszDirName);
199 if ((dwAttrs != 0xFFFFFFFF) && (dwAttrs & FILE_ATTRIBUTE_DIRECTORY))
200 {
201 return TRUE;
202 }
203 return FALSE;
204#else
205 struct stat fi;
206
207 if (stat (pszDirName, &fi) == -1 || !S_ISDIR(fi.st_mode))
208 {
209 return FALSE;
210 }
211 else
212 {
213 return TRUE;
214 }
215#endif
216 }
217
218 /*********************************************************************
219 **
220 ** FUNCTION: setupCreateDir
221 ** Replaced: CreateDir
222 **
223 ** DESCRIPTION: creates a directory
224 **
225 **
226 ** INPUTS:
227 **
228 ** RETURN:
229 ** NT: TRUE or FALSE
230 ** UNIX:
231 ** 0 : OK
232 ** -1 : Can't create directory
233 ** -2 : input exists and is not a directory
234 ** -3 : Can't write to directory
235 **
236 ** SIDE EFFECTS:
237 ** none
238 ** RESTRICTIONS:
239 ** None
240 ** MEMORY:
241 **
242 **********************************************************************/
243bool setupCreateDir(const char * pszDirName, int mode)
244 {
245#ifdef XP_WIN32
246 char *pszDelim;
247 char szDirName[MAX_PATH];
248 char szTmpDir[MAX_PATH] = "";
249 int cbLen;
250
251 if (!(pszDirName))
252 {
253 //setupLog(NULL, "CreateDir failed: passed in NULL parameter for directory");
254 return FALSE;
255 }
256
257 if (strlen(pszDirName) >= sizeof(szDirName))
258 {
259 //setupLog(NULL, "CreateDir failed: passed in too long directory");
260 return FALSE;
261 }
262
263 snprintf(szDirName, sizeof(szDirName), "%s", pszDirName);
264 if (!memchr(szDirName, 0, sizeof(szDirName))) return FALSE; /*overflow*/
265 pszDelim = szDirName;
266 while ((pszDelim = strchr(pszDelim, '/')) != NULL)
267 {
268 *pszDelim = '\\';
269 }
270
271 pszDelim = szDirName;
272 while ((pszDelim = strchr(pszDelim, '\\')) != NULL)
273 {
274 cbLen = (pszDelim - szDirName);
275 strncpy(szTmpDir, szDirName, cbLen);
276 szTmpDir[cbLen] = '\0';
277 CreateDirectory(szTmpDir, NULL);
278 pszDelim++;
279 }
280
281 if (!CreateDirectory(szDirName, NULL))
282 {
283// char szLog[MAX_PATH+50];
284 return FALSE;
285// snprintf(szLog, sizeof(szLog),
286// "setupCreateDir failed to create '%s'", pszDirName);
287// if (!memchr(szLog, 0, sizeof(szLog))) return FALSE; /*overflow*/
288 //setupLog(NULL, szLog);
289 }
290 return TRUE;
291#else
292 struct stat fi;
293 char *s;
294 char *t;
295
296 s = strdup(pszDirName);
297 t = s + 1;
298
299 while (1)
300 {
301 t = strchr(t, '/');
302
303 if (t)
304 *t = '\0';
305 if (stat(s, &fi) == -1)
306 {
307 if (mkdir(s, mode) == -1)
308 {
309 return FALSE;
310 }
311 }
312 else if (!S_ISDIR(fi.st_mode))
313 {
314 return FALSE;
315 }
316 if (t)
317 *t++ = '/';
318 else
319 break;
320 }
321
322 if (access(pszDirName, W_OK) == -1)
323 {
324 return FALSE;
325 }
326 return TRUE;
327#endif
328 }
329 /*********************************************************************
330 **
331 ** FUNCTION: setupConvertsPathSlashes
332 ** Replaced: ConvertsPathSlashes
333 **
334 ** DESCRIPTION: converts slashes TRUE UNIX to NT, FALSE NT to UNIX,
335 **
336 ** INPUTS:
337 **
338 ** RETURN:
339 ** TRUE or FALSE
340 **
341 ** SIDE EFFECTS:
342 ** none
343 ** RESTRICTIONS:
344 ** None
345 ** MEMORY:
346 **********************************************************************/
347int setupConvertPathSlashes(const char * lpszPath, char * lpszNewPath, int bType )
348 {
349 if (lpszPath == NULL)
350 return FALSE;
351
352 /* create reverse slashes and escape them */
353 while (*lpszPath)
354 {
355 if ((*lpszPath == '\\') || (*lpszPath == '/'))
356 {
357 if (bType)
358 *lpszNewPath = '\\';
359 else
360 *lpszNewPath = '/';
361 }
362 else
363 *lpszNewPath = *lpszPath;
364 lpszPath++;
365 lpszNewPath++;
366 }
367 return TRUE;
368 }
369
370
371unsigned int setupExecProgram(const char *program, const char *where)
372{
373 char curdir[MAX_PATH];
374 unsigned int ret;
375#ifdef XP_WIN32
376 char szWhere[MAX_PATH];
377 char szProgram[MAX_PATH];
378 STARTUPINFO si;
379 PROCESS_INFORMATION pi;
380 DWORD dwCreationFlags;
381#endif
382
383 if (!program)
384 {
385 //setupLogMessage("info", "setup", "setupExecProgram called with no program");
386 return 0;
387 }
388 if (!setupFileExists(program))
389 {
390 //setupLogMessage("info", "setup", "setupExecProgram called with invalid program %s", program);
391 }
392 getcwd(curdir, sizeof(curdir));
393
394 if (where)
395 {
396 chdir(where);
397 }
398#ifndef XP_WIN32
399 ret = system(program);
400#else
401 snprintf(szProgram, sizeof(szProgram), "%s", program);
402 if (!memchr(szProgram, 0, sizeof(szProgram))) {
403 //setupLogMessage("info", "setup", "Too long program %s", program);
404 return 0;
405 }
406 snprintf(szWhere, sizeof(szWhere), "%s", where);
407 if (!memchr(szWhere, 0, sizeof(szWhere))) {
408 //setupLogMessage("info", "setup", "Too long arg %s", where);
409 return 0;
410 }
411 memset(&si,0,sizeof(si));
412 si.cb = sizeof(si);
413 GetStartupInfo(&si);
414 si.wShowWindow = SW_SHOWDEFAULT;
415 dwCreationFlags = NORMAL_PRIORITY_CLASS | DETACHED_PROCESS;
416 ret = CreateProcess(NULL, szProgram, NULL, NULL, FALSE, dwCreationFlags, NULL, szWhere, &si, &pi);
417#endif
418
419 if (where)
420 {
421 chdir(curdir);
422 }
423
424 return ret;
425}
426
427
428/*********************************************************************
429**
430** FUNCTION: setupGetDiskFreeSpace
431** DESCRIPTION: get available disk space in Kilobyte
432**
433** INPUTS: Path name
434** OUTPUTS:
435** RETURN: available disk space in kilobyte
436** SIDE EFFECTS:
437** None
438** RESTRICTIONS:
439** None
440** MEMORY:
441**********************************************************************
442*/
443
444unsigned long setupGetDiskFreeSpace(const char *path)
445{
446#ifdef XP_WIN32
447 DWORD dwSectorsPerCluster;
448 DWORD dwBytesPerSector;
449 DWORD dwFreeClusters;
450 DWORD dwTotalClusters;
451
452 if (GetDiskFreeSpace(path, &dwSectorsPerCluster, &dwBytesPerSector, &dwFreeClusters, &dwTotalClusters))
453 {
454 long long clusterSize;
455
456 clusterSize = dwSectorsPerCluster * dwBytesPerSector;
457
458 return (unsigned long)((clusterSize / 1024.0) * dwFreeClusters);
459 }
460
461 return 0;
462#else
463 struct statvfs buf;
464
465 statvfs(path, &buf);
466
467 if (buf.f_frsize == 0)
468 return (unsigned long)(((unsigned long long)buf.f_bavail / 1024.0) * buf.f_bsize);
469 else
470 return (unsigned long)(((unsigned long long)buf.f_bavail / 1024.0) * buf.f_frsize);
471#endif
472}
473