Coverage Report

Created: 2026-10-09 18:49

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
    // return inode statistics of the file system where the given path is.
59
    // `total` is the number of inodes in the file system and `available` is the number of free
60
    // inodes usable by unprivileged processes. Some file systems (e.g. btrfs) allocate inodes
61
    // dynamically and report `total` as 0, callers must treat that as "unknown".
62
    Status get_inode_info(const Path& path, size_t* total, size_t* available);
63
    // Copy src path to dest path. If `src` is a directory, this method will call recursively for each directory entry.
64
    Status copy_path(const Path& src, const Path& dest);
65
    // return true if parent path contain sub path
66
    static bool contain_path(const Path& parent, const Path& sub);
67
    // return true if `parent` equals `child` or contains it as a descendant path.
68
    static bool equal_or_sub_path(const Path& parent, const Path& child);
69
    // delete dir or file
70
    Status delete_directory_or_file(const Path& path);
71
    // delete directory only if it is empty
72
    Status delete_empty_directory(const Path& dir);
73
    // change the file permission of the given path
74
    Status permission(const Path& file, std::filesystem::perms prms);
75
76
    static std::filesystem::perms PERMS_OWNER_RW;
77
78
    Status canonicalize_local_file(const std::string& dir, const std::string& file_path,
79
                                   std::string* full_path);
80
81
    // glob list the files match the path pattern.
82
    // the result will be saved in "res", in absolute path with file size.
83
    // "safe" means the path will be concat with the path prefix config::user_files_secure_path,
84
    // so that it can not list any files outside the config::user_files_secure_path
85
    Status safe_glob(const std::string& path, std::vector<FileInfo>* res);
86
    Status directory_size(const Path& dir_path, size_t* dir_size);
87
88
protected:
89
    Status create_file_impl(const Path& file, FileWriterPtr* writer,
90
                            const FileWriterOptions* opts) override;
91
    Status open_file_impl(const Path& file, FileReaderSPtr* reader,
92
                          const FileReaderOptions* opts) override;
93
    Status create_directory_impl(const Path& dir, bool failed_if_exists = false) override;
94
    Status delete_file_impl(const Path& file) override;
95
    Status delete_directory_impl(const Path& dir) override;
96
    Status delete_directory_or_file_impl(const Path& path);
97
    Status delete_empty_directory_impl(const Path& dir);
98
    Status batch_delete_impl(const std::vector<Path>& files) override;
99
    Status exists_impl(const Path& path, bool* res) const override;
100
    Status file_size_impl(const Path& file, int64_t* file_size) const override;
101
    Status list_impl(const Path& dir, bool only_file, std::vector<FileInfo>* files,
102
                     bool* exists) override;
103
    Status rename_impl(const Path& orig_name, const Path& new_name) override;
104
    Status link_file_impl(const Path& src, const Path& dest);
105
    Status md5sum_impl(const Path& file, std::string* md5sum);
106
    Status iterate_directory_impl(const std::string& dir,
107
                                  const std::function<bool(const FileInfo&)>& cb);
108
    Status get_space_info_impl(const Path& path, size_t* capacity, size_t* available);
109
    Status get_inode_info_impl(const Path& path, size_t* total, size_t* available);
110
    Status copy_path_impl(const Path& src, const Path& dest);
111
    Status permission_impl(const Path& file, std::filesystem::perms prms);
112
113
private:
114
    // a wrapper for glob(), return file list in "res"
115
    Status _glob(const std::string& pattern, std::vector<std::string>* res);
116
    LocalFileSystem();
117
118
    // `LocalFileSystem` always use absolute path as arguments
119
    // FIXME(plat1ko): Eliminate this method
120
766k
    Status absolute_path(const Path& path, Path& abs_path) const override {
121
766k
        return convert_to_abs_path(path, abs_path);
122
766k
    }
123
124
    friend const std::shared_ptr<LocalFileSystem>& global_local_filesystem();
125
};
126
127
const std::shared_ptr<LocalFileSystem>& global_local_filesystem();
128
129
} // namespace doris::io