osFile.c 19.2 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
#include "os.h"
17
#include "osSemaphore.h"
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#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) */
35

36
#else
37 38
#include <fcntl.h>
#include <sys/file.h>
39 40 41 42

#if !defined(_TD_DARWIN_64)
    #include <sys/sendfile.h>
#endif
43 44 45 46
#include <sys/stat.h>
#include <unistd.h>
#define LINUX_FILE_NO_TEXT_OPTION 0
#define O_TEXT                    LINUX_FILE_NO_TEXT_OPTION
47 48
#endif

wafwerar's avatar
wafwerar 已提交
49 50 51 52 53 54 55 56 57 58 59
#if defined(WINDOWS)
typedef int32_t FileFd;
typedef int32_t SocketFd;
#else
typedef int32_t FileFd;
typedef int32_t SocketFd;
#endif

typedef int32_t FileFd;

typedef struct TdFile {
wafwerar's avatar
wafwerar 已提交
60
  TdThreadRwlock rwlock;
wafwerar's avatar
wafwerar 已提交
61 62 63 64 65
  int      refId;
  FileFd   fd;
  FILE    *fp;
} * TdFilePtr, TdFile;

66 67
#define FILE_WITH_LOCK 1

68
void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) {
S
TD-4088  
Shengliang Guan 已提交
69
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
S
Shengliang Guan 已提交
70
  const char *tdengineTmpFileNamePrefix = "tdengine-";
S
TD-4088  
Shengliang Guan 已提交
71
  char        tmpPath[PATH_MAX];
S
Shengliang Guan 已提交
72

S
Shengliang Guan 已提交
73 74
  int32_t len = (int32_t)strlen(inputTmpDir);
  memcpy(tmpPath, inputTmpDir, len);
S
TD-4088  
Shengliang Guan 已提交
75 76 77

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

80
  strcpy(tmpPath + len, tdengineTmpFileNamePrefix);
S
TD-4088  
Shengliang Guan 已提交
81
  strcat(tmpPath, tdengineTmpFileNamePrefix);
S
Shengliang Guan 已提交
82 83 84
  if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) {
    strcat(tmpPath, fileNamePrefix);
    strcat(tmpPath, "-%d-%s");
S
Shengliang Guan 已提交
85
  }
S
Shengliang Guan 已提交
86

S
TD-4088  
Shengliang Guan 已提交
87 88
  char rand[8] = {0};
  taosRandStr(rand, tListLen(rand) - 1);
S
Shengliang Guan 已提交
89
  snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand);
S
TD-1912  
Shengliang Guan 已提交
90

S
TD-4088  
Shengliang Guan 已提交
91
#else
S
Shengliang Guan 已提交
92

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

95
  char    tmpPath[PATH_MAX];
S
Shengliang Guan 已提交
96 97
  int32_t len = strlen(inputTmpDir);
  memcpy(tmpPath, inputTmpDir, len);
S
TD-4088  
Shengliang Guan 已提交
98
  static uint64_t seqId = 0;
S
Shengliang Guan 已提交
99

S
TD-4088  
Shengliang Guan 已提交
100 101
  if (tmpPath[len - 1] != '/') {
    tmpPath[len++] = '/';
S
TD-1912  
Shengliang Guan 已提交
102
  }
S
Shengliang Guan 已提交
103

S
TD-4088  
Shengliang Guan 已提交
104 105 106 107
  strcpy(tmpPath + len, tdengineTmpFileNamePrefix);
  if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) {
    strcat(tmpPath, fileNamePrefix);
    strcat(tmpPath, "-%d-%s");
S
Shengliang Guan 已提交
108 109
  }

S
TD-4088  
Shengliang Guan 已提交
110 111 112
  char rand[32] = {0};

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

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

S
TD-4088  
Shengliang Guan 已提交
116
#endif
S
Shengliang Guan 已提交
117
}
S
TD-4088  
Shengliang Guan 已提交
118

119 120 121 122 123 124 125 126 127
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);
128
  TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ);
