aboutsummaryrefslogtreecommitdiff
path: root/src/installer/setupapi.cpp
blob: b90fd043cf25038410c5425cbd9cab6edad2c1e7 (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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc.  Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation version
 * 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * END COPYRIGHT BLOCK **/
#ifdef WINDOWS
#define XP_WIN32
#endif
#ifndef MAX_PATH
#define MAX_PATH 127
#endif
#ifndef TRUE
#define TRUE (1==1)
#endif
#ifndef FALSE
#define FALSE (1==0)
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <ctype.h>

#ifdef XP_WIN32
  #include <windows.h>
  #include <regstr.h>
  #include <direct.h>
  #include <io.h>    /* For _findfirst */
#else


  #include <sys/types.h>
  #include <sys/statvfs.h>
  #include <sys/socket.h>         /* socket, bind */
  #include <netinet/in.h>         /* htonl */
  #include <netdb.h>              /* gethostbyname */
  #include <arpa/inet.h>          /* inet_ntoa */
  #include <dirent.h>
  #include <assert.h>

#endif


#define MINPORT       1024
#define MAXPORT       65535



  /*********************************************************************
  **
  ** FUNCTION: setupIsDirEmpty
  ** Replaced: IsDirEmpty
  **
  ** DESCRIPTION: checks to see if directory empty
  **
  **
  ** INPUTS:
  **
  ** RETURN:
  **   TRUE or FALSE
  **
  ** SIDE EFFECTS:
  **      none
  ** RESTRICTIONS:
  **      None
  ** MEMORY:
  **********************************************************************/

int setupIsDirEmpty(const char * pszPath)
  {
#ifdef XP_WIN32
    int iReturn = TRUE;
    char szFileSpec[MAX_PATH];
    WIN32_FIND_DATA fd;
    HANDLE ff;

    if (!(pszPath))
    {
      //setupLog(NULL, "setupIsDirEmpty failed: passed in NULL parameter for directory Name");
      return FALSE;
    }
    snprintf(szFileSpec, sizeof(szFileSpec), "%s\\*", pszPath);
    if (!memchr(szFileSpec, 0, sizeof(szFileSpec))) return FALSE; /*overflow*/

    ff = FindFirstFile(szFileSpec, &fd);
    if (ff != INVALID_HANDLE_VALUE)
    {
      do
      {
        if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
        {
          iReturn = FALSE;
          break;
        }
      } while (FindNextFile(ff, &fd));
      FindClose(ff);
    }

    return (iReturn);
#else
    DIR *dirp;
    struct dirent *dp;
    bool rc = TRUE;

    dirp = opendir(pszPath);
    while ((dp = readdir(dirp)) != NULL)
    {
      if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, ".."))
      {
        rc = FALSE;
        break;
      }
    }
    closedir(dirp);
    return rc;
#endif

  }

  /*********************************************************************
  **
  ** FUNCTION: setupFileExist
  ** Replaced: FileExist
  **
  ** DESCRIPTION: returns 1 if file exists.
  **
  ** INPUTS:
  **
  ** RETURN:
  **   TRUE or FALSE
  **
  ** SIDE EFFECTS:
  **      none
  ** RESTRICTIONS:
  **      None
  ** MEMORY:
  **********************************************************************/
int setupFileExists(const char * pszFileName)
  {
    struct stat fi;

    if ((stat (pszFileName, &fi) != -1) && ((fi.st_mode & S_IFDIR) == 0))
    {
      return TRUE;
    }
    else
    {
      return FALSE;
    }
  }


  /*********************************************************************
  **
  ** FUNCTION: setupDirExists
  ** Replaced: DirExists
  **
  ** DESCRIPTION: checks to see if directory exists
  **
  **
  ** INPUTS:
  **
  ** RETURN:
  **   TRUE or FALSE
  **
  ** SIDE EFFECTS:
  **      none
  ** RESTRICTIONS:
  **      None
  ** MEMORY:
  **********************************************************************/
