osFile.c 17.4 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

wafwerar's avatar
wafwerar 已提交
19
#ifdef WINDOWS
20
#include <io.h>
wafwerar's avatar
wafwerar 已提交
21 22 23
#define F_OK 0
#define W_OK 2
#define R_OK 4
24

wafwerar's avatar
wafwerar 已提交
25 26
#define _SEND_FILE_STEP_ 1000

27
#else
28 29
#include <fcntl.h>
#include <sys/file.h>
30 31

#if !defined(_TD_DARWIN_64)
L
Liu Jicong 已提交
32
#include <sys/sendfile.h>
33
#endif
34 35 36 37
#include <sys/stat.h>
#include <unistd.h>
#define LINUX_FILE_NO_TEXT_OPTION 0
#define O_TEXT                    LINUX_FILE_NO_TEXT_OPTION
38 39
#endif

wafwerar's avatar
wafwerar 已提交
40 41 42 43 44 45 46 47 48 49 50
#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 已提交
51
  TdThreadRwlock rwlock;
L
Liu Jicong 已提交
52 53 54
  int            refId;
  FileFd         fd;
  FILE          *fp;
wafwerar's avatar
wafwerar 已提交
55 56
} * TdFilePtr, TdFile;

57 58
#define FILE_WITH_LOCK 1

59
void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, char *dstPath) {
wafwerar's avatar
wafwerar 已提交
60
#ifdef WINDOWS
S
Shengliang Guan 已提交
61
  const char *tdengineTmpFileNamePrefix = "tdengine-";
S
TD-4088  
Shengliang Guan 已提交
62
  char        tmpPath[PATH_MAX];
S
Shengliang Guan 已提交
63

S
Shengliang Guan 已提交
64 65
  int32_t len = (int32_t)strlen(inputTmpDir);
  memcpy(tmpPath, inputTmpDir, len);
S
TD-4088  
Shengliang Guan 已提交
66 67 68

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

71
  strcpy(tmpPath + len, tdengineTmpFileNamePrefix);
S
Shengliang Guan 已提交
72 73 74
  if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) {
    strcat(tmpPath, fileNamePrefix);
    strcat(tmpPath, "-%d-%s");
S
Shengliang Guan 已提交
75
  }
S
Shengliang Guan 已提交
76

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

S
TD-4088  
Shengliang Guan 已提交
81
#else
S
Shengliang Guan 已提交
82

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

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

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

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

S
TD-4088  
Shengliang Guan 已提交
100 101 102
  char rand[32] = {0};

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

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

S
TD-4088  
Shengliang Guan 已提交
106
#endif
S
Shengliang Guan 已提交
107
}
S
TD-4088  
Shengliang Guan 已提交
108

