tfile.c 3.4 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) {
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;
}

56 57
int64_t tfOpenReadWrite(const char *pathname, int32_t flags) {
  int32_t fd = taosOpenFileReadWrite(pathname);
S
TD-1895  
Shengliang Guan 已提交
58 59 60
  return tfOpenImp(fd);
}

61 62 63 64 65 66 67
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 已提交
68 69 70 71
  return tfOpenImp(fd);
}

int64_t tfClose(int64_t tfd) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
72
  return taosRemoveRef(tsFileRsetId, tfd);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
73 74
}

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

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

81
  int64_t ret = taosWriteFile(fd, buf, count);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
82
  if (ret < 0) terrno = TAOS_SYSTEM_ERROR(errno);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
83 84 85 86 87

  taosReleaseRef(tsFileRsetId, tfd);
  return ret;
}

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

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

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

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

S
TD-2276  
Shengliang Guan 已提交
101
int32_t tfFsync(int64_t tfd) {
S
TD-1895  
Shengliang Guan 已提交
102 103 104 105
  void *p = taosAcquireRef(tsFileRsetId, tfd);
  if (p == NULL) return -1;

  int32_t fd = (int32_t)(uintptr_t)p;
106
  int32_t code = taosFsyncFile(fd);
S
TD-2276  
Shengliang Guan 已提交
107 108 109

  taosReleaseRef(tsFileRsetId, tfd);
  return code;
S
TD-1895  
Shengliang Guan 已提交
110 111 112 113
}

bool tfValid(int64_t tfd) {
  void *p = taosAcquireRef(tsFileRsetId, tfd);
S
TD-2276  
Shengliang Guan 已提交
114 115 116 117
  if (p == NULL) return false;

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

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

  int32_t fd = (int32_t)(uintptr_t)p;
125
  int64_t ret = taosLSeekFile(fd, offset, whence);
S
TD-2276  
Shengliang Guan 已提交
126 127 128

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

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;
136
  int32_t code = taosFtruncateFile(fd, length);
S
TD-2276  
Shengliang Guan 已提交
137 138 139

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