io.h 2.5 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2010
//

羽飞's avatar
羽飞 已提交
15
#pragma once
羽飞's avatar
羽飞 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

#include <string>
#include <vector>

#include "common/defs.h"

namespace common {

/**
 * read data from file fileName, store data to data
 * if success, store file continent to data
 * if fail, return -1 and don't change data
 */
int readFromFile(const std::string &fileName, char *&data, size_t &fileSize);

羽飞's avatar
羽飞 已提交
31
int writeToFile(const std::string &fileName, const char *data, uint32_t dataSize, const char *openMode);
羽飞's avatar
羽飞 已提交
32 33 34 35

/**
 * return the line number which line.strip() isn't empty
 */
羽飞's avatar
羽飞 已提交
36
int getFileLines(const std::string &fileName, uint64_t &lineNum);
羽飞's avatar
羽飞 已提交
37 38 39 40 41 42 43 44 45 46 47

/** Get file list from the dir
 * don't care ".", "..", ".****" hidden files
 * just count regular files, don't care directory
 * @param[out]  fileList   file List
 * @param[in]   path       the search path
 * @param[in]   pattern    regex string, if not empty, the file should match
 * list
 * @param[in]   resursion  if this has been set, it will search subdirs
 * @return  0   if success, error code otherwise
 */
48 49
int getFileList(
    std::vector<std::string> &fileList, const std::string &path, const std::string &pattern, bool recursive);
羽飞's avatar
羽飞 已提交
50
int getFileNum(uint64_t &fileNum, const std::string &path, const std::string &pattern, bool recursive);
51
int getDirList(std::vector<std::string> &dirList, const std::string &path, const std::string &pattern);
羽飞's avatar
羽飞 已提交
52 53 54 55 56 57

int touch(const std::string &fileName);

/**
 * get file size
 */
羽飞's avatar
羽飞 已提交
58
int getFileSize(const char *filePath, uint64_t &fileLen);
羽飞's avatar
羽飞 已提交
59

羽飞's avatar
羽飞 已提交
60 61 62 63 64 65 66 67
/**
 * @brief 一次性写入所有指定数据
 * 
 * @param fd  写入的描述符
 * @param buf 写入的数据
 * @param size 写入多少数据
 * @return int 0 表示成功,否则返回errno
 */
羽飞's avatar
羽飞 已提交
68
int writen(int fd, const void *buf, int size);
69 70 71 72 73 74 75 76 77

/**
 * @brief 一次性读取指定长度的数据
 * 
 * @param fd  读取的描述符
 * @param buf 读取到这里
 * @param size 读取的数据长度
 * @return int 返回0表示成功。-1 表示读取到文件尾,并且没有读到size大小数据,其它表示errno
 */
羽飞's avatar
羽飞 已提交
78 79
int readn(int fd, void *buf, int size);

80
}  // namespace common