109
int64_t taosCopyFile(const char *from, const char *to) {
wafwerar's avatar
wafwerar 已提交
110
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
111 112 113 114 115
  if (CopyFile(from, to, 0)) {
    return 1;
  } else {
    return -1;
  }
116 117 118 119 120 121
#else
  char    buffer[4096];
  int64_t size = 0;
  int64_t bytes;

  // fidfrom = open(from, O_RDONLY);
122
  TdFilePtr pFileFrom = taosOpenFile(from, TD_FILE_READ);
123 124 125
  if (pFileFrom == NULL) goto _err;

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

154 155
int32_t taosRemoveFile(const char *path) { return remove(path); }

156
int32_t taosRenameFile(const char *oldName, const char *newName) {
wafwerar's avatar
wafwerar 已提交
157
#ifdef WINDOWS
158 159
  bool code = MoveFileEx(oldName, newName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
  if (!code) {
C
Cary Xu 已提交
160
    printf("failed to rename file %s to %s, reason:%s\n", oldName, newName, strerror(errno));
161 162
  }

163
  return !code;
164 165 166
#else
  int32_t code = rename(oldName, newName);
  if (code < 0) {
C
Cary Xu 已提交
167
    printf("failed to rename file %s to %s, reason:%s\n", oldName, newName, strerror(errno));
168 169 170 171 172 173 174
  }

  return code;
#endif
}

int32_t taosStatFile(const char *path, int64_t *size, int32_t *mtime) {
175
  struct stat fileStat;
wafwerar's avatar
wafwerar 已提交
176
#ifdef WINDOWS
177
  int32_t code = _stat(path, &fileStat);
178
#else
179
  int32_t code = stat(path, &fileStat);
180
#endif
181 182 183 184 185 186 187 188 189 190 191 192 193 194
  if (code < 0) {
    return code;
  }

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

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

  return 0;
}
wafwerar's avatar
wafwerar 已提交
195 196 197 198 199
int32_t taosDevInoFile(TdFilePtr pFile, int64_t *stDev, int64_t *stIno) {
  if (pFile == NULL) {
    return 0;
  }
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
200

wafwerar's avatar
wafwerar 已提交
201
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
202 203

  BY_HANDLE_FILE_INFORMATION bhfi;
204
  HANDLE                     handle = (HANDLE)_get_osfhandle(pFile->fd);
wafwerar's avatar
wafwerar 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217
  if (GetFileInformationByHandle(handle, &bhfi) == FALSE) {
    printf("taosFStatFile get file info fail.");
    return -1;
  }

  if (stDev != NULL) {
    *stDev = (int64_t)(bhfi.dwVolumeSerialNumber);
  }

  if (stIno != NULL) {
    *stIno = (int64_t)((((uint64_t)bhfi.nFileIndexHigh) << 32) + bhfi.nFileIndexLow);
  }

218
#else
219

wafwerar's avatar
wafwerar 已提交
220 221
  struct stat fileStat;
  int32_t     code = fstat(pFile->fd, &fileStat);
222
  if (code < 0) {
wafwerar's avatar
wafwerar 已提交
223
    printf("taosFStatFile run fstat fail.");
224 225 226 227 228 229 230 231 232 233
    return code;
  }

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

  if (stIno != NULL) {
    *stIno = fileStat.st_ino;
  }
wafwerar's avatar
wafwerar 已提交
234
#endif
235 236 237

  return 0;
}
238

239
void autoDelFileListAdd(const char *path) { return; }
240

241
TdFilePtr taosOpenFile(const char *path, int32_t tdFileOptions) {
L
Liu Jicong 已提交
242
  int   fd = -1;
243 244 245 246 247
  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 已提交
248
    } else if (tdFileOptions & TD_FILE_TRUNC) {
249
      mode = (tdFileOptions & TD_FILE_TEXT) ? "wt+" : "wb+";
L
Liu Jicong 已提交
250
    } else if ((tdFileOptions & TD_FILE_READ) && !(tdFileOptions & TD_FILE_WRITE)) {
251
      mode = (tdFileOptions & TD_FILE_TEXT) ? "rt" : "rb";
L
Liu Jicong 已提交
252
    } else {
253 254 255 256 257 258 259 260 261
      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;
262
    access |= (tdFileOptions & TD_FILE_CREATE) ? O_CREAT : 0;
263 264 265 266 267 268 269 270 271 272 273
    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;
L
Liu Jicong 已提交
274 275 276
#ifdef WINDOWS
    fd = _open(path, access, _S_IREAD | _S_IWRITE);
#else
277
    fd = open(path, access, S_IRWXU | S_IRWXG | S_IRWXO);
L
Liu Jicong 已提交
278
#endif
279 280 281 282 283
    if (fd == -1) {
      return NULL;
    }
  }

284
  if (tdFileOptions & TD_FILE_AUTO_DEL) {
285 286
    autoDelFileListAdd(path);
  }
287

wafwerar's avatar
wafwerar 已提交
288
  TdFilePtr pFile = (TdFilePtr)taosMemoryMalloc(sizeof(TdFile));
289
  if (pFile == NULL) {
290 291
    if (fd >= 0) close(fd);
    if (fp != NULL) fclose(fp);
292 293
    return NULL;
  }
294
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
295
  taosThreadRwlockInit(&(pFile->rwlock), NULL);
296
#endif
297 298 299 300 301 302
  pFile->fd = fd;
  pFile->fp = fp;
  pFile->refId = 0;
  return pFile;
}

L
Liu Jicong 已提交
303 304
int32_t taosCloseFile(TdFilePtr *ppFile) {
  int32_t code = 0;
305 306 307 308
  if (ppFile == NULL || *ppFile == NULL) {
    return 0;
  }
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
309
  taosThreadRwlockWrlock(&((*ppFile)->rwlock));
310
#endif
311 312 313 314 315 316
  if ((*ppFile)->fp != NULL) {
    fflush((*ppFile)->fp);
    fclose((*ppFile)->fp);
    (*ppFile)->fp = NULL;
  }
  if ((*ppFile)->fd >= 0) {
317
#ifdef WINDOWS
318 319
    HANDLE h = (HANDLE)_get_osfhandle((*ppFile)->fd);
    !FlushFileBuffers(h);
320
#else
L
Liu Jicong 已提交
321 322
    // warning: never fsync silently in base lib
    /*fsync((*ppFile)->fd);*/
323
#endif
L
Liu Jicong 已提交
324
    code = close((*ppFile)->fd);
325 326
    (*ppFile)->fd = -1;
  }
327
  (*ppFile)->refId = 0;
328
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
329 330
  taosThreadRwlockUnlock(&((*ppFile)->rwlock));
  taosThreadRwlockDestroy(&((*ppFile)->rwlock));
331
#endif
wafwerar's avatar
wafwerar 已提交
332
  taosMemoryFree(*ppFile);
333
  *ppFile = NULL;
L
Liu Jicong 已提交
334
  return code;
335 336 337
}

int64_t taosReadFile(TdFilePtr pFile, void *buf, int64_t count) {
338
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
339
  taosThreadRwlockRdlock(&(pFile->rwlock));
340
#endif
L
Liu Jicong 已提交
341
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
S
TD-1912  
Shengliang Guan 已提交
342 343
  int64_t leftbytes = count;
  int64_t readbytes;
344
  char   *tbuf = (char *)buf;
S
Shengliang Guan 已提交
345 346

  while (leftbytes > 0) {
347
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
348
    readbytes = _read(pFile->fd, (void *)tbuf, (uint32_t)leftbytes);
349
#else
350
    readbytes = read(pFile->fd, (void *)tbuf, (uint32_t)leftbytes);
351
#endif
S
Shengliang Guan 已提交
352 353 354 355
    if (readbytes < 0) {
      if (errno == EINTR) {
        continue;
      } else {
356
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
357
        taosThreadRwlockUnlock(&(pFile->rwlock));
358
#endif
S
Shengliang Guan 已提交
359 360 361
        return -1;
      }
    } else if (readbytes == 0) {
362
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
363
      taosThreadRwlockUnlock(&(pFile->rwlock));
364
#endif
S
TD-1912  
Shengliang Guan 已提交
365
      return (int64_t)(count - leftbytes);
S
Shengliang Guan 已提交
366 367 368 369 370 371
    }

    leftbytes -= readbytes;
    tbuf += readbytes;
  }

372
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
373
  taosThreadRwlockUnlock(&(pFile->rwlock));
374
#endif
S
TD-1912  
Shengliang Guan 已提交
375
  return count;
S
Shengliang Guan 已提交
376 377
}

378
int64_t taosPReadFile(TdFilePtr pFile, void *buf, int64_t count, int64_t offset) {
379
  if (pFile == NULL) {
380 381
    return 0;
  }
382
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
383
  taosThreadRwlockRdlock(&(pFile->rwlock));
384
#endif
L
Liu Jicong 已提交
385
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
wafwerar's avatar
wafwerar 已提交
386
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
387 388 389 390
  size_t pos = _lseek(pFile->fd, 0, SEEK_CUR);
  _lseek(pFile->fd, offset, SEEK_SET);
  int64_t ret = _read(pFile->fd, buf, count);
  _lseek(pFile->fd, pos, SEEK_SET);
wafwerar's avatar
wafwerar 已提交
391
#else
392
  int64_t ret = pread(pFile->fd, buf, count, offset);
wafwerar's avatar
wafwerar 已提交
393
#endif
394
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
395
  taosThreadRwlockUnlock(&(pFile->rwlock));
396 397
#endif
  return ret;
398 399 400
}

int64_t taosWriteFile(TdFilePtr pFile, const void *buf, int64_t count) {
wafwerar's avatar
wafwerar 已提交
401 402 403
  if (pFile == NULL) {
    return 0;
  }
404
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
405
  taosThreadRwlockWrlock(&(pFile->rwlock));
406
#endif
L
Liu Jicong 已提交
407
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
408

409
  int64_t nleft = count;
S
TD-1912  
Shengliang Guan 已提交
410
  int64_t nwritten = 0;
411
  char   *tbuf = (char *)buf;
S
Shengliang Guan 已提交
412 413

  while (nleft > 0) {
414
    nwritten = write(pFile->fd, (void *)tbuf, (uint32_t)nleft);
S
Shengliang Guan 已提交
415 416 417 418
    if (nwritten < 0) {
      if (errno == EINTR) {
        continue;
      }
419
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
420
      taosThreadRwlockUnlock(&(pFile->rwlock));
421
#endif
S
Shengliang Guan 已提交
422 423 424 425 426
      return -1;
    }
    nleft -= nwritten;
    tbuf += nwritten;
  }
427

428
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
429
  taosThreadRwlockUnlock(&(pFile->rwlock));
430
#endif
431
  return count;
S
Shengliang Guan 已提交
432 433
}

434
int64_t taosLSeekFile(TdFilePtr pFile, int64_t offset, int32_t whence) {
435
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
436
  taosThreadRwlockRdlock(&(pFile->rwlock));
437
#endif
L
Liu Jicong 已提交
438
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
wafwerar's avatar
wafwerar 已提交
439 440 441
#ifdef WINDOWS
  int64_t ret = _lseek(pFile->fd, offset, whence);
#else
L
Liu Jicong 已提交
442
  int64_t ret = lseek(pFile->fd, offset, whence);
wafwerar's avatar
wafwerar 已提交
443
#endif
444
#if FILE_WITH_LOCK
wafwerar's avatar
wafwerar 已提交
445
  taosThreadRwlockUnlock(&(pFile->rwlock));
446 447
#endif
  return ret;
448
}
S
Shengliang Guan 已提交
449

450
int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) {
451 452 453
  if (pFile == NULL) {
    return 0;
  }
L
Liu Jicong 已提交
454
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
455

456
  struct stat fileStat;
457
#ifdef WINDOWS
458
  int32_t code = _fstat(pFile->fd, &fileStat);
459
#else
460
  int32_t code = fstat(pFile->fd, &fileStat);
461
#endif
462 463 464
  if (code < 0) {
    return code;
  }
H
Hongze Cheng 已提交
465

466 467 468
  if (size != NULL) {
    *size = fileStat.st_size;
  }
H
Hongze Cheng 已提交
469

470 471 472
  if (mtime != NULL) {
    *mtime = fileStat.st_mtime;
  }
H
Hongze Cheng 已提交
473

474 475
  return 0;
}
H
Hongze Cheng 已提交
476

