Coverage Report

Created: 2026-03-13 09:37

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
6.41M
    do {                                                    \
37
6.41M
        Status _s;                                          \
38
6.43M
        if (bthread_self() == 0) {                          \
39
6.43M
            _s = (stmt);                                    \
40
18.4E
        } else {                                            \
41
18.4E
            auto task = [&] { _s = (stmt); };               \
Unexecuted instantiation: file_system.cpp:_ZZN5doris2io10FileSystem11create_fileERKNSt10filesystem7__cxx114pathEPSt10unique_ptrINS0_10FileWriterESt14default_deleteIS8_EEPKNS0_17FileWriterOptionsEENK3$_0clEv
Unexecuted instantiation: file_system.cpp:_ZZN5doris2io10FileSystem9open_fileERKNSt10filesystem7__cxx114pathEPSt10shared_ptrINS0_10FileReaderEEPKNS0_17FileReaderOptionsEENK3$_0clEv
Unexecuted instantiation: file_system.cpp:_ZZN5doris2io10FileSystem16create_directoryERKNSt10filesystem7__cxx114pathEbENK3$_0clEv
file_system.cpp:_ZZN5doris2io10FileSystem11delete_fileERKNSt10filesystem7__cxx114pathEENK3$_0clEv
Line
Count
Source
41
3
            auto task = [&] { _s = (stmt); };               \
Unexecuted instantiation: file_system.cpp:_ZZN5doris2io10FileSystem16delete_directoryERKNSt10filesystem7__cxx114pathEENK3$_0clEv
Unexecuted instantiation: file_system.cpp:_ZZN5doris2io10FileSystem12batch_deleteERKSt6vectorINSt10filesystem7__cxx114pathESaIS5_EEENK3$_0clEv
Unexecuted instantiation: file_system.cpp:_ZZNK5doris2io10FileSystem6existsERKNSt10filesystem7__cxx114pathEPbENK3$_0clEv
Unexecuted instantiation: file_system.cpp:_ZZNK5doris2io10FileSystem9file_sizeERKNSt10filesystem7__cxx114pathEPlENK3$_0clEv
Unexecuted instantiation: file_system.cpp:_ZZN5doris2io10FileSystem4listERKNSt10filesystem7__cxx114pathEbPSt6vectorINS0_8FileInfoESaIS8_EEPbENK3$_0clEv
Unexecuted instantiation: file_system.cpp:_ZZN5doris2io10FileSystem6renameERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: local_file_system.cpp:_ZZN5doris2io15LocalFileSystem24delete_directory_or_fileERKNSt10filesystem7__cxx114pathEENK3$_0clEv
Unexecuted instantiation: local_file_system.cpp:_ZZN5doris2io15LocalFileSystem9link_fileERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: local_file_system.cpp:_ZZN5doris2io15LocalFileSystem6md5sumERKNSt10filesystem7__cxx114pathEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEENK3$_0clEv
Unexecuted instantiation: local_file_system.cpp:_ZZN5doris2io15LocalFileSystem17iterate_directoryERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt8functionIFbRKNS0_8FileInfoEEEENK3$_0clEv
Unexecuted instantiation: local_file_system.cpp:_ZZN5doris2io15LocalFileSystem14get_space_infoERKNSt10filesystem7__cxx114pathEPmS7_ENK3$_0clEv
Unexecuted instantiation: local_file_system.cpp:_ZZN5doris2io15LocalFileSystem9copy_pathERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: local_file_system.cpp:_ZZN5doris2io15LocalFileSystem10permissionERKNSt10filesystem7__cxx114pathENS2_5permsEENK3$_0clEv
Unexecuted instantiation: remote_file_system.cpp:_ZZN5doris2io16RemoteFileSystem6uploadERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
Unexecuted instantiation: remote_file_system.cpp:_ZZN5doris2io16RemoteFileSystem12batch_uploadERKSt6vectorINSt10filesystem7__cxx114pathESaIS5_EES9_ENK3$_0clEv
Unexecuted instantiation: remote_file_system.cpp:_ZZN5doris2io16RemoteFileSystem8downloadERKNSt10filesystem7__cxx114pathES6_ENK3$_0clEv
42
18.4E
            AsyncIO::run_task(task, _type);                 \
43
18.4E
        }                                                   \
44
6.41M
        if (!_s) {                                          \
45
29.1k
            _s = Status::Error<false>(_s.code(), _s.msg()); \
46
29.1k
        }                                                   \
47
6.41M
        return _s;                                          \
48
6.41M
    } while (0);
