tfile.c 3.2 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 20 21 22
#include "os.h"
#include "taoserror.h"
#include "tulog.h"
#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) {
26
  close((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 51
  int64_t rid = taosAddRef(tsFileRsetId, p);
  if (rid < 0) close(fd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
52 53 54 55

  return rid;
}

S
TD-1895  
Shengliang Guan 已提交
56
int64_t tfOpen(const char *pathname, int32_t flags) {
S
TD-1207  
Shengliang Guan 已提交
57
  int32_t fd = open(pathname, flags | O_BINARY);
S
TD-1895  
Shengliang Guan 已提交
58 59 60 61
  return tfOpenImp(fd);
}

int64_t tfOpenM(const char *pathname, int32_t flags, mode_t mode) {
S
TD-1207  
Shengliang Guan 已提交
62
  int32_t fd = open(pathname, flags | O_BINARY, mode);
S
TD-1895  
Shengliang Guan 已提交
63 64 65 66
  return tfOpenImp(fd);
}

int64_t tfClose(int64_t tfd) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
67
  return taosRemoveRef(tsFileRsetId, tfd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
68 69
}

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

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

76
  int64_t ret = taosWrite(fd, buf, count);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
77
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
78 79 80 81 82

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
}

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

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

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

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

S
TD-2276  
Shengliang Guan 已提交
96
int32_t tfFsync(int64_t tfd) {
S
TD-1895  
Shengliang Guan 已提交
97 98 99 100
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
S
TD-4088  
Shengliang Guan 已提交
101
  int32_t code = taosFsync(fd);
S
TD-2276  
Shengliang Guan 已提交
102 103 104

  taosReleaseRef(tsFileRsetId, tfd);
  return code;
S
TD-1895  
Shengliang Guan 已提交
105 106 107 108
}

bool tfValid(int64_t tfd) {
  void *p = taosAcquireRef(tsFileRsetId, tfd);
S
TD-2276  
Shengliang Guan 已提交
109 110 111 112
  if (p == NULL) return false;

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

S
Shengliang Guan 已提交
115
int64_t tfLseek(int64_t tfd, int64_t offset, int32_t whence) {
S
TD-1895  
Shengliang Guan 已提交
116 117 118 119
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
S
TD-2276  
Shengliang Guan 已提交
120 121 122 123
  int64_t ret = taosLSeek(fd, offset, whence);

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
S
TD-1895  
Shengliang Guan 已提交
124 125 126 127 128 129 130
}

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;
S
TD-2276  
Shengliang Guan 已提交
131 132 133 134
  int32_t code = taosFtruncate(fd, length);

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