int setupDirExists(const char * pszDirName)
  {
#ifdef XP_WIN32
    unsigned int dwAttrs;
    if (!(pszDirName))
    {
      //setupLog(NULL, "DirExists failed: passed in NULL parameter for directory");
      return FALSE;
    }
    dwAttrs = GetFileAttributes(pszDirName);
    if ((dwAttrs != 0xFFFFFFFF) && (dwAttrs & FILE_ATTRIBUTE_DIRECTORY))
    {
      return TRUE;
    }
    return FALSE;
#else
    struct stat fi;

    if (stat (pszDirName, &fi) == -1 || !S_ISDIR(fi.st_mode))
    {
      return FALSE;
    }
    else
    {
      return TRUE;
    }
#endif
  }

  /*********************************************************************
  **
  ** FUNCTION: setupCreateDir
  ** Replaced: CreateDir
  **
  ** DESCRIPTION: creates a directory
  **
  **
  ** INPUTS:
  **
  ** RETURN:
  **   NT:  TRUE or FALSE
  **   UNIX:
  **       0 : OK
  **      -1 : Can't create directory
  **      -2 : input exists and is not a directory
  **      -3 : Can't write to directory
  **
  ** SIDE EFFECTS:
  **      none
  ** RESTRICTIONS:
  **      None
  ** MEMORY:
  **
  **********************************************************************/
bool setupCreateDir(const char * pszDirName, int mode)
  {
#ifdef XP_WIN32
    char *pszDelim;
    char szDirName[MAX_PATH];
    char szTmpDir[MAX_PATH] = "";
    int  cbLen;

    if (!(pszDirName))
    {
      //setupLog(NULL, "CreateDir failed: passed in NULL parameter for directory");
      return FALSE;
    }

    if (strlen(pszDirName) >= sizeof(szDirName))
    {
      //setupLog(NULL, "CreateDir failed: passed in too long directory");
      return FALSE;
    }

    snprintf(szDirName, sizeof(szDirName), "%s", pszDirName);
    if (!memchr(szDirName, 0, sizeof(szDirName))) return FALSE; /*overflow*/
    pszDelim = szDirName;
    while ((pszDelim = strchr(pszDelim, '/')) != NULL)
    {
      *pszDelim = '\\';
    }

    pszDelim = szDirName;
    while ((pszDelim = strchr(pszDelim, '\\')) != NULL)
    {
      cbLen = (pszDelim - szDirName);
      strncpy(szTmpDir, szDirName, cbLen);
      szTmpDir[cbLen] = '\0';
      CreateDirectory(szTmpDir, NULL);
      pszDelim++;
    }

    if (!CreateDirectory(szDirName, NULL))
    {
//      char szLog[MAX_PATH+50];
	return FALSE;
//      snprintf(szLog, sizeof(szLog),
//                      "setupCreateDir failed to create '%s'", pszDirName);
//      if (!memchr(szLog, 0, sizeof(szLog))) return FALSE; /*overflow*/
      //setupLog(NULL, szLog);
    }
    return TRUE;
#else
    struct stat fi;
    char *s;
    char *t;

    s = strdup(pszDirName);
    t = s + 1;

    while (1)
    {
      t = strchr(t, '/');

      if (t)
        *t = '\0';
      if (stat(s, &fi) == -1)
      {
        if (mkdir(s, mode) == -1)
        {
          return FALSE;
        }
      }
      else if (!S_ISDIR(fi.st_mode))
      {
        return FALSE;
      }
      if (t)
        *t++ = '/';
      else
        break;
    }

    if (access(pszDirName, W_OK) == -1)
    {
      return FALSE;
    }
    return TRUE;
#endif
  }
  /*********************************************************************
  **
  ** FUNCTION: setupConvertsPathSlashes
  ** Replaced: ConvertsPathSlashes
  **
  ** DESCRIPTION: converts slashes TRUE UNIX to NT, FALSE NT to UNIX,
  **
  ** INPUTS:
  **
  ** RETURN:
  **   TRUE or FALSE
  **
  ** SIDE EFFECTS:
  **      none
  ** RESTRICTIONS:
  **      None
  ** MEMORY:
  **********************************************************************/
