osEnv.c 2.3 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "osEnv.h"
S
os env  
Shengliang Guan 已提交
18
#include "os.h"
S
Shengliang Guan 已提交
19 20
#include "osSysinfo.h"

S
os env  
Shengliang Guan 已提交
21 22
SEnvVar env = {0};
char    configDir[PATH_MAX] = {0};
S
Shengliang Guan 已提交
23 24 25 26 27 28

#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)

extern taosWinSocketInit();

void osInit() {
S
os env  
Shengliang Guan 已提交
29
  srand(taosSafeRand());
S
Shengliang Guan 已提交
30 31 32 33 34 35 36
  taosWinSocketInit();

  const char *tmpDir = getenv("tmp");
  if (tmpDir == NULL) {
    tmpDir = getenv("temp");
  }
  if (tmpDir != NULL) {
S
os env  
Shengliang Guan 已提交
37
    strcpy(env.tempDir, tmpDir);
S
Shengliang Guan 已提交
38
  }
S
os env  
Shengliang Guan 已提交
39 40 41 42 43 44

  strcpy(configDir, "C:\\TDengine\\cfg");
  strcpy(env.dataDir, "C:\\TDengine\\data");
  strcpy(env.logDir, "C:\\TDengine\\log");
  strcpy(env.tempDir, "C:\\Windows\\Temp");
  strcpy(env.osName, "Windows");
S
Shengliang Guan 已提交
45 46 47 48
}

#elif defined(_TD_DARWIN_64)

S
os env  
Shengliang Guan 已提交
49 50 51 52 53 54 55 56
void osInit() {
  srand(taosSafeRand());
  strcpy(configDir, "/tmp/taosd");
  strcpy(env.dataDir, "/usr/local/var/lib/taos");
  strcpy(env.logDir, "/usr/local/var/log/taos");
  strcpy(env.tempDir, "/usr/local/etc/taos");
  strcpy(env.osName, "Darwin");
}
S
Shengliang Guan 已提交
57 58
#else

S
Shengliang Guan 已提交
59
void osInit() {
S
os env  
Shengliang Guan 已提交
60 61 62 63 64 65
  srand(taosSafeRand());
  strcpy(configDir, "/etc/taos");
  strcpy(env.dataDir, "/var/lib/taos");
  strcpy(env.logDir, "/var/log/taos");
  strcpy(env.tempDir, "/tmp");
  strcpy(env.osName, "Linux");
S
Shengliang Guan 已提交
66
}
S
Shengliang Guan 已提交
67 68

#endif
S
os env  
Shengliang Guan 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

SEnvVar *osEnv() { return &env; }

void osUpdate() {
  if (env.logDir[0] != 0) {
    taosGetDiskSize(env.logDir, &env.logSpace.size);
  }
  if (env.dataDir[0] != 0) {
    taosGetDiskSize(env.dataDir, &env.dataSpace.size);
  }
  if (env.tempDir[0] != 0) {
    taosGetDiskSize(env.tempDir, &env.tempSpace.size);
  }
}

bool osLogSpaceAvailable() { return env.logSpace.reserved < env.logSpace.size.avail; }

char *osLogDir() { return env.logDir; }
char *osTempDir() { return env.tempDir; }
char *osDataDir() { return env.dataDir; }
char *osName() { return env.osName; }