wFile.c 2.6 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
S
slguan 已提交
18
#include "tulog.h"
19
#include "tglobal.h"
S
slguan 已提交
20 21 22

void taosGetTmpfilePath(const char *fileNamePrefix, char *dstPath) {
  const char* tdengineTmpFileNamePrefix = "tdengine-";
23
  char tmpPath[PATH_MAX];
S
slguan 已提交
24

25 26 27 28 29
  int32_t len = (int32_t)strlen(tsTempDir);
  memcpy(tmpPath, tsTempDir, len);

  if (tmpPath[len - 1] != '/' && tmpPath[len - 1] != '\\') {
      tmpPath[len++] = '\\';
S
slguan 已提交
30
  }
31 32

  strcpy(tmpPath + len, tdengineTmpFileNamePrefix);
S
slguan 已提交
33 34 35 36 37 38 39 40 41 42 43
  strcat(tmpPath, tdengineTmpFileNamePrefix);
  if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) {
    strcat(tmpPath, fileNamePrefix);
    strcat(tmpPath, "-%d-%s");
  }
  
  char rand[8] = {0};
  taosRandStr(rand, tListLen(rand) - 1);
  snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand);
}

S
slguan 已提交
44 45
#define _SEND_FILE_STEP_ 1000

S
Shengliang Guan 已提交
46
int64_t taosFSendFile(FILE *out_file, FILE *in_file, int64_t *offset, int64_t count) {
S
slguan 已提交
47
  fseek(in_file, (int32_t)(*offset), 0);
S
Shengliang Guan 已提交
48
  int64_t writeLen = 0;
S
slguan 已提交
49 50
  uint8_t buffer[_SEND_FILE_STEP_] = { 0 };
  
S
Shengliang Guan 已提交
51
  for (int64_t len = 0; len < (count - _SEND_FILE_STEP_); len += _SEND_FILE_STEP_) {
S
slguan 已提交
52 53 54 55 56 57
    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);
S
Shengliang Guan 已提交
58
      return (int64_t)(writeLen + rlen);
S
slguan 已提交
59 60 61 62 63 64 65
    }
    else {
      fwrite(buffer, 1, _SEND_FILE_STEP_, in_file);
      writeLen += _SEND_FILE_STEP_;
    }
  }

S
Shengliang Guan 已提交
66
  int64_t remain = count - writeLen;
S
slguan 已提交
67
  if (remain > 0) {
68
    size_t rlen = fread(buffer, 1, (size_t) remain, in_file);
S
slguan 已提交
69 70 71 72
    if (rlen <= 0) {
      return writeLen;
    }
    else {
73
      fwrite(buffer, 1, (size_t) remain, out_file);
S
slguan 已提交
74 75 76 77 78 79
      writeLen += remain;
    }
  }

  return writeLen;
}
S
Shengliang Guan 已提交
80

S
TD-1912  
Shengliang Guan 已提交
81 82
int64_t taosSendFile(int32_t dfd, int32_t sfd, int64_t* offset, int64_t size) {
  uError("taosSendFile no implemented yet");
S
Shengliang Guan 已提交
83
  return 0;
84 85
}

S
TD-1912  
Shengliang Guan 已提交
86
int32_t taosFtruncate(int32_t fd, int64_t length) {
87 88
  uError("taosFtruncate no implemented yet");
  return 0;
S
Shengliang Guan 已提交
89
}