int   setupConvertPathSlashes(const char *  lpszPath, char *  lpszNewPath, int bType )
  {
    if (lpszPath == NULL)
      return FALSE;

    /* create reverse slashes and escape them */
    while (*lpszPath)
    {
      if ((*lpszPath == '\\') || (*lpszPath == '/'))
      {
        if (bType)
          *lpszNewPath = '\\';
        else
          *lpszNewPath = '/';
      }
      else
        *lpszNewPath = *lpszPath;
      lpszPath++;
      lpszNewPath++;
    }
    return TRUE;
  }


unsigned int setupExecProgram(const char *program, const char *where)
{
   char curdir[MAX_PATH];
   unsigned int ret;
#ifdef XP_WIN32
   char szWhere[MAX_PATH];
   char szProgram[MAX_PATH];
   STARTUPINFO si;
   PROCESS_INFORMATION pi;
   DWORD dwCreationFlags;
#endif

   if (!program)
   {
      //setupLogMessage("info", "setup", "setupExecProgram called with no program");
      return 0;
   }
   if (!setupFileExists(program))
   {
      //setupLogMessage("info", "setup", "setupExecProgram called with invalid program %s", program);
   }
   getcwd(curdir, sizeof(curdir));

   if (where)
   {
      chdir(where);
   }
#ifndef XP_WIN32
   ret = system(program);
#else
   snprintf(szProgram, sizeof(szProgram), "%s", program);
   if (!memchr(szProgram, 0, sizeof(szProgram))) {
      //setupLogMessage("info", "setup", "Too long program %s", program);
      return 0;
   }
   snprintf(szWhere, sizeof(szWhere), "%s", where);
   if (!memchr(szWhere, 0, sizeof(szWhere))) {
      //setupLogMessage("info", "setup", "Too long arg %s", where);
      return 0;
   }
   memset(&si,0,sizeof(si));
   si.cb = sizeof(si);
   GetStartupInfo(&si);
   si.wShowWindow = SW_SHOWDEFAULT;
   dwCreationFlags = NORMAL_PRIORITY_CLASS | DETACHED_PROCESS;
   ret = CreateProcess(NULL, szProgram, NULL, NULL, FALSE, dwCreationFlags, NULL, szWhere, &si, &pi);
#endif

   if (where)
   {
      chdir(curdir);
   }

   return ret;
}


/*********************************************************************
**
** FUNCTION:      setupGetDiskFreeSpace
** DESCRIPTION:   get available disk space in Kilobyte
**
** INPUTS:        Path name
** OUTPUTS:
** RETURN:        available disk space in kilobyte
** SIDE EFFECTS:
**      None
** RESTRICTIONS:
**      None
** MEMORY:
**********************************************************************
*/

unsigned long setupGetDiskFreeSpace(const char *path)
{
#ifdef XP_WIN32
  DWORD dwSectorsPerCluster;
  DWORD dwBytesPerSector;
  DWORD dwFreeClusters;
  DWORD dwTotalClusters;

  if (GetDiskFreeSpace(path, &dwSectorsPerCluster, &dwBytesPerSector, &dwFreeClusters, &dwTotalClusters))
  {
     long long clusterSize;

     clusterSize = dwSectorsPerCluster * dwBytesPerSector;

     return (unsigned long)((clusterSize / 1024.0) * dwFreeClusters);
  }

  return 0;
#else
  struct statvfs buf;

  statvfs(path, &buf);

  if (buf.f_frsize == 0)
    return (unsigned long)(((unsigned long long)buf.f_bavail / 1024.0) * buf.f_bsize);
  else
    return (unsigned long)(((unsigned long long)buf.f_bavail / 1024.0) * buf.f_frsize);
#endif
}