static int log_ring_file_open_fd()

in Sources/aliyun-log-c-sdk/log_ring_file.c [19:51]


static int log_ring_file_open_fd(log_ring_file *file, uint64_t offset, int32_t fileIndex, int32_t fileOffset)
{
    if (file->nowFD > 0 && file->nowFileIndex == fileIndex && file->nowOffset % file->maxFileSize == fileOffset)
    {
        return 0;
    }
    if (file->nowFD > 0)
    {
        close(file->nowFD);
        file->nowFD = -1;
    }
    file->fileRemoveFlags[fileIndex] = 0;
    char filePath[256] = {0};
    snprintf(filePath, 255, "%s_%03d", file->filePath, fileIndex);
    int openFlag = O_RDWR|O_CREAT;
    if (file->syncWrite)
    {
        openFlag |= O_SYNC;
    }
    file->nowFD = open(filePath, openFlag, 0644);
    if (file->nowFD < 0)
    {
        aos_error_log("open file failed %s, error %s", filePath, strerror(errno));
        return -1;
    }
    if (fileOffset != 0)
    {
        lseek(file->nowFD, fileOffset, SEEK_SET);
    }
    file->nowFileIndex = fileIndex;
    file->nowOffset = offset;
    return 0;
}