129 130 131
  if (pFileFrom == NULL) goto _err;

  // fidto = open(to, O_WRONLY | O_CREAT | O_EXCL, 0755);
132
  TdFilePtr pFileTo = taosOpenFile(to, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_EXCL);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  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);
155
  taosRemoveFile(to);
156 157 158 159
  return -1;
#endif
}

160 161
int32_t taosRemoveFile(const char *path) { return remove(path); }

162 163 164 165
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) {
166
    // printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno));
167 168 169 170 171 172
  }

  return code;
#else
  int32_t code = rename(oldName, newName);
  if (code < 0) {
173
    // printf("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno));
174 175 176 177 178 179 180 181 182 183 184
  }

  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;
185
  int32_t code = stat(path, &fileStat);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  if (code < 0) {
    return code;
  }

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

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

  return 0;
#endif
}
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
int32_t taosDevInoFile(const char *path, int64_t *stDev, int64_t *stIno) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
  struct stat fileStat;
  int32_t code = stat(path, &fileStat);
  if (code < 0) {
    return code;
  }

  if (stDev != NULL) {
    *stDev = fileStat.st_dev;
  }

  if (stIno != NULL) {
    *stIno = fileStat.st_ino;
  }

  return 0;
#endif
}
222

223
void autoDelFileListAdd(const char *path) { return; }
224

225
TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
226 227 228
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return NULL;
#else
229 230 231 232 233 234
  int fd = -1;
  FILE *fp = NULL;
  if (tdFileOptions & TD_FILE_STREAM) {
    char *mode = NULL;
    if (tdFileOptions & TD_FILE_APPEND) {
      mode = (tdFileOptions & TD_FILE_TEXT) ? "at+" : "ab+";
L
Liu Jicong 已提交
235
    } else if (tdFileOptions & TD_FILE_TRUNC) {
236
      mode = (tdFileOptions & TD_FILE_TEXT) ? "wt+" : "wb+";
L
Liu Jicong 已提交
237
    } else if ((tdFileOptions & TD_FILE_READ) && !(tdFileOptions & TD_FILE_WRITE)) {
238
      mode = (tdFileOptions & TD_FILE_TEXT) ? "rt" : "rb";
L
Liu Jicong 已提交
239
    } else {
240 241 242 243 244 245 246 247 248
      mode = (tdFileOptions & TD_FILE_TEXT) ? "rt+" : "rb+";
    }
    assert(!(tdFileOptions & TD_FILE_EXCL));
    fp = fopen(path, mode);
    if (fp == NULL) {
      return NULL;
    }
  } else {
    int access = O_BINARY;
249
    access |= (tdFileOptions & TD_FILE_CREATE) ? O_CREAT : 0;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    if ((tdFileOptions & TD_FILE_WRITE) && (tdFileOptions & TD_FILE_READ)) {
      access |= O_RDWR;
    } else if (tdFileOptions & TD_FILE_WRITE) {
      access |= O_WRONLY;
    } else if (tdFileOptions & TD_FILE_READ) {
      access |= O_RDONLY;
    }
    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;
    fd = open(path, access, S_IRWXU | S_IRWXG | S_IRWXO);
    if (fd == -1) {
      return NULL;
    }
  }

267
  if (tdFileOptions & TD_FILE_AUTO_DEL) {
268 269
    autoDelFileListAdd(path);
  }
270

wafwerar's avatar
wafwerar 已提交
271
  TdFilePtr pFile = (TdFilePtr)taosMemoryMalloc(sizeof(TdFile));
272
  if (pFile == NULL) {
273 274
    if (fd >= 0) close(fd);
    if (fp != NULL) fclose(fp);
275 276
    return NULL;
  }
277
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
278
  taosThreadRwlockInit(&(pFile->rwlock), NULL);
279
#endif
280 281 282 283 284 285 286 287 288 289 290
  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