477
int32_t taosLockFile(TdFilePtr pFile) {
wafwerar's avatar
wafwerar 已提交
478
#ifdef WINDOWS
479 480
  return 0;
#else
L
Liu Jicong 已提交
481
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
482

483 484 485
  return (int32_t)flock(pFile->fd, LOCK_EX | LOCK_NB);
#endif
}
H
Hongze Cheng 已提交
486

487
int32_t taosUnLockFile(TdFilePtr pFile) {
wafwerar's avatar
wafwerar 已提交
488
#ifdef WINDOWS
489 490
  return 0;
#else
L
Liu Jicong 已提交
491
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
492

493 494 495 496 497
  return (int32_t)flock(pFile->fd, LOCK_UN | LOCK_NB);
#endif
}

int32_t taosFtruncateFile(TdFilePtr pFile, int64_t l_size) {
wafwerar's avatar
wafwerar 已提交
498
#ifdef WINDOWS
499 500
  if (pFile->fd < 0) {
    errno = EBADF;
501
    printf("Ftruncate file error, fd arg was negative\n");
502
    return -1;
H
Hongze Cheng 已提交
503 504
  }

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

507 508 509 510
  LARGE_INTEGER li_0;
  li_0.QuadPart = (int64_t)0;
  BOOL cur = SetFilePointerEx(h, li_0, NULL, FILE_CURRENT);
  if (!cur) {
wafwerar's avatar
wafwerar 已提交
511
    printf("SetFilePointerEx Error getting current position in file.\n");
512 513
    return -1;
  }
H
Hongze Cheng 已提交
514

515 516 517 518 519
  LARGE_INTEGER li_size;
  li_size.QuadPart = l_size;
  BOOL cur2 = SetFilePointerEx(h, li_size, NULL, FILE_BEGIN);
  if (cur2 == 0) {
    int error = GetLastError();
wafwerar's avatar
wafwerar 已提交
520
    printf("SetFilePointerEx GetLastError is: %d\n", error);
521 522 523 524 525 526 527 528 529 530 531 532 533
    switch (error) {
      case ERROR_INVALID_HANDLE:
        errno = EBADF;
        break;
      default:
        errno = EIO;
        break;
    }
    return -1;
  }

  if (!SetEndOfFile(h)) {
    int error = GetLastError();
wafwerar's avatar
wafwerar 已提交
534
    printf("SetEndOfFile GetLastError is:%d", error);
535 536 537 538 539 540 541 542 543 544 545 546 547
    switch (error) {
      case ERROR_INVALID_HANDLE:
        errno = EBADF;
        break;
      default:
        errno = EIO;
        break;
    }
    return -1;
  }

  return 0;
#else
548 549 550
  if (pFile == NULL) {
    return 0;
  }
L
Liu Jicong 已提交
551
  assert(pFile->fd >= 0);  // Please check if you have closed the file.
552

553 554 555 556 557
  return ftruncate(pFile->fd, l_size);
#endif
}