49
#endif
50
51
enum class FileSystemType : uint8_t { LOCAL, S3, HDFS, BROKER, HTTP };
52
53
0
inline std::ostream& operator<<(std::ostream& ostr, FileSystemType type) {
54
0
    switch (type) {
55
0
    case FileSystemType::LOCAL:
56
0
        ostr << "LOCAL";
57
0
        return ostr;
58
0
    case FileSystemType::S3:
59
0
        ostr << "S3";
60
0
        return ostr;
61
0
    case FileSystemType::HDFS:
62
0
        ostr << "HDFS";
63
0
        return ostr;
64
0
    case FileSystemType::BROKER:
65
0
        ostr << "BROKER";
66
0
        return ostr;
67
0
    case FileSystemType::HTTP:
68
0
        ostr << "HTTP";
69
0
        return ostr;
70
0
    default:
71
0
        ostr << "UNKNOWN";
72
0
        return ostr;
73
0
    }
74
0
}
75
76
struct FileInfo {
77
    // only file name, no path
78
    std::string file_name;
79
    int64_t file_size;
80
    bool is_file;
81
};
82
83
// `FileSystem` providing an interface to access FS metadata. Each `RemoteFileSystem` instance
84
// (except temporary Remote FS) corresponds to a `StorageResource` configured in FE.
85
class FileSystem {
86
public:
87
    // Sometimes `FileSystem` instance maybe a temporary instance which has no corresponding
88
    // Resource, then use `TMP_FS_ID` as FS id.
89
    inline static const std::string TMP_FS_ID;
90
91
    // The following are public interface.
92
    // And derived classes should implement all xxx_impl methods.
93
    Status create_file(const Path& file, FileWriterPtr* writer,
94
                       const FileWriterOptions* opts = nullptr);
95
    Status open_file(const Path& file, FileReaderSPtr* reader,
96
                     const FileReaderOptions* opts = nullptr);
97
    Status create_directory(const Path& dir, bool failed_if_exists = false);
98
    Status delete_file(const Path& file);
99
    Status delete_directory(const Path& dir);
100
    Status batch_delete(const std::vector<Path>& files);
101
    Status exists(const Path& path, bool* res) const;
102
    Status file_size(const Path& file, int64_t* file_size) const;
103
    Status list(const Path& dir, bool only_file, std::vector<FileInfo>* files, bool* exists);
104
    Status rename(const Path& orig_name, const Path& new_name);
105
106
2.98M
    const std::string& id() const { return _id; }
107
    // file system type
108
1.00M
    FileSystemType type() const { return _type; }
109
110
944k
    virtual ~FileSystem() = default;
111
112
    // Each derived class should implement create method to create fs.
113
    FileSystem(const FileSystem&) = delete;
114
    const FileSystem& operator=(const FileSystem&) = delete;
115
116
protected:
117
    /// create file and return a FileWriter
118
    virtual Status create_file_impl(const Path& file, FileWriterPtr* writer,
119
                                    const FileWriterOptions* opts) = 0;
120
121
    /// open file and return a FileReader
122
    virtual Status open_file_impl(const Path& file, FileReaderSPtr* reader,
123
                                  const FileReaderOptions* opts) = 0;
124
125
    /// create directory recursively
126
    virtual Status create_directory_impl(const Path& dir, bool failed_if_exists = false) = 0;
127
128
    /// delete file.
129
    /// return OK if file does not exist
130
    /// return ERR if not a regular file
131
    virtual Status delete_file_impl(const Path& file) = 0;
132
133
    /// delete all files in "files"
134
    virtual Status batch_delete_impl(const std::vector<Path>& files) = 0;
135
136
    /// remove all under directory recursively
137
    /// return OK if dir does not exist
138
    /// return ERR if not a dir
139
    virtual Status delete_directory_impl(const Path& dir) = 0;
140
141
    /// check if path exist
142
    /// return OK and res = 1 means exist, res = 0 means does not exist
143
    /// return ERR otherwise
144
    virtual Status exists_impl(const Path& path, bool* res) const = 0;
145
146
    /// return OK and get size of given file, save in "file_size".
147
    /// return ERR otherwise
148
    virtual Status file_size_impl(const Path& file, int64_t* file_size) const = 0;
149
150
    /// return OK and list all objects in "dir", save in "files"
151
    /// return ERR otherwise
152
    /// will not traverse dir recursively.
153
    /// if "only_file" is true, will only return regular files, otherwise, return files and subdirs.
154
    /// the existence of dir will be saved in "exists"
155
    /// if "dir" does not exist, it will return Status::OK, but "exists" will to false
156
    virtual Status list_impl(const Path& dir, bool only_file, std::vector<FileInfo>* files,
157
                             bool* exists) = 0;
158
159
    /// rename file from orig_name to new_name
160
    virtual Status rename_impl(const Path& orig_name, const Path& new_name) = 0;
161
162
    // FIMXE(plat1ko): The implementation and semantics of this function are not completely
163
    // consistent, which is confused.
164
    virtual Status absolute_path(const Path& path, Path& abs_path) const = 0;
165
166
939k
    FileSystem(std::string id, FileSystemType type) : _id(std::move(id)), _type(type) {}
167
168
    // FS uid, corresponds to the `StorageResource` id generated by FE or MetaService.
169
    // Although resource id in FE is int64 now, we use string type for flexible extensibility.
170
    std::string _id;
171
    FileSystemType _type;
172
};
173
174
using FileSystemSPtr = std::shared_ptr<FileSystem>;
175
176
} // namespace doris::io