291 292 293 294
  if (ppFile == NULL || *ppFile == NULL) {
    return 0;
  }
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
295
  taosThreadRwlockWrlock(&((*ppFile)->rwlock));
296
#endif
297
  if (ppFile == NULL || *ppFile == NULL) {
298 299
    return 0;
  }
300 301 302 303 304 305 306 307 308 309
  if ((*ppFile)->fp != NULL) {
    fflush((*ppFile)->fp);
    fclose((*ppFile)->fp);
    (*ppFile)->fp = NULL;
  }
  if ((*ppFile)->fd >= 0) {
    fsync((*ppFile)->fd);
    close((*ppFile)->fd);
    (*ppFile)->fd = -1;
  }
310
  (*ppFile)->refId = 0;
311
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
312 313
  taosThreadRwlockUnlock(&((*ppFile)->rwlock));
  taosThreadRwlockDestroy(&((*ppFile)->rwlock));
314
#endif
wafwerar's avatar
wafwerar 已提交
315
  taosMemoryFree(*ppFile);
316 317 318 319 320 321
  *ppFile = NULL;
  return 0;
#endif
}

int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
322
  if (pFile == NULL) {
323 324
    return 0;
  }
325
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
326
  taosThreadRwlockRdlock(&(pFile->rwlock));
327
#endif
328
  assert(pFile->fd >= 0); // Please check if you have closed the file.
S
TD-1912  
Shengliang Guan 已提交
329 330
  int64_t leftbytes = count;
  int64_t readbytes;
331
  char   *tbuf = (char *)buf;
S
Shengliang Guan 已提交
332 333

  while (leftbytes > 0) {
334
    readbytes = read(pFile->fd, (void *)tbuf, (uint32_t)leftbytes);
S
Shengliang Guan 已提交
335 336 337 338
    if (readbytes < 0) {
      if (errno == EINTR) {
        continue;
      } else {
339
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
340
        taosThreadRwlockUnlock(&(pFile->rwlock));
341
#endif
S
Shengliang Guan 已提交
342 343 344
        return -1;
      }
    } else if (readbytes == 0) {
345
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
346
      taosThreadRwlockUnlock(&(pFile->rwlock));
347
#endif
S
TD-1912  
Shengliang Guan 已提交
348
      return (int64_t)(count - leftbytes);
S
Shengliang Guan 已提交
349 350 351 352 353 354
    }

    leftbytes -= readbytes;
    tbuf += readbytes;
  }

355
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
356
  taosThreadRwlockUnlock(&(pFile->rwlock));
357
#endif
S
TD-1912  
Shengliang Guan 已提交
358
  return count;
S
Shengliang Guan 已提交
359 360
}

361
int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) {
362
  if (pFile == NULL) {
363 364
    return 0;
  }
365
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
366
  taosThreadRwlockRdlock(&(pFile->rwlock));
367
#endif
368
  assert(pFile->fd >= 0); // Please check if you have closed the file.
369 370
  int64_t ret = pread(pFile->fd, buf, count, offset);
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
371
  taosThreadRwlockUnlock(&(pFile->rwlock));
372 373
#endif
  return ret;
374 375 376
}

int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
377 378 379
  if (pFile == NULL) {
    return 0;
  }
380
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
381
  taosThreadRwlockWrlock(&(pFile->rwlock));
382
#endif
383
  assert(pFile->fd >= 0); // Please check if you have closed the file.
384

385
  int64_t nleft = count;
S
TD-1912  
Shengliang Guan 已提交
386
  int64_t nwritten = 0;
387
  char   *tbuf = (char *)buf;
S
Shengliang Guan 已提交
388 389

  while (nleft > 0) {
390
    nwritten = write(pFile->fd, (void *)tbuf, (uint32_t)nleft);
S
Shengliang Guan 已提交
391 392 393 394
    if (nwritten < 0) {
      if (errno == EINTR) {
        continue;
      }
395
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
396
      taosThreadRwlockUnlock(&(pFile->rwlock));
397
#endif
S
Shengliang Guan 已提交
398 399 400 401 402
      return -1;
    }
    nleft -= nwritten;
    tbuf += nwritten;
  }
