Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/wasmfs/paths.h
6174 views
1
// Copyright 2022 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#pragma once
7
8
#include <cassert>
9
#include <fcntl.h>
10
#include <memory>
11
#include <string_view>
12
#include <variant>
13
14
#include "file.h"
15
16
namespace wasmfs::path {
17
18
// Typically -ENOTDIR or -ENOENT.
19
using Error = long;
20
21
// The parent directory and the name of an entry within it. The returned string
22
// view is either backed by the same memory as the view passed to `parseParent`
23
// or is a view into a static string.
24
using ParentChild = std::pair<std::shared_ptr<Directory>, std::string_view>;
25
26
// If the path refers to a link, whether we should follow that link. Links among
27
// the parent directories in the path are always followed.
28
enum LinkBehavior { FollowLinks, NoFollowLinks };
29
30
struct ParsedParent {
31
private:
32
std::variant<Error, ParentChild> val;
33
34
public:
35
ParsedParent(Error err) : val(err) {}
36
ParsedParent(ParentChild pair) : val(pair) {}
37
// Always ok to call, returns 0 if there is no error.
38
long getError() {
39
if (auto* err = std::get_if<Error>(&val)) {
40
assert(*err != 0 && "Unexpected zero error value");
41
return *err;
42
}
43
return 0;
44
}
45
// Call only after checking for an error.
46
ParentChild& getParentChild() {
47
auto* ptr = std::get_if<ParentChild>(&val);
48
assert(ptr && "Unhandled path parsing error!");
49
return *ptr;
50
}
51
};
52
53
ParsedParent parseParent(std::string_view path, __wasi_fd_t basefd = AT_FDCWD);
54
55
struct ParsedFile {
56
private:
57
std::variant<Error, std::shared_ptr<File>> val;
58
59
public:
60
ParsedFile(Error err) : val(err) {}
61
ParsedFile(std::shared_ptr<File> file) : val(std::move(file)) {}
62
// Always ok to call. Returns 0 if there is no error.
63
long getError() {
64
if (auto* err = std::get_if<Error>(&val)) {
65
assert(*err != 0 && "Unexpected zero error value");
66
return *err;
67
}
68
return 0;
69
}
70
// Call only after checking for an error.
71
std::shared_ptr<File>& getFile() {
72
auto* ptr = std::get_if<std::shared_ptr<File>>(&val);
73
return *ptr;
74
}
75
};
76
77
ParsedFile parseFile(std::string_view path,
78
__wasi_fd_t basefd = AT_FDCWD,
79
LinkBehavior links = FollowLinks);
80
81
// Like `parseFile`, but handle the cases where flags include `AT_EMPTY_PATH` or
82
// AT_SYMLINK_NOFOLLOW. Does not validate the flags since different callers have
83
// different allowed flags.
84
ParsedFile getFileAt(__wasi_fd_t fd, std::string_view path, int flags);
85
86
// Like `parseFile`, but parse the path relative to the given directory.
87
ParsedFile getFileFrom(std::shared_ptr<Directory> base, std::string_view path);
88
89
} // namespace wasmfs::path
90
91