Coverage Report

Created: 2026-10-26 08:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/file_system.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <butil/macros.h>
21
#include <glog/logging.h>
22
23
#include <cstdint>
24
#include <memory>
25
#include <string>
26
#include <vector>
27
28
#include "common/status.h"
29
#include "io/fs/file_reader_writer_fwd.h"
30
#include "io/fs/path.h"
31
32
namespace doris::io {
33
34
#ifndef FILESYSTEM_M
35
#define FILESYSTEM_M(stmt)                                  \
36
8.58M
    do {                                                    \
37
8.58M
        Status _s;                                          \
38
8.60M
        if (bthread_self() == 0) {                          \
39
8.60M
            _s = (stmt);                                    \
40
18.4E
        } else {                                            \
41
18.4E
            auto task = [&] { _s = (stmt); };               \
Unexecuted instantiation: unity_3_cxx.cxx:_ZZN5doris2io16RemoteFileSystem6uploadERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: unity_3_cxx.cxx:_ZZN5doris2io16RemoteFileSystem12batch_uploadERKSt6vectorINSt10filesystem7__cxx114pathESaIS5_EES9_ENK3$_0clEv
Unexecuted instantiation: unity_3_cxx.cxx:_ZZN5doris2io16RemoteFileSystem8downloadERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem24delete_directory_or_fileERKNSt10filesystem7__cxx114pathEENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem22delete_empty_directoryERKNSt10filesystem7__cxx114pathEENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem9link_fileERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem6md5sumERKNSt10filesystem7__cxx114pathEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem17iterate_directoryERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt8functionIFbRKNS0_8FileInfoEEEENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem14get_space_infoERKNSt10filesystem7__cxx114pathEPmS7_ENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem14get_inode_infoERKNSt10filesystem7__cxx114pathEPmS7_ENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem9copy_pathERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: unity_2_cxx.cxx:_ZZN5doris2io15LocalFileSystem10permissionERKNSt10filesystem7__cxx114pathENS2_5permsEENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZN5doris2io10FileSystem11create_fileERKNSt10filesystem7__cxx114pathEPSt10unique_ptrINS0_10FileWriterESt14default_deleteIS8_EEPKNS0_17FileWriterOptionsEENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZN5doris2io10FileSystem9open_fileERKNSt10filesystem7__cxx114pathEPSt10shared_ptrINS0_10FileReaderEEPKNS0_17FileReaderOptionsEENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZN5doris2io10FileSystem16create_directoryERKNSt10filesystem7__cxx114pathEbENK3$_0clEv
unity_1_cxx.cxx:_ZZN5doris2io10FileSystem11delete_fileERKNSt10filesystem7__cxx114pathEENK3$_0clEv
Line
Count
Source
41
3
            auto task = [&] { _s = (stmt); };               \
Unexecuted instantiation: unity_1_cxx.cxx:_ZZN5doris2io10FileSystem16delete_directoryERKNSt10filesystem7__cxx114pathEENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZN5doris2io10FileSystem12batch_deleteERKSt6vectorINSt10filesystem7__cxx114pathESaIS5_EEENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZNK5doris2io10FileSystem6existsERKNSt10filesystem7__cxx114pathEPbENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZNK5doris2io10FileSystem9file_sizeERKNSt10filesystem7__cxx114pathEPlENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZN5doris2io10FileSystem4listERKNSt10filesystem7__cxx114pathEbPSt6vectorINS0_8FileInfoESaIS8_EEPbENK3$_0clEv
Unexecuted instantiation: unity_1_cxx.cxx:_ZZN5doris2io10FileSystem6renameERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
42
18.4E
            AsyncIO::run_task(task, _type);                 \
43
18.4E
        }                                                   \
44
8.58M
        if (!_s) {                                          \
45
304k
            _s = Status::Error<false>(_s.code(), _s.msg()); \
46
304k
        }                                                   \
47
8.58M
        return _s;                                          \
48
8.58M
    } while (0);
49
#endif
50
51
// OBJECT_STORAGE (S3, S3-compatible stores and Azure Blob) keeps the value 1 the former S3
52
// member had.
53
enum class FileSystemType : uint8_t { LOCAL, OBJECT_STORAGE, HDFS, BROKER, HTTP };
54
55
1
inline std::ostream& operator<<(std::ostream& ostr, FileSystemType type) {
56
1
    switch (type) {
57
0
    case FileSystemType::LOCAL:
58
0
        ostr << "LOCAL";
59
0
        return ostr;
60
1
    case FileSystemType::OBJECT_STORAGE:
61
1
        ostr << "OBJECT_STORAGE";
62
1
        return ostr;
63
0
    case FileSystemType::HDFS:
64
0
        ostr << "HDFS";
65
0
        return ostr;
66
0
    case FileSystemType::BROKER:
67
0
        ostr << "BROKER";
68
0
        return ostr;
69
0
    case FileSystemType::HTTP:
70
0
        ostr << "HTTP";
71
0
        return ostr;
72
0
    default:
73
0
        ostr << "UNKNOWN";
74
0
        return ostr;
75
1
    }
76
1
}
77
78
struct FileInfo {
79
    // only file name, no path
80
    std::string file_name;
81
    int64_t file_size;
82
    bool is_file;
83
};
84
85
// `FileSystem` providing an interface to access FS metadata. Each `RemoteFileSystem` instance
86
// (except temporary Remote FS) corresponds to a `StorageResource` configured in FE.
87
class FileSystem {
88
public:
89
    // Sometimes `FileSystem` instance maybe a temporary instance which has no corresponding
90
    // Resource, then use `TMP_FS_ID` as FS id.
91
    inline static const std::string TMP_FS_ID;
92
93
    // The following are public interface.
94
    // And derived classes should implement all xxx_impl methods.
95
    Status create_file(const Path& file, FileWriterPtr* writer,
96
                       const FileWriterOptions* opts = nullptr);
97
    Status open_file(const Path& file, FileReaderSPtr* reader,
98
                     const FileReaderOptions* opts = nullptr);
99
    Status create_directory(const Path& dir, bool failed_if_exists = false);
100
    Status delete_file(const Path& file);
101
    Status delete_directory(const Path& dir);
102
    Status batch_delete(const std::vector<Path>& files);
103
    Status exists(const Path& path, bool* res) const;
104
    Status file_size(const Path& file, int64_t* file_size) const;
105
    Status list(const Path& dir, bool only_file, std::vector<FileInfo>* files, bool* exists);
106
    Status rename(const Path& orig_name, const Path& new_name);
107
108
4.75M
    const std::string& id() const { return _id; }
109
    // file system type
110
1.67M
    FileSystemType type() const { return _type; }
111
112
1.48M
    virtual ~FileSystem() = default;
113
114
    // Each derived class should implement create method to create fs.
115
    FileSystem(const FileSystem&) = delete;
116
    const FileSystem& operator=(const FileSystem&) = delete;
117
118
protected:
119
    /// create file and return a FileWriter
120
    virtual Status create_file_impl(const Path& file, FileWriterPtr* writer,
121
                                    const FileWriterOptions* opts) = 0;
122
123
    /// open file and return a FileReader
124
    virtual Status open_file_impl(const Path& file, FileReaderSPtr* reader,
125
                                  const FileReaderOptions* opts) = 0;
126
127
    /// create directory recursively
128
    virtual Status create_directory_impl(const Path& dir, bool failed_if_exists = false) = 0;
129
130
    /// delete file.
131
    /// return OK if file does not exist
132
    /// return ERR if not a regular file
133
    virtual Status delete_file_impl(const Path& file) = 0;
134
135
    /// delete all files in "files"
136
    virtual Status batch_delete_impl(const std::vector<Path>& files) = 0;
137
138
    /// remove all under directory recursively
139
    /// return OK if dir does not exist
140
    /// return ERR if not a dir
141
    virtual Status delete_directory_impl(const Path& dir) = 0;
142
143
    /// check if path exist
144
    /// return OK and res = 1 means exist, res = 0 means does not exist
145
    /// return ERR otherwise
146
    virtual Status exists_impl(const Path& path, bool* res) const = 0;
147
148
    /// return OK and get size of given file, save in "file_size".
149
    /// return ERR otherwise
150
    virtual Status file_size_impl(const Path& file, int64_t* file_size) const = 0;
151
152
    /// return OK and list all objects in "dir", save in "files"
153
    /// return ERR otherwise
154
    /// will not traverse dir recursively.
155
    /// if "only_file" is true, will only return regular files, otherwise, return files and subdirs.
156
    /// the existence of dir will be saved in "exists"
157
    /// if "dir" does not exist, it will return Status::OK, but "exists" will to false
158
    virtual Status list_impl(const Path& dir, bool only_file, std::vector<FileInfo>* files,
159
                             bool* exists) = 0;
160
161
    /// rename file from orig_name to new_name
162
    virtual Status rename_impl(const Path& orig_name, const Path& new_name) = 0;
163
164
    // FIMXE(plat1ko): The implementation and semantics of this function are not completely
165
    // consistent, which is confused.
166
    virtual Status absolute_path(const Path& path, Path& abs_path) const = 0;
167
168
1.47M
    FileSystem(std::string id, FileSystemType type) : _id(std::move(id)), _type(type) {}
169
170
    // FS uid, corresponds to the `StorageResource` id generated by FE or MetaService.
171
    // Although resource id in FE is int64 now, we use string type for flexible extensibility.
172
    std::string _id;
173
    FileSystemType _type;
174
};
175
176
using FileSystemSPtr = std::shared_ptr<FileSystem>;
177
178
} // namespace doris::io