~aleteoryx/muditaos

ref: cb5b5a37b8aeb5cb9d75d5df443078e1a1a67551 muditaos/module-vfs/src/purefs/fs/filesystem_syscalls.cpp -rw-r--r-- 6.1 KiB
cb5b5a37 — Lucjan Bryndza [EGD-5170] Fix native diropen 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include <purefs/fs/filesystem.hpp>
#include <errno.h>
#include <log/log.hpp>
#include <purefs/fs/filesystem_operations.hpp>
#include <purefs/fs/file_handle.hpp>
#include <purefs/fs/directory_handle.hpp>
#include <purefs/fs/thread_local_cwd.hpp>

namespace purefs::fs
{
    auto filesystem::stat_vfs(std::string_view path, struct ::statvfs &stat) const noexcept -> int
    {
        return invoke_fops(&filesystem_operations::stat_vfs, path, stat);
    }

    auto filesystem::stat(std::string_view file, struct stat &st) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::stat, file, st);
    }

    auto filesystem::unlink(std::string_view name) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::unlink, name);
    }

    auto filesystem::mkdir(std::string_view path, int mode) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::mkdir, path, mode);
    }

    auto filesystem::ioctl(std::string_view path, int cmd, void *arg) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::ioctl, path, cmd, arg);
    }

    auto filesystem::utimens(std::string_view path, std::array<timespec, 2> &tv) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::utimens, path, tv);
    }

    auto filesystem::flock(int fd, int cmd) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::flock, fd, cmd);
    }

    auto filesystem::isatty(int fd) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::isatty, fd);
    }

    auto filesystem::chmod(std::string_view path, mode_t mode) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::chmod, path, mode);
    }

    auto filesystem::write(int fd, const char *ptr, size_t len) noexcept -> ssize_t
    {
        return invoke_fops(&filesystem_operations::write, fd, ptr, len);
    }

    auto filesystem::read(int fd, char *ptr, size_t len) noexcept -> ssize_t
    {
        return invoke_fops(&filesystem_operations::read, fd, ptr, len);
    }

    auto filesystem::seek(int fd, off_t pos, int dir) noexcept -> off_t
    {
        return invoke_fops(&filesystem_operations::seek, fd, pos, dir);
    }

    auto filesystem::fstat(int fd, struct stat &st) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::fstat, fd, st);
    }

    auto filesystem::ftruncate(int fd, off_t len) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::ftruncate, fd, len);
    }

    auto filesystem::fsync(int fd) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::fsync, fd);
    }

    auto filesystem::fchmod(int fd, mode_t mode) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::fchmod, fd, mode);
    }

    auto filesystem::symlink(std::string_view existing, std::string_view newlink) noexcept -> int
    {
        return invoke_fops_same_mp(&filesystem_operations::symlink, existing, newlink);
    }

    auto filesystem::link(std::string_view existing, std::string_view newlink) noexcept -> int
    {
        return invoke_fops_same_mp(&filesystem_operations::link, existing, newlink);
    }

    auto filesystem::rename(std::string_view oldname, std::string_view newname) noexcept -> int
    {
        return invoke_fops_same_mp(&filesystem_operations::rename, oldname, newname);
    }

    auto filesystem::open(std::string_view path, int flags, int mode) noexcept -> int
    {
        const auto abspath     = absolute_path(path);
        auto [mountp, pathpos] = find_mount_point(abspath);
        if (!mountp) {
            LOG_ERROR("VFS: Unable to find mount point: %.*s", int(path.size()), path.data());
            return -ENOENT;
        }
        auto fsops = mountp->fs_ops();
        if (fsops) {
            auto fh = fsops->open(mountp, abspath, flags, mode);
            if (!fh) {
                LOG_ERROR("VFS: Unable to get fops");
                return -EBADF;
            }
            const auto err = fh->error();
            if (err) {
                return err;
            }
            return add_filehandle(fh);
        }
        else {
            LOG_ERROR("VFS: Unable to lock fops");
            return -EIO;
        }
    }

    auto filesystem::close(int fd) noexcept -> int
    {
        auto ret = invoke_fops(&filesystem_operations::close, fd);
        if (!ret) {
            ret = (remove_filehandle(fd)) ? (0) : (-EBADF);
        }
        return ret;
    }

    auto filesystem::diropen(std::string_view path) noexcept -> fsdir
    {
        const auto abspath     = absolute_path(path);
        auto [mountp, pathpos] = find_mount_point(abspath);
        if (!mountp) {
            LOG_ERROR("VFS: Unable to find mount point: %.*s", int(path.size()), path.data());
            return std::make_shared<internal::directory_handle>(nullptr, -ENOENT);
        }
        auto fsops = mountp->fs_ops();
        if (fsops) {
            auto dh = fsops->diropen(mountp, abspath);
            if (!dh) {
                LOG_ERROR("VFS: Unable to get diropen");
                return std::make_shared<internal::directory_handle>(nullptr, -ENXIO);
            }
            return dh;
        }
        else {
            LOG_ERROR("VFS: Unable to lock fops");
            return std::make_shared<internal::directory_handle>(nullptr, -EIO);
        }
    }

    auto filesystem::dirreset(fsdir dirstate) noexcept -> int
    {
        return invoke_fops(&filesystem_operations::dirreset, dirstate);
    }

    auto filesystem::dirnext(fsdir dirstate, std::string &filename, struct stat &filestat) noexcept -> int
    {
        if (!dirstate) {
            LOG_ERROR("No directory handle");
            return -ENXIO;
        }
        return invoke_fops(&filesystem_operations::dirnext, dirstate, filename, filestat);
    }

    auto filesystem::dirclose(fsdir dirstate) noexcept -> int
    {
        if (!dirstate) {
            LOG_ERROR("No directory handle");
            return -ENXIO;
        }
        return invoke_fops(&filesystem_operations::dirclose, dirstate);
    }

} // namespace purefs::fs