int32_t taosFsyncFile(TdFilePtr pFile) {
558 559 560 561
  if (pFile == NULL) {
    return 0;
  }

L
Liu Jicong 已提交
562 563
  // this implementation is WRONG
  // fflush is not a replacement of fsync
564
  if (pFile->fp != NULL) return fflush(pFile->fp);
565
  if (pFile->fd >= 0) {
566
#ifdef WINDOWS
567 568
    HANDLE h = (HANDLE)_get_osfhandle(pFile->fd);
    return !FlushFileBuffers(h);
569
#else
570
    return fsync(pFile->fd);
571
#endif
572
  }
573
  return 0;
H
Hongze Cheng 已提交
574 575
}

wafwerar's avatar
wafwerar 已提交
576 577 578
int64_t taosFSendFile(TdFilePtr pFileOut, TdFilePtr pFileIn, int64_t *offset, int64_t size) {
  if (pFileOut == NULL || pFileIn == NULL) {
    return 0;
S
TD-4088  
Shengliang Guan 已提交
579
  }
wafwerar's avatar
wafwerar 已提交
580
  assert(pFileIn->fd >= 0 && pFileOut->fd >= 0);
S
TD-4088  
Shengliang Guan 已提交
581

wafwerar's avatar
wafwerar 已提交
582
#ifdef WINDOWS
S
TD-4088  
Shengliang Guan 已提交
583

wafwerar's avatar
wafwerar 已提交
584
  _lseek(pFileIn->fd, (int32_t)(*offset), 0);
S
TD-4088  
Shengliang Guan 已提交
585 586 587
  int64_t writeLen = 0;
  uint8_t buffer[_SEND_FILE_STEP_] = {0};

wafwerar's avatar
wafwerar 已提交
588
  for (int64_t len = 0; len < (size - _SEND_FILE_STEP_); len += _SEND_FILE_STEP_) {
wafwerar's avatar
wafwerar 已提交
589
    size_t rlen = _read(pFileIn->fd, (void *)buffer, _SEND_FILE_STEP_);
S
TD-4088  
Shengliang Guan 已提交
590 591 592
    if (rlen <= 0) {
      return writeLen;
    } else if (rlen < _SEND_FILE_STEP_) {
wafwerar's avatar
wafwerar 已提交
593
      write(pFileOut->fd, (void *)buffer, (uint32_t)rlen);
S
TD-4088  
Shengliang Guan 已提交
594 595
      return (int64_t)(writeLen + rlen);
    } else {
wafwerar's avatar
wafwerar 已提交
596
      write(pFileOut->fd, (void *)buffer, _SEND_FILE_STEP_);
S
TD-4088  
Shengliang Guan 已提交
597 598 599 600
      writeLen += _SEND_FILE_STEP_;
    }
  }

wafwerar's avatar
wafwerar 已提交
601
  int64_t remain = size - writeLen;
S
TD-4088  
Shengliang Guan 已提交
602
  if (remain > 0) {
wafwerar's avatar
wafwerar 已提交
603
    size_t rlen = _read(pFileIn->fd, (void *)buffer, (size_t)remain);
S
TD-4088  
Shengliang Guan 已提交
604 605 606
    if (rlen <= 0) {
      return writeLen;
    } else {
wafwerar's avatar
wafwerar 已提交
607
      write(pFileOut->fd, (void *)buffer, (uint32_t)remain);
S
TD-4088  
Shengliang Guan 已提交
608 609 610 611 612 613
      writeLen += remain;
    }
  }
  return writeLen;

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

S
TD-4088  
Shengliang Guan 已提交
615 616 617 618 619
  int r = 0;
  if (offset) {
    r = fseek(in_file, *offset, SEEK_SET);
    if (r == -1) return -1;
  }
wafwerar's avatar
wafwerar 已提交
620
  off_t len = size;
S
TD-4088  
Shengliang Guan 已提交
621
  while (len > 0) {
L
Liu Jicong 已提交
622
    char  buf[1024 * 16];
S
TD-4088  
Shengliang Guan 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635
    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;
  }
wafwerar's avatar
wafwerar 已提交
636
  return size - len;
S
TD-4088  
Shengliang Guan 已提交
637 638 639

#else

S
TD-1912  
Shengliang Guan 已提交
640 641
  int64_t leftbytes = size;
  int64_t sentbytes;
S
Shengliang Guan 已提交
642 643

  while (leftbytes > 0) {
644
    sentbytes = sendfile(pFileOut->fd, pFileIn->fd, offset, leftbytes);
S
Shengliang Guan 已提交
645
    if (sentbytes == -1) {
S
TD-1985  
Shengliang Guan 已提交
646
      if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
S
Shengliang Guan 已提交
647 648 649 650 651
        continue;
      } else {
        return -1;
      }
    } else if (sentbytes == 0) {
S
TD-1912  
Shengliang Guan 已提交
652
      return (int64_t)(size - leftbytes);
S
Shengliang Guan 已提交
653 654 655 656 657 658
    }

    leftbytes -= sentbytes;
  }

  return size;
