main.c 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include "../src/todbc_log.h"

#ifdef _MSC_VER
#include <winsock2.h>
#include <windows.h>
#include "os.h"
#endif
#include <odbcinst.h>

10
#include <stdbool.h>
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
#include <stdio.h>
#include <string.h>

static void usage(const char *arg0);
static int do_install(int i, int argc, char *argv[]);
static int do_uninstall(int i, int argc, char *argv[]);

int main(int argc, char *argv[]) {
  for (int i = 1; i < argc; i++) {
    const char *arg = argv[i];
    if (strcmp(arg, "-h") == 0) {
      usage(argv[0]);
      return 0;
    } else if (strcmp(arg, "-i") == 0 ) {
      i = do_install(i + 1, argc, argv);
      if (i > 0) continue;
      return i == 0 ? 0 : 1;
    } else if (strcmp(arg, "-u") == 0 ) {
      i = do_uninstall(i + 1, argc, argv);
      if (i > 0) continue;
      return i == 0 ? 0 : 1;
    } else {
      fprintf(stderr, "unknown argument: [%s]\n", arg);
      return 1;
    }
  }
}

static void usage(const char *arg0) {
40
  fprintf(stderr, "%s -h | -i -n [TaosDriverName] -p [TaosDriverPath] | -u [-f] -n [TaosDriverName]\n", arg0);
41 42 43 44 45
  return;
}

static int do_install(int i, int argc, char *argv[]) {
  const char* driverName = NULL;
46
#ifdef _MSC_VER
47
  const char* driverFile = "todbc.dll";
48 49 50
#else
  const char* driverFile = "libtodbc.so";
#endif
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
  const char* driverPath = NULL;
  for (; i < argc; ++i) {
    const char *arg = argv[i];
    if (strcmp(arg, "-n") == 0) {
      i += 1;
      if (i >= argc) {
        fprintf(stderr, "expecting TaosDriverName, but got nothing\n");
        return -1;
      }
      arg = argv[i];
      if (strstr(arg, "TAOS") != arg) {
        fprintf(stderr, "TaosDriverName shall begin with 'TAOS': [%s]\n", arg);
        return -1;
      }
      driverName = arg;
    } else if (strcmp(arg, "-p") == 0) {
      i += 1;
      if (i >= argc) {
        fprintf(stderr, "expecting TaosDriverPath, but got nothing\n");
        return -1;
      }
      driverPath = argv[i];
    } else {
      fprintf(stderr, "unknown argument: [%s]\n", arg);
      return -1;
    }
  }
  if (!driverName) {
    fprintf(stderr, "TaosDriverName not specified\n");
    return -1;
  }
  if (!driverPath) {
    fprintf(stderr, "TaosDriverPath not specified\n");
    return -1;
  }
  char buf[8192];
  snprintf(buf, sizeof(buf), "%s%cDriver=%s%cFileUage=0%cConnectFunctions=YYN%c",
    driverName, 0, driverFile, 0, 0, 0);
89
  BOOL ok = 1;
90 91 92 93 94 95 96 97
  DWORD usageCount = 1;
  char installed[PATH_MAX + 1];
  WORD len = 0;
  ok = SQLInstallDriverEx(buf, driverPath, installed, sizeof(installed), &len, ODBC_INSTALL_INQUIRY, &usageCount);
  if (!ok) {
    fprintf(stderr, "failed to query TaosDriverName: [%s]\n", driverName);
    return -1;
  }
98 99 100 101 102 103 104
  int r = 0;
#ifdef _MSC_VER
  r = stricmp(driverPath, installed);
#else
  r = strcasecmp(driverPath, installed);
#endif
  if (r) {
105 106 107 108 109 110 111 112 113 114 115
    fprintf(stderr, "previously installed TaosDriver [%s] has different target path [%s]\n"
      "it shall be uninstalled before you can install it to different path [%s]\n",
      driverName, installed, driverPath);
    return -1;
  }
  ok = SQLInstallDriverEx(buf, driverPath, installed, sizeof(installed), &len, ODBC_INSTALL_COMPLETE, &usageCount);
  if (!ok) {
    fprintf(stderr, "failed to install TaosDriverName: [%s][%s]\n", driverName, driverPath);
    return -1;
  }

F
freemine 已提交
116 117
  fprintf(stderr, "ODBC driver [%s] has been installed in [%s], and UsageCount is now [%d]\n",
          driverName, driverPath, usageCount);
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
  return argc;
}

static int do_uninstall(int i, int argc, char *argv[]) {
  int forceful = 0;
  const char* driverName = NULL;
  for (; i < argc; ++i) {
    const char *arg = argv[i];
    if (strcmp(arg, "-f") == 0) {
      forceful = 1;
    } else if (strcmp(arg, "-n") == 0) {
      i += 1;
      if (i >= argc) {
        fprintf(stderr, "expecting TaosDriverName, but got nothing\n");
        return -1;
      }
      arg = argv[i];
      if (strstr(arg, "TAOS") != arg) {
        fprintf(stderr, "TaosDriverName shall begin with 'TAOS': [%s]\n", arg);
        return -1;
      }
      driverName = arg;
    } else {
      fprintf(stderr, "unknown argument: [%s]\n", arg);
      return -1;
    }
  }
  if (!driverName) {
    fprintf(stderr, "TaosDriverName not specified\n");
    return -1;
  }
149
  BOOL ok = 1;
150 151
  DWORD usageCount = 1;
  do {
152
    ok = SQLRemoveDriver(driverName, 0, &usageCount);
153 154 155 156
    if (!ok) {
      fprintf(stderr, "failed to remove driver [%s]\n", driverName);
      return -1;
    }
F
freemine 已提交
157 158 159 160
    if (!forceful) {
      fprintf(stderr, "UsageCount for ODBC driver [%s] is now: [%d]\n", driverName, usageCount);
      return argc;
    }
161
  } while (usageCount > 0);
F
freemine 已提交
162
  fprintf(stderr, "ODBC driver [%s] is now fully uninstalled\n", driverName);
163 164 165
  return argc;
}