tfile.c 4.0 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
TD-1895  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
17 18 19
#include "os.h"
#include "taoserror.h"
#include "tref.h"
dengyihao's avatar
dengyihao 已提交
20 21
#include "tutil.h"
#include "ulog.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
22

23
static int32_t tsFileRsetId = -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
24

L
Liu Jicong 已提交
25 26
static int8_t tfInited = 0;

dengyihao's avatar
dengyihao 已提交
27
static void tfCloseFile(void *p) { taosCloseFile((int32_t)(uintptr_t)p); }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
28

S
TD-1895  
Shengliang Guan 已提交
29
int32_t tfInit() {
L
Liu Jicong 已提交
30
  int8_t old = atomic_val_compare_exchange_8(&tfInited, 0, 1);
dengyihao's avatar
dengyihao 已提交
31
  if (old == 1) return 0;
S
TD-1895  
Shengliang Guan 已提交
32 33 34 35
  tsFileRsetId = taosOpenRef(2000, tfCloseFile);
  if (tsFileRsetId > 0) {
    return 0;
  } else {
L
Liu Jicong 已提交
36
    atomic_store_8(&tfInited, 0);
S
TD-1895  
Shengliang Guan 已提交
37 38
    return -1;
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
39 40
}

S
TD-1895  
Shengliang Guan 已提交
41
void tfCleanup() {
L
Liu Jicong 已提交
42
  atomic_store_8(&tfInited, 0);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
43 44 45 46
  if (tsFileRsetId >= 0) taosCloseRef(tsFileRsetId);
  tsFileRsetId = -1;
}

S
TD-1895  
Shengliang Guan 已提交
47
static int64_t tfOpenImp(int32_t fd) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
48 49 50
  if (fd < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
S
TD-1895  
Shengliang Guan 已提交
51
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
52

S
TD-1895  
Shengliang Guan 已提交
53
  void *  p = (void *)(int64_t)fd;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
54
  int64_t rid = taosAddRef(tsFileRsetId, p);
S
Shengliang Guan 已提交
55
  if (rid < 0) taosCloseFile(fd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
56 57 58 59

  return rid;
}

L
Liu Jicong 已提交
60 61 62 63 64
int64_t tfOpenRead(const char *pathname, int32_t flags) {
  int32_t fd = taosOpenFileRead(pathname);
  return tfOpenImp(fd);
}

65 66
int64_t tfOpenReadWrite(const char *pathname, int32_t flags) {
  int32_t fd = taosOpenFileReadWrite(pathname);
S
TD-1895  
Shengliang Guan 已提交
67 68 69
  return tfOpenImp(fd);
}

70 71 72 73 74 75 76
int64_t tfOpenCreateWrite(const char *pathname, int32_t flags, mode_t mode) {
  int32_t fd = taosOpenFileCreateWrite(pathname);
  return tfOpenImp(fd);
}

int64_t tfOpenCreateWriteAppend(const char *pathname, int32_t flags, mode_t mode) {
  int32_t fd = taosOpenFileCreateWriteAppend(pathname);
S
TD-1895  
Shengliang Guan 已提交
77 78 79
  return tfOpenImp(fd);
}

dengyihao's avatar
dengyihao 已提交
80
int64_t tfClose(int64_t tfd) { return taosRemoveRef(tsFileRsetId, tfd); }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
81

S
TD-1895  
Shengliang Guan 已提交
82
int64_t tfWrite(int64_t tfd, void *buf, int64_t count) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
83
  void *p = taosAcquireRef(tsFileRsetId, tfd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
84
  if (p == NULL) return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
85

86
  int32_t fd = (int32_t)(uintptr_t)p;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
87

88
  int64_t ret = taosWriteFile(fd, buf, count);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
89
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
90 91 92 93 94

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
}

S
TD-1895  
Shengliang Guan 已提交
95
int64_t tfRead(int64_t tfd, void *buf, int64_t count) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
96
  void *p = taosAcquireRef(tsFileRsetId, tfd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
97
  if (p == NULL) return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
98

99
  int32_t fd = (int32_t)(uintptr_t)p;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
100

101
  int64_t ret = taosReadFile(fd, buf, count);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
102
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
dengyihao's avatar
dengyihao 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
}

int64_t tfPread(int64_t tfd, void *buf, int64_t count, int32_t offset) {
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;

  int64_t ret = pread(fd, buf, count, offset);
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
116 117 118 119

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
}
S
TD-1895  
Shengliang Guan 已提交
120

S
TD-2276  
Shengliang Guan 已提交
121
int32_t tfFsync(int64_t tfd) {
S
TD-1895  
Shengliang Guan 已提交
122 123 124 125
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
126
  int32_t code = taosFsyncFile(fd);
S
TD-2276  
Shengliang Guan 已提交
127 128 129

  taosReleaseRef(tsFileRsetId, tfd);
  return code;
S
TD-1895  
Shengliang Guan 已提交
130 131 132 133
}

bool tfValid(int64_t tfd) {
  void *p = taosAcquireRef(tsFileRsetId, tfd);
S
TD-2276  
Shengliang Guan 已提交
134 135 136 137
  if (p == NULL) return false;

  taosReleaseRef(tsFileRsetId, tfd);
  return true;
S
TD-1895  
Shengliang Guan 已提交
138 139
}

S
Shengliang Guan 已提交
140
int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence) {
S
TD-1895  
Shengliang Guan 已提交
141 142 143 144
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
145
  int64_t ret = taosLSeekFile(fd, offset, whence);
S
TD-2276  
Shengliang Guan 已提交
146 147 148

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
S
TD-1895  
Shengliang Guan 已提交
149 150 151 152 153 154 155
}

int32_t tfFtruncate(int64_t tfd, int64_t length) {
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
156
  int32_t code = taosFtruncateFile(fd, length);
S
TD-2276  
Shengliang Guan 已提交
157 158 159

  taosReleaseRef(tsFileRsetId, tfd);
  return code;
S
TD-1895  
Shengliang Guan 已提交
160
}