osFile.c 14.7 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
15
#define ALLOW_FORBID_FUNC
S
Shengliang Guan 已提交
16 17
#include "os.h"

18 19
#define MAX_FPRINTFLINE_BUFFER_SIZE (1000)

S
Shengliang Guan 已提交
20
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <io.h>

#if defined(_MSDOS)
#define open _open
#endif

#if defined(_WIN32)
extern int openA(const char *, int, ...); /* MsvcLibX ANSI version of open */
extern int openU(const char *, int, ...); /* MsvcLibX UTF-8 version of open */
#if defined(_UTF8_SOURCE) || defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
#define open openU
#else /* _ANSI_SOURCE */
#define open openA
#endif /* defined(_UTF8_SOURCE) */
#endif /* defined(_WIN32) */
36

37
#else
38 39 40 41 42 43 44
#include <fcntl.h>
#include <sys/file.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <unistd.h>
#define LINUX_FILE_NO_TEXT_OPTION 0
#define O_TEXT                    LINUX_FILE_NO_TEXT_OPTION
45 46
#endif

47 48 49
typedef int32_t FileFd;

typedef struct TdFile {
50 51 52 53
  int    refId;
  FileFd fd;
  FILE  *fp;
} * TdFilePtr, TdFile;
54

55
void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) {
S
TD-4088  
Shengliang Guan 已提交
56
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
S
Shengliang Guan 已提交
57
  const char *tdengineTmpFileNamePrefix = "tdengine-";
S
TD-4088  
Shengliang Guan 已提交
58
  char        tmpPath[PATH_MAX];
S
Shengliang Guan 已提交
59

S
Shengliang Guan 已提交
60 61
  int32_t len = (int32_t)strlen(inputTmpDir);
  memcpy(tmpPath, inputTmpDir, len);
S
TD-4088  
Shengliang Guan 已提交
62 63 64

  if (tmpPath[len - 1] != '/' && tmpPath[len - 1] != '\\') {
    tmpPath[len++] = '\\';
65
  }
S
Shengliang Guan 已提交
66

67
  strcpy(tmpPath + len, tdengineTmpFileNamePrefix);
S
TD-4088  
Shengliang Guan 已提交
68
  strcat(tmpPath, tdengineTmpFileNamePrefix);
S
Shengliang Guan 已提交
69 70 71
  if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) {
    strcat(tmpPath, fileNamePrefix);
    strcat(tmpPath, "-%d-%s");
S
Shengliang Guan 已提交
72
  }
S
Shengliang Guan 已提交
73

S
TD-4088  
Shengliang Guan 已提交
74 75
  char rand[8] = {0};
  taosRandStr(rand, tListLen(rand) - 1);
S
Shengliang Guan 已提交
76
  snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand);
S
TD-1912  
Shengliang Guan 已提交
77

S
TD-4088  
Shengliang Guan 已提交
78
#else
S
Shengliang Guan 已提交
79

S
TD-4088  
Shengliang Guan 已提交
80
  const char *tdengineTmpFileNamePrefix = "tdengine-";
S
Shengliang Guan 已提交
81

82
  char    tmpPath[PATH_MAX];
S
Shengliang Guan 已提交
83 84
  int32_t len = strlen(inputTmpDir);
  memcpy(tmpPath, inputTmpDir, len);
S
TD-4088  
Shengliang Guan 已提交
85
  static uint64_t seqId = 0;
S
Shengliang Guan 已提交
86

S
TD-4088  
Shengliang Guan 已提交
87 88
  if (tmpPath[len - 1] != '/') {
    tmpPath[len++] = '/';
S
TD-1912  
Shengliang Guan 已提交
89
  }
S
Shengliang Guan 已提交
90

S
TD-4088  
Shengliang Guan 已提交
91 92 93 94
  strcpy(tmpPath + len, tdengineTmpFileNamePrefix);
  if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) {
    strcat(tmpPath, fileNamePrefix);
    strcat(tmpPath, "-%d-%s");
S
Shengliang Guan 已提交
95 96
  }

S
TD-4088  
Shengliang Guan 已提交
97 98 99
  char rand[32] = {0};

  sprintf(rand, "%" PRIu64, atomic_add_fetch_64(&seqId, 1));
S
Shengliang Guan 已提交
100

S
TD-4088  
Shengliang Guan 已提交
101
  snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand);
