tfile.c 3.7 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
#include "os.h"
#include "taoserror.h"
S
Shengliang Guan 已提交
19
#include "ulog.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
20 21 22
#include "tutil.h"
#include "tref.h"

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

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

S
TD-1895  
Shengliang Guan 已提交
27
static void tfCloseFile(void *p) {
S
Shengliang Guan 已提交
28
  taosCloseFile((int32_t)(uintptr_t)p);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
29 30
}

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

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

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

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

  return rid;
}

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

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

72 73 74 75 76 77 78
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 已提交
79 80 81 82
  return tfOpenImp(fd);
}

int64_t tfClose(int64_t tfd) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
83
  return taosRemoveRef(tsFileRsetId, tfd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
84 85
}

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

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

92
  int64_t ret = taosWriteFile(fd, buf, count);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
93
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
94 95 96 97 98

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
}

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

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

105
  int64_t ret = taosReadFile(fd, buf, count);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
106
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
107 108 109 110

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

S
TD-2276  
Shengliang Guan 已提交
112
int32_t tfFsync(int64_t tfd) {
S
TD-1895  
Shengliang Guan 已提交
113 114 115 116
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
117
  int32_t code = taosFsyncFile(fd);
S
TD-2276  
Shengliang Guan 已提交
118 119 120

  taosReleaseRef(tsFileRsetId, tfd);
  return code;
S
TD-1895  
Shengliang Guan 已提交
121 122 123 124
}

bool tfValid(int64_t tfd) {
  void *p = taosAcquireRef(tsFileRsetId, tfd);
S
TD-2276  
Shengliang Guan 已提交
125 126 127 128
  if (p == NULL) return false;

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

S
Shengliang Guan 已提交
131
int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence) {
S
TD-1895  
Shengliang Guan 已提交
132 133 134 135
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
136
  int64_t ret = taosLSeekFile(fd, offset, whence);
S
TD-2276  
Shengliang Guan 已提交
137 138 139

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
S
TD-1895  
Shengliang Guan 已提交
140 141 142 143 144 145 146
}

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;
147
  int32_t code = taosFtruncateFile(fd, length);
S
TD-2276  
Shengliang Guan 已提交
148 149 150

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