~aleteoryx/muditaos

ref: 178fba165e9a426fb2da84e4091b701ff610d280 muditaos/module-vfs/src/purefs/fs/filesystem_syscalls.cpp -rw-r--r-- 7.8 KiB
178fba16 — Lefucjusz [MOS-783] First part of new dir structure implementation 3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2017-2022, 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/directory_handle.hpp>
#include <purefs/fs/thread_local_cwd.hpp>
#include <purefs/fs/notifier.hpp>
#include <fcntl.h>

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

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

    auto filesystem::unlink(std::string_view name) noexcept -> int
    {
        const auto err = invoke_fops(iaccess::rw, &filesystem_operations::unlink, name);
        if (!err) {
            m_notifier->notify(name, inotify_flags::del);
        }
        return err;
    }

    auto filesystem::rmdir(std::string_view name) noexcept -> int
    {
        const auto err = invoke_fops(iaccess::rw, &filesystem_operations::rmdir, name);
        if (!err) {
            m_notifier->notify(name, inotify_flags::del);
        }
        return err;
    }

    auto filesystem::mkdir(std::string_view path, int mode) noexcept -> int
    {
        const auto err = invoke_fops(iaccess::rw, &filesystem_operations::mkdir, path, mode);
        if (!err) {
            m_notifier->notify(path, inotify_flags::dmodify);
        }
        return err;
    }

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

    auto filesystem::utimens(std::string_view path, std::array<timespec, 2> &tv) noexcept -> int
    {
        const auto err = invoke_fops(iaccess::ro, &filesystem_operations::utimens, path, tv);
        if (!err) {
            m_notifier->notify(path, inotify_flags::attrib);
        }
        return err;
    }

    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
    {
        const auto err = invoke_fops(iaccess::rw, &filesystem_operations::chmod, path, mode);
        if (!err) {
            m_notifier->notify(path, inotify_flags::attrib);
        }
        return err;
    }

    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
    {
        const auto err = invoke_fops(&filesystem_operations::fchmod, fd, mode);
        if (!err) {
            m_notifier->notify(fd, inotify_flags::attrib);
        }
        return err;
    }

    auto filesystem::symlink(std::string_view existing, std::string_view newlink) noexcept -> int
    {
        const auto err = invoke_fops_same_mp(&filesystem_operations::symlink, existing, newlink);
        if (!err) {
            m_notifier->notify(newlink, inotify_flags::dmodify);
        }
        return err;
    }

    auto filesystem::link(std::string_view existing, std::string_view newlink) noexcept -> int
    {
        const auto err = invoke_fops_same_mp(&filesystem_operations::link, existing, newlink);
        if (!err) {
            m_notifier->notify(newlink, inotify_flags::dmodify);
        }
        return err;
    }

    auto filesystem::rename(std::string_view oldname, std::string_view newname) noexcept -> int
    {
        const auto err = invoke_fops_same_mp(&filesystem_operations::rename, oldname, newname);
        if (!err) {
            m_notifier->notify(oldname, newname, inotify_flags::move_src);
            m_notifier->notify(newname, oldname, inotify_flags::move_dst);
        }
        return err;
    }

    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 specified mount point");
            return -ENOENT;
        }
        auto fsops = mountp->fs_ops();
        if (fsops) {
            if ((flags & O_ACCMODE) != O_RDONLY && (mountp->flags() & mount_flags::read_only)) {
                LOG_ERROR("Trying to open file with 'WR' flag on read-only filesystem");
                return -EACCES;
            }
            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;
            }
            const auto fd = add_filehandle(fh);
            m_notifier->notify_open(path, fd, (flags & O_ACCMODE) == O_RDONLY);
            return fd;
        }
        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);
            m_notifier->notify_close(fd);
        }
        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 specified mount point");
            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