S
Shengliang Guan 已提交
102

S
TD-4088  
Shengliang Guan 已提交
103
#endif
S
Shengliang Guan 已提交
104
}
S
TD-4088  
Shengliang Guan 已提交
105

106 107 108 109 110 111 112 113 114
int64_t taosCopyFile(const char *from, const char *to) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
  char    buffer[4096];
  int64_t size = 0;
  int64_t bytes;

  // fidfrom = open(from, O_RDONLY);
115
  TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ);
116 117 118
  if (pFileFrom == NULL) goto _err;

  // fidto = open(to, O_WRONLY | O_CREAT | O_EXCL, 0755);
119
  TdFilePtr pFileTo = taosOpenFile(to, TD_FILE_CTEATE | TD_FILE_WRITE | TD_FILE_EXCL);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  if (pFileTo == NULL) goto _err;

  while (true) {
    bytes = taosReadFile(pFileFrom, buffer, sizeof(buffer));
    if (bytes < 0) goto _err;
    if (bytes == 0) break;

    size += bytes;

    if (taosWriteFile(pFileTo, (void *)buffer, bytes) < bytes) goto _err;
    if (bytes < sizeof(buffer)) break;
  }

  taosFsyncFile(pFileTo);

  taosCloseFile(&pFileFrom);
  taosCloseFile(&pFileTo);
  return size;

_err:
  if (pFileFrom != NULL) taosCloseFile(&pFileFrom);
  if (pFileTo != NULL) taosCloseFile(&pFileTo);
  remove(to);
  return -1;
#endif
}

int32_t taosRenameFile(const char *oldName, const char *newName) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  int32_t code = MoveFileEx(oldName, newName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
  if (code < 0) {
151
    // printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno));
152 153 154 155 156 157
  }

  return code;
#else
  int32_t code = rename(oldName, newName);
  if (code < 0) {
158
    // printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno));
159 160 161 162 163 164 165 166 167 168 169
  }

  return code;
#endif
}

int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
  struct stat fileStat;
170
  int32_t code = stat(path, &fileStat);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  if (code < 0) {
    return code;
  }

  if (size != NULL) {
    *size = fileStat.st_size;
  }

  if (mtime != NULL) {
    *mtime = fileStat.st_mtime;
  }

  return 0;
#endif
}

187
void autoDelFileListAdd(const char *path) { return; }
188

189
TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
190 191 192 193 194 195
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return NULL;
#else
  int access = O_BINARY;
  char *mode = NULL;
  access |= (tdFileOptions & TD_FILE_CTEATE) ? O_CREAT : 0;
196
  if ((tdFileOptions & TD_FILE_WRITE) && (tdFileOptions & TD_FILE_READ)) {
197 198
    access |= O_RDWR;
    mode = (tdFileOptions & TD_FILE_TEXT) ? "rt+" : "rb+";
199
  } else if (tdFileOptions & TD_FILE_WRITE) {
200 201
    access |= O_WRONLY;
    mode = (tdFileOptions & TD_FILE_TEXT) ? "wt" : "wb";
202
  } else if (tdFileOptions & TD_FILE_READ) {
203 204 205 206 207 208 209
    access |= O_RDONLY;
    mode = (tdFileOptions & TD_FILE_TEXT) ? "rt" : "rb";
  }
  access |= (tdFileOptions & TD_FILE_TRUNC) ? O_TRUNC : 0;
  access |= (tdFileOptions & TD_FILE_APPEND) ? O_APPEND : 0;
  access |= (tdFileOptions & TD_FILE_TEXT) ? O_TEXT : 0;
  access |= (tdFileOptions & TD_FILE_EXCL) ? O_EXCL : 0;
210
  if (tdFileOptions & TD_FILE_AUTO_DEL) {
211 212 213
    autoDelFileListAdd(path);
  }
  int fd = open(path, access, S_IRWXU | S_IRWXG | S_IRWXO);
214
  if (fd == -1) {
215 216
    return NULL;
  }
217
  FILE *fp = fdopen(fd, mode);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  if (fp == NULL) {
    close(fd);
    return NULL;
  }
  TdFilePtr pFile = (TdFilePtr)malloc(sizeof(TdFile));
  if (pFile == NULL) {
    close(fd);
    fclose(fp);
    return NULL;
  }
  pFile->fd = fd;
  pFile->fp = fp;
  pFile->refId = 0;
  return pFile;