403

404
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
405
  taosThreadRwlockUnlock(&(pFile->rwlock));
406
#endif
407
  return count;
S
Shengliang Guan 已提交
408 409
}

410
int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) {
411 412 413
  if (pFile == NULL) {
    return 0;
  }
414
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
415
  taosThreadRwlockRdlock(&(pFile->rwlock));
416
#endif
417
  assert(pFile->fd >= 0); // Please check if you have closed the file.
418 419
  int64_t ret = lseek(pFile->fd, (long)offset, whence);
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
420
  taosThreadRwlockUnlock(&(pFile->rwlock));
421 422
#endif
  return ret;
423
}
S
Shengliang Guan 已提交
424

425
int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
S
Shengliang Guan 已提交
426 427 428
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
429 430 431
  if (pFile == NULL) {
    return 0;
  }
432
  assert(pFile->fd >= 0); // Please check if you have closed the file.
433

434
  struct stat fileStat;
435
  int32_t code = fstat(pFile->fd, &fileStat);
436 437 438
  if (code < 0) {
    return code;
  }
H
Hongze Cheng 已提交
439

440 441 442
  if (size != NULL) {
    *size = fileStat.st_size;
  }
H
Hongze Cheng 已提交
443

444 445 446
  if (mtime != NULL) {
    *mtime = fileStat.st_mtime;
  }
H
Hongze Cheng 已提交
447

448 449 450
  return 0;
#endif
}
H
Hongze Cheng 已提交
451

452 453 454 455
int32_t taosLockFile(TdFilePtr pFile) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
456 457 458
  if (pFile == NULL) {
    return 0;
  }
459
  assert(pFile->fd >= 0); // Please check if you have closed the file.
460

461 462 463
  return (int32_t)flock(pFile->fd, LOCK_EX | LOCK_NB);
#endif
}
H
Hongze Cheng 已提交
464

465 466 467 468
int32_t taosUnLockFile(TdFilePtr pFile) {
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
469 470 471
  if (pFile == NULL) {
    return 0;
  }
472
  assert(pFile->fd >= 0); // Please check if you have closed the file.
473

474 475 476 477 478 479 480 481 482 483
  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 已提交
484 485
  }

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

488 489 490 491 492 493 494
  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 已提交
495

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  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
529 530 531
  if (pFile == NULL) {
    return 0;
  }
532
  assert(pFile->fd >= 0); // Please check if you have closed the file.
533

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
  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
550 551 552 553 554
  if (pFile == NULL) {
    return 0;
  }

  if (pFile->fp != NULL) return fflush(pFile->fp);
555
  if (pFile->fd >= 0) return fsync(pFile->fd);
556 557

  return 0;
S
Shengliang Guan 已提交
558
#endif
H
Hongze Cheng 已提交
559 560
}

S
TD-4088  
Shengliang Guan 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
#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 已提交
631

