Coverage Report

Created: 2026-03-14 13:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/local_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 <cstdint>
21
#include <ctime>
22
#include <functional>
23
#include <memory>
24
#include <string>
25
#include <vector>
26
27
#include "common/status.h"
28
#include "io/fs/file_system.h"
29
#include "io/fs/path.h"
30
31
namespace doris::io {
32
33
class LocalFileSystem final : public FileSystem {
34
public:
35
    ~LocalFileSystem() override;
36
37
    static Status convert_to_abs_path(const Path& path, Path& abs_path);
38
39
    /// hard link dest file to src file
40
    Status link_file(const Path& src, const Path& dest);
41
42
    // Canonicalize 'path' by applying the following conversions:
43
    // - Converts a relative path into an absolute one using the cwd.
44
    // - Converts '.' and '..' references.
45
    // - Resolves all symbolic links.
46
    //
47
    // All directory entries in 'path' must exist on the filesystem.
48
    Status canonicalize(const Path& path, std::string* real_path);
49
    // Check if the given path is directory
50
    Status is_directory(const Path& path, bool* res);
51
    // Calc md5sum of given file
52
    Status md5sum(const Path& file, std::string* md5sum);
53
    // iterate the given dir and execute cb on each entry
54
    Status iterate_directory(const std::string& dir,
55
                             const std::function<bool(const FileInfo&)>& cb);
56
    // return disk available space where the given path is.
57
    Status get_space_info(const Path& path, size_t* capacity, size_t* available);
58
    // Copy src path to dest path. If `src` is a directory, this method will call recursively for each directory entry.
59
    Status copy_path(const Path& src, const Path& dest);
60
    // return true if parent path contain sub path
61
    static bool contain_path(const Path& parent, const Path& sub);
62
    // delete dir or file
63
    Status delete_directory_or_file(const Path& path);
64
    // change the file permission of the given path
65
    Status permission(const Path& file, std::filesystem::perms prms);
66
67
    static std::filesystem::perms PERMS_OWNER_RW;
68
69
    Status canonicalize_local_file(const std::string& dir, const std::string& file_path,
70
                                   std::string* full_path);
71
72
    // glob list the files match the path pattern.
73
    // the result will be saved in "res", in absolute path with file size.
74
    // "safe" means the path will be concat with the path prefix config::user_files_secure_path,
75
    // so that it can not list any files outside the config::user_files_secure_path
76
    Status safe_glob(const std::string& path, std::vector<FileInfo>* res);
77
    Status directory_size(const Path& dir_path, size_t* dir_size);
78
79
protected:
80
    Status create_file_impl(const Path& file, FileWriterPtr* writer,
81
                            const FileWriterOptions* opts) override;
82
    Status open_file_impl(const Path& file, FileReaderSPtr* reader,
83
                          const FileReaderOptions* opts) override;
84
    Status create_directory_impl(const Path& dir, bool failed_if_exists = false) override;
85
    Status delete_file_impl(const Path& file) override;
86
    Status delete_directory_impl(const Path& dir) override;
87
    Status delete_directory_or_file_impl(const Path& path);
88
    Status batch_delete_impl(const std::vector<Path>& files) override;
89
    Status exists_impl(const Path& path, bool* res) const override;
90
    Status file_size_impl(const Path& file, int64_t* file_size) const override;
91
    Status list_impl(const Path& dir, bool only_file, std::vector<FileInfo>* files,
92
                     bool* exists) override;
93
    Status rename_impl(const Path& orig_name, const Path& new_name) override;
94
    Status link_file_impl(const Path& src, const Path& dest);
95
    Status md5sum_impl(const Path& file, std::string* md5sum);
96
    Status iterate_directory_impl(const std::string& dir,
97
                                  const std::function<bool(const FileInfo&)>& cb);
98
    Status get_space_info_impl(const Path& path, size_t* capacity, size_t* available);
99
    Status copy_path_impl(const Path& src, const Path& dest);
100
    Status permission_impl(const Path& file, std::filesystem::perms prms);
101
102
private:
103
    // a wrapper for glob(), return file list in "res"
104
    Status _glob(const std::string& pattern, std::vector<std::string>* res);
105
    LocalFileSystem();
106
107
    // `LocalFileSystem` always use absolute path as arguments
108
    // FIXME(plat1ko): Eliminate this method
109
2.53M
    Status absolute_path(const Path& path, Path& abs_path) const override {
110
2.53M
        return convert_to_abs_path(path, abs_path);
111
2.53M
    }
112
113
    friend const std::shared_ptr<LocalFileSystem>& global_local_filesystem();
114
};
115
116
const std::shared_ptr<LocalFileSystem>& global_local_filesystem();
117
118
} // namespace doris::io