#endif
}

int64_t taosCloseFile(TdFilePtr *ppFile) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
239
  if (ppFile == NULL || *ppFile == NULL || (*ppFile)->fd == -1) {
240 241
    return 0;
  }
242
  fflush((*ppFile)->fp);
243
  fsync((*ppFile)->fd);
244 245 246 247 248 249 250 251 252 253 254
  close((*ppFile)->fd);
  (*ppFile)->fd = -1;
  (*ppFile)->fp = NULL;
  (*ppFile)->refId = 0;
  free(*ppFile);
  *ppFile = NULL;
  return 0;
#endif
}

int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
255
  if (pFile == NULL) {
256 257
    return 0;
  }
S
TD-1912  
Shengliang Guan 已提交
258 259
  int64_t leftbytes = count;
  int64_t readbytes;
260
  char   *tbuf = (char *)buf;
S
Shengliang Guan 已提交
261 262

  while (leftbytes > 0) {
263
    readbytes = read(pFile->fd, (void *)tbuf, (uint32_t)leftbytes);
S
Shengliang Guan 已提交
264 265 266 267 268 269 270
    if (readbytes < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        return -1;
      }
    } else if (readbytes == 0) {
S
TD-1912  
Shengliang Guan 已提交
271
      return (int64_t)(count - leftbytes);
S
Shengliang Guan 已提交
272 273 274 275 276 277
    }

    leftbytes -= readbytes;
    tbuf += readbytes;
  }

S
TD-1912  
Shengliang Guan 已提交
278
  return count;
S
Shengliang Guan 已提交
279 280
}

281
int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) {
282
  if (pFile == NULL) {
283 284 285 286 287 288 289
    return 0;
  }
  return pread(pFile->fd, buf, count, offset);
}

int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
  int64_t nleft = count;
S
TD-1912  
Shengliang Guan 已提交
290
  int64_t nwritten = 0;
291
  char   *tbuf = (char *)buf;
S
Shengliang Guan 已提交
292 293

  while (nleft > 0) {
294
    nwritten = write(pFile->fd, (void *)tbuf, (uint32_t)nleft);
S
Shengliang Guan 已提交
295 296 297 298
    if (nwritten < 0) {
      if (errno == EINTR) {
        continue;
      }
299
      fflush(pFile->fp);
300
      fsync(pFile->fd);
S
Shengliang Guan 已提交
301 302 303 304 305 306
      return -1;
    }
    nleft -= nwritten;
    tbuf += nwritten;
  }

307
  fflush(pFile->fp);
308
  fsync(pFile->fd);
309
  return count;
S
Shengliang Guan 已提交
310 311
}

312
int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) {
313
  if (pFile == NULL) return -1;
314
  return (int64_t)lseek(pFile->fd, (long)offset, whence);
315
}
S
Shengliang Guan 已提交
316

317
int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
S
Shengliang Guan 已提交
318 319 320
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
321
  struct stat fileStat;
322
  int32_t code = fstat(pFile->fd, &fileStat);
323 324 325
  if (code < 0) {
    return code;
  }
H
Hongze Cheng 已提交
326

327 328 329
  if (size != NULL) {
    *size = fileStat.st_size;
  }
H
Hongze Cheng 已提交
330

331 332 333
  if (mtime != NULL) {
    *mtime = fileStat.st_mtime;
  }
H
Hongze Cheng 已提交
334

335 336 337
  return 0;
#endif
}
H
Hongze Cheng 已提交
338

339 340 341 342 343 344 345
int32_t taosLockFile(TdFilePtr pFile) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
  return (int32_t)flock(pFile->fd, LOCK_EX | LOCK_NB);
#endif
}
H
Hongze Cheng 已提交
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360
int32_t taosUnLockFile(TdFilePtr pFile) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
  return (int32_t)flock(pFile->fd, LOCK_UN | LOCK_NB);
#endif
}

int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  if (pFile->fd < 0) {
    errno = EBADF;
    uError("%s\n", "fd arg was negative");
    return -1;
H
Hongze Cheng 已提交
361 362
  }

363
  HANDLE h = (HANDLE)_get_osfhandle(pFile->fd);
H
Hongze Cheng 已提交
364