S
TD-4088  
Shengliang Guan 已提交
632 633 634 635 636 637 638 639
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) {
640
    char buf[1024 * 16];
S
TD-4088  
Shengliang Guan 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    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) {
665
    char buf[1024 * 16];
S
TD-4088  
Shengliang Guan 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679
    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

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
// int64_t taosSendFile(int fdDst, TdFilePtr pFileSrc, int64_t *offset, int64_t size) {
//   if (pFileSrc == NULL) {
//     return 0;
//   }
//   assert(pFileSrc->fd >= 0);

//   int64_t leftbytes = size;
//   int64_t sentbytes;

//   while (leftbytes > 0) {
//     sentbytes = sendfile(fdDst, pFileSrc->fd, offset, leftbytes);
//     if (sentbytes == -1) {
//       if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
//         continue;
//       } else {
//         return -1;
//       }
//     } else if (sentbytes == 0) {
//       return (int64_t)(size - leftbytes);
//     }

//     leftbytes -= sentbytes;
//   }

//   return size;
// }

int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size) {
  if (pFileOut == NULL || pFileIn == NULL) {
709 710
    return 0;
  }
711
  assert(pFileIn->fd >= 0 && pFileOut->fd >= 0);
S
TD-1912  
Shengliang Guan 已提交
712 713
  int64_t leftbytes = size;
  int64_t sentbytes;
S
Shengliang Guan 已提交
714 715

  while (leftbytes > 0) {
716
    sentbytes = sendfile(pFileOut->fd, pFileIn->fd, offset, leftbytes);
S
Shengliang Guan 已提交
717
    if (sentbytes == -1) {
S
TD-1985  
Shengliang Guan 已提交
718
      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
S
Shengliang Guan 已提交
719 720 721 722 723
        continue;
      } else {
        return -1;
      }
    } else if (sentbytes == 0) {
S
TD-1912  
Shengliang Guan 已提交
724
      return (int64_t)(size - leftbytes);
S
Shengliang Guan 已提交
725 726 727 728 729 730 731
    }

    leftbytes -= sentbytes;
  }

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

S
Shengliang Guan 已提交
733 734
#endif

735
void taosFprintfFile(TdFilePtr pFile, const char *format, ...) {
736 737 738 739 740
  if (pFile == NULL) {
    return;
  }
  assert(pFile->fp != NULL);

741
  va_list ap;
742
  va_start(ap, format);
743
  vfprintf(pFile->fp, format, ap);
744 745
  va_end(ap);
  fflush(pFile->fp);
S
Shengliang Guan 已提交
746 747
}

748
#if !defined(WINDOWS)
749
void *taosMmapReadOnlyFile(TdFilePtr pFile, int64_t length) {
750 751 752
  if (pFile == NULL) {
    return NULL;
  }
753
  assert(pFile->fd >= 0); // Please check if you have closed the file.
S
Shengliang Guan 已提交
754

755 756
  void *ptr = mmap(NULL, length, PROT_READ, MAP_SHARED, pFile->fd, 0);
  return ptr;
S
Shengliang Guan 已提交
757
}
758
#endif
S
Shengliang Guan 已提交
759

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

762
int32_t taosUmaskFile(int32_t maskVal) {
S
Shengliang Guan 已提交
763 764 765
#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)
  return 0;
#else
766
  return umask(maskVal);
S
TD-4088  
Shengliang Guan 已提交
767
#endif
768 769
}

770
int32_t taosGetErrorFile(TdFilePtr pFile) { return errno; }
wafwerar's avatar
wafwerar 已提交
771
int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) {
772 773 774 775 776
  if (pFile == NULL) {
    return -1;
  }
  assert(pFile->fp != NULL);

777 778
  size_t len = 0;
  return getline(ptrBuf, &len, pFile->fp);
779
}
L
Liu Jicong 已提交
780
int32_t taosEOFFile(TdFilePtr pFile) {
781 782 783 784 785
  if (pFile == NULL) {
    return 0;
  }
  assert(pFile->fp != NULL);

L
Liu Jicong 已提交
786
  return feof(pFile->fp);
L
fix  
Liu Jicong 已提交
787
}
788 789 790

#if !defined(WINDOWS)

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
bool taosCheckAccessFile(const char *pathname, int32_t tdFileAccessOptions) {
  int flags = 0;

  if (tdFileAccessOptions & TD_FILE_ACCESS_EXIST_OK) {
    flags |= F_OK;
  }

  if (tdFileAccessOptions & TD_FILE_ACCESS_READ_OK) {
    flags |= R_OK;
  }

  if (tdFileAccessOptions & TD_FILE_ACCESS_WRITE_OK) {
    flags |= W_OK;
  }

  return access(pathname, flags) == 0;
}
808 809 810 811

bool taosCheckExistFile(const char *pathname) { return taosCheckAccessFile(pathname, TD_FILE_ACCESS_EXIST_OK); };

#endif // WINDOWS