Coverage Report

Created: 2026-05-29 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/local_file_system.cpp
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
#include "io/fs/local_file_system.h"
19
20
#include <fcntl.h>
21
#include <fmt/format.h>
22
#include <glob.h>
23
#include <glog/logging.h>
24
#include <openssl/md5.h>
25
#include <sys/mman.h>
26
#include <sys/stat.h>
27
#include <unistd.h>
28
29
#include <filesystem>
30
#include <iomanip>
31
#include <istream>
32
#include <system_error>
33
#include <utility>
34
35
#include "common/exception.h"
36
#include "cpp/sync_point.h"
37
#include "io/fs/err_utils.h"
38
#include "io/fs/file_system.h"
39
#include "io/fs/file_writer.h"
40
#include "io/fs/local_file_reader.h"
41
#include "io/fs/local_file_writer.h"
42
#include "runtime/thread_context.h"
43
#include "storage/data_dir.h"
44
#include "util/async_io.h" // IWYU pragma: keep
45
#include "util/debug_points.h"
46
#include "util/defer_op.h"
47
48
namespace doris::io {
49
50
std::filesystem::perms LocalFileSystem::PERMS_OWNER_RW =
51
        std::filesystem::perms::owner_read | std::filesystem::perms::owner_write;
52
53
8
LocalFileSystem::LocalFileSystem() : FileSystem(FileSystem::TMP_FS_ID, FileSystemType::LOCAL) {}
54
55
3
LocalFileSystem::~LocalFileSystem() = default;
56
57
Status LocalFileSystem::create_file_impl(const Path& file, FileWriterPtr* writer,
58
128k
                                         const FileWriterOptions* opts) {
59
128k
    VLOG_DEBUG << "create file: " << file.native()
60
0
               << ", sync_data: " << (opts ? opts->sync_file_data : true);
61
128k
    TEST_SYNC_POINT_RETURN_WITH_VALUE("LocalFileSystem::create_file_impl",
62
128k
                                      Status::IOError("inject io error"));
63
    // O_TRUNC: if file already exists (last tmp), clear the content
64
128k
    int fd = ::open(file.c_str(), O_TRUNC | O_WRONLY | O_CREAT | O_CLOEXEC, 0666);
65
128k
    DBUG_EXECUTE_IF("LocalFileSystem.create_file_impl.open_file_failed", {
66
        // spare '.testfile' to make bad disk checker happy
67
128k
        auto sub_path = dp->param<std::string>("sub_path", "");
68
128k
        if ((sub_path.empty() && file.filename().compare(kTestFilePath)) ||
69
128k
            (!sub_path.empty() && file.native().find(sub_path) != std::string::npos)) {
70
128k
            ::close(fd);
71
128k
            fd = -1;
72
128k
            errno = EIO;
73
128k
            LOG(WARNING) << Status::IOError("debug open io error: {}", file.native());
74
128k
        }
75
128k
    });
76
128k
    if (-1 == fd) {
77
3
        return localfs_error(errno, fmt::format("failed to create file {}", file.native()));
78
3
    }
79
128k
    bool sync_data = opts != nullptr ? opts->sync_file_data : true;
80
128k
    *writer = std::make_unique<LocalFileWriter>(file, fd, sync_data);
81
128k
    return Status::OK();
82
128k
}
83
84
Status LocalFileSystem::open_file_impl(const Path& file, FileReaderSPtr* reader,
85
123k
                                       const FileReaderOptions* opts) {
86
123k
    TEST_SYNC_POINT_RETURN_WITH_VALUE("LocalFileSystem::open_file_impl",
87
123k
                                      Status::IOError("inject io error"));
88
123k
    int64_t fsize = opts ? opts->file_size : -1;
89
123k
    if (fsize < 0) {
90
122k
        RETURN_IF_ERROR(file_size_impl(file, &fsize));
91
122k
    }
92
116k
    int fd = -1;
93
116k
    RETRY_ON_EINTR(fd, open(file.c_str(), O_RDONLY));
94
116k
    DBUG_EXECUTE_IF("LocalFileSystem.create_file_impl.open_file_failed", {
95
        // spare '.testfile' to make bad disk checker happy
96
116k
        auto sub_path = dp->param<std::string>("sub_path", "");
97
116k
        if ((sub_path.empty() && file.filename().compare(kTestFilePath)) ||
98
116k
            (!sub_path.empty() && file.native().find(sub_path) != std::string::npos)) {
99
116k
            ::close(fd);
100
116k
            fd = -1;
101
116k
            errno = EIO;
102
116k
            LOG(WARNING) << Status::IOError("debug open io error: {}", file.native());
103
116k
        }
104
116k
    });
105
116k
    if (fd < 0) {
106
0
        return localfs_error(errno, fmt::format("failed to open {}", file.native()));
107
0
    }
108
116k
    *reader = std::make_shared<LocalFileReader>(file, fsize, fd);
109
116k
    return Status::OK();
110
116k
}
111
112
240k
Status LocalFileSystem::create_directory_impl(const Path& dir, bool failed_if_exists) {
113
18.4E
    VLOG_DEBUG << "create directory: " << dir.native()
114
18.4E
               << ", failed_if_exists: " << failed_if_exists;
115
240k
    bool exists = true;
116
240k
    RETURN_IF_ERROR(exists_impl(dir, &exists));
117
240k
    if (exists && failed_if_exists) {
118
0
        return Status::AlreadyExist("failed to create {}, already exists", dir.native());
119
0
    }
120
240k
    if (!exists) {
121
228k
        std::error_code ec;
122
228k
        std::filesystem::create_directories(dir, ec);
123
228k
        if (ec) {
124
0
            return localfs_error(ec, fmt::format("failed to create {}", dir.native()));
125
0
        }
126
228k
    }
127
240k
    return Status::OK();
128
240k
}
129
130
251k
Status LocalFileSystem::delete_file_impl(const Path& file) {
131
251k
    VLOG_DEBUG << "delete file: " << file.native();
132
251k
    bool exists = true;
133
251k
    RETURN_IF_ERROR(exists_impl(file, &exists));
134
251k
    if (!exists) {
135
126k
        return Status::OK();
136
126k
    }
137
124k
    if (!std::filesystem::is_regular_file(file)) {
138
1
        return Status::InternalError("failed to delete {}, not a file", file.native());
139
1
    }
140
124k
    std::error_code ec;
141
124k
    std::filesystem::remove(file, ec);
142
124k
    if (ec) {
143
0
        return localfs_error(ec, fmt::format("failed to delete {}", file.native()));
144
0
    }
145
124k
    return Status::OK();
146
124k
}
147
148
7.09k
Status LocalFileSystem::delete_directory_impl(const Path& dir) {
149
7.09k
    VLOG_DEBUG << "delete directory: " << dir.native();
150
7.09k
    bool exists = true;
151
7.09k
    RETURN_IF_ERROR(exists_impl(dir, &exists));
152
7.09k
    if (!exists) {
153
990
        return Status::OK();
154
990
    }
155
6.10k
    if (!std::filesystem::is_directory(dir)) {
156
1
        return Status::InternalError("failed to delete {}, not a directory", dir.native());
157
1
    }
158
6.10k
    std::error_code ec;
159
6.10k
    std::filesystem::remove_all(dir, ec);
160
6.10k
    if (ec) {
161
0
        return localfs_error(ec, fmt::format("failed to delete {}", dir.native()));
162
0
    }
163
6.10k
    return Status::OK();
164
6.10k
}
165
166
18
Status LocalFileSystem::delete_directory_or_file(const Path& path) {
167
18
    FILESYSTEM_M(delete_directory_or_file_impl(path));
168
0
}
169
170
116k
Status LocalFileSystem::delete_empty_directory(const Path& dir) {
171
116k
    FILESYSTEM_M(delete_empty_directory_impl(dir));
172
0
}
173
174
116k
Status LocalFileSystem::delete_empty_directory_impl(const Path& dir) {
175
116k
    Path path;
176
116k
    RETURN_IF_ERROR(absolute_path(dir, path));
177
116k
    VLOG_DEBUG << "delete empty directory: " << path.native();
178
116k
    int ret = 0;
179
116k
    RETRY_ON_EINTR(ret, rmdir(path.c_str()));
180
116k
    if (ret != 0) {
181
4
        std::error_code ec(errno, std::generic_category());
182
4
        if (ec == std::errc::no_such_file_or_directory) {
183
1
            return Status::OK();
184
1
        }
185
3
        return localfs_error(ec, fmt::format("failed to delete empty directory {}", path.native()));
186
4
    }
187
116k
    return Status::OK();
188
116k
}
189
190
18
Status LocalFileSystem::delete_directory_or_file_impl(const Path& path) {
191
18
    bool is_dir;
192
18
    RETURN_IF_ERROR(is_directory(path, &is_dir));
193
18
    if (is_dir) {
194
1
        return delete_directory_impl(path);
195
17
    } else {
196
17
        return delete_file_impl(path);
197
17
    }
198
18
}
199
200
0
Status LocalFileSystem::batch_delete_impl(const std::vector<Path>& files) {
201
0
    for (auto& file : files) {
202
0
        RETURN_IF_ERROR(delete_file_impl(file));
203
0
    }
204
0
    return Status::OK();
205
0
}
206
207
1.74M
Status LocalFileSystem::exists_impl(const Path& path, bool* res) const {
208
1.74M
    std::error_code ec;
209
1.74M
    *res = std::filesystem::exists(path, ec);
210
1.74M
    if (ec) {
211
0
        return localfs_error(ec, fmt::format("failed to check exists {}", path.native()));
212
0
    }
213
1.74M
    return Status::OK();
214
1.74M
}
215
216
137k
Status LocalFileSystem::file_size_impl(const Path& file, int64_t* file_size) const {
217
137k
    std::error_code ec;
218
137k
    *file_size = std::filesystem::file_size(file, ec);
219
137k
    if (ec) {
220
7.67k
        return localfs_error(ec, fmt::format("failed to get file size {}", file.native()));
221
7.67k
    }
222
129k
    return Status::OK();
223
137k
}
224
225
62.8k
Status LocalFileSystem::directory_size(const Path& dir_path, size_t* dir_size) {
226
62.8k
    *dir_size = 0;
227
62.8k
    if (std::filesystem::exists(dir_path) && std::filesystem::is_directory(dir_path)) {
228
1.02M
        for (const auto& entry : std::filesystem::recursive_directory_iterator(dir_path)) {
229
1.02M
            if (std::filesystem::is_regular_file(entry)) {
230
5.98k
                *dir_size += std::filesystem::file_size(entry);
231
5.98k
            }
232
1.02M
        }
233
62.8k
        return Status::OK();
234
62.8k
    }
235
    // TODO(plat1ko): Use error code according to std::error_code
236
0
    return Status::InternalError("faile to get dir size {}", dir_path.native());
237
62.8k
}
238
239
Status LocalFileSystem::list_impl(const Path& dir, bool only_file, std::vector<FileInfo>* files,
240
833k
                                  bool* exists) {
241
833k
    RETURN_IF_ERROR(exists_impl(dir, exists));
242
833k
    if (!(*exists)) {
243
120k
        return Status::OK();
244
120k
    }
245
713k
    std::error_code ec;
246
713k
    try {
247
764k
        for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) {
248
764k
            if (only_file && !entry.is_regular_file()) {
249
132
                continue;
250
132
            }
251
764k
            FileInfo file_info;
252
764k
            file_info.file_name = entry.path().filename();
253
764k
            file_info.is_file = entry.is_regular_file(ec);
254
764k
            if (ec) {
255
0
                break;
256
0
            }
257
764k
            if (file_info.is_file) {
258
186k
                file_info.file_size = entry.file_size(ec);
259
186k
                if (ec) {
260
2
                    break;
261
2
                }
262
186k
            }
263
764k
            files->push_back(std::move(file_info));
264
764k
        }
265
713k
    } catch (const std::filesystem::filesystem_error& e) {
266
        // although `directory_iterator(dir, ec)` does not throw an exception,
267
        // it may throw an exception during iterator++, so we need to catch the exception here
268
0
        return localfs_error(e.code(), fmt::format("failed to list {}, error message: {}",
269
0
                                                   dir.native(), e.what()));
270
0
    }
271
713k
    if (ec) {
272
3
        return localfs_error(ec, fmt::format("failed to list {}", dir.native()));
273
3
    }
274
713k
    return Status::OK();
275
713k
}
276
277
123k
Status LocalFileSystem::rename_impl(const Path& orig_name, const Path& new_name) {
278
18.4E
    VLOG_DEBUG << "rename file: " << orig_name.native() << " to " << new_name.native();
279
123k
    TEST_SYNC_POINT_RETURN_WITH_VALUE("LocalFileSystem::rename",
280
123k
                                      Status::IOError("inject io error"));
281
123k
    std::error_code ec;
282
123k
    std::filesystem::rename(orig_name, new_name, ec);
283
123k
    if (ec) {
284
0
        return localfs_error(ec, fmt::format("failed to rename {} to {}", orig_name.native(),
285
0
                                             new_name.native()));
286
0
    }
287
123k
    return Status::OK();
288
123k
}
289
290
77
Status LocalFileSystem::link_file(const Path& src, const Path& dest) {
291
77
    FILESYSTEM_M(link_file_impl(src, dest));
292
0
}
293
294
77
Status LocalFileSystem::link_file_impl(const Path& src, const Path& dest) {
295
77
    VLOG_DEBUG << "link file: " << src.native() << " to " << dest.native();
296
77
    if (::link(src.c_str(), dest.c_str()) != 0) {
297
0
        return localfs_error(errno, fmt::format("failed to create hard link from {} to {}",
298
0
                                                src.native(), dest.native()));
299
0
    }
300
77
    return Status::OK();
301
77
}
302
303
187
Status LocalFileSystem::canonicalize(const Path& path, std::string* real_path) {
304
187
    std::error_code ec;
305
187
    Path res = std::filesystem::canonical(path, ec);
306
187
    if (ec) {
307
0
        return localfs_error(ec, fmt::format("failed to canonicalize {}", path.native()));
308
0
    }
309
187
    *real_path = res.string();
310
187
    return Status::OK();
311
187
}
312
313
30
Status LocalFileSystem::is_directory(const Path& path, bool* res) {
314
30
    std::error_code ec;
315
30
    *res = std::filesystem::is_directory(path, ec);
316
30
    if (ec) {
317
2
        return localfs_error(ec, fmt::format("failed to canonicalize {}", path.native()));
318
2
    }
319
28
    return Status::OK();
320
30
}
321
322
0
Status LocalFileSystem::md5sum(const Path& file, std::string* md5sum) {
323
0
    FILESYSTEM_M(md5sum_impl(file, md5sum));
324
0
}
325
326
0
Status LocalFileSystem::md5sum_impl(const Path& file, std::string* md5sum) {
327
0
    int fd = open(file.c_str(), O_RDONLY);
328
0
    if (fd < 0) {
329
0
        return localfs_error(errno,
330
0
                             fmt::format("failed to open file for md5sum {}", file.native()));
331
0
    }
332
333
0
    struct stat statbuf;
334
0
    if (fstat(fd, &statbuf) < 0) {
335
0
        std::string err = errno_to_str();
336
0
        close(fd);
337
0
        return localfs_error(errno, fmt::format("failed to stat file {}", file.native()));
338
0
    }
339
0
    size_t file_len = statbuf.st_size;
340
0
    void* buf = mmap(nullptr, file_len, PROT_READ, MAP_SHARED, fd, 0);
341
342
0
    unsigned char result[MD5_DIGEST_LENGTH];
343
0
    MD5((unsigned char*)buf, file_len, result);
344
0
    munmap(buf, file_len);
345
346
0
    std::stringstream ss;
347
0
    for (int32_t i = 0; i < MD5_DIGEST_LENGTH; i++) {
348
0
        ss << std::setfill('0') << std::setw(2) << std::hex << (int)result[i];
349
0
    }
350
0
    ss >> *md5sum;
351
352
0
    close(fd);
353
0
    return Status::OK();
354
0
}
355
356
Status LocalFileSystem::iterate_directory(const std::string& dir,
357
908
                                          const std::function<bool(const FileInfo& file)>& cb) {
358
908
    FILESYSTEM_M(iterate_directory_impl(dir, cb));
359
0
}
360
361
Status LocalFileSystem::iterate_directory_impl(
362
908
        const std::string& dir, const std::function<bool(const FileInfo& file)>& cb) {
363
908
    bool exists = true;
364
908
    std::vector<FileInfo> files;
365
908
    RETURN_IF_ERROR(list_impl(dir, false, &files, &exists));
366
908
    for (auto& file : files) {
367
69
        if (!cb(file)) {
368
2
            break;
369
2
        }
370
69
    }
371
908
    return Status::OK();
372
908
}
373
374
82.7k
Status LocalFileSystem::get_space_info(const Path& dir, size_t* capacity, size_t* available) {
375
82.7k
    FILESYSTEM_M(get_space_info_impl(dir, capacity, available));
376
0
}
377
378
82.5k
Status LocalFileSystem::get_space_info_impl(const Path& path, size_t* capacity, size_t* available) {
379
82.5k
    std::error_code ec;
380
82.5k
    std::filesystem::space_info info = std::filesystem::space(path, ec);
381
82.5k
    if (ec) {
382
15.9k
        return localfs_error(
383
15.9k
                ec, fmt::format("failed to get available space for path {}", path.native()));
384
15.9k
    }
385
66.5k
    *capacity = info.capacity;
386
66.5k
    *available = info.available;
387
66.5k
    return Status::OK();
388
82.5k
}
389
390
2
Status LocalFileSystem::copy_path(const Path& src, const Path& dest) {
391
2
    FILESYSTEM_M(copy_path_impl(src, dest));
392
0
}
393
394
2
Status LocalFileSystem::copy_path_impl(const Path& src, const Path& dest) {
395
2
    VLOG_DEBUG << "copy from " << src.native() << " to " << dest.native();
396
2
    std::error_code ec;
397
2
    std::filesystem::copy(src, dest, std::filesystem::copy_options::recursive, ec);
398
2
    if (ec) {
399
0
        return localfs_error(
400
0
                ec, fmt::format("failed to copy from {} to {}", src.native(), dest.native()));
401
0
    }
402
2
    return Status::OK();
403
2
}
404
405
32
bool LocalFileSystem::contain_path(const Path& parent_, const Path& sub_) {
406
32
    Path parent = parent_.lexically_normal();
407
32
    Path sub = sub_.lexically_normal();
408
32
    if (parent == sub) {
409
0
        return true;
410
0
    }
411
412
32
    if (parent.filename() == ".") {
413
0
        parent.remove_filename();
414
0
    }
415
416
    // We're also not interested in the file's name.
417
32
    if (sub.has_filename()) {
418
32
        sub.remove_filename();
419
32
    }
420
    // If dir has more components than file, then file can't possibly reside in dir.
421
32
    auto dir_len = std::distance(parent.begin(), parent.end());
422
32
    auto file_len = std::distance(sub.begin(), sub.end());
423
32
    if (dir_len > file_len) {
424
0
        return false;
425
0
    }
426
32
    auto p_it = parent.begin();
427
32
    auto s_it = sub.begin();
428
48
    for (; p_it != parent.end() && !p_it->string().empty(); ++p_it, ++s_it) {
429
16
        if (!(*p_it == *s_it)) {
430
0
            return false;
431
0
        }
432
16
    }
433
32
    return true;
434
32
}
435
436
8
bool LocalFileSystem::equal_or_sub_path(const Path& parent, const Path& child) {
437
8
    auto parent_path = parent.lexically_normal();
438
8
    auto child_path = child.lexically_normal();
439
8
    auto parent_it = parent_path.begin();
440
8
    auto child_it = child_path.begin();
441
35
    for (; parent_it != parent_path.end() && child_it != child_path.end();
442
28
         ++parent_it, ++child_it) {
443
28
        if (*parent_it != *child_it) {
444
1
            return false;
445
1
        }
446
28
    }
447
7
    return parent_it == parent_path.end();
448
8
}
449
450
1.00M
const std::shared_ptr<LocalFileSystem>& global_local_filesystem() {
451
1.00M
    static std::shared_ptr<LocalFileSystem> local_fs(new LocalFileSystem());
452
1.00M
    return local_fs;
453
1.00M
}
454
455
Status LocalFileSystem::canonicalize_local_file(const std::string& dir,
456
                                                const std::string& file_path,
457
28
                                                std::string* full_path) {
458
28
    const std::string absolute_path = dir + "/" + file_path;
459
28
    std::string canonical_path;
460
28
    RETURN_IF_ERROR(canonicalize(absolute_path, &canonical_path));
461
28
    if (!contain_path(dir, canonical_path)) {
462
0
        return Status::InvalidArgument("file path is not allowed: {}", canonical_path);
463
0
    }
464
465
28
    *full_path = canonical_path;
466
28
    return Status::OK();
467
28
}
468
469
31
Status LocalFileSystem::safe_glob(const std::string& path, std::vector<FileInfo>* res) {
470
31
    if (path.find("..") != std::string::npos) {
471
1
        return Status::InvalidArgument("can not contain '..' in path");
472
1
    }
473
30
    std::string full_path = config::user_files_secure_path + "/" + path;
474
30
    std::vector<std::string> files;
475
30
    RETURN_IF_ERROR(_glob(full_path, &files));
476
28
    for (auto& file : files) {
477
28
        FileInfo fi;
478
28
        fi.is_file = true;
479
28
        RETURN_IF_ERROR(canonicalize_local_file("", file, &(fi.file_name)));
480
28
        RETURN_IF_ERROR(file_size_impl(fi.file_name, &(fi.file_size)));
481
28
        res->push_back(std::move(fi));
482
28
    }
483
24
    return Status::OK();
484
24
}
485
486
30
Status LocalFileSystem::_glob(const std::string& pattern, std::vector<std::string>* res) {
487
30
    glob_t glob_result;
488
30
    memset(&glob_result, 0, sizeof(glob_result));
489
490
30
    int rc = glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
491
30
    if (rc != 0) {
492
6
        globfree(&glob_result);
493
6
        return Status::InternalError("failed to glob {}: {}", pattern, glob_err_to_str(rc));
494
6
    }
495
496
52
    for (size_t i = 0; i < glob_result.gl_pathc; ++i) {
497
28
        res->push_back(std::string(glob_result.gl_pathv[i]));
498
28
    }
499
500
24
    globfree(&glob_result);
501
24
    return Status::OK();
502
30
}
503
504
36
Status LocalFileSystem::permission(const Path& file, std::filesystem::perms prms) {
505
36
    FILESYSTEM_M(permission_impl(file, prms));
506
0
}
507
508
36
Status LocalFileSystem::permission_impl(const Path& file, std::filesystem::perms prms) {
509
36
    std::error_code ec;
510
36
    std::filesystem::permissions(file, prms, ec);
511
36
    if (ec) {
512
0
        return localfs_error(ec, fmt::format("failed to change file permission {}", file.native()));
513
0
    }
514
36
    return Status::OK();
515
36
}
516
517
2.39M
Status LocalFileSystem::convert_to_abs_path(const Path& input_path_str, Path& abs_path) {
518
    // valid path include:
519
    //   1. abc/def                         will return abc/def
520
    //   2. /abc/def                        will return /abc/def
521
    //   3. file:/abc/def                   will return /abc/def
522
    //   4. file://<authority>/abc/def      will return /abc/def
523
2.39M
    std::string path_str = input_path_str;
524
2.39M
    size_t slash = path_str.find('/');
525
2.39M
    if (slash == 0) {
526
2.32M
        abs_path = input_path_str;
527
2.32M
        return Status::OK();
528
2.32M
    }
529
530
    // Initialize scheme and authority
531
73.3k
    std::string scheme;
532
73.3k
    size_t start = 0;
533
534
    // Parse URI scheme
535
73.3k
    size_t colon = path_str.find(':');
536
73.3k
    if (colon != std::string::npos && (slash == std::string::npos || colon < slash)) {
537
        // Has a scheme
538
10
        scheme = path_str.substr(0, colon);
539
10
        if (scheme != "file") {
540
3
            return Status::InternalError(
541
3
                    "Only supports `file` type scheme, like 'file:///path', 'file:/path'.");
542
3
        }
543
7
        start = colon + 1;
544
7
    }
545
546
    // Parse URI authority, if any
547
73.3k
    if (path_str.compare(start, 2, "//") == 0 && path_str.length() - start > 2) {
548
        // Has authority
549
        // such as : path_str = "file://authority/abc/def"
550
        // and now : start = 5
551
6
        size_t next_slash = path_str.find('/', start + 2);
552
        // now : next_slash = 16
553
6
        if (next_slash == std::string::npos) {
554
1
            return Status::InternalError(
555
1
                    "This input string only has authority, but has no path information");
556
1
        }
557
        // We will skit authority
558
        // now : start = 16
559
5
        start = next_slash;
560
5
    }
561
562
    // URI path is the rest of the string
563
73.3k
    abs_path = path_str.substr(start);
564
73.3k
    return Status::OK();
565
73.3k
}
566
567
} // namespace doris::io