365 366 367 368 369 370 371
  LARGE_INTEGER li_0;
  li_0.QuadPart = (int64_t)0;
  BOOL cur = SetFilePointerEx(h, li_0, NULL, FILE_CURRENT);
  if (!cur) {
    uError("SetFilePointerEx Error getting current position in file.\n");
    return -1;
  }
H
Hongze Cheng 已提交
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  LARGE_INTEGER li_size;
  li_size.QuadPart = l_size;
  BOOL cur2 = SetFilePointerEx(h, li_size, NULL, FILE_BEGIN);
  if (cur2 == 0) {
    int error = GetLastError();
    uError("SetFilePointerEx GetLastError is: %d\n", error);
    switch (error) {
      case ERROR_INVALID_HANDLE:
        errno = EBADF;
        break;
      default:
        errno = EIO;
        break;
    }
    return -1;
  }

  if (!SetEndOfFile(h)) {
    int error = GetLastError();
    uError("SetEndOfFile GetLastError is:%d", error);
    switch (error) {
      case ERROR_INVALID_HANDLE:
        errno = EBADF;
        break;
      default:
        errno = EIO;
        break;
    }
    return -1;
  }

  return 0;
#else
  return ftruncate(pFile->fd, l_size);
#endif
}

int32_t taosFsyncFile(TdFilePtr pFile) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  if (pFile->fd < 0) {
    errno = EBADF;
    uError("%s\n", "fd arg was negative");
    return -1;
  }

  HANDLE h = (HANDLE)_get_osfhandle(pFile->fd);

  return FlushFileBuffers(h);
#else
422
  return fflush(pFile->fp);
S
Shengliang Guan 已提交
423
#endif
H
Hongze Cheng 已提交
424 425
}

S
TD-4088  
Shengliang Guan 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)

#define _SEND_FILE_STEP_ 1000

int64_t taosFSendFile(FILE *out_file, FILE *in_file, int64_t *offset, int64_t count) {
  fseek(in_file, (int32_t)(*offset), 0);
  int64_t writeLen = 0;
  uint8_t buffer[_SEND_FILE_STEP_] = {0};

  for (int64_t len = 0; len < (count - _SEND_FILE_STEP_); len += _SEND_FILE_STEP_) {
    size_t rlen = fread(buffer, 1, _SEND_FILE_STEP_, in_file);
    if (rlen <= 0) {
      return writeLen;
    } else if (rlen < _SEND_FILE_STEP_) {
      fwrite(buffer, 1, rlen, out_file);
      return (int64_t)(writeLen + rlen);
    } else {
      fwrite(buffer, 1, _SEND_FILE_STEP_, in_file);
      writeLen += _SEND_FILE_STEP_;
    }
  }

  int64_t remain = count - writeLen;
  if (remain > 0) {
    size_t rlen = fread(buffer, 1, (size_t)remain, in_file);
    if (rlen <= 0) {
      return writeLen;
    } else {
      fwrite(buffer, 1, (size_t)remain, out_file);
      writeLen += remain;
    }
  }

  return writeLen;
}

int64_t taosSendFile(SocketFd dfd, FileFd sfd, int64_t *offset, int64_t count) {
  if (offset != NULL) lseek(sfd, (int32_t)(*offset), 0);

  int64_t writeLen = 0;
  uint8_t buffer[_SEND_FILE_STEP_] = {0};

  for (int64_t len = 0; len < (count - _SEND_FILE_STEP_); len += _SEND_FILE_STEP_) {
    int32_t rlen = (int32_t)read(sfd, buffer, _SEND_FILE_STEP_);
    if (rlen <= 0) {
      return writeLen;
    } else if (rlen < _SEND_FILE_STEP_) {
      taosWriteSocket(dfd, buffer, rlen);
      return (int64_t)(writeLen + rlen);
    } else {
      taosWriteSocket(dfd, buffer, _SEND_FILE_STEP_);
      writeLen += _SEND_FILE_STEP_;
    }
  }

  int64_t remain = count - writeLen;
  if (remain > 0) {
    int32_t rlen = read(sfd, buffer, (int32_t)remain);
    if (rlen <= 0) {
      return writeLen;
    } else {
      taosWriteSocket(sfd, buffer, (int32_t)remain);
      writeLen += remain;
    }
  }

  return writeLen;
}

#elif defined(_TD_DARWIN_64)
S
TD-1912  
Shengliang Guan 已提交
496