S
Shengliang Guan 已提交
659
#endif
wafwerar's avatar
wafwerar 已提交
660
}
S
Shengliang Guan 已提交
661

662
void taosFprintfFile(TdFilePtr pFile, const char *format, ...) {
663 664 665 666 667
  if (pFile == NULL) {
    return;
  }
  assert(pFile->fp != NULL);

668
  va_list ap;
669
  va_start(ap, format);
670
  vfprintf(pFile->fp, format, ap);
671 672
  va_end(ap);
  fflush(pFile->fp);
S
Shengliang Guan 已提交
673 674
}

675
bool taosValidFile(TdFilePtr pFile) { return pFile != NULL && pFile->fd > 0; }
S
Shengliang Guan 已提交
676

677
int32_t taosUmaskFile(int32_t maskVal) {
wafwerar's avatar
wafwerar 已提交
678
#ifdef WINDOWS
S
Shengliang Guan 已提交
679 680
  return 0;
#else
681
  return umask(maskVal);
S
TD-4088  
Shengliang Guan 已提交
682
#endif
683 684
}

685
int32_t taosGetErrorFile(TdFilePtr pFile) { return errno; }
wafwerar's avatar
wafwerar 已提交
686
int64_t taosGetLineFile(TdFilePtr pFile, char **__restrict ptrBuf) {
L
Liu Jicong 已提交
687
  if (pFile == NULL || ptrBuf == NULL) {
688 689
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
690 691 692
  if (*ptrBuf != NULL) {
    taosMemoryFreeClear(*ptrBuf);
  }
693
  assert(pFile->fp != NULL);
wafwerar's avatar
wafwerar 已提交
694 695 696 697 698 699 700 701 702 703
#ifdef WINDOWS
  *ptrBuf = taosMemoryMalloc(1024);
  if (*ptrBuf == NULL) return -1;
  if (fgets(*ptrBuf, 1023, pFile->fp) == NULL) {
    taosMemoryFreeClear(*ptrBuf);
    return -1;
  }
  (*ptrBuf)[1023] = 0;
  return strlen(*ptrBuf);
#else
704 705
  size_t len = 0;
  return getline(ptrBuf, &len, pFile->fp);
wafwerar's avatar
wafwerar 已提交
706
#endif
707
}
wafwerar's avatar
wafwerar 已提交
708
int64_t taosGetsFile(TdFilePtr pFile, int32_t maxSize, char *__restrict buf) {
L
Liu Jicong 已提交
709
  if (pFile == NULL || buf == NULL) {
wafwerar's avatar
wafwerar 已提交
710 711 712 713 714 715 716 717
    return -1;
  }
  assert(pFile->fp != NULL);
  if (fgets(buf, maxSize, pFile->fp) == NULL) {
    return -1;
  }
  return strlen(buf);
}
L
Liu Jicong 已提交
718
int32_t taosEOFFile(TdFilePtr pFile) {
719 720 721 722 723
  if (pFile == NULL) {
    return 0;
  }
  assert(pFile->fp != NULL);

L
Liu Jicong 已提交
724
  return feof(pFile->fp);
L
fix  
Liu Jicong 已提交
725
}
726

727 728 729 730 731 732 733 734 735 736 737 738 739 740
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;
  }
wafwerar's avatar
wafwerar 已提交
741 742 743
#ifdef WINDOWS
  return _access(pathname, flags) == 0;
#else
744
  return access(pathname, flags) == 0;
wafwerar's avatar
wafwerar 已提交
745
#endif
746
}
747 748

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