osFile.h 4.2 KB
Newer Older
S
Shengliang Guan 已提交
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
Shengliang Guan 已提交
16 17
#ifndef _TD_OS_FILE_H_
#define _TD_OS_FILE_H_
S
Shengliang Guan 已提交
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

23 24
#include "osSocket.h"

25 26
#if defined(WINDOWS)
typedef int32_t FileFd;
27
typedef int32_t SocketFd;
28 29 30 31 32 33
#else
typedef int32_t FileFd;
typedef int32_t SocketFd;
#endif

int64_t taosRead(FileFd fd, void *buf, int64_t count);
34
// If the error is in a third-party library, place this header file under the third-party library header file.
35 36 37
#ifndef ALLOW_FORBID_FUNC
    #define open OPEN_FUNC_TAOS_FORBID
    #define fopen FOPEN_FUNC_TAOS_FORBID
38 39 40 41 42 43
    #define access ACCESS_FUNC_TAOS_FORBID
    #define stat STAT_FUNC_TAOS_FORBID
    #define lstat LSTAT_FUNC_TAOS_FORBID
    #define fstat FSTAT_FUNC_TAOS_FORBID
    #define close CLOSE_FUNC_TAOS_FORBID
    #define fclose FCLOSE_FUNC_TAOS_FORBID
44 45
    #define fsync FSYNC_FUNC_TAOS_FORBID
    // #define fflush FFLUSH_FUNC_TAOS_FORBID
S
TD-4088  
Shengliang Guan 已提交
46 47
#endif

S
Shengliang Guan 已提交
48 49 50 51
#ifndef PATH_MAX
#define PATH_MAX 256
#endif

52 53 54 55 56 57 58 59 60
typedef int32_t FileFd;

typedef struct TdFile {
  pthread_rwlock_t rwlock;
  int      refId;
  FileFd   fd;
  FILE    *fp;
} * TdFilePtr, TdFile;

61 62 63 64 65 66 67 68 69 70
typedef struct TdFile *TdFilePtr;
 
#define TD_FILE_CTEATE    0x0001
#define TD_FILE_WRITE     0x0002
#define TD_FILE_READ      0x0004
#define TD_FILE_TRUNC     0x0008
#define TD_FILE_APPEND    0x0010
#define TD_FILE_TEXT      0x0020
#define TD_FILE_AUTO_DEL  0x0040
#define TD_FILE_EXCL      0x0080
71
#define TD_FILE_STREAM    0x0100   // Only support taosFprintfFile, taosGetLineFile, taosEOFFile
72 73 74 75 76
TdFilePtr taosOpenFile(const char *path,int32_t tdFileOptions);

#define TD_FILE_ACCESS_EXIST_OK 0x1
#define TD_FILE_ACCESS_READ_OK  0x2
#define TD_FILE_ACCESS_WRITE_OK 0x4
77
bool    taosCheckAccessFile(const char *pathname, int mode);
78 79 80 81 82 83
 
int32_t taosLockFile(TdFilePtr pFile);
int32_t taosUnLockFile(TdFilePtr pFile);
 
int32_t taosUmaskFile(int32_t maskVal);
 
S
Shengliang Guan 已提交
84
int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime);
85
int32_t taosDevInoFile(const char *path, int64_t *stDev, int64_t *stIno);
86
int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime);
87
bool    taosCheckExistFile(const char *pathname);
88 89 90 91 92 93 94 95
 
int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence);
int32_t taosFtruncateFile(TdFilePtr pFile, int64_t length);
int32_t taosFsyncFile(TdFilePtr pFile);
 
int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count);
int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset);
int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count);
96
void    taosFprintfFile(TdFilePtr pFile, const char *format, ...);
97 98 99 100 101

#if defined(WINDOWS)
#define __restrict__
#endif // WINDOWS

102
int64_t taosGetLineFile(TdFilePtr pFile, char ** __restrict__ ptrBuf);
103

104 105 106 107
int32_t taosEOFFile(TdFilePtr pFile);
 
int64_t taosCloseFile(TdFilePtr *ppFile);
 
S
Shengliang Guan 已提交
108 109
int32_t taosRenameFile(const char *oldName, const char *newName);
int64_t taosCopyFile(const char *from, const char *to);
110
int32_t taosRemoveFile(const char *path);
111
 
112
void    taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath);
113 114 115 116 117 118 119 120

#if defined(_TD_DARWIN_64)
typedef int32_t SocketFd;

int64_t taosSendFile(SocketFd fdDst, FileFd pFileSrc, int64_t *offset, int64_t size);
int64_t taosFSendFile(FILE *pFileOut, FILE *pFileIn, int64_t *offset, int64_t size);
#else
int64_t taosSendFile(SocketFd fdDst, TdFilePtr pFileSrc, int64_t *offset, int64_t size);
121
int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size);
122
#endif
123 124 125

void *taosMmapReadOnlyFile(TdFilePtr pFile, int64_t length);
bool taosValidFile(TdFilePtr pFile);
S
TD-4088  
Shengliang Guan 已提交
126

127
int32_t taosGetErrorFile(TdFilePtr pFile);
S
TD-1912  
Shengliang Guan 已提交
128

S
Shengliang Guan 已提交
129 130 131 132
#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
133
#endif /*_TD_OS_FILE_H_*/