S
TD-4088  
Shengliang Guan 已提交
497 498 499 500 501 502 503 504
int64_t taosFSendFile(FILE *out_file, FILE *in_file, int64_t *offset, int64_t count) {
  int r = 0;
  if (offset) {
    r = fseek(in_file, *offset, SEEK_SET);
    if (r == -1) return -1;
  }
  off_t len = count;
  while (len > 0) {
505
    char buf[1024 * 16];
S
TD-4088  
Shengliang Guan 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    off_t n = sizeof(buf);
    if (len < n) n = len;
    size_t m = fread(buf, 1, n, in_file);
    if (m < n) {
      int e = ferror(in_file);
      if (e) return -1;
    }
    if (m == 0) break;
    if (m != fwrite(buf, 1, m, out_file)) {
      return -1;
    }
    len -= m;
  }
  return count - len;
}

int64_t taosSendFile(SocketFd dfd, FileFd sfd, int64_t *offset, int64_t count) {
  int r = 0;
  if (offset) {
    r = lseek(sfd, *offset, SEEK_SET);
    if (r == -1) return -1;
  }
  off_t len = count;
  while (len > 0) {
530
    char buf[1024 * 16];
S
TD-4088  
Shengliang Guan 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544
    off_t n = sizeof(buf);
    if (len < n) n = len;
    size_t m = read(sfd, buf, n);
    if (m == -1) return -1;
    if (m == 0) break;
    size_t l = write(dfd, buf, m);
    if (l == -1) return -1;
    len -= l;
  }
  return count - len;
}

#else

545
int64_t taosSendFile(SocketFd fdDst, TdFilePtr pFileSrc, int64_t *offset, int64_t size) {
S
TD-1912  
Shengliang Guan 已提交
546 547
  int64_t leftbytes = size;
  int64_t sentbytes;
S
Shengliang Guan 已提交
548 549

  while (leftbytes > 0) {
550
    sentbytes = sendfile(fdDst, pFileSrc->fd, offset, leftbytes);
S
Shengliang Guan 已提交
551
    if (sentbytes == -1) {
S
TD-1985  
Shengliang Guan 已提交
552
      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
S
Shengliang Guan 已提交
553 554 555 556 557
        continue;
      } else {
        return -1;
      }
    } else if (sentbytes == 0) {
S
TD-1912  
Shengliang Guan 已提交
558
      return (int64_t)(size - leftbytes);
S
Shengliang Guan 已提交
559 560 561 562 563 564 565
    }

    leftbytes -= sentbytes;
  }

  return size;
}
S
TD-1912  
Shengliang Guan 已提交
566

567 568
int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size) {
  return taosSendFile(pFileOut->fd, pFileIn, offset, size);
S
Shengliang Guan 已提交
569
}
S
TD-4088  
Shengliang Guan 已提交
570

S
Shengliang Guan 已提交
571 572
#endif

573
void taosFprintfFile(TdFilePtr pFile, const char *format, ...) {
574 575
  char           buffer[MAX_FPRINTFLINE_BUFFER_SIZE] = {0};
  va_list ap;
576
  va_start(ap, format);
577
  vfprintf(pFile->fp, format, ap);
578 579
  va_end(ap);
  fflush(pFile->fp);
S
Shengliang Guan 已提交
580 581
}

582 583
void *taosMmapReadOnlyFile(TdFilePtr pFile, int64_t length) {
  if (pFile == NULL) return NULL;
S
Shengliang Guan 已提交
584

585 586
  void *ptr = mmap(NULL, length, PROT_READ, MAP_SHARED, pFile->fd, 0);
  return ptr;
S
Shengliang Guan 已提交
587 588
}

589
bool taosValidFile(TdFilePtr pFile) { return pFile != NULL; }
S
Shengliang Guan 已提交
590

591
int32_t taosUmaskFile(int32_t maskVal) {
S
Shengliang Guan 已提交
592 593 594
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
595
  return umask(maskVal);
S
TD-4088  
Shengliang Guan 已提交
596
#endif
597 598
}

599 600
int32_t taosGetErrorFile(TdFilePtr pFile) { return errno; }
int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict__ ptrBuf) {
601 602
  size_t len = 0;
  return getline(ptrBuf, &len, pFile->fp);
603
}
604
int32_t taosEOFFile(TdFilePtr pFile) { return feof(pFile->fp); }