~aleteoryx/muditaos

ref: 3780b4844c170a0e8f5f3b1fa615f0732009bbd8 muditaos/board/linux/libiosyscalls/include/iosyscalls.hpp -rw-r--r-- 1.2 KiB
3780b484 — Lucjan Bryndza [EGD-5074] Add native fscore for emulator 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

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

namespace vfsn::linux::internal
{
    bool redirect_to_image();
    bool is_image_handle(const FILE* fil);
    int native_fd_to_image_fd(int fd);
    int image_fd_to_native_fd(int fd);
    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};
    };
    FILEX* allocate_filex(int fd);
    bool is_filex(const void* fd);
    void remove_filex(FILEX *fil);

    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 0;
        }
        auto ret = (vfs.get()->*lfs_fun)(std::forward<Args>(args)...);
        if(ret < 0) {
            errno = -ret;
            ret = -1;
        }
        return ret;
    }
}