~aleteoryx/muditaos

ref: 3cbbeff551230786ae13c23a7bf4fa8c50099896 muditaos/board/linux/libiosyscalls/src/iosyscalls-internal.hpp -rw-r--r-- 1.6 KiB
3cbbeff5 — Lefucjusz [MOS-1011] Fix frequency switching stability 2 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include <stdio.h>
#include <purefs/vfs_subsystem.hpp>

struct __dirstream;

namespace vfsn::linux::internal
{
    void set_image_path(const char *newImageName);
    void set_sysroot(const char *newSysroot);
    bool redirect_to_image();
    bool is_image_handle(const FILE *fil);
    bool is_image_fd(int fd);
    bool redirect_to_image(const char *inpath);
    const char *npath_translate(const char *inpath, char *buffer);

    struct FILEX
    {
        int fd{0};
        int error{0};
        int ungetchar{-1};
    };

    int to_native_fd(int fd);
    int to_image_fd(int fd);

    FILEX *allocate_filex(int fd);
    bool is_filex(const void *fd);
    void remove_filex(FILEX *fil);

    void add_DIR_to_image_list(__dirstream *indir);
    void remove_DIR_from_image_list(__dirstream *indir);
    bool is_image_DIR(__dirstream *indir);

    template <class Base, typename T, typename... Args>
    auto invoke_fs(T Base::*lfs_fun, Args &&...args)
        -> decltype((static_cast<Base *>(nullptr)->*lfs_fun)(std::forward<Args>(args)...))
    {
        auto vfs = purefs::subsystem::vfs_core();
        if (!vfs) {
            errno = EIO;
            return -1;
        }
        auto ret = (vfs.get()->*lfs_fun)(std::forward<Args>(args)...);
        if (ret < 0) {
            errno = -ret;
            ret   = -1;
        }
        else {
            errno = 0;
        }
        return ret;
    }
} // namespace vfsn::linux::internal