tfile.c 3.5 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

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

S
TD-1895  
Shengliang Guan 已提交
29 30 31 32 33 34 35
int32_t tfInit() {
  tsFileRsetId = taosOpenRef(2000, tfCloseFile);
  if (tsFileRsetId > 0) {
    return 0;
  } else {
    return -1;
  }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
36 37
}

S
TD-1895  
Shengliang Guan 已提交
38
void tfCleanup() {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
39 40 41 42
  if (tsFileRsetId >= 0) taosCloseRef(tsFileRsetId);
  tsFileRsetId = -1;
}

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

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

  return rid;
}

L
Liu Jicong 已提交
56 57 58 59 60
int64_t tfOpenRead(const char *pathname, int32_t flags) {
  int32_t fd = taosOpenFileRead(pathname);
  return tfOpenImp(fd);
}

61 62
int64_t tfOpenReadWrite(const char *pathname, int32_t flags) {
  int32_t fd = taosOpenFileReadWrite(pathname);
S
TD-1895  
Shengliang Guan 已提交
63 64 65
  return tfOpenImp(fd);
}

66 67 68 69 70 71 72
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 已提交
73 74 75 76
  return tfOpenImp(fd);
}

int64_t tfClose(int64_t tfd) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
77
  return taosRemoveRef(tsFileRsetId, tfd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
78 79
}

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

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

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

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
}

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

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

99
  int64_t ret = taosReadFile(fd, buf, count);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
100
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
101 102 103 104

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

S
TD-2276  
Shengliang Guan 已提交
106
int32_t tfFsync(int64_t tfd) {
S
TD-1895  
Shengliang Guan 已提交
107 108 109 110
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
111
  int32_t code = taosFsyncFile(fd);
S
TD-2276  
Shengliang Guan 已提交
112 113 114

  taosReleaseRef(tsFileRsetId, tfd);
  return code;
S
TD-1895  
Shengliang Guan 已提交
115 116 117 118
}

bool tfValid(int64_t tfd) {
  void *p = taosAcquireRef(tsFileRsetId, tfd);
S
TD-2276  
Shengliang Guan 已提交
119 120 121 122
  if (p == NULL) return false;

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

S
Shengliang Guan 已提交
125
int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence) {
S
TD-1895  
Shengliang Guan 已提交
126 127 128 129
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
130
  int64_t ret = taosLSeekFile(fd, offset, whence);
S
TD-2276  
Shengliang Guan 已提交
131 132 133

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
S
TD-1895  
Shengliang Guan 已提交
134 135 136 137 138 139 140
}

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;
141
  int32_t code = taosFtruncateFile(fd, length);
S
TD-2276  
Shengliang Guan 已提交
142 143 144

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